diff options
-rw-r--r-- | src/crepe/api/AudioSource.cpp | 4 | ||||
-rw-r--r-- | src/crepe/api/AudioSource.h | 15 | ||||
-rw-r--r-- | src/crepe/facade/SoundContext.cpp | 6 | ||||
-rw-r--r-- | src/crepe/facade/SoundContext.h | 1 | ||||
-rw-r--r-- | src/crepe/system/AudioSystem.cpp | 23 | ||||
-rw-r--r-- | src/crepe/system/AudioSystem.h | 1 | ||||
-rw-r--r-- | src/test/AudioTest.cpp | 14 |
7 files changed, 35 insertions, 29 deletions
diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp index c646aeb..cc70801 100644 --- a/src/crepe/api/AudioSource.cpp +++ b/src/crepe/api/AudioSource.cpp @@ -10,10 +10,10 @@ AudioSource::AudioSource(game_object_id_t id, const Asset & src) : void AudioSource::play(bool looping) { this->loop = looping; - this->playing = true; + this->oneshot_play = true; } void AudioSource::stop() { - this->playing = false; + this->oneshot_stop = true; } diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 8dc1645..1899c22 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -42,8 +42,19 @@ private: //! This audio source's clip const Asset source; - //! If this source is playing audio - bool playing = false; + /** + * \name One-shot state variables + * + * These variables trigger function calls when set to true, and are unconditionally reset on + * every system update. + * + * \{ + */ + //! Play this sample + bool oneshot_play = false; + //! Stop this sample + bool oneshot_stop = false; + //! \} private: //! AudioSystem::ComponentPrivate diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp index 3e9a3d1..3ae5956 100644 --- a/src/crepe/facade/SoundContext.cpp +++ b/src/crepe/facade/SoundContext.cpp @@ -34,9 +34,3 @@ void SoundContext::set_loop(Sound & resource, Sound::Handle & handle, bool loop) this->engine.setLooping(handle.handle, loop); } -bool SoundContext::get_playing(Sound::Handle & handle) { - // See Soloud::stopVoice_internal in soloud/src/core/soloud_core_voiceops.cpp for why this is - // the correct method to use here (samples are currently never paused) - return this->engine.isValidVoiceHandle(handle.handle); -} - diff --git a/src/crepe/facade/SoundContext.h b/src/crepe/facade/SoundContext.h index e02977e..91d3fe9 100644 --- a/src/crepe/facade/SoundContext.h +++ b/src/crepe/facade/SoundContext.h @@ -28,7 +28,6 @@ public: void stop(Sound::Handle &); void set_volume(Sound &, Sound::Handle &, float); void set_loop(Sound &, Sound::Handle &, bool); - bool get_playing(Sound::Handle &); private: SoLoud::Soloud engine; diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp index 98aff58..b105a4d 100644 --- a/src/crepe/system/AudioSystem.cpp +++ b/src/crepe/system/AudioSystem.cpp @@ -13,14 +13,12 @@ void AudioSystem::update() { RefVector<AudioSource> components = component_manager.get_components_by_type<AudioSource>(); for (AudioSource & component : components) { - if (!component.active) continue; - Sound & resource = resource_manager.get<Sound>(component.source); if (component.private_data.empty()) { auto & data = component.private_data.set<ComponentPrivate>(); this->update_last(component, data); - data.last_playing = false; // always start + data.last_active = false; } auto & data = component.private_data.get<ComponentPrivate>(); @@ -31,27 +29,26 @@ void AudioSystem::update() { } void AudioSystem::diff_update(AudioSource & component, ComponentPrivate & data, Sound & resource) { - bool update_playing = component.playing != data.last_playing; bool update_volume = component.volume != data.last_volume; bool update_loop = component.loop != data.last_loop; bool update_active = component.active != data.last_active; if (update_active) { if (component.active) { - update_playing = true; - if (component.play_on_awake) - component.playing = true; + component.oneshot_play = component.play_on_awake; } else { this->context.stop(data.handle); return; } } if (!component.active) return; - if (update_playing) { - if (component.playing) data.handle = this->context.play(resource); - else this->context.stop(data.handle); - } else { - component.playing = this->context.get_playing(data.handle); + if (component.oneshot_play) { + data.handle = this->context.play(resource); + component.oneshot_play = false; + } + if (component.oneshot_stop) { + this->context.stop(data.handle); + component.oneshot_stop = false; } if (update_volume) { this->context.set_volume(resource, data.handle, component.volume); @@ -63,8 +60,8 @@ void AudioSystem::diff_update(AudioSource & component, ComponentPrivate & data, void AudioSystem::update_last(const AudioSource & component, ComponentPrivate & data) { data.last_active = component.active; + if (!component.active) return; data.last_loop = component.loop; - data.last_playing = component.playing; data.last_volume = component.volume; } diff --git a/src/crepe/system/AudioSystem.h b/src/crepe/system/AudioSystem.h index 3404878..dee82f6 100644 --- a/src/crepe/system/AudioSystem.h +++ b/src/crepe/system/AudioSystem.h @@ -26,7 +26,6 @@ private: * \{ */ typeof(AudioSource::active) last_active; - typeof(AudioSource::playing) last_playing; typeof(AudioSource::volume) last_volume; typeof(AudioSource::loop) last_loop; //! \} diff --git a/src/test/AudioTest.cpp b/src/test/AudioTest.cpp index c6f0097..2a2d0e1 100644 --- a/src/test/AudioTest.cpp +++ b/src/test/AudioTest.cpp @@ -1,3 +1,4 @@ +#include "util/Log.h" #include <gtest/gtest.h> #include <future> @@ -27,24 +28,29 @@ public: AudioSource & sfx2 = entity.add_component<AudioSource>("mwe/audio/sfx2.wav"); AudioSource & sfx3 = entity.add_component<AudioSource>("mwe/audio/sfx3.wav"); + void SetUp() override { + bgm.play_on_awake = true; + } }; TEST_F(AudioTest, Default) { bool example_done = false; future example = async([&](){ - // Start the background track - bgm.play(); + // Start the background track. This happens automatically due to the play_on_awake property + // being true. The following call is optional and doesn't start two simultanious voices if + // left in: + // bgm.play(); // Play each sample sequentially while pausing and resuming the background track this_thread::sleep_for(500ms); sfx1.play(); this_thread::sleep_for(500ms); sfx2.play(); - bgm.stop(); + bgm.active = false; this_thread::sleep_for(500ms); sfx3.play(); - bgm.play(); + bgm.active = true; this_thread::sleep_for(500ms); // Play all samples simultaniously |