CUGL
Cornell University Game Library
CUSoundLoader.h
1 //
2 // CUSoundLoader.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a specific implementation of the Loader class to load
6 // sound assets (e.g. in-memory audio files). A sound asset is identified by
7 // both its source file and its volume.
8 //
9 // As with all of our loaders, this loader is designed to be attached to an
10 // asset manager. In addition, this class uses our standard shared-pointer
11 // architecture.
12 //
13 // 1. The constructor does not perform any initialization; it just sets all
14 // attributes to their defaults.
15 //
16 // 2. All initialization takes place via init methods, which can fail if an
17 // object is initialized more than once.
18 //
19 // 3. All allocation takes place via static constructors which return a shared
20 // pointer.
21 //
22 //
23 // CUGL zlib License:
24 // This software is provided 'as-is', without any express or implied
25 // warranty. In no event will the authors be held liable for any damages
26 // arising from the use of this software.
27 //
28 // Permission is granted to anyone to use this software for any purpose,
29 // including commercial applications, and to alter it and redistribute it
30 // freely, subject to the following restrictions:
31 //
32 // 1. The origin of this software must not be misrepresented; you must not
33 // claim that you wrote the original software. If you use this software
34 // in a product, an acknowledgment in the product documentation would be
35 // appreciated but is not required.
36 //
37 // 2. Altered source versions must be plainly marked as such, and must not
38 // be misrepresented as being the original software.
39 //
40 // 3. This notice may not be removed or altered from any source distribution.
41 //
42 // Author: Walker White
43 // Version: 1/7/16
44 //
45 #ifndef __CU_SOUND_LOADER_H__
46 #define __CU_SOUND_LOADER_H__
47 #include <cugl/assets/CULoader.h>
48 #include <cugl/audio/CUSound.h>
49 
50 namespace cugl {
51 
69 class SoundLoader : public Loader<Sound> {
70 private:
72  CU_DISALLOW_COPY_AND_ASSIGN(SoundLoader);
73 
74 protected:
76  float _volume;
77 
78 #pragma mark Asset Loading
79 
95  void materialize(const std::string& key, const std::shared_ptr<Sound>& sound,
96  float volume, LoaderCallback callback);
97 
116  virtual bool read(const std::string& key, const std::string& source,
117  LoaderCallback callback, bool async) override;
118 
142  virtual bool read(const std::shared_ptr<JsonValue>& json,
143  LoaderCallback callback, bool async) override;
144 
145 
146 public:
147 #pragma mark -
148 #pragma mark Constructors
149 
155  SoundLoader();
156 
168  void dispose() override {
169  _assets.clear();
170  _loader = nullptr;
171  }
172 
185  static std::shared_ptr<SoundLoader> alloc() {
186  std::shared_ptr<SoundLoader> result = std::make_shared<SoundLoader>();
187  return (result->init() ? result : nullptr);
188  }
189 
204  static std::shared_ptr<SoundLoader> alloc(const std::shared_ptr<ThreadPool>& threads) {
205  std::shared_ptr<SoundLoader> result = std::make_shared<SoundLoader>();
206  return (result->init(threads) ? result : nullptr);
207  }
208 
209 #pragma mark -
210 #pragma mark Properties
211 
219  float getVolume() const { return _volume; }
220 
229  void setVolume(float volume) { _volume = volume; }
230 
231 };
232 
233 }
234 #endif /* __CU_SOUND_LOADER_H__ */
std::shared_ptr< ThreadPool > _loader
Definition: CULoader.h:102
void setVolume(float volume)
Definition: CUSoundLoader.h:229
std::unordered_map< std::string, std::shared_ptr< Sound > > _assets
Definition: CULoader.h:678
std::function< void(const std::string &key, bool success)> LoaderCallback
Definition: CULoader.h:81
virtual bool read(const std::string &key, const std::string &source, LoaderCallback callback, bool async) override
void dispose() override
Definition: CUSoundLoader.h:168
Definition: CULoader.h:675
float getVolume() const
Definition: CUSoundLoader.h:219
static std::shared_ptr< SoundLoader > alloc(const std::shared_ptr< ThreadPool > &threads)
Definition: CUSoundLoader.h:204
static std::shared_ptr< SoundLoader > alloc()
Definition: CUSoundLoader.h:185
Definition: CUAnimationNode.h:52
Definition: CUSoundLoader.h:69
float _volume
Definition: CUSoundLoader.h:76
void materialize(const std::string &key, const std::shared_ptr< Sound > &sound, float volume, LoaderCallback callback)