aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-13 12:31:59 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-13 12:31:59 +0100
commitc58fbbefd5a426c38b1182e9e760f149f0091670 (patch)
treecc512d4cf90addfd7bca66c0407afdaabff8d0a5 /src/crepe
parent827f09031e2d3cc15e956b242774a4566e1403c1 (diff)
move some files from `loek/tests` to `loek/audio`
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/Resource.h33
-rw-r--r--src/crepe/facade/Sound.cpp30
-rw-r--r--src/crepe/facade/Sound.h14
-rw-r--r--src/crepe/facade/SoundContext.cpp5
-rw-r--r--src/crepe/facade/SoundContext.h5
5 files changed, 56 insertions, 31 deletions
diff --git a/src/crepe/Resource.h b/src/crepe/Resource.h
new file mode 100644
index 0000000..dcf3dbd
--- /dev/null
+++ b/src/crepe/Resource.h
@@ -0,0 +1,33 @@
+#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 648ec81..b8ea71a 100644
--- a/src/crepe/facade/Sound.cpp
+++ b/src/crepe/facade/Sound.cpp
@@ -1,26 +1,24 @@
+#include <memory>
+
+#include "../Asset.h"
#include "../util/log.h"
#include "Sound.h"
#include "SoundContext.h"
using namespace crepe;
+using namespace std;
-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));
-}
+Sound::Sound(SoundContext & ctx) : context(ctx) { dbg_trace(); }
-void Sound::load(std::unique_ptr<Asset> res) {
- this->sample.load(res->canonical());
+unique_ptr<Resource> Sound::clone(const Asset & src) const {
+ auto instance = make_unique<Sound>(*this);
+ instance->sample.load(src.canonical());
+ return instance;
}
void Sound::play() {
- SoundContext & ctx = SoundContext::get_instance();
+ SoundContext & ctx = this->context;
if (ctx.engine.getPause(this->handle)) {
// resume if paused
ctx.engine.setPause(this->handle, false);
@@ -32,13 +30,13 @@ void Sound::play() {
}
void Sound::pause() {
- SoundContext & ctx = SoundContext::get_instance();
+ SoundContext & ctx = this->context;
if (ctx.engine.getPause(this->handle)) return;
ctx.engine.setPause(this->handle, true);
}
void Sound::rewind() {
- SoundContext & ctx = SoundContext::get_instance();
+ SoundContext & ctx = this->context;
if (!ctx.engine.isValidVoiceHandle(this->handle)) return;
ctx.engine.seek(this->handle, 0);
}
@@ -46,7 +44,7 @@ void Sound::rewind() {
void Sound::set_volume(float volume) {
this->volume = volume;
- SoundContext & ctx = SoundContext::get_instance();
+ SoundContext & ctx = this->context;
if (!ctx.engine.isValidVoiceHandle(this->handle)) return;
ctx.engine.setVolume(this->handle, this->volume);
}
@@ -54,7 +52,7 @@ void Sound::set_volume(float volume) {
void Sound::set_looping(bool looping) {
this->looping = looping;
- SoundContext & ctx = SoundContext::get_instance();
+ SoundContext & ctx = this->context;
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 183bd7c..e5b2f19 100644
--- a/src/crepe/facade/Sound.h
+++ b/src/crepe/facade/Sound.h
@@ -4,11 +4,13 @@
#include <soloud/soloud.h>
#include <soloud/soloud_wav.h>
-#include "../Asset.h"
+#include "../Resource.h"
namespace crepe {
-class Sound {
+class SoundContext;
+
+class Sound : public Resource {
public:
/**
* \brief Pause this sample
@@ -64,15 +66,13 @@ public:
bool get_looping() const { return this->looping; }
public:
- Sound(const char * src);
- Sound(std::unique_ptr<Asset> res);
-
-private:
- void load(std::unique_ptr<Asset> res);
+ Sound(SoundContext & ctx);
+ std::unique_ptr<Resource> clone(const Asset & src) const override;
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 5e5a3a9..b5f3db3 100644
--- a/src/crepe/facade/SoundContext.cpp
+++ b/src/crepe/facade/SoundContext.cpp
@@ -4,11 +4,6 @@
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 d3123d2..8d9e396 100644
--- a/src/crepe/facade/SoundContext.h
+++ b/src/crepe/facade/SoundContext.h
@@ -7,12 +7,10 @@
namespace crepe {
class SoundContext {
-private:
+public:
SoundContext();
virtual ~SoundContext();
- // singleton
- static SoundContext & get_instance();
SoundContext(const SoundContext &) = delete;
SoundContext(SoundContext &&) = delete;
SoundContext & operator=(const SoundContext &) = delete;
@@ -20,6 +18,7 @@ private:
private:
SoLoud::Soloud engine;
+ //! Sound directly calls methods on \c engine
friend class Sound;
};