Cornell Cocos
Cornell Extensions to Cocos2d
All Classes Functions Variables Enumerations Enumerator Friends
CUSoundLoader.h
1 //
2 // CUSoundLoader.h
3 // Cornell Extensions to Cocos2D
4 //
5 // This module provides a specific implementation of the Loader class to load
6 // sounds. It uses the experimental AudioEngine instead of SimpleAudioEngine.
7 // This method requires a few modifications to the Cocos2d platform files to
8 // work properly. As AudioEngine is marked as experimental anyway, we felt
9 // this was okay.
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_SOUND_LOADER__
26 #define __CU_SOUND_LOADER__
27 #include <unordered_set>
28 #include "CUSound.h"
29 #include "CULoader.h"
30 
31 NS_CC_BEGIN
32 
56 class CC_DLL SoundLoader : public Loader<Sound> {
57 private:
59  CC_DISALLOW_COPY_AND_ASSIGN(SoundLoader);
60 
61 protected:
62 #pragma mark -
63 #pragma mark Sound Coordinator
64 
70  class Coordinator {
71  private:
73  std::unordered_map<std::string,Sound*> _sources;
75  std::unordered_map<std::string,int> _refcnts;
77  std::unordered_map<std::string,std::vector<std::function<void(Sound* s)>>> _callbacks;
78  public:
80  size_t instances;
81 
87  Coordinator();
88 
94  ~Coordinator();
95 
104  bool isLoaded(std::string source) const { return _sources.find(source) != _sources.end(); }
105 
114  bool isPending(std::string source) const { return _callbacks.find(source) != _callbacks.end(); }
115 
116 
117 #pragma mark Allocation Methods
118 
128  Sound* load(std::string source);
129 
142  void loadAsync(std::string source, std::function<void(Sound* s)> callback);
143 
156  void allocate(std::string source, bool success, bool preload = true);
157 
168  void release(Sound* sound);
169  };
170 
173 
174 #pragma mark -
175 #pragma mark Sound Loader
176 
177  std::unordered_set<std::string> _squeue;
178 
189  void allocate(std::string key, Sound* sound);
190 
191 
192 public:
193 #pragma mark Activation/Deactivation
194 
203  static SoundLoader* create();
204 
216  void start() override;
217 
229  void stop() override;
230 
231 
232 #pragma mark Loading/Unloading
233 
243  size_t waitCount() const override { return _squeue.size(); }
244 
258  Sound* load(std::string key, std::string source) override;
259 
274  void loadAsync(std::string key, std::string source) override;
275 
287  void unload(std::string key) override;
288 
298  void unloadAll() override;
299 
300 
301 CC_CONSTRUCTOR_ACCESS:
302 #pragma mark Initializers
303 
306  SoundLoader() : Loader<Sound>() {}
307 
313  virtual ~SoundLoader() { if (_active) { stop(); } }
314 
315 };
316 
317 NS_CC_END
318 
319 #endif /* defined(__CU_SOUND_LOADER__) */
size_t instances
Definition: CUSoundLoader.h:80
virtual void start()
Definition: CULoader.h:66
Definition: CUSoundLoader.h:70
Definition: CUSound.h:42
size_t waitCount() const override
Definition: CUSoundLoader.h:243
bool isPending(std::string source) const
Definition: CUSoundLoader.h:114
virtual void unload(std::string key)
Definition: CULoader.h:125
bool isLoaded(std::string source) const
Definition: CUSoundLoader.h:104
virtual void unloadAll()
Definition: CULoader.h:138
static Coordinator * _gCoordinator
Definition: CUSoundLoader.h:172
Definition: CUSoundLoader.h:56
virtual void stop()
Definition: CULoader.h:82
Definition: CULoader.h:237
virtual T * load(std::string key, std::string source)
Definition: CULoader.h:310
std::unordered_set< std::string > _squeue
Definition: CUSoundLoader.h:177
virtual void loadAsync(std::string key, std::string source)
Definition: CULoader.h:111