2 * Copyright (c) 2016 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-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"
28 #include "third-party/picojson.h"
30 #include <dali/dali.h>
33 using namespace picojson;
37 using namespace GameUtils;
39 GameScene::GameScene()
43 GameScene::~GameScene()
47 bool GameScene::Load(const char *filename)
50 if( !LoadFile( filename, bytes ) )
56 bytes.push_back( '\0');
59 picojson::parse( root, bytes.data() );
63 if( root.is<object>() )
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>()
91 if( !vRotation.is<null>() )
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>()
102 if( !vScale.is<null>() )
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>()
112 if( !vSize.is<null>() )
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>()
122 GameModel* model( NULL );
123 GameTexture* texture( NULL );
125 if( !vModel.is<null>() )
127 std::string& strModel = vModel.get<std::string>();
128 model = GetResource( strModel.c_str(), mModelCache );
131 if( !vTexture.is<null>() )
133 std::string& strTexture = vTexture.get<std::string>();
134 texture = GetResource( strTexture.c_str(), mTextureCache );
137 if( !model || !texture )
143 entity->GetGameRenderer().SetModel( model );
144 entity->GetGameRenderer().SetMainTexture( texture );
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 )
164 Actor actor( mEntities[i]->GetActor() );
165 actor.SetAnchorPoint( AnchorPoint::CENTER );
166 actor.SetParentOrigin( ParentOrigin::CENTER );
167 mRootActor.Add( actor );
168 mEntities[i]->UpdateRenderer();
172 mCamera.Initialise( 60.0f, 0.1f, 100.0f );
177 Dali::Actor& GameScene::GetRootActor()