Removed duplicated code by creating third-party folder
[platform/core/uifw/dali-demo.git] / examples / fpp-game / game-scene.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <stdio.h>
19 #include <string.h>
20
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"
27
28 #include "third-party/pico-json.h"
29
30 #include <dali/dali.h>
31
32 using namespace Dali;
33 using namespace picojson;
34
35 using std::vector;
36
37 using namespace GameUtils;
38
39 GameScene::GameScene()
40 {
41 }
42
43 GameScene::~GameScene()
44 {
45 }
46
47 bool GameScene::Load(Window window, const char* filename)
48 {
49   ByteArray bytes;
50   if(!LoadFile(filename, bytes))
51   {
52     return false;
53   }
54
55   // add EOL
56   bytes.push_back('\0');
57
58   picojson::value root;
59   picojson::parse(root, bytes.data());
60
61   bool failed(false);
62
63   if(root.is<object>())
64   {
65     object rootObject = root.get<object>();
66     for(object::iterator it = rootObject.begin(); it != rootObject.end(); ++it)
67     {
68       std::string entityName((*it).first);
69
70       GameEntity* entity = new GameEntity(entityName.c_str());
71       mEntities.PushBack(entity);
72
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");
80
81       if(!vLocation.is<null>())
82       {
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>()));
88       }
89
90       if(!vRotation.is<null>())
91       {
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>())));
98       }
99
100       if(!vScale.is<null>())
101       {
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>()));
107       }
108
109       if(!vSize.is<null>())
110       {
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>()));
116       }
117
118       GameModel*   model(NULL);
119       GameTexture* texture(NULL);
120
121       if(!vModel.is<null>())
122       {
123         std::string& strModel = vModel.get<std::string>();
124         model                 = GetResource(strModel.c_str(), mModelCache);
125       }
126
127       if(!vTexture.is<null>())
128       {
129         std::string& strTexture = vTexture.get<std::string>();
130         texture                 = GetResource(strTexture.c_str(), mTextureCache);
131       }
132
133       if(!model || !texture)
134       {
135         failed = true;
136         break;
137       }
138
139       entity->GetGameRenderer().SetModel(model);
140       entity->GetGameRenderer().SetMainTexture(texture);
141     }
142   }
143
144   if(failed)
145   {
146     return false;
147   }
148
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)
158   {
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();
164   }
165
166   // update camera
167   mCamera.Initialise(window.GetRenderTaskList().GetTask(0).GetCameraActor(), 60.0f, 0.1f, 100.0f, window.GetSize());
168
169   return true;
170 }
171
172 Dali::Actor& GameScene::GetRootActor()
173 {
174   return mRootActor;
175 }