diff options
-rw-r--r-- | src/crepe/Resource.h | 33 | ||||
-rw-r--r-- | src/crepe/facade/Sound.cpp | 30 | ||||
-rw-r--r-- | src/crepe/facade/Sound.h | 14 | ||||
-rw-r--r-- | src/crepe/facade/SoundContext.cpp | 5 | ||||
-rw-r--r-- | src/crepe/facade/SoundContext.h | 5 |
5 files changed, 31 insertions, 56 deletions
diff --git a/src/crepe/Resource.h b/src/crepe/Resource.h deleted file mode 100644 index dcf3dbd..0000000 --- a/src/crepe/Resource.h +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include <memory> - -namespace crepe { - -class ResourceManager; -class Asset; - -/** - * Resource is an interface class used to represent a (deserialized) game - * resource (e.g. textures, sounds). - */ -class Resource { -private: - /** - * \brief Prototype pattern clone function. - * - * \param src Source file of new resource (abstraction for file saved on - * disk) - * - * \returns New instance of concrete resource - */ - virtual std::unique_ptr<Resource> clone(const Asset & src) const = 0; - /** - * The resource manager uses \c clone to create new instances of the concrete - * resource class. This may be used to inherit references to classes that - * would otherwise need to be implemented as singletons. - */ - friend class ResourceManager; -}; - -} // namespace crepe diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp index b8ea71a..648ec81 100644 --- a/src/crepe/facade/Sound.cpp +++ b/src/crepe/facade/Sound.cpp @@ -1,24 +1,26 @@ -#include <memory> - -#include "../Asset.h" #include "../util/log.h" #include "Sound.h" #include "SoundContext.h" using namespace crepe; -using namespace std; -Sound::Sound(SoundContext & ctx) : context(ctx) { dbg_trace(); } +Sound::Sound(std::unique_ptr<Asset> res) { + dbg_trace(); + this->load(std::move(res)); +} + +Sound::Sound(const char * src) { + dbg_trace(); + this->load(std::make_unique<Asset>(src)); +} -unique_ptr<Resource> Sound::clone(const Asset & src) const { - auto instance = make_unique<Sound>(*this); - instance->sample.load(src.canonical()); - return instance; +void Sound::load(std::unique_ptr<Asset> res) { + this->sample.load(res->canonical()); } void Sound::play() { - SoundContext & ctx = this->context; + SoundContext & ctx = SoundContext::get_instance(); if (ctx.engine.getPause(this->handle)) { // resume if paused ctx.engine.setPause(this->handle, false); @@ -30,13 +32,13 @@ void Sound::play() { } void Sound::pause() { - SoundContext & ctx = this->context; + SoundContext & ctx = SoundContext::get_instance(); if (ctx.engine.getPause(this->handle)) return; ctx.engine.setPause(this->handle, true); } void Sound::rewind() { - SoundContext & ctx = this->context; + SoundContext & ctx = SoundContext::get_instance(); if (!ctx.engine.isValidVoiceHandle(this->handle)) return; ctx.engine.seek(this->handle, 0); } @@ -44,7 +46,7 @@ void Sound::rewind() { void Sound::set_volume(float volume) { this->volume = volume; - SoundContext & ctx = this->context; + SoundContext & ctx = SoundContext::get_instance(); if (!ctx.engine.isValidVoiceHandle(this->handle)) return; ctx.engine.setVolume(this->handle, this->volume); } @@ -52,7 +54,7 @@ void Sound::set_volume(float volume) { void Sound::set_looping(bool looping) { this->looping = looping; - SoundContext & ctx = this->context; + SoundContext & ctx = SoundContext::get_instance(); if (!ctx.engine.isValidVoiceHandle(this->handle)) return; ctx.engine.setLooping(this->handle, this->looping); } diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h index e5b2f19..183bd7c 100644 --- a/src/crepe/facade/Sound.h +++ b/src/crepe/facade/Sound.h @@ -4,13 +4,11 @@ #include <soloud/soloud.h> #include <soloud/soloud_wav.h> -#include "../Resource.h" +#include "../Asset.h" namespace crepe { -class SoundContext; - -class Sound : public Resource { +class Sound { public: /** * \brief Pause this sample @@ -66,13 +64,15 @@ public: bool get_looping() const { return this->looping; } public: - Sound(SoundContext & ctx); - std::unique_ptr<Resource> clone(const Asset & src) const override; + Sound(const char * src); + Sound(std::unique_ptr<Asset> res); + +private: + void load(std::unique_ptr<Asset> res); private: SoLoud::Wav sample; SoLoud::handle handle; - SoundContext & context; float volume = 1.0f; bool looping = false; diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp index b5f3db3..5e5a3a9 100644 --- a/src/crepe/facade/SoundContext.cpp +++ b/src/crepe/facade/SoundContext.cpp @@ -4,6 +4,11 @@ using namespace crepe; +SoundContext & SoundContext::get_instance() { + static SoundContext instance; + return instance; +} + SoundContext::SoundContext() { dbg_trace(); engine.init(); diff --git a/src/crepe/facade/SoundContext.h b/src/crepe/facade/SoundContext.h index 8d9e396..d3123d2 100644 --- a/src/crepe/facade/SoundContext.h +++ b/src/crepe/facade/SoundContext.h @@ -7,10 +7,12 @@ namespace crepe { class SoundContext { -public: +private: SoundContext(); virtual ~SoundContext(); + // singleton + static SoundContext & get_instance(); SoundContext(const SoundContext &) = delete; SoundContext(SoundContext &&) = delete; SoundContext & operator=(const SoundContext &) = delete; @@ -18,7 +20,6 @@ public: private: SoLoud::Soloud engine; - //! Sound directly calls methods on \c engine friend class Sound; }; |