2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include "game-camera.h"
22 #include "game-entity.h"
23 #include "game-model.h"
24 #include "game-renderer.h"
25 #include "game-scene.h"
26 #include "game-texture.h"
28 #include "third-party/pico-json.h"
30 #include <dali/dali.h>
33 using namespace picojson;
37 using namespace GameUtils;
39 GameScene::GameScene()
43 GameScene::~GameScene()
47 bool GameScene::Load(Window window, const char* filename)
50 if(!LoadFile(filename, bytes))
56 bytes.push_back('\0');
59 picojson::parse(root, bytes.data());
65 object rootObject = root.get<object>();
66 for(object::iterator it = rootObject.begin(); it != rootObject.end(); ++it)
68 std::string entityName((*it).first);
70 GameEntity* entity = new GameEntity(entityName.c_str());
71 mEntities.PushBack(entity);
73 value& val((*it).second);
74 value& vLocation = val.get("location");
75 value& vRotation = val.get("rotation");
76 value& vScale = val.get("scale");
77 value& vSize = val.get("size");
78 value& vModel = val.get("model");
79 value& vTexture = val.get("texture");
81 if(!vLocation.is<null>())
83 array& location = vLocation.get<array>();
84 entity->SetLocation(Vector3(
85 location.at(0).get<double>(),
86 location.at(1).get<double>(),
87 location.at(2).get<double>()));
90 if(!vRotation.is<null>())
92 array& rotation = vRotation.get<array>();
93 entity->SetRotation(Quaternion(Vector4(
94 -rotation.at(0).get<double>(),
95 rotation.at(1).get<double>(),
96 -rotation.at(2).get<double>(),
97 rotation.at(3).get<double>())));
100 if(!vScale.is<null>())
102 array& scale = vScale.get<array>();
103 entity->SetScale(Vector3(
104 scale.at(0).get<double>(),
105 scale.at(1).get<double>(),
106 scale.at(2).get<double>()));
109 if(!vSize.is<null>())
111 array& size = vSize.get<array>();
112 entity->SetSize(Vector3(
113 size.at(0).get<double>(),
114 size.at(1).get<double>(),
115 size.at(2).get<double>()));
118 GameModel* model(NULL);
119 GameTexture* texture(NULL);
121 if(!vModel.is<null>())
123 std::string& strModel = vModel.get<std::string>();
124 model = GetResource(strModel.c_str(), mModelCache);
127 if(!vTexture.is<null>())
129 std::string& strTexture = vTexture.get<std::string>();
130 texture = GetResource(strTexture.c_str(), mTextureCache);
133 if(!model || !texture)
139 entity->GetGameRenderer().SetModel(model);
140 entity->GetGameRenderer().SetMainTexture(texture);
149 // add all to the window
150 mRootActor = Actor::New();
151 mRootActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
152 mRootActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
153 window.GetRootLayer().Add(mRootActor);
154 mRootActor.SetProperty(Actor::Property::SCALE, Vector3(-1.0, 1.0, 1.0));
155 mRootActor.SetProperty(Actor::Property::POSITION, Vector3(0.0, 0.0, 0.0));
156 mRootActor.SetProperty(Actor::Property::ORIENTATION, Quaternion(Degree(90), Vector3(1.0, 0.0, 0.0)));
157 for(size_t i = 0; i < mEntities.Size(); ++i)
159 Actor actor(mEntities[i]->GetActor());
160 actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
161 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
162 mRootActor.Add(actor);
163 mEntities[i]->UpdateRenderer();
167 mCamera.Initialise(window.GetRenderTaskList().GetTask(0).GetCameraActor(), 60.0f, 0.1f, 100.0f, window.GetSize());
172 Dali::Actor& GameScene::GetRootActor()