Updated demos to use DALi clang-format
[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 <inttypes.h>
22 #include <stdint.h>
23 #include <vector>
24
25 #include "game-camera.h"
26 #include "game-container.h"
27 #include "game-utils.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    * 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] window The window to load the scene on
61    * @param[in] filename Path to the scene file
62    * @return true if suceess
63    */
64   bool Load(Dali::Window window, const char* filename);
65
66   /**
67    * Loads resource ( model or texture ) or gets if from cache if already loaded
68    * @param[in] filename Path to the resource file
69    * @param[in] cache Reference to the cache array to be used
70    * @return Pointer to the resource or NULL otherwise
71    */
72   template<typename T>
73   T* GetResource(const char* filename, GameContainer<T*>& cache);
74
75   /**
76    * Returns scene root actor
77    * @return Parent actor of the whole game scene
78    */
79   Dali::Actor& GetRootActor();
80
81 private:
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 template<typename T>
93 T* GameScene::GetResource(const char* filename, GameContainer<T*>& cache)
94 {
95   std::string path(DEMO_GAME_DIR);
96   path += "/";
97   path += filename;
98
99   uint32_t hash(GameUtils::HashString(path.c_str()));
100
101   for(typename GameContainer<T*>::Iterator iter = cache.Begin(); iter != cache.End(); ++iter)
102   {
103     if((*iter)->GetUniqueId() == hash)
104     {
105       return (*iter);
106     }
107   }
108
109   // load resource
110   T* resource = new T(path.c_str());
111   if(!resource->IsReady())
112   {
113     return NULL;
114   }
115
116   cache.PushBack(resource);
117
118   return resource;
119 }
120
121 #endif