diff options
Diffstat (limited to 'src/crepe')
-rw-r--r-- | src/crepe/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/Component.h | 11 | ||||
-rw-r--r-- | src/crepe/Resource.cpp | 6 | ||||
-rw-r--r-- | src/crepe/Resource.h | 26 | ||||
-rw-r--r-- | src/crepe/api/AssetManager.cpp | 17 | ||||
-rw-r--r-- | src/crepe/api/AudioSource.cpp | 20 | ||||
-rw-r--r-- | src/crepe/api/AudioSource.h | 60 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 9 | ||||
-rw-r--r-- | src/crepe/api/ResourceManager.cpp | 41 | ||||
-rw-r--r-- | src/crepe/api/ResourceManager.h | 69 | ||||
-rw-r--r-- | src/crepe/facade/Sound.cpp | 31 | ||||
-rw-r--r-- | src/crepe/facade/Sound.h | 26 | ||||
-rw-r--r-- | src/crepe/facade/SoundContext.cpp | 5 | ||||
-rw-r--r-- | src/crepe/facade/SoundContext.h | 6 | ||||
-rw-r--r-- | src/crepe/system/AudioSystem.cpp | 55 | ||||
-rw-r--r-- | src/crepe/system/AudioSystem.h | 21 | ||||
-rw-r--r-- | src/crepe/system/CMakeLists.txt | 2 |
17 files changed, 339 insertions, 68 deletions
diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 7e176e7..df15b8f 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -3,6 +3,7 @@ target_sources(crepe PUBLIC ComponentManager.cpp Component.cpp Collider.cpp + Resource.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -12,6 +13,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Collider.h ValueBroker.h ValueBroker.hpp + Resource.h ) add_subdirectory(api) diff --git a/src/crepe/Component.h b/src/crepe/Component.h index dc17721..67bcc68 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -16,7 +16,12 @@ class Component { public: //! Whether the component is active bool active = true; - //! The id of the GameObject this component belongs to + /** + * \brief The id of the GameObject this component belongs to + * + * \note Only systems are supposed to use this member, but since friend + * relations aren't inherited this needs to be public. + */ const game_object_id_t game_object_id; protected: @@ -24,7 +29,7 @@ protected: * \param id The id of the GameObject this component belongs to */ Component(game_object_id_t id); - //! Only the ComponentManager can create components + //! Only ComponentManager can create components friend class ComponentManager; Component(const Component &) = delete; @@ -45,6 +50,8 @@ public: * \return The maximum number of instances for this component */ virtual int get_instances_max() const { return -1; } + //! Only ComponentManager needs to know the max instance count + friend class ComponentManager; }; } // namespace crepe diff --git a/src/crepe/Resource.cpp b/src/crepe/Resource.cpp new file mode 100644 index 0000000..e254695 --- /dev/null +++ b/src/crepe/Resource.cpp @@ -0,0 +1,6 @@ +#include "Resource.h" + +using namespace crepe; + +Resource::Resource(const Asset & asset) { } + diff --git a/src/crepe/Resource.h b/src/crepe/Resource.h new file mode 100644 index 0000000..a0c8859 --- /dev/null +++ b/src/crepe/Resource.h @@ -0,0 +1,26 @@ +#pragma once + +namespace crepe { + +class ResourceManager; +class Asset; + +/** + * Resource is an interface class used to represent a (deserialized) game + * resource (e.g. textures, sounds). + */ +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/AssetManager.cpp b/src/crepe/api/AssetManager.cpp deleted file mode 100644 index 3925758..0000000 --- a/src/crepe/api/AssetManager.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "util/Log.h" - -#include "AssetManager.h" - -using namespace crepe; - -AssetManager & AssetManager::get_instance() { - static AssetManager instance; - return instance; -} - -AssetManager::~AssetManager() { - dbg_trace(); - this->asset_cache.clear(); -} - -AssetManager::AssetManager() { dbg_trace(); } diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp new file mode 100644 index 0000000..4baac9a --- /dev/null +++ b/src/crepe/api/AudioSource.cpp @@ -0,0 +1,20 @@ +#include "AudioSource.h" + +using namespace crepe; +using namespace std; + +AudioSource::AudioSource(game_object_id_t id, const Asset & src) : + Component(id), + source(src) +{ } + +void AudioSource::play(bool looping) { + this->loop = looping; + this->playing = true; +} + +void AudioSource::stop() { + this->playing = false; + this->rewind = true; +} + diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h new file mode 100644 index 0000000..1264790 --- /dev/null +++ b/src/crepe/api/AudioSource.h @@ -0,0 +1,60 @@ +#pragma once + +#include "../Component.h" +#include "../types.h" + +#include "Asset.h" + +namespace crepe { + +class AudioSystem; + +//! Audio source component +class AudioSource : public Component { + //! AudioSource components are handled by AudioSystem + friend class AudioSystem; + +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; + +public: + //! Start or resume this audio source + void play(bool looping = false); + //! Stop this audio source + void stop(); + +public: + //! Play when this component becomes active + bool play_on_awake = false; + //! Repeat the current audio clip during playback + bool loop = false; + //! Normalized volume (0.0 - 1.0) + float volume = 1.0; + +private: + //! This audio source's clip + const Asset source; + + //! If this source is playing audio + bool playing = false; + //! Rewind the sample location + bool rewind = false; + +private: + //! 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/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 50c51ed..a2e21fa 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -1,5 +1,5 @@ target_sources(crepe PUBLIC - # AudioSource.cpp + AudioSource.cpp BehaviorScript.cpp GameObject.cpp Rigidbody.cpp @@ -7,7 +7,7 @@ target_sources(crepe PUBLIC Transform.cpp Color.cpp Texture.cpp - AssetManager.cpp + ResourceManager.cpp Sprite.cpp SaveManager.cpp Config.cpp @@ -26,7 +26,7 @@ target_sources(crepe PUBLIC ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES - # AudioSource.h + AudioSource.h BehaviorScript.h Config.h Script.h @@ -39,8 +39,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Vector2.hpp Color.h Texture.h - AssetManager.h - AssetManager.hpp + ResourceManager.h SaveManager.h Scene.h Metadata.h diff --git a/src/crepe/api/ResourceManager.cpp b/src/crepe/api/ResourceManager.cpp new file mode 100644 index 0000000..7877ed9 --- /dev/null +++ b/src/crepe/api/ResourceManager.cpp @@ -0,0 +1,41 @@ +#include <stdexcept> + +#include "util/Log.h" + +#include "ResourceManager.h" + +using namespace crepe; +using namespace std; + +ResourceManager & ResourceManager::get_instance() { + static ResourceManager instance; + return instance; +} + +ResourceManager::~ResourceManager() { dbg_trace(); } +ResourceManager::ResourceManager() { dbg_trace(); } + +void ResourceManager::clear() { + this->resources.clear(); +} + +template <typename T> +T & ResourceManager::cache(const Asset & asset) { + dbg_trace(); + static_assert(is_base_of<Resource, T>::value, "cache must recieve a derivative class of Resource"); + + if (!this->resources.contains(asset)) + this->resources[asset] = make_unique<T>(asset); + + Resource * resource = this->resources.at(asset).get(); + T * concrete_resource = dynamic_cast<T *>(resource); + + if (concrete_resource == nullptr) + throw runtime_error(format("ResourceManager: mismatch between requested type and actual type of resource ({})", asset.get_path())); + + return *concrete_resource; +} + +#include "../facade/Sound.h" +template Sound & ResourceManager::cache(const Asset &); + diff --git a/src/crepe/api/ResourceManager.h b/src/crepe/api/ResourceManager.h new file mode 100644 index 0000000..efdd5c5 --- /dev/null +++ b/src/crepe/api/ResourceManager.h @@ -0,0 +1,69 @@ +#pragma once + +#include <memory> +#include <unordered_map> + +#include "Asset.h" +#include "Resource.h" + +namespace crepe { + +class Sound; + +/** + * \brief The ResourceManager is responsible for storing and managing assets over + * multiple scenes. + * + * 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. + */ +class ResourceManager { + +private: + //! A cache that holds all the assets, accessible by their file path, over multiple scenes. + std::unordered_map<const Asset, std::unique_ptr<Resource>> resources; + +private: + ResourceManager(); // dbg_trace + virtual ~ResourceManager(); // dbg_trace + + ResourceManager(const ResourceManager &) = delete; + ResourceManager(ResourceManager &&) = delete; + ResourceManager & operator=(const ResourceManager &) = delete; + ResourceManager & operator=(ResourceManager &&) = delete; + +public: + /** + * \brief Retrieves the singleton instance of the ResourceManager. + * + * \return A reference to the single instance of the ResourceManager. + */ + static ResourceManager & get_instance(); + +public: + /** + * \brief Caches an asset by loading it from the given file path. + * + * \param file_path The path to the asset file to load. + * \param reload If true, the asset will be reloaded from the file, even if + * it is already cached. + * \tparam T The type of asset to cache (e.g., texture, sound, etc.). + * + * \return A reference to the resource + * + * This template function caches the asset at the given file path. If the + * asset is already cached, the existing instance will be returned. + * Otherwise, the concrete resource will be instantiated and added to the + * cache. + */ + template <typename T> + T & cache(const Asset & asset); + + //! Clear the resource cache + void clear(); +}; + +} // namespace crepe + diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp index 4d3abf5..52496af 100644 --- a/src/crepe/facade/Sound.cpp +++ b/src/crepe/facade/Sound.cpp @@ -1,3 +1,4 @@ +#include "../api/Asset.h" #include "../util/Log.h" #include "Sound.h" @@ -6,20 +7,13 @@ using namespace crepe; using namespace std; -Sound::Sound(unique_ptr<Asset> res) { +Sound::Sound(const Asset & src) : Resource(src) { + this->sample.load(src.get_path().c_str()); dbg_trace(); - this->load(std::move(res)); } +Sound::~Sound() { dbg_trace(); } -Sound::Sound(const char * src) { - dbg_trace(); - this->load(make_unique<Asset>(src)); -} - -void Sound::load(unique_ptr<Asset> res) { this->sample.load(res->get_path().c_str()); } - -void Sound::play() { - SoundContext & ctx = SoundContext::get_instance(); +void Sound::play(SoundContext & ctx) { if (ctx.engine.getPause(this->handle)) { // resume if paused ctx.engine.setPause(this->handle, false); @@ -30,30 +24,25 @@ void Sound::play() { } } -void Sound::pause() { - SoundContext & ctx = SoundContext::get_instance(); +void Sound::pause(SoundContext & ctx) { if (ctx.engine.getPause(this->handle)) return; ctx.engine.setPause(this->handle, true); } -void Sound::rewind() { - SoundContext & ctx = SoundContext::get_instance(); +void Sound::rewind(SoundContext & ctx) { if (!ctx.engine.isValidVoiceHandle(this->handle)) return; ctx.engine.seek(this->handle, 0); } -void Sound::set_volume(float volume) { +void Sound::set_volume(SoundContext & ctx, float volume) { this->volume = volume; - - SoundContext & ctx = SoundContext::get_instance(); if (!ctx.engine.isValidVoiceHandle(this->handle)) return; ctx.engine.setVolume(this->handle, this->volume); } -void Sound::set_looping(bool looping) { +void Sound::set_looping(SoundContext & ctx, bool looping) { this->looping = looping; - - SoundContext & ctx = SoundContext::get_instance(); 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 4c68f32..b0b80f8 100644 --- a/src/crepe/facade/Sound.h +++ b/src/crepe/facade/Sound.h @@ -1,28 +1,31 @@ #pragma once -#include <memory> #include <soloud/soloud.h> #include <soloud/soloud_wav.h> -#include "../api/Asset.h" +#include "../Resource.h" namespace crepe { +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. */ -class Sound { +class Sound : public Resource { public: + Sound(const Asset & src); + ~Sound(); // dbg_trace /** * \brief Pause this sample * * Pauses this sound if it is playing, or does nothing if it is already paused. The playhead * position is saved, such that calling \c play() after this function makes the sound resume. */ - void pause(); + void pause(SoundContext & ctx); /** * \brief Play this sample * @@ -32,7 +35,7 @@ public: * \c play() while the sound is already playing causes multiple instances of the sample to * play simultaniously. The sample started last is the one that is controlled afterwards. */ - void play(); + void play(SoundContext & ctx); /** * \brief Reset playhead position * @@ -40,13 +43,13 @@ public: * from the start of the sample. If the sound is not paused before calling this function, * this function will stop playback. */ - void rewind(); + void rewind(SoundContext & ctx); /** * \brief Set playback volume / gain * * \param volume Volume (0 = muted, 1 = full volume) */ - void set_volume(float volume); + void set_volume(SoundContext & ctx, float volume); /** * \brief Get playback volume / gain * @@ -58,7 +61,7 @@ public: * * \param looping Looping behavior (false = one-shot, true = loop) */ - void set_looping(bool looping); + void set_looping(SoundContext & ctx, bool looping); /** * \brief Get looping behavior * @@ -66,13 +69,6 @@ public: */ bool get_looping() const { return this->looping; } -public: - Sound(const char * src); - Sound(std::unique_ptr<Asset> res); - -private: - void load(std::unique_ptr<Asset> res); - private: SoLoud::Wav sample; SoLoud::handle handle; diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp index deb2b62..b65dfb2 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(); diff --git a/src/crepe/facade/SoundContext.h b/src/crepe/facade/SoundContext.h index d703c16..d22ff7a 100644 --- a/src/crepe/facade/SoundContext.h +++ b/src/crepe/facade/SoundContext.h @@ -13,18 +13,18 @@ namespace crepe { * the methods for playing \c Sound instances. It is part of the sound facade. */ class SoundContext { -private: - // singleton +public: SoundContext(); virtual ~SoundContext(); + SoundContext(const SoundContext &) = delete; SoundContext(SoundContext &&) = delete; SoundContext & operator=(const SoundContext &) = delete; SoundContext & operator=(SoundContext &&) = delete; private: - static SoundContext & get_instance(); SoLoud::Soloud engine; + //! Sound directly calls methods on \c engine friend class Sound; }; diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp new file mode 100644 index 0000000..b7ac1f2 --- /dev/null +++ b/src/crepe/system/AudioSystem.cpp @@ -0,0 +1,55 @@ +#include "AudioSystem.h" +#include "ComponentManager.h" + +#include "../api/AudioSource.h" + +using namespace crepe; +using namespace std; + +void AudioSystem::update() { + ComponentManager & mgr = this->component_manager; + vector<reference_wrapper<AudioSource>> components = mgr.get_components_by_type<AudioSource>(); + + for (auto component_ref : components) { + AudioSource & component = component_ref.get(); + if (!component.active) continue; + + /** + * How this is supposed to work: + * - Get an instance of Sound for this resource/component combo (Sound + * instance is supposed to be unique per component, even if they use the + * same underlying asset). + * OR + * - Use the same instance of Sound if this is what the cache returns + * (= what the game programmer's wishes to do). + * + * NOT supposed to happen but still the case: + * - Below function call causes assets to be cached unintentionally + * - Cached assets are deleted at the end of a scene (i think?) + * - I'm not sure if the ResourceManager is even supposed to have a public + * `.clear()` method since the control over resource lifetime is + * explicitly handed over to the game programmer by using ResourceManager + * to cache/uncache. I believe the proper methods are supposed to be: + * + * - get() get a reference to resource (used here) + * - clear() clears NON-cached assets + * - cache() marks asset as "do not delete at end of scene" + * - uncache() undoes the above + * + * I think somewhere in the above function calls a unique identifier for + * the Asset/GameObject should be given to make sure the unique instance + * shit works as intended. The resource manager is also used for things + * other than sounds. + * + * Also need to check: + * - Is it an issue if there are multiple AudioSource components playing + * the same sample (= identical Asset), while they are all triggered + * using the same underlying instance of Sound (esp. w/ + * play/pause/retrigger behavior). + */ + Sound & sound = this->resman.cache<Sound>(component.source); + + // 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..d0b4f9a --- /dev/null +++ b/src/crepe/system/AudioSystem.h @@ -0,0 +1,21 @@ +#pragma once + +#include "../facade/SoundContext.h" +#include "../api/ResourceManager.h" + +#include "System.h" + +namespace crepe { + +class AudioSystem : public System { +public: + using System::System; + void update() override; + +private: + SoundContext context {}; + ResourceManager & resman = ResourceManager::get_instance(); +}; + +} // namespace crepe + diff --git a/src/crepe/system/CMakeLists.txt b/src/crepe/system/CMakeLists.txt index d658b25..f507b90 100644 --- a/src/crepe/system/CMakeLists.txt +++ b/src/crepe/system/CMakeLists.txt @@ -5,6 +5,7 @@ target_sources(crepe PUBLIC PhysicsSystem.cpp CollisionSystem.cpp RenderSystem.cpp + AudioSystem.cpp AnimatorSystem.cpp ) @@ -14,5 +15,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES PhysicsSystem.h CollisionSystem.h RenderSystem.h + AudioSystem.h AnimatorSystem.h ) |