From c58fbbefd5a426c38b1182e9e760f149f0091670 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 13 Nov 2024 12:31:59 +0100 Subject: move some files from `loek/tests` to `loek/audio` --- src/crepe/facade/SoundContext.cpp | 5 ----- 1 file changed, 5 deletions(-) (limited to 'src/crepe/facade/SoundContext.cpp') diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp index 5e5a3a9..b5f3db3 100644 --- a/src/crepe/facade/SoundContext.cpp +++ b/src/crepe/facade/SoundContext.cpp @@ -4,11 +4,6 @@ using namespace crepe; -SoundContext & SoundContext::get_instance() { - static SoundContext instance; - return instance; -} - SoundContext::SoundContext() { dbg_trace(); engine.init(); -- cgit v1.2.3 From e4be73051a68b552c44280bbe9836dd4f02972d8 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 29 Nov 2024 20:35:17 +0100 Subject: audio system kinda working --- src/crepe/api/AudioSource.cpp | 1 - src/crepe/api/AudioSource.h | 2 -- src/crepe/api/Config.h | 25 +++++++++++++++++++++++-- src/crepe/facade/Sound.h | 7 +------ src/crepe/facade/SoundContext.cpp | 31 +++++++++++++++++++++++++++++-- src/crepe/facade/SoundContext.h | 10 ++++++++++ src/crepe/system/AudioSystem.cpp | 30 ++++++++++++++++++++++++------ src/crepe/system/AudioSystem.h | 2 +- src/crepe/util/Log.cpp | 2 +- src/crepe/util/Log.h | 16 ---------------- 10 files changed, 89 insertions(+), 37 deletions(-) (limited to 'src/crepe/facade/SoundContext.cpp') diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp index 4baac9a..c646aeb 100644 --- a/src/crepe/api/AudioSource.cpp +++ b/src/crepe/api/AudioSource.cpp @@ -15,6 +15,5 @@ void AudioSource::play(bool looping) { void AudioSource::stop() { this->playing = false; - this->rewind = true; } diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 9d76f0b..8dc1645 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -44,8 +44,6 @@ private: //! If this source is playing audio bool playing = false; - //! Rewind the sample location - bool rewind = false; private: //! AudioSystem::ComponentPrivate diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 693400a..7be506e 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -1,8 +1,10 @@ #pragma once +#include + #include "../util/Log.h" + #include "types.h" -#include namespace crepe { @@ -18,7 +20,20 @@ struct Config final { static Config & get_instance(); //! Logging-related settings - Log::Config log; + struct { + /** + * \brief Log level + * + * Only messages with equal or higher priority than this value will be logged. + */ + Log::Level level = Log::Level::INFO; + /** + * \brief Colored log output + * + * Enables log coloring using ANSI escape codes. + */ + bool color = true; + } log; //! Save manager struct { @@ -62,6 +77,12 @@ struct Config final { */ std::string root_pattern = ".crepe-root"; } asset; + + //! Audio system settings + struct { + //! Max amount of simultanious voices + unsigned int voices = 32; + } audio; }; } // namespace crepe diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h index 10d7c3c..35bccdb 100644 --- a/src/crepe/facade/Sound.h +++ b/src/crepe/facade/Sound.h @@ -20,13 +20,8 @@ public: Sound(const Asset & src); ~Sound(); // dbg_trace - class Handle { - private: + struct Handle { SoLoud::handle handle; - float volume = 1.0f; - bool looping = false; - - friend class SoundContext; }; private: diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp index b65dfb2..3e9a3d1 100644 --- a/src/crepe/facade/SoundContext.cpp +++ b/src/crepe/facade/SoundContext.cpp @@ -6,10 +6,37 @@ using namespace crepe; SoundContext::SoundContext() { dbg_trace(); - engine.init(); + this->engine.init(); + this->engine.setMaxActiveVoiceCount(this->config.audio.voices); } SoundContext::~SoundContext() { dbg_trace(); - engine.deinit(); + this->engine.deinit(); } + +Sound::Handle SoundContext::play(Sound & resource) { + return { + .handle = this->engine.play(resource.sample, this->default_volume), + }; +} + +void SoundContext::stop(Sound::Handle & handle) { + this->engine.stop(handle.handle); +} + +void SoundContext::set_volume(Sound & resource, Sound::Handle & handle, float volume) { + this->engine.setVolume(handle.handle, volume); + this->default_volume = volume; +} + +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 286ced8..e02977e 100644 --- a/src/crepe/facade/SoundContext.h +++ b/src/crepe/facade/SoundContext.h @@ -2,6 +2,8 @@ #include +#include "../api/Config.h" + #include "Sound.h" namespace crepe { @@ -22,9 +24,17 @@ public: SoundContext & operator=(const SoundContext &) = delete; SoundContext & operator=(SoundContext &&) = delete; + Sound::Handle play(Sound & resource); + 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; + float default_volume = 1.0f; + + Config & config = Config::get_instance(); }; } // namespace crepe diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp index 191dbbb..98aff58 100644 --- a/src/crepe/system/AudioSystem.cpp +++ b/src/crepe/system/AudioSystem.cpp @@ -20,6 +20,7 @@ void AudioSystem::update() { if (component.private_data.empty()) { auto & data = component.private_data.set(); this->update_last(component, data); + data.last_playing = false; // always start } auto & data = component.private_data.get(); @@ -29,18 +30,35 @@ void AudioSystem::update() { } } -void AudioSystem::diff_update(AudioSource & component, const ComponentPrivate & data, Sound & resource) { +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.rewind) { - component.playing = false; - // this->context.rewind(resource, data.handle); + if (update_active) { + if (component.active) { + update_playing = true; + if (component.play_on_awake) + component.playing = true; + } 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 (update_volume) { + this->context.set_volume(resource, data.handle, component.volume); + } + if (update_loop) { + this->context.set_loop(resource, data.handle, component.loop); } - } void AudioSystem::update_last(const AudioSource & component, ComponentPrivate & data) { diff --git a/src/crepe/system/AudioSystem.h b/src/crepe/system/AudioSystem.h index 4650178..3404878 100644 --- a/src/crepe/system/AudioSystem.h +++ b/src/crepe/system/AudioSystem.h @@ -34,7 +34,7 @@ private: void update_last(const AudioSource & component, ComponentPrivate & data); - void diff_update(AudioSource & component, const ComponentPrivate & data, Sound & resource); + void diff_update(AudioSource & component, ComponentPrivate & data, Sound & resource); private: SoundContext context {}; diff --git a/src/crepe/util/Log.cpp b/src/crepe/util/Log.cpp index bc86c7e..84d80a8 100644 --- a/src/crepe/util/Log.cpp +++ b/src/crepe/util/Log.cpp @@ -25,7 +25,7 @@ string Log::prefix(const Level & level) { } void Log::log(const Level & level, const string & msg) { - auto & cfg = crepe::Config::get_instance(); + auto & cfg = Config::get_instance(); if (level < cfg.log.level) return; string out = Log::prefix(level) + msg; diff --git a/src/crepe/util/Log.h b/src/crepe/util/Log.h index 914145a..fc0bb3a 100644 --- a/src/crepe/util/Log.h +++ b/src/crepe/util/Log.h @@ -77,22 +77,6 @@ private: * \return Colored message severity prefix string */ static std::string prefix(const Level & level); - -public: - struct Config { - /** - * \brief Log level - * - * Only messages with equal or higher priority than this value will be logged. - */ - Level level = INFO; - /** - * \brief Colored log output - * - * Enables log coloring using ANSI escape codes. - */ - bool color = true; - }; }; } // namespace crepe -- cgit v1.2.3 From 30e2b2b0cbb503d83a087d8d326940c3c4bc8fff Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 30 Nov 2024 15:08:57 +0100 Subject: fix audio system implementation --- src/crepe/api/AudioSource.cpp | 4 ++-- src/crepe/api/AudioSource.h | 15 +++++++++++++-- src/crepe/facade/SoundContext.cpp | 6 ------ src/crepe/facade/SoundContext.h | 1 - src/crepe/system/AudioSystem.cpp | 23 ++++++++++------------- src/crepe/system/AudioSystem.h | 1 - src/test/AudioTest.cpp | 14 ++++++++++---- 7 files changed, 35 insertions(+), 29 deletions(-) (limited to 'src/crepe/facade/SoundContext.cpp') 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 components = component_manager.get_components_by_type(); for (AudioSource & component : components) { - if (!component.active) continue; - Sound & resource = resource_manager.get(component.source); if (component.private_data.empty()) { auto & data = component.private_data.set(); this->update_last(component, data); - data.last_playing = false; // always start + data.last_active = false; } auto & data = component.private_data.get(); @@ -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 #include @@ -27,24 +28,29 @@ public: AudioSource & sfx2 = entity.add_component("mwe/audio/sfx2.wav"); AudioSource & sfx3 = entity.add_component("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 -- cgit v1.2.3 From 9eff2e24fa4cf0ffad2b47cc922a6558bc1a9fa1 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 30 Nov 2024 16:42:21 +0100 Subject: `make format` --- src/crepe/Resource.cpp | 3 +-- src/crepe/api/AudioSource.cpp | 12 ++++-------- src/crepe/api/AudioSource.h | 4 ++-- src/crepe/api/LoopManager.cpp | 5 +---- src/crepe/api/LoopManager.h | 2 +- src/crepe/api/Scene.h | 3 ++- src/crepe/api/Script.cpp | 1 - src/crepe/api/Script.h | 4 ++-- src/crepe/facade/Sound.cpp | 9 ++++----- src/crepe/facade/SoundContext.cpp | 5 +---- src/crepe/manager/ComponentManager.cpp | 2 +- src/crepe/manager/Manager.cpp | 3 +-- src/crepe/manager/Manager.h | 3 +-- src/crepe/manager/Mediator.h | 4 ++-- src/crepe/manager/ResourceManager.cpp | 8 ++------ src/crepe/manager/ResourceManager.hpp | 13 +++++++------ src/crepe/manager/SaveManager.cpp | 4 ++-- src/crepe/system/AudioSystem.cpp | 10 +++++----- src/crepe/system/AudioSystem.h | 6 +++--- src/crepe/system/PhysicsSystem.cpp | 2 +- src/crepe/system/RenderSystem.cpp | 2 +- src/crepe/system/ScriptSystem.cpp | 2 +- src/crepe/util/Private.cpp | 17 +++++------------ src/crepe/util/Private.h | 5 ++--- src/crepe/util/Private.hpp | 18 ++++++++---------- src/test/AudioTest.cpp | 15 +++++++-------- src/test/ECSTest.cpp | 3 ++- src/test/EventTest.cpp | 6 ++---- src/test/ParticleTest.cpp | 3 ++- src/test/PhysicsTest.cpp | 3 ++- src/test/PrivateTest.cpp | 5 +---- src/test/RenderSystemTest.cpp | 3 ++- src/test/ResourceManagerTest.cpp | 16 ++++++---------- src/test/SceneManagerTest.cpp | 7 ++++--- src/test/ScriptEventTest.cpp | 7 +++---- src/test/ScriptSceneTest.cpp | 3 +-- src/test/ScriptTest.cpp | 3 +-- src/test/ScriptTest.h | 8 +++++--- src/test/main.cpp | 3 +-- 39 files changed, 99 insertions(+), 133 deletions(-) (limited to 'src/crepe/facade/SoundContext.cpp') diff --git a/src/crepe/Resource.cpp b/src/crepe/Resource.cpp index e254695..27b4c4b 100644 --- a/src/crepe/Resource.cpp +++ b/src/crepe/Resource.cpp @@ -2,5 +2,4 @@ using namespace crepe; -Resource::Resource(const Asset & asset) { } - +Resource::Resource(const Asset & asset) {} diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp index cc70801..7b05cb1 100644 --- a/src/crepe/api/AudioSource.cpp +++ b/src/crepe/api/AudioSource.cpp @@ -3,17 +3,13 @@ using namespace crepe; using namespace std; -AudioSource::AudioSource(game_object_id_t id, const Asset & src) : - Component(id), - source(src) -{ } +AudioSource::AudioSource(game_object_id_t id, const Asset & src) + : Component(id), + source(src) {} void AudioSource::play(bool looping) { this->loop = looping; this->oneshot_play = true; } -void AudioSource::stop() { - this->oneshot_stop = true; -} - +void AudioSource::stop() { this->oneshot_stop = true; } diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 1899c22..63b4bc4 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -4,8 +4,8 @@ #include "../types.h" #include "../util/Private.h" -#include "GameObject.h" #include "Asset.h" +#include "GameObject.h" namespace crepe { @@ -20,6 +20,7 @@ protected: AudioSource(game_object_id_t id, const Asset & source); //! Only ComponentManager can create components friend class ComponentManager; + public: // But std::unique_ptr needs to be able to destoy this component again virtual ~AudioSource() = default; @@ -62,4 +63,3 @@ private: }; } // namespace crepe - diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index b277185..731cfb7 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -22,9 +22,7 @@ LoopManager::LoopManager() { this->load_system(); } -void LoopManager::process_input() { - this->sdl_context.handle_events(this->game_running); -} +void LoopManager::process_input() { this->sdl_context.handle_events(this->game_running); } void LoopManager::start() { this->setup(); @@ -69,4 +67,3 @@ void LoopManager::render() { } void LoopManager::update() {} - diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 6ea5ccc..d8910a0 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -2,10 +2,10 @@ #include +#include "../facade/SDLContext.h" #include "../manager/ComponentManager.h" #include "../manager/SceneManager.h" #include "../system/System.h" -#include "../facade/SDLContext.h" #include "LoopTimer.h" diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h index 66dad17..9f1e8ce 100644 --- a/src/crepe/api/Scene.h +++ b/src/crepe/api/Scene.h @@ -2,8 +2,8 @@ #include -#include "../util/OptionalRef.h" #include "../manager/Mediator.h" +#include "../util/OptionalRef.h" namespace crepe { @@ -37,6 +37,7 @@ public: // TODO: Late references should ALWAYS be private! This is currently kept as-is so unit tests // keep passing, but this reference should not be directly accessible by the user!!! + protected: /** * \name Late references diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp index a27838e..4091fd4 100644 --- a/src/crepe/api/Script.cpp +++ b/src/crepe/api/Script.cpp @@ -25,4 +25,3 @@ void Script::set_next_scene(const string & name) { SceneManager & mgr = mediator.scene_manager; mgr.set_next_scene(name); } - diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index e1f86b2..1b339b0 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -2,10 +2,10 @@ #include +#include "../manager/EventManager.h" +#include "../manager/Mediator.h" #include "../types.h" #include "../util/OptionalRef.h" -#include "../manager/Mediator.h" -#include "../manager/EventManager.h" namespace crepe { diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp index 0df1f48..33a0c47 100644 --- a/src/crepe/facade/Sound.cpp +++ b/src/crepe/facade/Sound.cpp @@ -23,26 +23,25 @@ Sound::~Sound() { dbg_trace(); } // ctx.engine.setLooping(this->handle, this->looping); // } // } -// +// // void Sound::pause(SoundContext & ctx) { // if (ctx.engine.getPause(this->handle)) return; // ctx.engine.setPause(this->handle, true); // } -// +// // void Sound::rewind(SoundContext & ctx) { // if (!ctx.engine.isValidVoiceHandle(this->handle)) return; // ctx.engine.seek(this->handle, 0); // } -// +// // void Sound::set_volume(SoundContext & ctx, float volume) { // this->volume = volume; // if (!ctx.engine.isValidVoiceHandle(this->handle)) return; // ctx.engine.setVolume(this->handle, this->volume); // } -// +// // void Sound::set_looping(SoundContext & ctx, bool looping) { // this->looping = looping; // if (!ctx.engine.isValidVoiceHandle(this->handle)) return; // ctx.engine.setLooping(this->handle, this->looping); // } - diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp index 3ae5956..470b3cc 100644 --- a/src/crepe/facade/SoundContext.cpp +++ b/src/crepe/facade/SoundContext.cpp @@ -21,9 +21,7 @@ Sound::Handle SoundContext::play(Sound & resource) { }; } -void SoundContext::stop(Sound::Handle & handle) { - this->engine.stop(handle.handle); -} +void SoundContext::stop(Sound::Handle & handle) { this->engine.stop(handle.handle); } void SoundContext::set_volume(Sound & resource, Sound::Handle & handle, float volume) { this->engine.setVolume(handle.handle, volume); @@ -33,4 +31,3 @@ void SoundContext::set_volume(Sound & resource, Sound::Handle & handle, float vo void SoundContext::set_loop(Sound & resource, Sound::Handle & handle, bool loop) { this->engine.setLooping(handle.handle, loop); } - diff --git a/src/crepe/manager/ComponentManager.cpp b/src/crepe/manager/ComponentManager.cpp index 5a96158..80cf8b4 100644 --- a/src/crepe/manager/ComponentManager.cpp +++ b/src/crepe/manager/ComponentManager.cpp @@ -1,6 +1,6 @@ #include "../api/GameObject.h" -#include "../util/Log.h" #include "../types.h" +#include "../util/Log.h" #include "ComponentManager.h" diff --git a/src/crepe/manager/Manager.cpp b/src/crepe/manager/Manager.cpp index fe7c936..1182785 100644 --- a/src/crepe/manager/Manager.cpp +++ b/src/crepe/manager/Manager.cpp @@ -2,5 +2,4 @@ using namespace crepe; -Manager::Manager(Mediator & mediator) : mediator(mediator) { } - +Manager::Manager(Mediator & mediator) : mediator(mediator) {} diff --git a/src/crepe/manager/Manager.h b/src/crepe/manager/Manager.h index 9adfd0b..4f21ef4 100644 --- a/src/crepe/manager/Manager.h +++ b/src/crepe/manager/Manager.h @@ -13,5 +13,4 @@ protected: Mediator & mediator; }; -} - +} // namespace crepe diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index 475aed9..e9c10b1 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -3,8 +3,8 @@ #include "../util/OptionalRef.h" // TODO: remove these singletons: -#include "SaveManager.h" #include "EventManager.h" +#include "SaveManager.h" namespace crepe { @@ -32,4 +32,4 @@ struct Mediator { OptionalRef resource_manager; }; -} +} // namespace crepe diff --git a/src/crepe/manager/ResourceManager.cpp b/src/crepe/manager/ResourceManager.cpp index 87585ad..7c01808 100644 --- a/src/crepe/manager/ResourceManager.cpp +++ b/src/crepe/manager/ResourceManager.cpp @@ -18,17 +18,13 @@ void ResourceManager::clear() { }); } -void ResourceManager::clear_all() { - this->resources.clear(); -} +void ResourceManager::clear_all() { this->resources.clear(); } void ResourceManager::set_persistent(const Asset & asset, bool persistent) { this->get_entry(asset).persistent = persistent; } ResourceManager::CacheEntry & ResourceManager::get_entry(const Asset & asset) { - if (!this->resources.contains(asset)) - this->resources[asset] = {}; + if (!this->resources.contains(asset)) this->resources[asset] = {}; return this->resources.at(asset); } - diff --git a/src/crepe/manager/ResourceManager.hpp b/src/crepe/manager/ResourceManager.hpp index 8270bc5..5167d71 100644 --- a/src/crepe/manager/ResourceManager.hpp +++ b/src/crepe/manager/ResourceManager.hpp @@ -9,18 +9,19 @@ namespace crepe { template T & ResourceManager::get(const Asset & asset) { using namespace std; - static_assert(is_base_of::value, "cache must recieve a derivative class of Resource"); + static_assert(is_base_of::value, + "cache must recieve a derivative class of Resource"); CacheEntry & entry = this->get_entry(asset); - if (entry.resource == nullptr) - entry.resource = make_unique(asset); + if (entry.resource == nullptr) entry.resource = make_unique(asset); T * concrete_resource = dynamic_cast(entry.resource.get()); if (concrete_resource == nullptr) - throw runtime_error(format("ResourceManager: mismatch between requested type and actual type of resource ({})", asset.get_path())); + throw runtime_error(format("ResourceManager: mismatch between requested type and " + "actual type of resource ({})", + asset.get_path())); return *concrete_resource; } -} - +} // namespace crepe diff --git a/src/crepe/manager/SaveManager.cpp b/src/crepe/manager/SaveManager.cpp index 121d017..d4ed1c1 100644 --- a/src/crepe/manager/SaveManager.cpp +++ b/src/crepe/manager/SaveManager.cpp @@ -1,7 +1,7 @@ +#include "../ValueBroker.h" +#include "../api/Config.h" #include "../facade/DB.h" #include "../util/Log.h" -#include "../api/Config.h" -#include "../ValueBroker.h" #include "SaveManager.h" diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp index 84a101a..0696b34 100644 --- a/src/crepe/system/AudioSystem.cpp +++ b/src/crepe/system/AudioSystem.cpp @@ -10,7 +10,8 @@ using namespace std; void AudioSystem::update() { ComponentManager & component_manager = this->mediator.component_manager; ResourceManager & resource_manager = this->mediator.resource_manager; - RefVector components = component_manager.get_components_by_type(); + RefVector components + = component_manager.get_components_by_type(); for (AudioSource & component : components) { Sound & resource = resource_manager.get(component.source); @@ -28,7 +29,8 @@ void AudioSystem::update() { } } -void AudioSystem::diff_update(AudioSource & component, ComponentPrivate & data, Sound & resource) { +void AudioSystem::diff_update(AudioSource & component, ComponentPrivate & data, + Sound & resource) { SoundContext & context = this->get_context(); if (component.active != data.last_active) { @@ -64,8 +66,6 @@ void AudioSystem::update_last(const AudioSource & component, ComponentPrivate & } SoundContext & AudioSystem::get_context() { - if (this->context.empty()) - this->context.set(); + if (this->context.empty()) this->context.set(); return this->context.get(); } - diff --git a/src/crepe/system/AudioSystem.h b/src/crepe/system/AudioSystem.h index a004c60..c941470 100644 --- a/src/crepe/system/AudioSystem.h +++ b/src/crepe/system/AudioSystem.h @@ -1,8 +1,8 @@ #pragma once -#include "../facade/SoundContext.h" -#include "../facade/Sound.h" #include "../api/AudioSource.h" +#include "../facade/Sound.h" +#include "../facade/SoundContext.h" #include "System.h" @@ -37,9 +37,9 @@ private: protected: virtual SoundContext & get_context(); + private: Private context; }; } // namespace crepe - diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp index eba9dfa..bebcf3d 100644 --- a/src/crepe/system/PhysicsSystem.cpp +++ b/src/crepe/system/PhysicsSystem.cpp @@ -1,10 +1,10 @@ #include -#include "../manager/ComponentManager.h" #include "../api/Config.h" #include "../api/Rigidbody.h" #include "../api/Transform.h" #include "../api/Vector2.h" +#include "../manager/ComponentManager.h" #include "PhysicsSystem.h" diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 4e97b3e..0ad685c 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -5,12 +5,12 @@ #include #include -#include "../manager/ComponentManager.h" #include "../api/Camera.h" #include "../api/ParticleEmitter.h" #include "../api/Sprite.h" #include "../api/Transform.h" #include "../facade/SDLContext.h" +#include "../manager/ComponentManager.h" #include "RenderSystem.h" diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index 2e16eb0..d6b2ca1 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -1,6 +1,6 @@ -#include "../manager/ComponentManager.h" #include "../api/BehaviorScript.h" #include "../api/Script.h" +#include "../manager/ComponentManager.h" #include "ScriptSystem.h" diff --git a/src/crepe/util/Private.cpp b/src/crepe/util/Private.cpp index cb4cb5b..262620d 100644 --- a/src/crepe/util/Private.cpp +++ b/src/crepe/util/Private.cpp @@ -2,18 +2,14 @@ using namespace crepe; -bool Private::empty() const noexcept { - return this->instance == nullptr; -} +bool Private::empty() const noexcept { return this->instance == nullptr; } Private::~Private() { if (this->instance == nullptr) return; this->destructor(this->instance); } -Private::Private(Private && other) { - *this = std::move(other); -} +Private::Private(Private && other) { *this = std::move(other); } Private & Private::operator=(Private && other) { // TODO: ideally this function checks for self-assignment @@ -22,13 +18,10 @@ Private & Private::operator=(Private && other) { this->type = other.type; other.instance = nullptr; - other.destructor = [](void*){}; - - return *this; -} + other.destructor = [](void *) {}; -Private::Private(const Private & other) { } -Private & Private::operator=(const Private & other) { return *this; } +Private::Private(const Private & other) {} +Private & Private::operator=(const Private & other) { return *this; } diff --git a/src/crepe/util/Private.h b/src/crepe/util/Private.h index 6dd28bb..62a2e1a 100644 --- a/src/crepe/util/Private.h +++ b/src/crepe/util/Private.h @@ -1,7 +1,7 @@ #pragma once -#include #include +#include namespace crepe { @@ -28,7 +28,6 @@ private: void * instance = nullptr; }; -} +} // namespace crepe #include "Private.hpp" - diff --git a/src/crepe/util/Private.hpp b/src/crepe/util/Private.hpp index d6ab23f..3a87a9f 100644 --- a/src/crepe/util/Private.hpp +++ b/src/crepe/util/Private.hpp @@ -1,7 +1,7 @@ #pragma once -#include #include +#include #include "Private.h" @@ -11,10 +11,8 @@ template T & Private::set(Args &&... args) { if (!this->empty()) this->destructor(this->instance); T * instance = new T(std::forward(args)...); - this->instance = static_cast(instance); - this->destructor = [](void * instance) { - delete static_cast(instance); - }; + this->instance = static_cast(instance); + this->destructor = [](void * instance) { delete static_cast(instance); }; this->type = typeid(T); return *instance; } @@ -22,12 +20,12 @@ T & Private::set(Args &&... args) { template T & Private::get() { using namespace std; - if (this->empty()) - throw out_of_range("Private: get() called on empty object"); + if (this->empty()) throw out_of_range("Private: get() called on empty object"); type_index requested_type = typeid(T); if (this->type != requested_type) - throw logic_error(format("Private: get() called with [T = {}] (actual is [T = {}])", requested_type.name(), this->type.name())); - return *static_cast(this->instance); + throw logic_error(format("Private: get() called with [T = {}] (actual is [T = {}])", + requested_type.name(), this->type.name())); + return *static_cast(this->instance); } -} +} // namespace crepe diff --git a/src/test/AudioTest.cpp b/src/test/AudioTest.cpp index 9c3cb9c..14f57bd 100644 --- a/src/test/AudioTest.cpp +++ b/src/test/AudioTest.cpp @@ -1,10 +1,10 @@ -#include #include +#include -#include -#include #include #include +#include +#include #include using namespace std; @@ -26,21 +26,21 @@ private: public: using AudioSystem::AudioSystem; StrictMock context; - virtual SoundContext & get_context() { - return this->context; - } + virtual SoundContext & get_context() { return this->context; } }; private: Mediator mediator; ComponentManager component_manager{mediator}; ResourceManager resource_manager{mediator}; + public: - TestAudioSystem system {mediator}; + TestAudioSystem system{mediator}; TestSoundContext & context = system.context; private: GameObject entity = component_manager.new_object("name"); + public: AudioSource & component = entity.add_component("mwe/audio/bgm.ogg"); }; @@ -150,4 +150,3 @@ TEST_F(AudioTest, PlayOnActive) { system.update(); } } - diff --git a/src/test/ECSTest.cpp b/src/test/ECSTest.cpp index 22c4fe7..3e6c61c 100644 --- a/src/test/ECSTest.cpp +++ b/src/test/ECSTest.cpp @@ -2,17 +2,18 @@ #define protected public -#include #include #include #include #include +#include using namespace std; using namespace crepe; class ECSTest : public ::testing::Test { Mediator m; + public: ComponentManager mgr{m}; }; diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp index 350dd07..dccd554 100644 --- a/src/test/EventTest.cpp +++ b/src/test/EventTest.cpp @@ -2,9 +2,9 @@ #include #include -#include #include #include +#include using namespace std; using namespace std::chrono_literals; @@ -37,9 +37,7 @@ public: }; TEST_F(EventManagerTest, EventSubscription) { - EventHandler key_handler = [](const KeyPressEvent & e) { - return true; - }; + EventHandler key_handler = [](const KeyPressEvent & e) { return true; }; // Subscribe to KeyPressEvent EventManager::get_instance().subscribe(key_handler, 1); diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index 4e9fa4e..a659fe5 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -7,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -17,6 +17,7 @@ using namespace crepe; class ParticlesTest : public ::testing::Test { Mediator m; + public: ComponentManager component_manager{m}; ParticleSystem particle_system{m}; diff --git a/src/test/PhysicsTest.cpp b/src/test/PhysicsTest.cpp index 01b7c51..43af8e4 100644 --- a/src/test/PhysicsTest.cpp +++ b/src/test/PhysicsTest.cpp @@ -1,8 +1,8 @@ -#include #include #include #include #include +#include #include #include @@ -12,6 +12,7 @@ using namespace crepe; class PhysicsTest : public ::testing::Test { Mediator m; + public: ComponentManager component_manager{m}; PhysicsSystem system{m}; diff --git a/src/test/PrivateTest.cpp b/src/test/PrivateTest.cpp index 0ea67d6..454789e 100644 --- a/src/test/PrivateTest.cpp +++ b/src/test/PrivateTest.cpp @@ -27,9 +27,7 @@ unsigned PrivateTest::constructors; unsigned PrivateTest::destructors; TEST_F(PrivateTest, Empty) { - { - Private foo; - } + { Private foo; } EXPECT_EQ(PrivateTest::constructors, 0); EXPECT_EQ(PrivateTest::destructors, 0); @@ -155,4 +153,3 @@ TEST_F(PrivateTest, DoubleAssignment) { EXPECT_EQ(PrivateTest::constructors, 2); EXPECT_EQ(PrivateTest::destructors, 2); } - diff --git a/src/test/RenderSystemTest.cpp b/src/test/RenderSystemTest.cpp index 3528e46..c105dcb 100644 --- a/src/test/RenderSystemTest.cpp +++ b/src/test/RenderSystemTest.cpp @@ -7,11 +7,11 @@ #define protected public #include -#include #include #include #include #include +#include #include @@ -21,6 +21,7 @@ using namespace testing; class RenderSystemTest : public Test { Mediator m; + public: ComponentManager mgr{m}; RenderSystem sys{m}; diff --git a/src/test/ResourceManagerTest.cpp b/src/test/ResourceManagerTest.cpp index 1f56e23..b6be3c0 100644 --- a/src/test/ResourceManagerTest.cpp +++ b/src/test/ResourceManagerTest.cpp @@ -3,9 +3,9 @@ #define private public #define protected public -#include -#include #include +#include +#include using namespace std; using namespace crepe; @@ -13,6 +13,7 @@ using namespace testing; class ResourceManagerTest : public Test { Mediator mediator; + public: ResourceManager resource_manager{mediator}; @@ -25,19 +26,15 @@ public: public: const unsigned instance; - TestResource(const Asset & src) - : Resource(src), - instance(this->instances++) { } + TestResource(const Asset & src) : Resource(src), instance(this->instances++) {} ~TestResource() { this->instances--; } - bool operator == (const TestResource & other) const { + bool operator==(const TestResource & other) const { return this->instance == other.instance; } }; private: - void SetUp() override { - TestResource::instances = 0; - } + void SetUp() override { TestResource::instances = 0; } }; unsigned ResourceManagerTest::TestResource::instances = 0; @@ -72,4 +69,3 @@ TEST_F(ResourceManagerTest, Persistent) { resource_manager.clear_all(); EXPECT_EQ(TestResource::instances, 0); } - diff --git a/src/test/SceneManagerTest.cpp b/src/test/SceneManagerTest.cpp index d027d89..9bb260c 100644 --- a/src/test/SceneManagerTest.cpp +++ b/src/test/SceneManagerTest.cpp @@ -1,13 +1,13 @@ #include -#include -#include -#include #include #include #include #include #include +#include +#include +#include using namespace std; using namespace crepe; @@ -57,6 +57,7 @@ private: class SceneManagerTest : public ::testing::Test { Mediator m; + public: ComponentManager component_mgr{m}; SceneManager scene_mgr{m}; diff --git a/src/test/ScriptEventTest.cpp b/src/test/ScriptEventTest.cpp index 7a9abbb..5da31e7 100644 --- a/src/test/ScriptEventTest.cpp +++ b/src/test/ScriptEventTest.cpp @@ -4,13 +4,13 @@ #define private public #define protected public -#include -#include #include #include #include #include #include +#include +#include #include #include "ScriptTest.h" @@ -32,7 +32,7 @@ TEST_F(ScriptEventTest, Inactive) { EventManager & evmgr = this->event_manager; unsigned event_count = 0; - script.subscribe([&](const MyEvent &){ + script.subscribe([&](const MyEvent &) { event_count++; return true; }); @@ -48,4 +48,3 @@ TEST_F(ScriptEventTest, Inactive) { evmgr.trigger_event(); EXPECT_EQ(1, event_count); } - diff --git a/src/test/ScriptSceneTest.cpp b/src/test/ScriptSceneTest.cpp index f96ae8b..9ee1e52 100644 --- a/src/test/ScriptSceneTest.cpp +++ b/src/test/ScriptSceneTest.cpp @@ -4,8 +4,8 @@ #define private public #define protected public -#include #include "ScriptTest.h" +#include using namespace std; using namespace crepe; @@ -28,4 +28,3 @@ TEST_F(ScriptSceneTest, Inactive) { script.set_next_scene(non_default_value); EXPECT_EQ(non_default_value, scene_manager.next_scene); } - diff --git a/src/test/ScriptTest.cpp b/src/test/ScriptTest.cpp index 6d0d5fb..1d2d6dd 100644 --- a/src/test/ScriptTest.cpp +++ b/src/test/ScriptTest.cpp @@ -1,5 +1,5 @@ -#include #include +#include // stupid hack to allow access to private/protected members under test #define private public @@ -75,4 +75,3 @@ TEST_F(ScriptTest, UpdateInactive) { system.update(); } } - diff --git a/src/test/ScriptTest.h b/src/test/ScriptTest.h index 9a71ba7..1bbfdd3 100644 --- a/src/test/ScriptTest.h +++ b/src/test/ScriptTest.h @@ -1,22 +1,24 @@ #pragma once -#include #include +#include -#include -#include #include #include +#include +#include class ScriptTest : public testing::Test { protected: crepe::Mediator mediator; + public: crepe::ComponentManager component_manager{mediator}; crepe::ScriptSystem system{mediator}; class MyScript : public crepe::Script { // NOTE: explicitly stating `public:` is not required on actual scripts + public: MOCK_METHOD(void, init, (), (override)); MOCK_METHOD(void, update, (), (override)); diff --git a/src/test/main.cpp b/src/test/main.cpp index 54f74fd..ed2aed5 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -1,5 +1,5 @@ -#include #include +#include using namespace crepe; using namespace testing; @@ -26,4 +26,3 @@ int main(int argc, char ** argv) { return RUN_ALL_TESTS(); } - -- cgit v1.2.3 From b8194e02679dc88f5c0a240da83a4700ec5200cf Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 30 Nov 2024 17:27:16 +0100 Subject: add doxygen comments + clean up --- src/crepe/Resource.h | 18 +++++------ src/crepe/api/AudioSource.h | 9 ++++-- src/crepe/facade/Sound.cpp | 34 --------------------- src/crepe/facade/Sound.h | 9 ++++-- src/crepe/facade/SoundContext.cpp | 7 ++--- src/crepe/facade/SoundContext.h | 48 +++++++++++++++++++++++++---- src/crepe/manager/ResourceManager.h | 44 ++++++++++++++++++++++----- src/crepe/system/AudioSystem.cpp | 4 +-- src/crepe/system/AudioSystem.h | 29 ++++++++++++++++-- src/crepe/util/Private.h | 60 +++++++++++++++++++++++++++++++++++-- src/crepe/util/Private.hpp | 2 +- src/test/AudioTest.cpp | 16 +++++----- 12 files changed, 197 insertions(+), 83 deletions(-) (limited to 'src/crepe/facade/SoundContext.cpp') diff --git a/src/crepe/Resource.h b/src/crepe/Resource.h index a0c8859..a2d65df 100644 --- a/src/crepe/Resource.h +++ b/src/crepe/Resource.h @@ -6,21 +6,19 @@ class ResourceManager; class Asset; /** - * Resource is an interface class used to represent a (deserialized) game - * resource (e.g. textures, sounds). + * \brief Resource interface + * + * Resource is an interface class used to represent a (deserialized) game resource (e.g. + * textures, sounds). Resources are always created from \ref Asset "assets" by ResourceManager. + * + * The game programmer has the ability to use the ResourceManager to keep instances of concrete + * resources between scenes, preventing them from being reinstantiated during a scene + * transition. */ class Resource { public: Resource(const Asset & src); virtual ~Resource() = default; - -private: - /** - * The resource manager uses \c clone to create new instances of the concrete - * resource class. This may be used to inherit references to classes that - * would otherwise need to be implemented as singletons. - */ - friend class ResourceManager; }; } // namespace crepe diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 63b4bc4..330e8e1 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -17,16 +17,19 @@ class AudioSource : public Component { friend class AudioSystem; protected: + /** + * \param source Sound sample to load + */ AudioSource(game_object_id_t id, const Asset & source); - //! Only ComponentManager can create components + //! Only ComponentManager creates components friend class ComponentManager; public: - // But std::unique_ptr needs to be able to destoy this component again + // std::unique_ptr needs to be able to destoy this component virtual ~AudioSource() = default; public: - //! Start or resume this audio source + //! Start this audio source void play(bool looping = false); //! Stop this audio source void stop(); diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp index 33a0c47..ad50637 100644 --- a/src/crepe/facade/Sound.cpp +++ b/src/crepe/facade/Sound.cpp @@ -2,7 +2,6 @@ #include "../util/Log.h" #include "Sound.h" -#include "SoundContext.h" using namespace crepe; using namespace std; @@ -12,36 +11,3 @@ Sound::Sound(const Asset & src) : Resource(src) { dbg_trace(); } Sound::~Sound() { dbg_trace(); } - -// void Sound::play(SoundContext & ctx) { -// if (ctx.engine.getPause(this->handle)) { -// // resume if paused -// ctx.engine.setPause(this->handle, false); -// } else { -// // or start new sound -// this->handle = ctx.engine.play(this->sample, this->volume); -// ctx.engine.setLooping(this->handle, this->looping); -// } -// } -// -// void Sound::pause(SoundContext & ctx) { -// if (ctx.engine.getPause(this->handle)) return; -// ctx.engine.setPause(this->handle, true); -// } -// -// void Sound::rewind(SoundContext & ctx) { -// if (!ctx.engine.isValidVoiceHandle(this->handle)) return; -// ctx.engine.seek(this->handle, 0); -// } -// -// void Sound::set_volume(SoundContext & ctx, float volume) { -// this->volume = volume; -// if (!ctx.engine.isValidVoiceHandle(this->handle)) return; -// ctx.engine.setVolume(this->handle, this->volume); -// } -// -// void Sound::set_looping(SoundContext & ctx, bool looping) { -// this->looping = looping; -// if (!ctx.engine.isValidVoiceHandle(this->handle)) return; -// ctx.engine.setLooping(this->handle, this->looping); -// } diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h index 35bccdb..a78a2a7 100644 --- a/src/crepe/facade/Sound.h +++ b/src/crepe/facade/Sound.h @@ -12,21 +12,24 @@ class SoundContext; /** * \brief Sound resource facade * - * This class is a wrapper around a \c SoLoud::Wav instance, which holds a - * single sample. It is part of the sound facade. + * This class is a wrapper around a \c SoLoud::Wav instance, which holds a single sample. It is + * part of the sound facade. */ class Sound : public Resource { public: Sound(const Asset & src); ~Sound(); // dbg_trace + //! Voice handle wrapper struct Handle { + //! Voice handle (soloud), used by SoundContext SoLoud::handle handle; }; private: + //! Deserialized resource (soloud) SoLoud::Wav sample; - + //! SoundContext uses \c sample friend class SoundContext; }; diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp index 470b3cc..8bd7e74 100644 --- a/src/crepe/facade/SoundContext.cpp +++ b/src/crepe/facade/SoundContext.cpp @@ -17,17 +17,16 @@ SoundContext::~SoundContext() { Sound::Handle SoundContext::play(Sound & resource) { return { - .handle = this->engine.play(resource.sample, this->default_volume), + .handle = this->engine.play(resource.sample, 1.0f), }; } void SoundContext::stop(Sound::Handle & handle) { this->engine.stop(handle.handle); } -void SoundContext::set_volume(Sound & resource, Sound::Handle & handle, float volume) { +void SoundContext::set_volume(Sound::Handle & handle, float volume) { this->engine.setVolume(handle.handle, volume); - this->default_volume = volume; } -void SoundContext::set_loop(Sound & resource, Sound::Handle & handle, bool loop) { +void SoundContext::set_loop(Sound::Handle & handle, bool loop) { this->engine.setLooping(handle.handle, loop); } diff --git a/src/crepe/facade/SoundContext.h b/src/crepe/facade/SoundContext.h index c651cd5..3bc8be5 100644 --- a/src/crepe/facade/SoundContext.h +++ b/src/crepe/facade/SoundContext.h @@ -11,8 +11,8 @@ namespace crepe { /** * \brief Sound engine facade * - * This class is a wrapper around a \c SoLoud::Soloud instance, which provides - * the methods for playing \c Sound instances. It is part of the sound facade. + * This class is a wrapper around a \c SoLoud::Soloud instance, which provides the methods for + * playing \c Sound instances. It is part of the sound facade. */ class SoundContext { public: @@ -24,15 +24,51 @@ public: SoundContext & operator=(const SoundContext &) = delete; SoundContext & operator=(SoundContext &&) = delete; + /** + * \brief Play a sample + * + * Plays a Sound from the beginning of the sample and returns a handle to control it later. + * + * \param resource Sound instance to play + * + * \returns Handle to control this voice + */ virtual Sound::Handle play(Sound & resource); - virtual void stop(Sound::Handle &); - virtual void set_volume(Sound &, Sound::Handle &, float); - virtual void set_loop(Sound &, Sound::Handle &, bool); + /** + * \brief Stop a voice immediately if it is still playing + * + * \note This function does nothing if the handle is invalid or if the sound is already + * stopped / finished playing. + * + * \param handle Voice handle returned by SoundContext::play + */ + virtual void stop(Sound::Handle & handle); + /** + * \brief Change the volume of a voice + * + * \note This function does nothing if the handle is invalid or if the sound is already + * stopped / finished playing. + * + * \param handle Voice handle returned by SoundContext::play + * \param volume New gain value (0=silent, 1=default) + */ + virtual void set_volume(Sound::Handle & handle, float volume); + /** + * \brief Set the looping behavior of a voice + * + * \note This function does nothing if the handle is invalid or if the sound is already + * stopped / finished playing. + * + * \param handle Voice handle returned by SoundContext::play + * \param loop Looping behavior (false=oneshot, true=loop) + */ + virtual void set_loop(Sound::Handle & handle, bool loop); private: + //! Abstracted class SoLoud::Soloud engine; - float default_volume = 1.0f; + //! Config reference Config & config = Config::get_instance(); }; diff --git a/src/crepe/manager/ResourceManager.h b/src/crepe/manager/ResourceManager.h index e7e6abc..84b275d 100644 --- a/src/crepe/manager/ResourceManager.h +++ b/src/crepe/manager/ResourceManager.h @@ -11,13 +11,11 @@ namespace crepe { /** - * \brief The ResourceManager is responsible for storing and managing assets over - * multiple scenes. + * \brief Owner of concrete Resource instances * - * The ResourceManager ensures that assets are loaded once and can be accessed - * across different scenes. It caches assets to avoid reloading them every time - * a scene is loaded. Assets are retained in memory until the ResourceManager is - * destroyed, at which point the cached assets are cleared. + * ResourceManager caches concrete Resource instances per Asset. Concrete resources are + * destroyed at the end of scenes by default, unless the game programmer marks them as + * persistent. */ class ResourceManager : public Manager { public: @@ -25,21 +23,53 @@ public: virtual ~ResourceManager(); // dbg_trace private: + //! Cache entry struct CacheEntry { + //! Concrete resource instance std::unique_ptr resource = nullptr; + //! Prevent ResourceManager::clear from removing this entry bool persistent = false; }; - //! A cache that holds all the assets, accessible by their file path, over multiple scenes. + //! Internal cache std::unordered_map resources; + /** + * \brief Ensure a cache entry exists for this asset and return a mutable reference to it + * + * \param asset Asset the concrete resource is instantiated from + * + * \returns Mutable reference to cache entry + */ CacheEntry & get_entry(const Asset & asset); public: + /** + * \brief Mark a resource as persistent (i.e. used across multiple scenes) + * + * \param asset Asset the concrete resource is instantiated from + * \param persistent Whether this resource is persistent (true=keep, false=destroy) + */ void set_persistent(const Asset & asset, bool persistent); + /** + * \brief Retrieve reference to concrete Resource by Asset + * + * \param asset Asset the concrete resource is instantiated from + * \tparam Resource Concrete derivative of Resource + * + * This class instantiates the concrete resource if it is not yet stored in the internal + * cache, or returns a reference to the cached resource if it already exists. + * + * \returns Reference to concrete resource + * + * \throws std::runtime_error if the \c Resource parameter does not match with the actual + * type of the resource stored in the cache for this Asset + */ template Resource & get(const Asset & asset); + //! Clear non-persistent resources from cache void clear(); + //! Clear all resources from cache regardless of persistence void clear_all(); }; diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp index 0696b34..26913c0 100644 --- a/src/crepe/system/AudioSystem.cpp +++ b/src/crepe/system/AudioSystem.cpp @@ -52,10 +52,10 @@ void AudioSystem::diff_update(AudioSource & component, ComponentPrivate & data, component.oneshot_stop = false; } if (component.volume != data.last_volume) { - context.set_volume(resource, data.handle, component.volume); + context.set_volume(data.handle, component.volume); } if (component.loop != data.last_loop) { - context.set_loop(resource, data.handle, component.loop); + context.set_loop(data.handle, component.loop); } } diff --git a/src/crepe/system/AudioSystem.h b/src/crepe/system/AudioSystem.h index c941470..4d21883 100644 --- a/src/crepe/system/AudioSystem.h +++ b/src/crepe/system/AudioSystem.h @@ -14,9 +14,7 @@ public: void update() override; private: - /** - * \brief Private data stored by AudioSystem on AudioSource component - */ + //! Private data stored by AudioSystem on AudioSource component struct ComponentPrivate { //! This sample's voice handle Sound::Handle handle; @@ -31,14 +29,39 @@ private: //! \} }; + /** + * \brief Update `last_*` members of \c data + * + * Copies all component properties stored for comparison between AudioSystem::update() calls + * + * \param component Source properties + * \param data Destination properties + */ void update_last(const AudioSource & component, ComponentPrivate & data); + /** + * \brief Compare update component + * + * Compares properties of \c component and \c data, and calls SoundContext functions where + * applicable. + * + * \param component AudioSource component to update + * \param data AudioSource's private data + * \param resource Sound instance for AudioSource's Asset + */ void diff_update(AudioSource & component, ComponentPrivate & data, Sound & resource); protected: + /** + * \brief Get SoundContext + * + * SoundContext is retrieved through this function instead of being a direct member of + * AudioSystem to aid with testability. + */ virtual SoundContext & get_context(); private: + //! Actually stores SoundContext if the base AudioSystem::get_context implementation is used Private context; }; diff --git a/src/crepe/util/Private.h b/src/crepe/util/Private.h index 62a2e1a..d725a5e 100644 --- a/src/crepe/util/Private.h +++ b/src/crepe/util/Private.h @@ -5,26 +5,82 @@ namespace crepe { +/** + * \brief Utility for storing type hidden from user + * + * This class can be used to store types which cannot be used in the API directly due to header + * distribution limitations. This class is similar to `std::any`, but provides a method for + * retrieving a mutable reference to the stored object. + */ class Private { public: Private() = default; ~Private(); + /** + * \name Copy + * + * \note These functions do not do anything, resulting in `*this` being an empty (default) + * instance. + * + * \{ + */ Private(const Private &); - Private(Private &&); Private & operator=(const Private &); + //! \} + /** + * \name Move + * + * These functions actually move the stored type if present. + * + * \{ + */ + Private(Private &&); Private & operator=(Private &&); + //! \} + /** + * \brief Get the stored object + * + * \tparam T Type of stored object + * + * \returns Mutable reference to stored object + * + * \throws std::out_of_range if this instance does not contain any object + * \throws std::logic_error if the stored type and requested type differ + */ template - T & get(); + T & get() const; + /** + * \brief Create and store an arbitrary object + * + * \tparam T Type of object + * \tparam Args Perfect forwarding arguments + * \param args Perfect forwarding arguments + * + * All arguments to this function are forwarded using `std::forward` to the constructor of T. + * + * \returns Mutable reference to stored object + * + * \note If this instance already contained an object, this function implicitly destroys the + * previous object. + */ template T & set(Args &&... args); + /** + * \brief Check if this instance contains an object + * + * \returns `true` if this instance is empty, `false` if it contains an object + */ bool empty() const noexcept; private: + //! Wrapper for destructor call of stored object type std::function destructor; + //! Stored object's type std::type_index type = typeid(void); + //! Stored object void * instance = nullptr; }; diff --git a/src/crepe/util/Private.hpp b/src/crepe/util/Private.hpp index 3a87a9f..b2174c0 100644 --- a/src/crepe/util/Private.hpp +++ b/src/crepe/util/Private.hpp @@ -18,7 +18,7 @@ T & Private::set(Args &&... args) { } template -T & Private::get() { +T & Private::get() const { using namespace std; if (this->empty()) throw out_of_range("Private: get() called on empty object"); type_index requested_type = typeid(T); diff --git a/src/test/AudioTest.cpp b/src/test/AudioTest.cpp index 14f57bd..7644ab7 100644 --- a/src/test/AudioTest.cpp +++ b/src/test/AudioTest.cpp @@ -18,8 +18,8 @@ private: public: MOCK_METHOD(Sound::Handle, play, (Sound & resource), (override)); MOCK_METHOD(void, stop, (Sound::Handle &), (override)); - MOCK_METHOD(void, set_volume, (Sound &, Sound::Handle &, float), (override)); - MOCK_METHOD(void, set_loop, (Sound &, Sound::Handle &, bool), (override)); + MOCK_METHOD(void, set_volume, (Sound::Handle &, float), (override)); + MOCK_METHOD(void, set_loop, (Sound::Handle &, bool), (override)); }; class TestAudioSystem : public AudioSystem { @@ -48,8 +48,8 @@ public: TEST_F(AudioTest, Default) { EXPECT_CALL(context, play(_)).Times(0); EXPECT_CALL(context, stop(_)).Times(0); - EXPECT_CALL(context, set_volume(_, _, _)).Times(0); - EXPECT_CALL(context, set_loop(_, _, _)).Times(0); + EXPECT_CALL(context, set_volume(_, _)).Times(0); + EXPECT_CALL(context, set_loop(_, _)).Times(0); system.update(); } @@ -95,14 +95,14 @@ TEST_F(AudioTest, Volume) { { InSequence seq; - EXPECT_CALL(context, set_volume(_, _, _)).Times(0); + EXPECT_CALL(context, set_volume(_, _)).Times(0); component.volume += 0.2; } { InSequence seq; - EXPECT_CALL(context, set_volume(_, _, component.volume)).Times(1); + EXPECT_CALL(context, set_volume(_, component.volume)).Times(1); system.update(); } } @@ -113,14 +113,14 @@ TEST_F(AudioTest, Looping) { { InSequence seq; - EXPECT_CALL(context, set_loop(_, _, _)).Times(0); + EXPECT_CALL(context, set_loop(_, _)).Times(0); component.loop = !component.loop; } { InSequence seq; - EXPECT_CALL(context, set_loop(_, _, component.loop)).Times(1); + EXPECT_CALL(context, set_loop(_, component.loop)).Times(1); system.update(); } } -- cgit v1.2.3 From 7a8657dfe019104aced61a5b63e63f61ad919f7a Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Mon, 2 Dec 2024 16:13:08 +0100 Subject: remove `Private` --- src/crepe/api/AudioSource.h | 15 +++- src/crepe/facade/Sound.h | 6 -- src/crepe/facade/SoundContext.cpp | 23 +++--- src/crepe/facade/SoundContext.h | 15 +++- src/crepe/facade/SoundHandle.h | 13 ++++ src/crepe/system/AudioSystem.cpp | 44 +++++------ src/crepe/system/AudioSystem.h | 29 ++----- src/crepe/util/CMakeLists.txt | 3 - src/crepe/util/Private.cpp | 27 ------- src/crepe/util/Private.h | 89 ---------------------- src/crepe/util/Private.hpp | 31 -------- src/test/AudioTest.cpp | 8 +- src/test/CMakeLists.txt | 1 - src/test/PrivateTest.cpp | 155 -------------------------------------- 14 files changed, 78 insertions(+), 381 deletions(-) create mode 100644 src/crepe/facade/SoundHandle.h delete mode 100644 src/crepe/util/Private.cpp delete mode 100644 src/crepe/util/Private.h delete mode 100644 src/crepe/util/Private.hpp delete mode 100644 src/test/PrivateTest.cpp (limited to 'src/crepe/facade/SoundContext.cpp') diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 330e8e1..7c1f161 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -2,7 +2,7 @@ #include "../Component.h" #include "../types.h" -#include "../util/Private.h" +#include "../facade/SoundHandle.h" #include "Asset.h" #include "GameObject.h" @@ -59,10 +59,17 @@ private: //! Stop this sample bool oneshot_stop = false; //! \} + /** + * \name State diffing variables + * \{ + */ + typeof(active) last_active = false; + typeof(volume) last_volume = volume; + typeof(loop) last_loop = loop; + //! \} + //! This source's voice handle + SoundHandle voice{}; -private: - //! AudioSystem::ComponentPrivate - Private private_data; }; } // namespace crepe diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h index a78a2a7..85d141b 100644 --- a/src/crepe/facade/Sound.h +++ b/src/crepe/facade/Sound.h @@ -20,12 +20,6 @@ public: Sound(const Asset & src); ~Sound(); // dbg_trace - //! Voice handle wrapper - struct Handle { - //! Voice handle (soloud), used by SoundContext - SoLoud::handle handle; - }; - private: //! Deserialized resource (soloud) SoLoud::Wav sample; diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp index 8bd7e74..d18afc6 100644 --- a/src/crepe/facade/SoundContext.cpp +++ b/src/crepe/facade/SoundContext.cpp @@ -15,18 +15,23 @@ SoundContext::~SoundContext() { this->engine.deinit(); } -Sound::Handle SoundContext::play(Sound & resource) { - return { - .handle = this->engine.play(resource.sample, 1.0f), - }; +SoundHandle SoundContext::play(Sound & resource) { + SoLoud::handle real_handle = this->engine.play(resource.sample, 1.0f); + SoundHandle handle = this->next_handle; + this->registry[handle] = real_handle; + this->next_handle++; + return handle; } -void SoundContext::stop(Sound::Handle & handle) { this->engine.stop(handle.handle); } +void SoundContext::stop(const SoundHandle & handle) { + this->engine.stop(this->registry[handle]); +} -void SoundContext::set_volume(Sound::Handle & handle, float volume) { - this->engine.setVolume(handle.handle, volume); +void SoundContext::set_volume(const SoundHandle & handle, float volume) { + this->engine.setVolume(this->registry[handle], volume); } -void SoundContext::set_loop(Sound::Handle & handle, bool loop) { - this->engine.setLooping(handle.handle, loop); +void SoundContext::set_loop(const SoundHandle & handle, bool loop) { + this->engine.setLooping(this->registry[handle], loop); } + diff --git a/src/crepe/facade/SoundContext.h b/src/crepe/facade/SoundContext.h index 3bc8be5..102f928 100644 --- a/src/crepe/facade/SoundContext.h +++ b/src/crepe/facade/SoundContext.h @@ -4,6 +4,7 @@ #include "../api/Config.h" +#include "SoundHandle.h" #include "Sound.h" namespace crepe { @@ -33,7 +34,7 @@ public: * * \returns Handle to control this voice */ - virtual Sound::Handle play(Sound & resource); + virtual SoundHandle play(Sound & resource); /** * \brief Stop a voice immediately if it is still playing * @@ -42,7 +43,7 @@ public: * * \param handle Voice handle returned by SoundContext::play */ - virtual void stop(Sound::Handle & handle); + virtual void stop(const SoundHandle & handle); /** * \brief Change the volume of a voice * @@ -52,7 +53,7 @@ public: * \param handle Voice handle returned by SoundContext::play * \param volume New gain value (0=silent, 1=default) */ - virtual void set_volume(Sound::Handle & handle, float volume); + virtual void set_volume(const SoundHandle & handle, float volume); /** * \brief Set the looping behavior of a voice * @@ -62,7 +63,7 @@ public: * \param handle Voice handle returned by SoundContext::play * \param loop Looping behavior (false=oneshot, true=loop) */ - virtual void set_loop(Sound::Handle & handle, bool loop); + virtual void set_loop(const SoundHandle & handle, bool loop); private: //! Abstracted class @@ -70,6 +71,12 @@ private: //! Config reference Config & config = Config::get_instance(); + + //! Sound handle registry + std::unordered_map registry; + //! Unique handle counter + SoundHandle next_handle = 0; + }; } // namespace crepe diff --git a/src/crepe/facade/SoundHandle.h b/src/crepe/facade/SoundHandle.h new file mode 100644 index 0000000..131d28c --- /dev/null +++ b/src/crepe/facade/SoundHandle.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +namespace crepe { + +/** + * \brief Voice handle returned by + */ +typedef size_t SoundHandle; + +} + diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp index 26913c0..c1cde8b 100644 --- a/src/crepe/system/AudioSystem.cpp +++ b/src/crepe/system/AudioSystem.cpp @@ -16,56 +16,50 @@ void AudioSystem::update() { for (AudioSource & component : components) { Sound & resource = resource_manager.get(component.source); - if (component.private_data.empty()) { - auto & data = component.private_data.set(); - this->update_last(component, data); - data.last_active = false; - } - auto & data = component.private_data.get(); - - this->diff_update(component, data, resource); + this->diff_update(component, resource); - this->update_last(component, data); + this->update_last(component); } } -void AudioSystem::diff_update(AudioSource & component, ComponentPrivate & data, - Sound & resource) { +void AudioSystem::diff_update(AudioSource & component, Sound & resource) { SoundContext & context = this->get_context(); - if (component.active != data.last_active) { + if (component.active != component.last_active) { if (component.active) { component.oneshot_play = component.play_on_awake; } else { - context.stop(data.handle); + context.stop(component.voice); return; } } if (!component.active) return; if (component.oneshot_play) { - data.handle = context.play(resource); + component.voice = context.play(resource); component.oneshot_play = false; } if (component.oneshot_stop) { - context.stop(data.handle); + context.stop(component.voice); component.oneshot_stop = false; } - if (component.volume != data.last_volume) { - context.set_volume(data.handle, component.volume); + if (component.volume != component.last_volume) { + context.set_volume(component.voice, component.volume); } - if (component.loop != data.last_loop) { - context.set_loop(data.handle, component.loop); + if (component.loop != component.last_loop) { + context.set_loop(component.voice, component.loop); } } -void AudioSystem::update_last(const AudioSource & component, ComponentPrivate & data) { - data.last_active = component.active; - data.last_loop = component.loop; - data.last_volume = component.volume; +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.empty()) this->context.set(); - return this->context.get(); + if (this->context == nullptr) + this->context = make_unique(); + return *this->context.get(); } + diff --git a/src/crepe/system/AudioSystem.h b/src/crepe/system/AudioSystem.h index 4d21883..2ddc443 100644 --- a/src/crepe/system/AudioSystem.h +++ b/src/crepe/system/AudioSystem.h @@ -14,30 +14,14 @@ public: void update() override; private: - //! Private data stored by AudioSystem on AudioSource component - struct ComponentPrivate { - //! This sample's voice handle - Sound::Handle handle; - - /** - * \name State diffing variables - * \{ - */ - typeof(AudioSource::active) last_active; - typeof(AudioSource::volume) last_volume; - typeof(AudioSource::loop) last_loop; - //! \} - }; - /** - * \brief Update `last_*` members of \c data + * \brief Update `last_*` members of \c component * * Copies all component properties stored for comparison between AudioSystem::update() calls * - * \param component Source properties - * \param data Destination properties + * \param component AudioSource component to update */ - void update_last(const AudioSource & component, ComponentPrivate & data); + void update_last(AudioSource & component); /** * \brief Compare update component @@ -46,10 +30,9 @@ private: * applicable. * * \param component AudioSource component to update - * \param data AudioSource's private data * \param resource Sound instance for AudioSource's Asset */ - void diff_update(AudioSource & component, ComponentPrivate & data, Sound & resource); + void diff_update(AudioSource & component, Sound & resource); protected: /** @@ -61,8 +44,8 @@ protected: virtual SoundContext & get_context(); private: - //! Actually stores SoundContext if the base AudioSystem::get_context implementation is used - Private context; + //! SoundContext + std::unique_ptr context = nullptr; }; } // namespace crepe diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt index f49d851..94ed906 100644 --- a/src/crepe/util/CMakeLists.txt +++ b/src/crepe/util/CMakeLists.txt @@ -1,7 +1,6 @@ target_sources(crepe PUBLIC LogColor.cpp Log.cpp - Private.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -12,7 +11,5 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Proxy.hpp OptionalRef.h OptionalRef.hpp - Private.h - Private.hpp ) diff --git a/src/crepe/util/Private.cpp b/src/crepe/util/Private.cpp deleted file mode 100644 index 262620d..0000000 --- a/src/crepe/util/Private.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "Private.h" - -using namespace crepe; - -bool Private::empty() const noexcept { return this->instance == nullptr; } - -Private::~Private() { - if (this->instance == nullptr) return; - this->destructor(this->instance); -} - -Private::Private(Private && other) { *this = std::move(other); } - -Private & Private::operator=(Private && other) { - // TODO: ideally this function checks for self-assignment - this->instance = other.instance; - this->destructor = other.destructor; - this->type = other.type; - - other.instance = nullptr; - other.destructor = [](void *) {}; - - return *this; -} - -Private::Private(const Private & other) {} -Private & Private::operator=(const Private & other) { return *this; } diff --git a/src/crepe/util/Private.h b/src/crepe/util/Private.h deleted file mode 100644 index d725a5e..0000000 --- a/src/crepe/util/Private.h +++ /dev/null @@ -1,89 +0,0 @@ -#pragma once - -#include -#include - -namespace crepe { - -/** - * \brief Utility for storing type hidden from user - * - * This class can be used to store types which cannot be used in the API directly due to header - * distribution limitations. This class is similar to `std::any`, but provides a method for - * retrieving a mutable reference to the stored object. - */ -class Private { -public: - Private() = default; - ~Private(); - /** - * \name Copy - * - * \note These functions do not do anything, resulting in `*this` being an empty (default) - * instance. - * - * \{ - */ - Private(const Private &); - Private & operator=(const Private &); - //! \} - /** - * \name Move - * - * These functions actually move the stored type if present. - * - * \{ - */ - Private(Private &&); - Private & operator=(Private &&); - //! \} - - /** - * \brief Get the stored object - * - * \tparam T Type of stored object - * - * \returns Mutable reference to stored object - * - * \throws std::out_of_range if this instance does not contain any object - * \throws std::logic_error if the stored type and requested type differ - */ - template - T & get() const; - - /** - * \brief Create and store an arbitrary object - * - * \tparam T Type of object - * \tparam Args Perfect forwarding arguments - * \param args Perfect forwarding arguments - * - * All arguments to this function are forwarded using `std::forward` to the constructor of T. - * - * \returns Mutable reference to stored object - * - * \note If this instance already contained an object, this function implicitly destroys the - * previous object. - */ - template - T & set(Args &&... args); - - /** - * \brief Check if this instance contains an object - * - * \returns `true` if this instance is empty, `false` if it contains an object - */ - bool empty() const noexcept; - -private: - //! Wrapper for destructor call of stored object type - std::function destructor; - //! Stored object's type - std::type_index type = typeid(void); - //! Stored object - void * instance = nullptr; -}; - -} // namespace crepe - -#include "Private.hpp" diff --git a/src/crepe/util/Private.hpp b/src/crepe/util/Private.hpp deleted file mode 100644 index b2174c0..0000000 --- a/src/crepe/util/Private.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include -#include - -#include "Private.h" - -namespace crepe { - -template -T & Private::set(Args &&... args) { - if (!this->empty()) this->destructor(this->instance); - T * instance = new T(std::forward(args)...); - this->instance = static_cast(instance); - this->destructor = [](void * instance) { delete static_cast(instance); }; - this->type = typeid(T); - return *instance; -} - -template -T & Private::get() const { - using namespace std; - if (this->empty()) throw out_of_range("Private: get() called on empty object"); - type_index requested_type = typeid(T); - if (this->type != requested_type) - throw logic_error(format("Private: get() called with [T = {}] (actual is [T = {}])", - requested_type.name(), this->type.name())); - return *static_cast(this->instance); -} - -} // namespace crepe diff --git a/src/test/AudioTest.cpp b/src/test/AudioTest.cpp index 7644ab7..774fdb8 100644 --- a/src/test/AudioTest.cpp +++ b/src/test/AudioTest.cpp @@ -16,10 +16,10 @@ class AudioTest : public Test { private: class TestSoundContext : public SoundContext { public: - MOCK_METHOD(Sound::Handle, play, (Sound & resource), (override)); - MOCK_METHOD(void, stop, (Sound::Handle &), (override)); - MOCK_METHOD(void, set_volume, (Sound::Handle &, float), (override)); - MOCK_METHOD(void, set_loop, (Sound::Handle &, bool), (override)); + MOCK_METHOD(SoundHandle, play, (Sound & resource), (override)); + MOCK_METHOD(void, stop, (const SoundHandle &), (override)); + MOCK_METHOD(void, set_volume, (const SoundHandle &, float), (override)); + MOCK_METHOD(void, set_loop, (const SoundHandle &, bool), (override)); }; class TestAudioSystem : public AudioSystem { diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 8c4b855..4174926 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -16,5 +16,4 @@ target_sources(test_main PUBLIC Vector2Test.cpp ScriptEventTest.cpp ScriptSceneTest.cpp - PrivateTest.cpp ) diff --git a/src/test/PrivateTest.cpp b/src/test/PrivateTest.cpp deleted file mode 100644 index 454789e..0000000 --- a/src/test/PrivateTest.cpp +++ /dev/null @@ -1,155 +0,0 @@ -#include - -#include - -using namespace std; -using namespace crepe; -using namespace testing; - -class PrivateTest : public Test { -public: - static unsigned constructors; - static unsigned destructors; - - void SetUp() override { - PrivateTest::constructors = 0; - PrivateTest::destructors = 0; - } - - class TestClass { - public: - TestClass() { PrivateTest::constructors++; } - ~TestClass() { PrivateTest::destructors++; } - }; - class Unrelated {}; -}; -unsigned PrivateTest::constructors; -unsigned PrivateTest::destructors; - -TEST_F(PrivateTest, Empty) { - { Private foo; } - - EXPECT_EQ(PrivateTest::constructors, 0); - EXPECT_EQ(PrivateTest::destructors, 0); -} - -TEST_F(PrivateTest, WithObject) { - { - Private foo; - foo.set(); - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 0); - } - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 1); -} - -TEST_F(PrivateTest, EmptyException) { - Private foo; - EXPECT_THROW(foo.get(), std::out_of_range); - - foo.set(); - EXPECT_NO_THROW(foo.get()); -} - -TEST_F(PrivateTest, IncorrectTypeException) { - Private foo; - foo.set(); - - EXPECT_THROW(foo.get(), std::logic_error); - EXPECT_NO_THROW(foo.get()); -} - -TEST_F(PrivateTest, MoveConstructor) { - { - Private foo; - foo.set(); - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 0); - - Private bar(std::move(foo)); - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 0); - } - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 1); -} - -TEST_F(PrivateTest, MoveOperator) { - { - Private foo; - foo.set(); - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 0); - - Private bar = std::move(foo); - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 0); - } - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 1); -} - -TEST_F(PrivateTest, CopyConstructor) { - { - Private foo; - foo.set(); - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 0); - - Private bar(foo); - - EXPECT_TRUE(bar.empty()); - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 0); - } - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 1); -} - -TEST_F(PrivateTest, CopyOperator) { - { - Private foo; - foo.set(); - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 0); - - Private bar = foo; - - EXPECT_TRUE(bar.empty()); - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 0); - } - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 1); -} - -TEST_F(PrivateTest, DoubleAssignment) { - { - Private foo; - foo.set(); - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 0); - - foo.set(); - - EXPECT_EQ(PrivateTest::constructors, 2); - EXPECT_EQ(PrivateTest::destructors, 1); - } - - EXPECT_EQ(PrivateTest::constructors, 2); - EXPECT_EQ(PrivateTest::destructors, 2); -} -- cgit v1.2.3 From 1e9e564f3806d07c7b0dc445c4ae2e738350fc83 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 5 Dec 2024 17:12:56 +0100 Subject: `make format` --- src/crepe/api/AudioSource.h | 3 +-- src/crepe/facade/SoundContext.cpp | 1 - src/crepe/facade/SoundContext.h | 3 +-- src/crepe/facade/SoundHandle.h | 3 +-- src/crepe/system/AudioSystem.cpp | 4 +--- src/test/ResourceManagerTest.cpp | 9 ++------- 6 files changed, 6 insertions(+), 17 deletions(-) (limited to 'src/crepe/facade/SoundContext.cpp') diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 7c1f161..b20e490 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -1,8 +1,8 @@ #pragma once #include "../Component.h" -#include "../types.h" #include "../facade/SoundHandle.h" +#include "../types.h" #include "Asset.h" #include "GameObject.h" @@ -69,7 +69,6 @@ private: //! \} //! This source's voice handle SoundHandle voice{}; - }; } // namespace crepe diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp index d18afc6..b1f8cb3 100644 --- a/src/crepe/facade/SoundContext.cpp +++ b/src/crepe/facade/SoundContext.cpp @@ -34,4 +34,3 @@ void SoundContext::set_volume(const SoundHandle & handle, float volume) { void SoundContext::set_loop(const SoundHandle & handle, bool loop) { this->engine.setLooping(this->registry[handle], loop); } - diff --git a/src/crepe/facade/SoundContext.h b/src/crepe/facade/SoundContext.h index 102f928..d986c59 100644 --- a/src/crepe/facade/SoundContext.h +++ b/src/crepe/facade/SoundContext.h @@ -4,8 +4,8 @@ #include "../api/Config.h" -#include "SoundHandle.h" #include "Sound.h" +#include "SoundHandle.h" namespace crepe { @@ -76,7 +76,6 @@ private: std::unordered_map registry; //! Unique handle counter SoundHandle next_handle = 0; - }; } // namespace crepe diff --git a/src/crepe/facade/SoundHandle.h b/src/crepe/facade/SoundHandle.h index 131d28c..b7925fc 100644 --- a/src/crepe/facade/SoundHandle.h +++ b/src/crepe/facade/SoundHandle.h @@ -9,5 +9,4 @@ namespace crepe { */ typedef size_t SoundHandle; -} - +} // namespace crepe diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp index c1cde8b..b2c1dc6 100644 --- a/src/crepe/system/AudioSystem.cpp +++ b/src/crepe/system/AudioSystem.cpp @@ -58,8 +58,6 @@ void AudioSystem::update_last(AudioSource & component) { } SoundContext & AudioSystem::get_context() { - if (this->context == nullptr) - this->context = make_unique(); + if (this->context == nullptr) this->context = make_unique(); return *this->context.get(); } - diff --git a/src/test/ResourceManagerTest.cpp b/src/test/ResourceManagerTest.cpp index 0789ef0..44a5921 100644 --- a/src/test/ResourceManagerTest.cpp +++ b/src/test/ResourceManagerTest.cpp @@ -75,12 +75,7 @@ TEST_F(ResourceManagerTest, Persistent) { } TEST_F(ResourceManagerTest, UnmatchedType) { - EXPECT_NO_THROW({ - resource_manager.get(asset_a); - }); + EXPECT_NO_THROW({ resource_manager.get(asset_a); }); - EXPECT_THROW({ - resource_manager.get(asset_a); - }, runtime_error); + EXPECT_THROW({ resource_manager.get(asset_a); }, runtime_error); } - -- cgit v1.2.3