1c322ee9e8ecdff681166af3ee374ad737d6b5a2
[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 <string.h>
19 #include <stdio.h>
20
21 #include "game-scene.h"
22 #include "game-model.h"
23 #include "game-texture.h"
24 #include "game-entity.h"
25 #include "game-renderer.h"
26 #include "game-camera.h"
27
28 #include "third-party/picojson.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
91       if( !vRotation.is<null>() )
92       {
93         array& rotation = vRotation.get<array>();
94         entity->SetRotation( Quaternion( Vector4(
95                               -rotation.at(0).get<double>(),
96                               rotation.at(1).get<double>(),
97                               -rotation.at(2).get<double>(),
98                               rotation.at(3).get<double>()
99                               )) );
100       }
101
102       if( !vScale.is<null>() )
103       {
104         array& scale = vScale.get<array>();
105         entity->SetScale( Vector3(
106                               scale.at(0).get<double>(),
107                               scale.at(1).get<double>(),
108                               scale.at(2).get<double>()
109                               ));
110       }
111
112       if( !vSize.is<null>() )
113       {
114         array& size = vSize.get<array>();
115         entity->SetSize( Vector3(
116                               size.at(0).get<double>(),
117                               size.at(1).get<double>(),
118                               size.at(2).get<double>()
119                               ));
120       }
121
122       GameModel* model( NULL );
123       GameTexture* texture( NULL );
124
125       if( !vModel.is<null>() )
126       {
127         std::string& strModel = vModel.get<std::string>();
128         model = GetResource( strModel.c_str(), mModelCache );
129       }
130
131       if( !vTexture.is<null>() )
132       {
133         std::string& strTexture = vTexture.get<std::string>();
134         texture = GetResource( strTexture.c_str(), mTextureCache );
135       }
136
137       if( !model || !texture )
138       {
139         failed = true;
140         break;
141       }
142
143       entity->GetGameRenderer().SetModel( model );
144       entity->GetGameRenderer().SetMainTexture( texture );
145     }
146   }
147
148   if( failed )
149   {
150     return false;
151   }
152
153   // add all to the window
154   mRootActor = Actor::New();
155   mRootActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
156   mRootActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
157   window.GetRootLayer().Add( mRootActor );
158   mRootActor.SetProperty( Actor::Property::SCALE, Vector3( -1.0, 1.0, 1.0 ) );
159   mRootActor.SetProperty( Actor::Property::POSITION, Vector3( 0.0, 0.0, 0.0 ) );
160   mRootActor.SetProperty( Actor::Property::ORIENTATION, Quaternion( Degree( 90 ), Vector3( 1.0, 0.0, 0.0 ) ) );
161   for( size_t i = 0; i < mEntities.Size(); ++i )
162   {
163     Actor actor( mEntities[i]->GetActor() );
164     actor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
165     actor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
166     mRootActor.Add( actor );
167     mEntities[i]->UpdateRenderer();
168   }
169
170   // update camera
171   mCamera.Initialise( window.GetRenderTaskList().GetTask(0).GetCameraActor(), 60.0f, 0.1f, 100.0f, window.GetSize() );
172
173   return true;
174 }
175
176 Dali::Actor& GameScene::GetRootActor()
177 {
178   return mRootActor;
179 }