From 827f09031e2d3cc15e956b242774a4566e1403c1 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 7 Nov 2024 20:52:06 +0100 Subject: more WIP audio system --- src/crepe/Component.h | 2 -- src/crepe/api/AudioSource.cpp | 21 ++++++++++----------- src/crepe/api/AudioSource.h | 34 +++++++++++++++++++++++----------- src/crepe/system/AudioSystem.cpp | 24 ++++++++++++++++++++++++ src/crepe/system/AudioSystem.h | 21 +++++++++++++++++++++ src/crepe/system/CMakeLists.txt | 3 +++ src/crepe/system/ScriptSystem.cpp | 3 +-- src/crepe/system/System.cpp | 7 +++++++ src/crepe/system/System.h | 10 +++++++++- 9 files changed, 98 insertions(+), 27 deletions(-) create mode 100644 src/crepe/system/AudioSystem.cpp create mode 100644 src/crepe/system/AudioSystem.h create mode 100644 src/crepe/system/System.cpp 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 - 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 -#include "../facade/Sound.h" - #include "AudioSource.h" using namespace crepe; +using namespace std; -AudioSource::AudioSource(std::unique_ptr audio_clip) { - this->sound = std::make_unique(std::move(audio_clip)); -} - -void AudioSource::play() { return this->play(false); } +AudioSource::AudioSource(game_object_id_t id, unique_ptr 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 audio_clip); + AudioSource(game_object_id_t id, std::unique_ptr 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 audio_clip; - //! TODO: ????? - bool play_on_awake; + const std::unique_ptr 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; + //! 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> components = this->compmgr.get_components_by_type(); + + 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