Cornell Cocos
Cornell Extensions to Cocos2d
All Classes Functions Variables Enumerations Enumerator Friends
CULoader.h
1 //
2 // CULoader.h
3 // Cornell Extensions to Cocos2D
4 //
5 // This module provides the base templates for loader classes. Our goal with
6 // this module is to create a modular loader system that is as close to
7 // Nathan Sweet's flexible and modular AssetManager for libGDX. To do this in
8 // C++ requires three layers:
9 //
10 // The first layer is a polymorphic base that is used the AssetManager for
11 // adding and removing loaders. It is the C++ equivalent of a Java interface.
12 //
13 // The second layer is a template that provides type correctness when accessing
14 // components in the AssetManager. It has generic functionality that is
15 // necessary for all loaders, but is missing the information needed to load
16 // the specific asset.
17 //
18 // The top layer is a specific class for each asset that implements the specific
19 // loading functionality.
20 //
21 // This module implements the first two layers. As they are both a template and
22 // a pure polymorphic class, only a header file is necessary. There is no
23 // associated CPP file with this header.
24 //
25 // Author: Walker White
26 // Version: 12/10/15
27 //
28 #ifndef __CU_LOADER_H__
29 #define __CU_LOADER_H__
30 
31 #include <cocos2d.h>
32 #include <unordered_map>
33 
34 NS_CC_BEGIN
35 
36 #pragma mark -
37 #pragma mark Polymorphic Base
38 
45 class CC_DLL BaseLoader : public Ref {
46 protected:
48  bool _active;
49 
50 public:
51 #pragma mark Activation/Deactivation
52 
66  virtual void start() { _active = true; }
67 
82  virtual void stop() { _active = false; }
83 
92  bool isActive() const { return _active; }
93 
94 
95 #pragma mark Loading/Unloading
96 
111  virtual void loadAsync(std::string key, std::string source) {}
112 
125  virtual void unload(std::string key) {}
126 
138  virtual void unloadAll() {}
139 
140 
141 #pragma mark Progress Monitoring
142 
154  virtual size_t loadCount() const { return 0; }
155 
169  virtual size_t waitCount() const { return 0; }
170 
180  bool isComplete() const { return waitCount() == 0; }
181 
196  float progress() const {
197  size_t size = loadCount()+waitCount();
198  return (size == 0 ? 0.0f : ((float)loadCount())/size);
199  }
200 
201 
202 CC_CONSTRUCTOR_ACCESS:
203 #pragma mark Initializers
204 
210  BaseLoader() : Ref(), _active(false) {}
211 
218  virtual ~BaseLoader() { stop(); }
219 };
220 
221 
222 #pragma mark -
223 #pragma mark Templated Middle Layer
224 
236 template <class T>
237 class CC_DLL Loader : public BaseLoader {
238 protected:
240  std::unordered_map<std::string, T*> _assets;
241 
242 public:
243 #pragma mark Asset Access
244 
253  virtual bool contains(std::string key) const { return _assets.find(key) != _assets.end(); }
254 
265  virtual T* get(std::string key) const {
266  auto it = _assets.find(key);
267  return (it == _assets.end() ? nullptr : it->second);
268  }
269 
280  T* operator[](std::string key) const { return get(key); }
281 
282 
283 #pragma mark Asset Loading
284 
293  virtual size_t loadCount() const override { return _assets.size(); }
294 
310  virtual T* load(std::string key, std::string source) { return nullptr; }
311 
312 
313 CC_CONSTRUCTOR_ACCESS:
314 #pragma mark Initializers
315 
321  Loader() : BaseLoader() {}
322 };
323 
324 NS_CC_END
325 
326 #endif /* defined(__CU_LOADER_H__) */
Definition: CULoader.h:45
virtual void start()
Definition: CULoader.h:66
virtual size_t loadCount() const
Definition: CULoader.h:154
virtual size_t loadCount() const override
Definition: CULoader.h:293
std::unordered_map< std::string, T * > _assets
Definition: CULoader.h:240
virtual bool contains(std::string key) const
Definition: CULoader.h:253
bool isComplete() const
Definition: CULoader.h:180
virtual void unload(std::string key)
Definition: CULoader.h:125
T * operator[](std::string key) const
Definition: CULoader.h:280
virtual void unloadAll()
Definition: CULoader.h:138
virtual ~BaseLoader()
Definition: CULoader.h:218
bool _active
Definition: CULoader.h:48
virtual void stop()
Definition: CULoader.h:82
float progress() const
Definition: CULoader.h:196
Definition: CULoader.h:237
virtual T * load(std::string key, std::string source)
Definition: CULoader.h:310
virtual size_t waitCount() const
Definition: CULoader.h:169
bool isActive() const
Definition: CULoader.h:92
virtual void loadAsync(std::string key, std::string source)
Definition: CULoader.h:111