Fixed DGEUF-1841.
[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) 2020 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 #include <dali/public-api/adaptor-framework/window.h>
31
32 class GameCamera;
33 class GameEntity;
34 class GameTexture;
35 class GameModel;
36
37 /**
38  * Container based types owning heap allocated data of specifed types
39  */
40 typedef GameContainer< GameEntity* > EntityArray;
41 typedef GameContainer< GameTexture* > TextureArray;
42 typedef GameContainer< GameModel* > ModelArray;
43
44 class GameScene
45 {
46 public:
47
48   /**
49    * Creates an instance of the GameScene
50    */
51   GameScene();
52
53   /**
54    * Destroys an instance of the GameScene
55    */
56   ~GameScene();
57
58   /**
59    * Loads scene from formatted JSON file, returns true on success
60    *
61    * @param[in] window The window to load the scene on
62    * @param[in] filename Path to the scene file
63    * @return true if suceess
64    */
65   bool Load( Dali::Window window, const char* filename );
66
67   /**
68    * Loads resource ( model or texture ) or gets if from cache if already loaded
69    * @param[in] filename Path to the resource file
70    * @param[in] cache Reference to the cache array to be used
71    * @return Pointer to the resource or NULL otherwise
72    */
73   template <typename T>
74   T* GetResource( const char* filename, GameContainer<T*>& cache );
75
76   /**
77    * Returns scene root actor
78    * @return Parent actor of the whole game scene
79    */
80   Dali::Actor& GetRootActor();
81
82 private:
83
84   EntityArray     mEntities;
85   GameCamera      mCamera;
86
87   // internal scene cache
88   ModelArray      mModelCache;
89   TextureArray    mTextureCache;
90
91   Dali::Actor     mRootActor;
92 };
93
94
95 template<typename T>
96 T* GameScene::GetResource( const char* filename, GameContainer<T*>& cache )
97 {
98   std::string path( DEMO_GAME_DIR );
99   path += "/";
100   path += filename;
101
102   uint32_t hash( GameUtils::HashString( path.c_str() ) );
103
104   for( typename GameContainer<T*>::Iterator iter = cache.Begin(); iter != cache.End(); ++iter )
105   {
106     if( (*iter)->GetUniqueId() == hash )
107     {
108       return (*iter);
109     }
110   }
111
112   // load resource
113   T* resource = new T( path.c_str() );
114   if( !resource->IsReady() )
115   {
116     return NULL;
117   }
118
119   cache.PushBack( resource );
120
121   return resource;
122 }
123
124
125 #endif