diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-10 13:40:42 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-10 13:40:42 +0100 |
commit | d80c98ee03e59e0c38e025a7fe83ab4c39115fb8 (patch) | |
tree | c31c861b82d384e64071b4e7f64dfafe99b888c0 /src/crepe/system/AudioSystem.cpp | |
parent | b2c72ee8ce282bb13b0fbeb2ddb01fdfd6ad1280 (diff) | |
parent | 7a8657dfe019104aced61a5b63e63f61ad919f7a (diff) |
font progress
Diffstat (limited to 'src/crepe/system/AudioSystem.cpp')
-rw-r--r-- | src/crepe/system/AudioSystem.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp new file mode 100644 index 0000000..c1cde8b --- /dev/null +++ b/src/crepe/system/AudioSystem.cpp @@ -0,0 +1,65 @@ +#include "AudioSystem.h" + +#include "../manager/ComponentManager.h" +#include "../manager/ResourceManager.h" +#include "../types.h" + +using namespace crepe; +using namespace std; + +void AudioSystem::update() { + ComponentManager & component_manager = this->mediator.component_manager; + ResourceManager & resource_manager = this->mediator.resource_manager; + RefVector<AudioSource> components + = component_manager.get_components_by_type<AudioSource>(); + + for (AudioSource & component : components) { + Sound & resource = resource_manager.get<Sound>(component.source); + + this->diff_update(component, resource); + + this->update_last(component); + } +} + +void AudioSystem::diff_update(AudioSource & component, Sound & resource) { + SoundContext & context = this->get_context(); + + if (component.active != component.last_active) { + if (component.active) { + component.oneshot_play = component.play_on_awake; + } else { + context.stop(component.voice); + return; + } + } + if (!component.active) return; + + if (component.oneshot_play) { + component.voice = context.play(resource); + component.oneshot_play = false; + } + if (component.oneshot_stop) { + context.stop(component.voice); + component.oneshot_stop = false; + } + if (component.volume != component.last_volume) { + context.set_volume(component.voice, component.volume); + } + if (component.loop != component.last_loop) { + context.set_loop(component.voice, component.loop); + } +} + +void AudioSystem::update_last(AudioSource & component) { + component.last_active = component.active; + component.last_loop = component.loop; + component.last_volume = component.volume; +} + +SoundContext & AudioSystem::get_context() { + if (this->context == nullptr) + this->context = make_unique<SoundContext>(); + return *this->context.get(); +} + |