Cornell Cocos
Cornell Extensions to Cocos2d
CUTextureLoader.h
1 //
2 // CUTextureLoader.h
3 // Cornell Extensions to Cocos2D
4 //
5 // This module provides a specific implementation of the Loader class to load
6 // textures. Technically, a texture should be identified by both its source
7 // file and its texture parameters. However, Cocos2D does not allow you to
8 // to do that -- each texture can be only loaded once. Refactoring this would
9 // be a lot more work than we are comfortable with.
10 //
11 // As with all of our loaders, this loader is designed to be attached to a scene.
12 // This is the natural was to do things, as Cocos2d is scene based. However,
13 // its asset loading is typically done through the director, which is global.
14 // This makes is hard to determine when it is safe to unload an asset. Even
15 // though the current scene many not need it, it may be used by another active
16 // scene. Unloading the asset would corrupt that scene.
17 //
18 // This loader solves this problem by have a static coordinator behind the
19 // scenes. This coordinate is shared across all loader instances. It decides
20 // When an asset is truly ready to be unloaded.
21 //
22 // Author: Walker White
23 // Version: 12/10/15
24 //
25 #ifndef __CU_TEXTURE_LOADER__
26 #define __CU_TEXTURE_LOADER__
27 #include <unordered_set>
28 #include <renderer/CCTexture2D.h>
29 #include "CULoader.h"
30 
31 NS_CC_BEGIN
32 
59 class CC_DLL TextureLoader : public Loader<Texture2D> {
60 private:
62  CC_DISALLOW_COPY_AND_ASSIGN(TextureLoader);
63 
64 protected:
65 #pragma mark -
66 #pragma mark Texture Coordinator
67 
73  class Coordinator {
74  private:
76  std::unordered_map<GLuint,std::string> _sources;
78  std::unordered_map<std::string,Texture2D*> _objects;
80  std::unordered_map<std::string, int> _refcnts;
82  std::unordered_map<std::string,std::vector<std::function<void(Texture2D* s)>>> _callbacks;
83 
84  public:
86  size_t instances;
87 
93  Coordinator();
94 
100  ~Coordinator();
101 
110  bool isLoaded(std::string source) const { return _objects.find(source) != _objects.end(); }
111 
120  bool isPending(std::string source) const { return _callbacks.find(source) != _callbacks.end(); }
121 
122 
123 #pragma mark Allocation Methods
124 
134  Texture2D* load(std::string source);
135 
148  void loadAsync(std::string source, std::function<void(Texture2D* s)> callback);
149 
161  void allocate(Texture2D* texture, std::string source);
162 
173  void release(Texture2D* texture);
174  };
175 
178 
179 
180 #pragma mark -
181 #pragma mark Texture Loader
182 
183  Texture2D::TexParams _default;
184 
186  std::unordered_set<std::string> _tqueue;
187 
199  void allocate(std::string key, Texture2D* texture, const Texture2D::TexParams& params);
200 
201 
202 public:
203 #pragma mark Activation/Deactivation
204 
213  static TextureLoader* create();
214 
226  void start() override;
227 
238  void stop() override;
239 
240 
241 #pragma mark Loading/Unloading
242 
252  size_t waitCount() const override { return _tqueue.size(); }
253 
269  Texture2D* load(std::string key, std::string source) override { return load(key,source,_default); }
270 
285  Texture2D* load(std::string key, std::string source, const Texture2D::TexParams& params);
286 
303  void loadAsync(std::string key, std::string source) override { loadAsync(key,source,_default); }
304 
320  void loadAsync(std::string key, std::string source, const Texture2D::TexParams& params);
321 
333  void unload(std::string key) override;
334 
344  void unloadAll() override;
345 
346 
347 #pragma mark Defaults
348 
355  const Texture2D::TexParams& getDefaultParameters() const { return _default; }
356 
364  void setDefaultParameters(const Texture2D::TexParams& params) { _default = params; }
365 
366 
367 CC_CONSTRUCTOR_ACCESS:
368 #pragma mark Initializers
369 
372  TextureLoader() : Loader<Texture2D>() {}
373 
379  virtual ~TextureLoader() { if (_active) { stop(); } }
380 };
381 
382 NS_CC_END
383 
384 #endif /* defined(__CU_TEXTURE_LOADER__) */
static Coordinator * _gCoordinator
Definition: CUTextureLoader.h:177
Texture2D * load(std::string key, std::string source) override
Definition: CUTextureLoader.h:269
size_t waitCount() const override
Definition: CUTextureLoader.h:252
virtual void start()
Definition: CULoader.h:66
Definition: CUTextureLoader.h:73
void loadAsync(std::string key, std::string source) override
Definition: CUTextureLoader.h:303
size_t instances
Definition: CUTextureLoader.h:86
std::unordered_set< std::string > _tqueue
Definition: CUTextureLoader.h:186
bool isLoaded(std::string source) const
Definition: CUTextureLoader.h:110
Texture2D::TexParams _default
Definition: CUTextureLoader.h:183
void setDefaultParameters(const Texture2D::TexParams &params)
Definition: CUTextureLoader.h:364
Definition: CUTextureLoader.h:59
virtual void unload(std::string key)
Definition: CULoader.h:125
const Texture2D::TexParams & getDefaultParameters() const
Definition: CUTextureLoader.h:355
bool isPending(std::string source) const
Definition: CUTextureLoader.h:120
virtual void unloadAll()
Definition: CULoader.h:138
virtual void stop()
Definition: CULoader.h:82
Definition: CULoader.h:237
virtual T * load(std::string key, std::string source)
Definition: CULoader.h:310
virtual void loadAsync(std::string key, std::string source)
Definition: CULoader.h:111