aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/system')
-rw-r--r--src/crepe/system/AudioSystem.cpp56
-rw-r--r--src/crepe/system/AudioSystem.h21
-rw-r--r--src/crepe/system/CMakeLists.txt2
3 files changed, 79 insertions, 0 deletions
diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp
new file mode 100644
index 0000000..c8dae9d
--- /dev/null
+++ b/src/crepe/system/AudioSystem.cpp
@@ -0,0 +1,56 @@
+#include "AudioSystem.h"
+#include "ComponentManager.h"
+
+#include "../api/AudioSource.h"
+
+using namespace crepe;
+using namespace std;
+
+void AudioSystem::update() {
+ ComponentManager & mgr = this->component_manager;
+ vector<reference_wrapper<AudioSource>> components = mgr.get_components_by_type<AudioSource>();
+
+ for (auto component_ref : components) {
+ AudioSource & component = component_ref.get();
+ if (!component.active) continue;
+
+ /**
+ * How this is supposed to work:
+ * - Get an instance of Sound for this resource/component combo (Sound
+ * instance is supposed to be unique per component, even if they use the
+ * same underlying asset).
+ * OR
+ * - Use the same instance of Sound if this is what the cache returns
+ * (= what the game programmer's wishes to do).
+ *
+ * NOT supposed to happen but still the case:
+ * - Below function call causes assets to be cached unintentionally
+ * - Cached assets are deleted at the end of a scene (i think?)
+ * - I'm not sure if the ResourceManager is even supposed to have a public
+ * `.clear()` method since the control over resource lifetime is
+ * explicitly handed over to the game programmer by using ResourceManager
+ * to cache/uncache. I believe the proper methods are supposed to be:
+ *
+ * - get() get a reference to resource (used here)
+ * - clear() clears NON-cached assets
+ * - cache() marks asset as "do not delete at end of scene"
+ * - uncache() undoes the above
+ *
+ * I think somewhere in the above function calls a unique identifier for
+ * the Asset/GameObject should be given to make sure the unique instance
+ * shit works as intended. The resource manager is also used for things
+ * other than sounds.
+ *
+ * Also need to check:
+ * - Is it an issue if there are multiple AudioSource components playing
+ * the same sample (= identical Asset), while they are all triggered
+ * using the same underlying instance of Sound (esp. w/
+ * play/pause/retrigger behavior).
+ */
+ Sound & sound = this->resman.cache<Sound>(component.source);
+ sound.set_context(this->context);
+
+ // TODO: lots of state diffing
+ }
+}
+
diff --git a/src/crepe/system/AudioSystem.h b/src/crepe/system/AudioSystem.h
new file mode 100644
index 0000000..d0b4f9a
--- /dev/null
+++ b/src/crepe/system/AudioSystem.h
@@ -0,0 +1,21 @@
+#pragma once
+
+#include "../facade/SoundContext.h"
+#include "../api/ResourceManager.h"
+
+#include "System.h"
+
+namespace crepe {
+
+class AudioSystem : public System {
+public:
+ using System::System;
+ void update() override;
+
+private:
+ SoundContext context {};
+ ResourceManager & resman = ResourceManager::get_instance();
+};
+
+} // namespace crepe
+
diff --git a/src/crepe/system/CMakeLists.txt b/src/crepe/system/CMakeLists.txt
index d658b25..f507b90 100644
--- a/src/crepe/system/CMakeLists.txt
+++ b/src/crepe/system/CMakeLists.txt
@@ -5,6 +5,7 @@ target_sources(crepe PUBLIC
PhysicsSystem.cpp
CollisionSystem.cpp
RenderSystem.cpp
+ AudioSystem.cpp
AnimatorSystem.cpp
)
@@ -14,5 +15,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
PhysicsSystem.h
CollisionSystem.h
RenderSystem.h
+ AudioSystem.h
AnimatorSystem.h
)