Cornell Cocos
Cornell Extensions to Cocos2d
All Classes Functions Variables Enumerations Enumerator Friends
CUFontLoader.h
1 //
2 // CUFontLoader.h
3 // Cornell Extensions to Cocos2D
4 //
5 // This module provides a specific implementation of the Loader class to load
6 // True Type fonts. Because of how Cocos2D renders fonts, a font is defined by
7 // both its source file and its size. Fonts of different size are always
8 // different fonts.
9 //
10 // As with all of our loaders, this loader is designed to be attached to a scene.
11 // This is the natural was to do things, as Cocos2d is scene based. However,
12 // its asset loading is typically done through the director, which is global.
13 // This makes is hard to determine when it is safe to unload an asset. Even
14 // though the current scene many not need it, it may be used by another active
15 // scene. Unloading the asset would corrupt that scene.
16 //
17 // This loader solves this problem by have a static coordinator behind the
18 // scenes. This coordinate is shared across all loader instances. It decides
19 // When an asset is truly ready to be unloaded.
20 //
21 // Author: Walker White
22 // Version: 12/10/15
23 //
24 #ifndef __CU_FONT_LOADER__
25 #define __CU_FONT_LOADER__
26 #include <unordered_set>
27 #include <mutex>
28 #include "CULoader.h"
29 #include "CUTTFont.h"
30 #include "CUThreadPool.h"
31 
32 
33 NS_CC_BEGIN
34 
35 #define DEFAULT_SIZE 12.0f
36 
62 class CC_DLL FontLoader : public Loader<TTFont> {
63 private:
65  CC_DISALLOW_COPY_AND_ASSIGN(FontLoader);
66 
67 protected:
68 #pragma mark -
69 #pragma mark Font Coordinator
70 
76  class Coordinator {
77  private:
79  std::unordered_map<std::string,TTFont*> _objects;
81  std::unordered_map<std::string, int> _refcnts;
83  std::unordered_map<std::string,std::vector<std::function<void(TTFont* s)>>> _callbacks;
84 
86  ThreadPool* _threads;
88  std::mutex _mutex;
89 
90  public:
92  size_t instances;
93 
99  Coordinator();
100 
106  ~Coordinator();
107 
116  bool isLoaded(std::string id) const { return _objects.find(id) != _objects.end(); }
117 
126  bool isPending(std::string id) const { return _callbacks.find(id) != _callbacks.end(); }
127 
128 
129 #pragma mark Allocation Methods
130 
141  TTFont* load(std::string source, float size);
142 
155  void loadAsync(std::string source, float size, std::function<void(TTFont* s)> callback);
156 
170  TTFont* allocateSync(TTFont* texture);
171 
185  void allocateAsync(TTFont* texture, FontAtlas* atlas);
186 
197  void release(TTFont* font);
198  };
199 
202 
203 #pragma mark -
204 #pragma mark Font Loader
205 
206  float _default;
207 
209  std::unordered_set<std::string> _fqueue;
210 
221  void allocate(std::string key, TTFont* font);
222 
223 
224 public:
225 #pragma mark Activation/Deactivation
226 
235  static FontLoader* create();
236 
248  void start() override;
249 
260  void stop() override;
261 
262 
263 #pragma mark Loading/Unloading
264 
274  size_t waitCount() const override { return _fqueue.size(); }
275 
291  TTFont* load(std::string key, std::string source) override { return load(key,source,_default); }
292 
307  TTFont* load(std::string key, std::string source, float size);
308 
325  void loadAsync(std::string key, std::string source) override { loadAsync(key,source,_default); }
326 
342  void loadAsync(std::string key, std::string source, float size);
343 
355  void unload(std::string key) override;
356 
366  void unloadAll() override;
367 
368 
369 #pragma mark Default Parameters
370 
377  float getDefaultSize() const { return _default; }
378 
386  void setDefaultSize(float size) { _default = size; }
387 
388 
389 CC_CONSTRUCTOR_ACCESS:
390 #pragma mark Initializers
391 
394  FontLoader() : Loader<TTFont>(), _default(DEFAULT_SIZE) {}
395 
401  virtual ~FontLoader() { if (_active) { stop(); } }
402 
403 };
404 
405 NS_CC_END
406 
407 #endif /* defined(__CU_FONT_LOADER__) */
size_t instances
Definition: CUFontLoader.h:92
virtual void start()
Definition: CULoader.h:66
virtual ~FontLoader()
Definition: CUFontLoader.h:401
size_t waitCount() const override
Definition: CUFontLoader.h:274
Definition: CUFontLoader.h:62
float getDefaultSize() const
Definition: CUFontLoader.h:377
Definition: CUThreadPool.h:51
void setDefaultSize(float size)
Definition: CUFontLoader.h:386
std::unordered_set< std::string > _fqueue
Definition: CUFontLoader.h:209
float _default
Definition: CUFontLoader.h:206
bool isLoaded(std::string id) const
Definition: CUFontLoader.h:116
virtual void unload(std::string key)
Definition: CULoader.h:125
void loadAsync(std::string key, std::string source) override
Definition: CUFontLoader.h:325
static Coordinator * _gCoordinator
Definition: CUFontLoader.h:201
bool isPending(std::string id) const
Definition: CUFontLoader.h:126
virtual void unloadAll()
Definition: CULoader.h:138
Definition: CUTTFont.h:38
TTFont * load(std::string key, std::string source) override
Definition: CUFontLoader.h:291
virtual void stop()
Definition: CULoader.h:82
Definition: CULoader.h:237
Definition: CUFontLoader.h:76
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