[dali_1.2.16] Merge branch 'devel/master'
[platform/core/uifw/dali-demo.git] / examples / fpp-game / game-scene.h
1 #ifndef GAME_SCENE_H
2 #define GAME_SCENE_H
3
4 /*
5  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 #include <vector>
22 #include <stdint.h>
23 #include <inttypes.h>
24
25 #include "game-container.h"
26 #include "game-utils.h"
27 #include "game-camera.h"
28
29 #include <dali/public-api/actors/actor.h>
30
31 class GameCamera;
32 class GameEntity;
33 class GameTexture;
34 class GameModel;
35
36 /**
37  * Container based types owning heap allocated data of specifed types
38  */
39 typedef GameContainer< GameEntity* > EntityArray;
40 typedef GameContainer< GameTexture* > TextureArray;
41 typedef GameContainer< GameModel* > ModelArray;
42
43 class GameScene
44 {
45 public:
46
47   /**
48    * Creates an instance of the GameScene
49    */
50   GameScene();
51
52   /**
53    * Destroys an instance of the GameScene
54    */
55   ~GameScene();
56
57   /**
58    * Loads scene from formatted JSON file, returns true on success
59    *
60    * @param[in] filename Path to the scene file
61    * @return true if suceess
62    */
63   bool Load( const char* filename );
64
65   /**
66    * Loads resource ( model or texture ) or gets if from cache if already loaded
67    * @param[in] filename Path to the resource file
68    * @param[in] cache Reference to the cache array to be used
69    * @return Pointer to the resource or NULL otherwise
70    */
71   template <typename T>
72   T* GetResource( const char* filename, GameContainer<T*>& cache );
73
74   /**
75    * Returns scene root actor
76    * @return Parent actor of the whole game scene
77    */
78   Dali::Actor& GetRootActor();
79
80 private:
81
82   EntityArray     mEntities;
83   GameCamera      mCamera;
84
85   // internal scene cache
86   ModelArray      mModelCache;
87   TextureArray    mTextureCache;
88
89   Dali::Actor     mRootActor;
90 };
91
92
93 template<typename T>
94 T* GameScene::GetResource( const char* filename, GameContainer<T*>& cache )
95 {
96   std::string path( DEMO_GAME_DIR );
97   path += "/";
98   path += filename;
99
100   uint32_t hash( GameUtils::HashString( path.c_str() ) );
101
102   for( typename GameContainer<T*>::Iterator iter = cache.Begin(); iter != cache.End(); ++iter )
103   {
104     if( (*iter)->GetUniqueId() == hash )
105     {
106       return (*iter);
107     }
108   }
109
110   // load resource
111   T* resource = new T( path.c_str() );
112   if( !resource->IsReady() )
113   {
114     return NULL;
115   }
116
117   cache.PushBack( resource );
118
119   return resource;
120 }
121
122
123 #endif