aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-07 20:52:06 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-07 20:52:06 +0100
commit827f09031e2d3cc15e956b242774a4566e1403c1 (patch)
tree5ac1f025afea82ce892272deafcf8918ef518d33 /src
parent1c4156ee127b14760ed3b1a0cd16ad12180c7ac6 (diff)
more WIP audio system
Diffstat (limited to 'src')
-rw-r--r--src/crepe/Component.h2
-rw-r--r--src/crepe/api/AudioSource.cpp21
-rw-r--r--src/crepe/api/AudioSource.h34
-rw-r--r--src/crepe/system/AudioSystem.cpp24
-rw-r--r--src/crepe/system/AudioSystem.h21
-rw-r--r--src/crepe/system/CMakeLists.txt3
-rw-r--r--src/crepe/system/ScriptSystem.cpp3
-rw-r--r--src/crepe/system/System.cpp7
-rw-r--r--src/crepe/system/System.h10
9 files changed, 98 insertions, 27 deletions
diff --git a/src/crepe/Component.h b/src/crepe/Component.h
index 0fe60b2..e8d18d9 100644
--- a/src/crepe/Component.h
+++ b/src/crepe/Component.h
@@ -2,8 +2,6 @@
#include "types.h"
-#include <cstdint>
-
namespace crepe {
class ComponentManager;
diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp
index 63fd0d7..b0cf28c 100644
--- a/src/crepe/api/AudioSource.cpp
+++ b/src/crepe/api/AudioSource.cpp
@@ -1,23 +1,22 @@
#include <memory>
-#include "../facade/Sound.h"
-
#include "AudioSource.h"
using namespace crepe;
+using namespace std;
-AudioSource::AudioSource(std::unique_ptr<Asset> audio_clip) {
- this->sound = std::make_unique<crepe::Sound>(std::move(audio_clip));
-}
-
-void AudioSource::play() { return this->play(false); }
+AudioSource::AudioSource(game_object_id_t id, unique_ptr<Asset> audio_clip) :
+ Component(id),
+ audio_clip(std::move(audio_clip))
+{ }
void AudioSource::play(bool looping) {
- this->sound->set_looping(looping);
- this->sound->play();
+ this->loop = looping;
+ this->playing = true;
}
void AudioSource::stop() {
- this->sound->pause();
- this->sound->rewind();
+ this->playing = false;
+ this->rewind = true;
}
+
diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h
index 1e24ae8..fd2b6f1 100644
--- a/src/crepe/api/AudioSource.h
+++ b/src/crepe/api/AudioSource.h
@@ -4,36 +4,48 @@
#include "../Asset.h"
#include "../Component.h"
+#include "../types.h"
namespace crepe {
-class Sound;
-
//! Audio source component
class AudioSource : public Component {
public:
- AudioSource(std::unique_ptr<Asset> audio_clip);
+ AudioSource(game_object_id_t id, std::unique_ptr<Asset> audio_clip);
virtual ~AudioSource() = default;
public:
//! Start or resume this audio source
- void play();
- void play(bool looping);
+ void play(bool looping = false);
//! Stop this audio source
void stop();
public:
//! Sample file location
- std::unique_ptr<Asset> audio_clip;
- //! TODO: ?????
- bool play_on_awake;
+ const std::unique_ptr<Asset> audio_clip;
+ //! Play when this component becomes active
+ bool play_on_awake = false;
//! Repeat the current audio clip during playback
- bool loop;
+ bool loop = false;
//! Normalized volume (0.0 - 1.0)
- float volume;
+ float volume = 1.0;
+
+private:
+ //! If this source is playing audio
+ bool playing = false;
+ //! Rewind the sample location
+ bool rewind = false;
private:
- std::unique_ptr<Sound> sound;
+ //! Value of \c active after last system update
+ bool last_active = false;
+ //! Value of \c playing after last system update
+ bool last_playing = false;
+ //! Value of \c volume after last system update
+ float last_volume = 1.0;
+ //! Value of \c loop after last system update
+ bool last_loop = false;
};
} // namespace crepe
+
diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp
new file mode 100644
index 0000000..93c0955
--- /dev/null
+++ b/src/crepe/system/AudioSystem.cpp
@@ -0,0 +1,24 @@
+#pragma once
+
+#include "AudioSystem.h"
+#include "ComponentManager.h"
+
+#include "../api/AudioSource.h"
+
+using namespace crepe;
+using namespace std;
+
+AudioSystem::AudioSystem(SoundContext & ctx) : ctx(ctx) {}
+
+void AudioSystem::update() {
+ vector<reference_wrapper<AudioSource>> components = this->compmgr.get_components_by_type<AudioSource>();
+
+ for (auto component_ref : components) {
+ AudioSource & component = component_ref.get();
+ if (!component.active) continue;
+
+ // TODO: fetch Sound instance from resourcemanager
+ // 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..8fb681f
--- /dev/null
+++ b/src/crepe/system/AudioSystem.h
@@ -0,0 +1,21 @@
+#pragma once
+
+#include "../facade/SoundContext.h"
+
+#include "System.h"
+
+namespace crepe {
+
+class AudioSystem : public System {
+public:
+ AudioSystem(SoundContext & ctx);
+
+public:
+ void update();
+
+private:
+ SoundContext & ctx;
+};
+
+} // namespace crepe
+
diff --git a/src/crepe/system/CMakeLists.txt b/src/crepe/system/CMakeLists.txt
index ff6f66f..ca62add 100644
--- a/src/crepe/system/CMakeLists.txt
+++ b/src/crepe/system/CMakeLists.txt
@@ -1,9 +1,11 @@
target_sources(crepe PUBLIC
+ System.cpp
ParticleSystem.cpp
ScriptSystem.cpp
PhysicsSystem.cpp
CollisionSystem.cpp
RenderSystem.cpp
+ AudioSystem.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -12,4 +14,5 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
PhysicsSystem.h
CollisionSystem.h
RenderSystem.h
+ AudioSystem.h
)
diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp
index f2673e7..68fbb02 100644
--- a/src/crepe/system/ScriptSystem.cpp
+++ b/src/crepe/system/ScriptSystem.cpp
@@ -22,9 +22,8 @@ void ScriptSystem::update() {
forward_list<Script *> ScriptSystem::get_scripts() {
forward_list<Script *> scripts = {};
- ComponentManager & mgr = ComponentManager::get_instance();
vector<reference_wrapper<BehaviorScript>> behavior_scripts
- = mgr.get_components_by_type<BehaviorScript>();
+ = this->compmgr.get_components_by_type<BehaviorScript>();
for (auto behavior_script_ref : behavior_scripts) {
BehaviorScript & behavior_script = behavior_script_ref.get();
diff --git a/src/crepe/system/System.cpp b/src/crepe/system/System.cpp
new file mode 100644
index 0000000..fa51d2f
--- /dev/null
+++ b/src/crepe/system/System.cpp
@@ -0,0 +1,7 @@
+#include "System.h"
+
+using namespace crepe;
+
+// TODO: ComponentManager shouldn't be a singleton
+System::System() : compmgr(ComponentManager::get_instance()) {}
+
diff --git a/src/crepe/system/System.h b/src/crepe/system/System.h
index 3b81bef..5091977 100644
--- a/src/crepe/system/System.h
+++ b/src/crepe/system/System.h
@@ -1,14 +1,22 @@
#pragma once
+#include "../ComponentManager.h"
+
namespace crepe {
+//! ECS system base class
class System {
public:
+ //! Process components belonging to this system
virtual void update() = 0;
public:
- System() = default;
+ System();
virtual ~System() = default;
+
+public:
+ //! Reference to component manager
+ ComponentManager & compmgr;
};
} // namespace crepe