Cornell Cocos
Cornell Extensions to Cocos2d
CUSceneManager.h
1 //
2 // CUSceneManager.h
3 // Cornell Extensions to Cocos2D
4 //
5 // This class manages a collection of loaders for a single scene. This allows us to
6 // easily load and unload assets that are attached to a single Cocos2D scene. As
7 // Cocos2D is scene oriented, this is a natural way to handle assets.
8 //
9 // Author: Walker White
10 // Version: 12/10/15
11 //
12 #ifndef __CU_SCENE_MANAGER_H__
13 #define __CU_SCENE_MANAGER_H__
14 #include <unordered_map>
15 #include <cocos2d.h>
16 #include "CULoader.h"
17 
18 NS_CC_BEGIN
19 
20 #pragma mark -
21 #pragma mark Scene Manager
22 
33 class CC_DLL SceneManager: public Ref {
34 private:
36  CC_DISALLOW_COPY_AND_ASSIGN(SceneManager);
37 
38 
39 protected:
41  bool _active;
42 
44  std::unordered_map<size_t,BaseLoader*> _handlers;
45 
46 public:
47 #pragma mark Activation/Deactivation
48 
57  static SceneManager* create();
58 
70  void start();
71 
82  void stop();
83 
93  bool isActive() const { return _active; }
94 
95 
96 #pragma mark Loader Attachment
97 
110  template<typename T>
111  bool attach(Loader<T>* loader) {
112  size_t hash = typeid(T).hash_code();
113  auto it = _handlers.find(hash);
114  if (it != _handlers.end()) {
115  return false;
116  }
117  _handlers[hash] = loader;
118  loader->retain();
119  if (_active && !loader->isActive()) {
120  loader->start();
121  } else if (!_active && loader->isActive()) {
122  loader->stop();
123  }
124  return true;
125  }
126 
134  template<typename T>
135  bool isAttached() {
136  size_t hash = typeid(T).hash_code();
137  return _handlers.find(hash) != _handlers.end();
138  }
139 
149  template<typename T>
150  bool detach() {
151  size_t hash = typeid(T).hash_code();
152  auto it = _handlers.find(hash);
153  if (it == _handlers.end()) {
154  return false;
155  }
156  it->second->release();
157  _handlers.erase(hash);
158  return true;
159  }
160 
169  void detachAll() {
170  for(auto it = _handlers.begin(); it != _handlers.end(); ++it) {
171  it->second->release();
172  }
173  _handlers.clear();
174  }
175 
183  template<typename T>
185  size_t hash = typeid(T).hash_code();
186  auto it = _handlers.find(hash);
187  if (it == _handlers.end()) {
188  CCASSERT(false, "No loader assigned for given type");
189  return nullptr;
190  }
191 
192  return ((Loader<T>*)it->second);
193  }
194 
195 
196 #pragma mark Loading/Unloading
197 
209  size_t loadCount() const;
210 
224  size_t waitCount() const;
225 
235  bool isComplete() const { return waitCount() == 0; }
236 
251  float progress() const {
252  size_t size = loadCount()+waitCount();
253  return (size == 0 ? 0.0f : ((float)loadCount())/size);
254  }
255 
265  template<typename T>
266  T* get(std::string key) const {
267  size_t hash = typeid(T).hash_code();
268  auto it = _handlers.find(hash);
269  if (it == _handlers.end()) {
270  CCASSERT(false, "No loader assigned for given type");
271  return nullptr;
272  }
273 
274  return ((Loader<T>*)it->second)->get(key);
275  }
276 
277 
291  template<typename T>
292  T* load(std::string key, std::string source) {
293  size_t hash = typeid(T).hash_code();
294  auto it = _handlers.find(hash);
295  if (it != _handlers.end()) {
296  Loader<T>* ref = (Loader<T>*)(it->second);
297  return ref->load(key,source);
298  }
299 
300  CCASSERT(false, "No loader assigned for given type");
301  return nullptr;
302  }
303 
317  template<typename T>
318  void loadAsync(std::string key, std::string source) {
319  size_t hash = typeid(T).hash_code();
320  auto it = _handlers.find(hash);
321  if (it != _handlers.end()) {
322  it->second->loadAsync(key,source);
323  return;
324  }
325 
326  CCASSERT(false, "No loader assigned for given type");
327  }
328 
339  template<typename T>
340  void unload(std::string key) {
341  size_t hash = typeid(T).hash_code();
342  auto it = _handlers.find(hash);
343  if (it != _handlers.end()) {
344  it->second->unload(key);
345  return;
346  }
347 
348  CCASSERT(false, "No loader assigned for given type");
349  }
350 
358  void unloadAll() {
359  for(auto it = _handlers.begin(); it != _handlers.end(); ++it) {
360  it->second->unloadAll();
361  }
362  }
363 
364 
365 CC_CONSTRUCTOR_ACCESS:
366 #pragma mark Initializers
367 
370  SceneManager() : _active(false) {}
371 
377  ~SceneManager() { if (_active) { stop(); } detachAll(); }
378 };
379 
380 NS_CC_END
381 
382 #endif /* defined(__CU_SCENE_MANAGER_H__) */
virtual void start()
Definition: CULoader.h:66
T * load(std::string key, std::string source)
Definition: CUSceneManager.h:292
std::unordered_map< size_t, BaseLoader * > _handlers
Definition: CUSceneManager.h:44
bool detach()
Definition: CUSceneManager.h:150
void loadAsync(std::string key, std::string source)
Definition: CUSceneManager.h:318
Definition: CUSceneManager.h:33
bool _active
Definition: CUSceneManager.h:41
bool isAttached()
Definition: CUSceneManager.h:135
bool isComplete() const
Definition: CUSceneManager.h:235
bool isActive() const
Definition: CUSceneManager.h:93
bool attach(Loader< T > *loader)
Definition: CUSceneManager.h:111
void detachAll()
Definition: CUSceneManager.h:169
virtual void stop()
Definition: CULoader.h:82
Definition: CULoader.h:237
float progress() const
Definition: CUSceneManager.h:251
Loader< T > * access()
Definition: CUSceneManager.h:184
void unloadAll()
Definition: CUSceneManager.h:358
virtual T * load(std::string key, std::string source)
Definition: CULoader.h:310
bool isActive() const
Definition: CULoader.h:92
void unload(std::string key)
Definition: CUSceneManager.h:340