Merge "homescreen benchmark can use buttons" into devel/master
[platform/core/uifw/dali-demo.git] / examples / fpp-game / game-scene.cpp
1 /*
2  * Copyright (c) 2016 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(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 stage
154   Stage stage = Stage::GetCurrent();
155   mRootActor = Actor::New();
156   mRootActor.SetAnchorPoint( AnchorPoint::CENTER );
157   mRootActor.SetParentOrigin( ParentOrigin::CENTER );
158   stage.GetRootLayer().Add( mRootActor );
159   mRootActor.SetScale( -1.0, 1.0, 1.0 );
160   mRootActor.SetPosition( 0.0, 0.0, 0.0 );
161   mRootActor.SetOrientation( Degree( 90 ), Vector3( 1.0, 0.0, 0.0 ));
162   for( size_t i = 0; i < mEntities.Size(); ++i )
163   {
164     Actor actor( mEntities[i]->GetActor() );
165     actor.SetAnchorPoint( AnchorPoint::CENTER );
166     actor.SetParentOrigin( ParentOrigin::CENTER );
167     mRootActor.Add( actor );
168     mEntities[i]->UpdateRenderer();
169   }
170
171   // update camera
172   mCamera.Initialise( 60.0f, 0.1f, 100.0f );
173
174   return true;
175 }
176
177 Dali::Actor& GameScene::GetRootActor()
178 {
179   return mRootActor;
180 }