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/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 ++-- 7 files changed, 16 insertions(+), 21 deletions(-) (limited to 'src/crepe/manager') 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" -- 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/manager') 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 f4824f5e7e6cee12bec602f3240770945a73d043 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 6 Dec 2024 17:13:59 +0100 Subject: add more fetching functions to ComponentManager --- src/crepe/api/Script.h | 7 +++++ src/crepe/manager/ComponentManager.cpp | 14 ++++++++++ src/crepe/manager/ComponentManager.h | 18 ++++++++++-- src/crepe/manager/ComponentManager.hpp | 51 ++++++++++++++++++++-------------- src/test/ECSTest.cpp | 34 +++++++++++++++++++++++ 5 files changed, 101 insertions(+), 23 deletions(-) (limited to 'src/crepe/manager') diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 1b339b0..5862bae 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -84,6 +84,13 @@ protected: template RefVector get_components() const; + /** + * \copydoc ComponentManager::get_components_by_id + * \see ComponentManager::get_components_by_id + */ + template + RefVector get_components_by_id(game_object_id_t id) const; + /** * \brief Log a message using Log::logf * diff --git a/src/crepe/manager/ComponentManager.cpp b/src/crepe/manager/ComponentManager.cpp index 80cf8b4..44774d9 100644 --- a/src/crepe/manager/ComponentManager.cpp +++ b/src/crepe/manager/ComponentManager.cpp @@ -1,4 +1,5 @@ #include "../api/GameObject.h" +#include "../api/Metadata.h" #include "../types.h" #include "../util/Log.h" @@ -61,3 +62,16 @@ GameObject ComponentManager::new_object(const string & name, const string & tag, void ComponentManager::set_persistent(game_object_id_t id, bool persistent) { this->persistent[id] = persistent; } + +set ComponentManager::get_objects_by_name(const string & name) const { + return this->get_objects_by_predicate([name](const Metadata & data) { + return data.name == name; + }); +} + +set ComponentManager::get_objects_by_tag(const string & tag) const { + return this->get_objects_by_predicate([tag](const Metadata & data) { + return data.tag == tag; + }); +} + diff --git a/src/crepe/manager/ComponentManager.h b/src/crepe/manager/ComponentManager.h index ad37586..5f2cf3c 100644 --- a/src/crepe/manager/ComponentManager.h +++ b/src/crepe/manager/ComponentManager.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "../Component.h" #include "../types.h" @@ -136,6 +137,17 @@ public: RefVector get_components_by_type() const; private: + template + std::set get_objects_by_predicate(const std::function & pred) const; + + std::set get_objects_by_name(const std::string & name) const; + std::set get_objects_by_tag(const std::string & tag) const; + +private: + template + using by_type = std::unordered_map; + template + using by_id_index = std::vector; /** * \brief The components * @@ -146,8 +158,7 @@ private: * The first vector is for the ids of the GameObjects and the second vector is for the * components (because a GameObject might have multiple components). */ - std::unordered_map>>> - components; + by_type>>> components; //! Persistent flag for each GameObject std::unordered_map persistent; @@ -156,6 +167,9 @@ private: game_object_id_t next_id = 0; }; +// template <> +// RefVector ComponentManager::get_components_by_id(game_object_id_t id) const; + } // namespace crepe #include "ComponentManager.hpp" diff --git a/src/crepe/manager/ComponentManager.hpp b/src/crepe/manager/ComponentManager.hpp index ffb38ec..25c2747 100644 --- a/src/crepe/manager/ComponentManager.hpp +++ b/src/crepe/manager/ComponentManager.hpp @@ -95,32 +95,24 @@ template RefVector ComponentManager::get_components_by_id(game_object_id_t id) const { using namespace std; - // Determine the type of T (this is used as the key of the unordered_map<>) - type_index type = typeid(T); - - // Create an empty vector<> - RefVector component_vector; - - if (this->components.find(type) == this->components.end()) return component_vector; - - // Get the correct vector<> - const vector>> & component_array = this->components.at(type); - - // Make sure that the id (that we are looking for) is within the boundaries of the vector<> - if (id >= component_array.size()) return component_vector; + static_assert(is_base_of::value, + "get_components_by_id must recieve a derivative class of Component"); - // Loop trough the whole vector<> - for (const unique_ptr & component_ptr : component_array[id]) { - // Cast the unique_ptr to a raw pointer - T * casted_component = static_cast(component_ptr.get()); + type_index type = typeid(T); + if (!this->components.contains(type)) return {}; - if (casted_component == nullptr) continue; + const by_id_index>> & components_by_id = this->components.at(type); + if (id >= components_by_id.size()) return {}; - // Add the dereferenced raw pointer to the vector<> - component_vector.push_back(*casted_component); + RefVector out = {}; + const vector> & components = components_by_id.at(id); + for (auto & component_ptr : components) { + if (component_ptr == nullptr) continue; + Component & component = *component_ptr.get(); + out.push_back(static_cast(component)); } - return component_vector; + return out; } template @@ -158,4 +150,21 @@ RefVector ComponentManager::get_components_by_type() const { return component_vector; } +template +std::set ComponentManager::get_objects_by_predicate(const std::function & pred) const { + using namespace std; + + set objects = {}; + RefVector components = this->get_components_by_type(); + + for (const T & component : components) { + game_object_id_t id = dynamic_cast(component).game_object_id; + if (objects.contains(id)) continue; + if (!pred(component)) continue; + objects.insert(id); + } + + return objects; +} + } // namespace crepe diff --git a/src/test/ECSTest.cpp b/src/test/ECSTest.cpp index 3e6c61c..a169b3b 100644 --- a/src/test/ECSTest.cpp +++ b/src/test/ECSTest.cpp @@ -1,6 +1,7 @@ #include #define protected public +#define private public #include #include @@ -387,3 +388,36 @@ TEST_F(ECSTest, resetPersistent) { EXPECT_EQ(metadata.size(), 0); EXPECT_EQ(transform.size(), 0); } + +TEST_F(ECSTest, GetByName) { + GameObject foo = mgr.new_object("foo"); + GameObject bar = mgr.new_object("bar"); + + { + auto objects = mgr.get_objects_by_name(""); + EXPECT_EQ(objects.size(), 0); + } + + { + auto objects = mgr.get_objects_by_name("foo"); + EXPECT_EQ(objects.size(), 1); + EXPECT_TRUE(objects.contains(foo.id)); + } +} + +TEST_F(ECSTest, GetByTag) { + GameObject foo = mgr.new_object("foo", "common tag"); + GameObject bar = mgr.new_object("bar", "common tag"); + + { + auto objects = mgr.get_objects_by_tag(""); + EXPECT_EQ(objects.size(), 0); + } + + { + auto objects = mgr.get_objects_by_tag("common tag"); + EXPECT_EQ(objects.size(), 2); + EXPECT_TRUE(objects.contains(foo.id)); + EXPECT_TRUE(objects.contains(bar.id)); + } +} -- cgit v1.2.3 From 90c6bf03e59fdec64f850310bcbff45ae86f69e3 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 7 Dec 2024 14:18:54 +0100 Subject: more script utilities --- src/crepe/api/Script.h | 12 ++++++++ src/crepe/api/Script.hpp | 27 +++++++++++++++--- src/crepe/manager/ComponentManager.h | 20 ++++++++++++++ src/crepe/manager/ComponentManager.hpp | 23 ++++++++++++++++ src/test/CMakeLists.txt | 1 + src/test/ECSTest.cpp | 50 ++++++++++++++++++++++++++++++++-- src/test/ScriptECSTest.cpp | 42 ++++++++++++++++++++++++++++ src/test/ScriptEventTest.cpp | 2 +- src/test/ScriptSceneTest.cpp | 3 +- src/test/ScriptTest.cpp | 3 +- src/test/ScriptTest.h | 2 ++ 11 files changed, 175 insertions(+), 10 deletions(-) create mode 100644 src/test/ScriptECSTest.cpp (limited to 'src/crepe/manager') diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 5862bae..a040608 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -90,6 +90,18 @@ protected: */ template RefVector get_components_by_id(game_object_id_t id) const; + /** + * \copydoc ComponentManager::get_components_by_name + * \see ComponentManager::get_components_by_name + */ + template + RefVector get_components_by_name(const std::string & name) const; + /** + * \copydoc ComponentManager::get_components_by_tag + * \see ComponentManager::get_components_by_tag + */ + template + RefVector get_components_by_tag(const std::string & tag) const; /** * \brief Log a message using Log::logf diff --git a/src/crepe/api/Script.hpp b/src/crepe/api/Script.hpp index 45f1ff1..16e0dc5 100644 --- a/src/crepe/api/Script.hpp +++ b/src/crepe/api/Script.hpp @@ -20,10 +20,7 @@ T & Script::get_component() const { template RefVector Script::get_components() const { - Mediator & mediator = this->mediator; - ComponentManager & mgr = mediator.component_manager; - - return mgr.get_components_by_id(this->game_object_id); + return this->get_components_by_id(this->game_object_id); } template @@ -56,4 +53,26 @@ void Script::subscribe(const EventHandler & callback) { this->subscribe_internal(callback, EventManager::CHANNEL_ALL); } +template +RefVector Script::get_components_by_id(game_object_id_t id) const { + Mediator & mediator = this->mediator; + ComponentManager & mgr = mediator.component_manager; + + return mgr.get_components_by_id(id); +} +template +RefVector Script::get_components_by_name(const std::string & name) const { + Mediator & mediator = this->mediator; + ComponentManager & mgr = mediator.component_manager; + + return mgr.get_components_by_name(name); +} +template +RefVector Script::get_components_by_tag(const std::string & tag) const { + Mediator & mediator = this->mediator; + ComponentManager & mgr = mediator.component_manager; + + return mgr.get_components_by_tag(tag); +} + } // namespace crepe diff --git a/src/crepe/manager/ComponentManager.h b/src/crepe/manager/ComponentManager.h index 5f2cf3c..4e53954 100644 --- a/src/crepe/manager/ComponentManager.h +++ b/src/crepe/manager/ComponentManager.h @@ -135,10 +135,30 @@ public: */ template RefVector get_components_by_type() const; + /** + * \brief Get all components of a specific type on a GameObject with name \c name + * + * \tparam T The type of the component + * \param name Metadata::name for the same game_object_id as the returned components + * \return Components matching criteria + */ + template + RefVector get_components_by_name(const std::string & name) const; + /** + * \brief Get all components of a specific type on a GameObject with tag \c tag + * + * \tparam T The type of the component + * \param name Metadata::tag for the same game_object_id as the returned components + * \return Components matching criteria + */ + template + RefVector get_components_by_tag(const std::string & tag) const; private: template std::set get_objects_by_predicate(const std::function & pred) const; + template + RefVector get_components_by_ids(const std::set & ids) const; std::set get_objects_by_name(const std::string & name) const; std::set get_objects_by_tag(const std::string & tag) const; diff --git a/src/crepe/manager/ComponentManager.hpp b/src/crepe/manager/ComponentManager.hpp index 25c2747..52df368 100644 --- a/src/crepe/manager/ComponentManager.hpp +++ b/src/crepe/manager/ComponentManager.hpp @@ -167,4 +167,27 @@ std::set ComponentManager::get_objects_by_predicate(const std: return objects; } +template +RefVector ComponentManager::get_components_by_ids(const std::set & ids) const { + using namespace std; + + RefVector out = {}; + for (game_object_id_t id : ids) { + RefVector components = get_components_by_id(id); + out.insert(out.end(), components.begin(), components.end()); + } + + return out; +} + +template +RefVector ComponentManager::get_components_by_name(const std::string & name) const { + return this->get_components_by_ids(this->get_objects_by_name(name)); +} + +template +RefVector ComponentManager::get_components_by_tag(const std::string & tag) const { + return this->get_components_by_ids(this->get_objects_by_tag(tag)); +} + } // namespace crepe diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index e19d7de..43f564a 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -15,4 +15,5 @@ target_sources(test_main PUBLIC InputTest.cpp ScriptEventTest.cpp ScriptSceneTest.cpp + ScriptECSTest.cpp ) diff --git a/src/test/ECSTest.cpp b/src/test/ECSTest.cpp index a169b3b..ed5341e 100644 --- a/src/test/ECSTest.cpp +++ b/src/test/ECSTest.cpp @@ -17,6 +17,10 @@ class ECSTest : public ::testing::Test { public: ComponentManager mgr{m}; + + class TestComponent : public Component { + using Component::Component; + }; }; TEST_F(ECSTest, createGameObject) { @@ -389,7 +393,7 @@ TEST_F(ECSTest, resetPersistent) { EXPECT_EQ(transform.size(), 0); } -TEST_F(ECSTest, GetByName) { +TEST_F(ECSTest, IDByName) { GameObject foo = mgr.new_object("foo"); GameObject bar = mgr.new_object("bar"); @@ -405,7 +409,7 @@ TEST_F(ECSTest, GetByName) { } } -TEST_F(ECSTest, GetByTag) { +TEST_F(ECSTest, IDByTag) { GameObject foo = mgr.new_object("foo", "common tag"); GameObject bar = mgr.new_object("bar", "common tag"); @@ -421,3 +425,45 @@ TEST_F(ECSTest, GetByTag) { EXPECT_TRUE(objects.contains(bar.id)); } } + +TEST_F(ECSTest, ComponentsByName) { + GameObject foo = mgr.new_object("foo"); + foo.add_component(); + GameObject bar = mgr.new_object("bar"); + bar.add_component(); + bar.add_component(); + + { + auto objects = mgr.get_components_by_name(""); + EXPECT_EQ(objects.size(), 0); + } + + { + auto objects = mgr.get_components_by_name("foo"); + EXPECT_EQ(objects.size(), 1); + } + + { + auto objects = mgr.get_components_by_name("bar"); + EXPECT_EQ(objects.size(), 2); + } +} + +TEST_F(ECSTest, ComponentsByTag) { + GameObject foo = mgr.new_object("foo", "common tag"); + foo.add_component(); + GameObject bar = mgr.new_object("bar", "common tag"); + bar.add_component(); + bar.add_component(); + + { + auto objects = mgr.get_components_by_tag(""); + EXPECT_EQ(objects.size(), 0); + } + + { + auto objects = mgr.get_components_by_tag("common tag"); + EXPECT_EQ(objects.size(), 3); + } +} + diff --git a/src/test/ScriptECSTest.cpp b/src/test/ScriptECSTest.cpp new file mode 100644 index 0000000..4477e55 --- /dev/null +++ b/src/test/ScriptECSTest.cpp @@ -0,0 +1,42 @@ +#include + +#define protected public + +#include +#include +#include +#include +#include +#include + +#include "ScriptTest.h" + +using namespace std; +using namespace crepe; +using namespace testing; + +class ScriptECSTest : public ScriptTest { +public: + class TestComponent : public Component { + using Component::Component; + }; +}; + +TEST_F(ScriptECSTest, GetOwnComponent) { + MyScript & script = this->script; + Metadata & metadata = script.get_component(); + + EXPECT_EQ(metadata.name, OBJ_NAME); +} + +TEST_F(ScriptECSTest, GetOwnComponents) { + const unsigned COUNT = 4; + + for (unsigned i = 0; i < COUNT; i++) + entity.add_component(); + + MyScript & script = this->script; + RefVector components = script.get_components(); + + EXPECT_EQ(components.size(), COUNT); +} diff --git a/src/test/ScriptEventTest.cpp b/src/test/ScriptEventTest.cpp index 5da31e7..c1b4028 100644 --- a/src/test/ScriptEventTest.cpp +++ b/src/test/ScriptEventTest.cpp @@ -26,7 +26,7 @@ public: class MyEvent : public Event {}; }; -TEST_F(ScriptEventTest, Inactive) { +TEST_F(ScriptEventTest, Default) { BehaviorScript & behaviorscript = this->behaviorscript; MyScript & script = this->script; EventManager & evmgr = this->event_manager; diff --git a/src/test/ScriptSceneTest.cpp b/src/test/ScriptSceneTest.cpp index 9ee1e52..8e849c1 100644 --- a/src/test/ScriptSceneTest.cpp +++ b/src/test/ScriptSceneTest.cpp @@ -18,7 +18,7 @@ public: class MyScene : public Scene {}; }; -TEST_F(ScriptSceneTest, Inactive) { +TEST_F(ScriptSceneTest, Default) { BehaviorScript & behaviorscript = this->behaviorscript; MyScript & script = this->script; @@ -28,3 +28,4 @@ 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 1d2d6dd..b0b2546 100644 --- a/src/test/ScriptTest.cpp +++ b/src/test/ScriptTest.cpp @@ -6,7 +6,6 @@ #define protected public #include "ScriptTest.h" -#include using namespace std; using namespace crepe; @@ -14,7 +13,6 @@ using namespace testing; void ScriptTest::SetUp() { auto & mgr = this->component_manager; - GameObject entity = mgr.new_object("name"); BehaviorScript & component = entity.add_component(); this->behaviorscript = component; @@ -75,3 +73,4 @@ TEST_F(ScriptTest, UpdateInactive) { system.update(); } } + diff --git a/src/test/ScriptTest.h b/src/test/ScriptTest.h index 1bbfdd3..309e016 100644 --- a/src/test/ScriptTest.h +++ b/src/test/ScriptTest.h @@ -11,10 +11,12 @@ class ScriptTest : public testing::Test { protected: crepe::Mediator mediator; + static constexpr const char * OBJ_NAME = "foo"; public: crepe::ComponentManager component_manager{mediator}; crepe::ScriptSystem system{mediator}; + crepe::GameObject entity = component_manager.new_object(OBJ_NAME); class MyScript : public crepe::Script { // NOTE: explicitly stating `public:` is not required on actual scripts -- cgit v1.2.3 From f3009ab8f0785a54d3fd83c0d758c8ebd901adda Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 7 Dec 2024 14:19:26 +0100 Subject: `make format` --- src/crepe/manager/ComponentManager.cpp | 11 ++++------- src/crepe/manager/ComponentManager.h | 5 +++-- src/crepe/manager/ComponentManager.hpp | 11 +++++++---- src/test/ECSTest.cpp | 1 - src/test/ScriptECSTest.cpp | 5 ++--- src/test/ScriptSceneTest.cpp | 1 - src/test/ScriptTest.cpp | 1 - 7 files changed, 16 insertions(+), 19 deletions(-) (limited to 'src/crepe/manager') diff --git a/src/crepe/manager/ComponentManager.cpp b/src/crepe/manager/ComponentManager.cpp index 44774d9..df30d27 100644 --- a/src/crepe/manager/ComponentManager.cpp +++ b/src/crepe/manager/ComponentManager.cpp @@ -64,14 +64,11 @@ void ComponentManager::set_persistent(game_object_id_t id, bool persistent) { } set ComponentManager::get_objects_by_name(const string & name) const { - return this->get_objects_by_predicate([name](const Metadata & data) { - return data.name == name; - }); + return this->get_objects_by_predicate( + [name](const Metadata & data) { return data.name == name; }); } set ComponentManager::get_objects_by_tag(const string & tag) const { - return this->get_objects_by_predicate([tag](const Metadata & data) { - return data.tag == tag; - }); + return this->get_objects_by_predicate( + [tag](const Metadata & data) { return data.tag == tag; }); } - diff --git a/src/crepe/manager/ComponentManager.h b/src/crepe/manager/ComponentManager.h index 4e53954..685cae5 100644 --- a/src/crepe/manager/ComponentManager.h +++ b/src/crepe/manager/ComponentManager.h @@ -1,10 +1,10 @@ #pragma once #include +#include #include #include #include -#include #include "../Component.h" #include "../types.h" @@ -156,7 +156,8 @@ public: private: template - std::set get_objects_by_predicate(const std::function & pred) const; + std::set + get_objects_by_predicate(const std::function & pred) const; template RefVector get_components_by_ids(const std::set & ids) const; diff --git a/src/crepe/manager/ComponentManager.hpp b/src/crepe/manager/ComponentManager.hpp index 52df368..9e70865 100644 --- a/src/crepe/manager/ComponentManager.hpp +++ b/src/crepe/manager/ComponentManager.hpp @@ -101,7 +101,8 @@ RefVector ComponentManager::get_components_by_id(game_object_id_t id) const { type_index type = typeid(T); if (!this->components.contains(type)) return {}; - const by_id_index>> & components_by_id = this->components.at(type); + const by_id_index>> & components_by_id + = this->components.at(type); if (id >= components_by_id.size()) return {}; RefVector out = {}; @@ -151,7 +152,8 @@ RefVector ComponentManager::get_components_by_type() const { } template -std::set ComponentManager::get_objects_by_predicate(const std::function & pred) const { +std::set +ComponentManager::get_objects_by_predicate(const std::function & pred) const { using namespace std; set objects = {}; @@ -168,9 +170,10 @@ std::set ComponentManager::get_objects_by_predicate(const std: } template -RefVector ComponentManager::get_components_by_ids(const std::set & ids) const { +RefVector +ComponentManager::get_components_by_ids(const std::set & ids) const { using namespace std; - + RefVector out = {}; for (game_object_id_t id : ids) { RefVector components = get_components_by_id(id); diff --git a/src/test/ECSTest.cpp b/src/test/ECSTest.cpp index ed5341e..af2b7b0 100644 --- a/src/test/ECSTest.cpp +++ b/src/test/ECSTest.cpp @@ -466,4 +466,3 @@ TEST_F(ECSTest, ComponentsByTag) { EXPECT_EQ(objects.size(), 3); } } - diff --git a/src/test/ScriptECSTest.cpp b/src/test/ScriptECSTest.cpp index 4477e55..1ec33ba 100644 --- a/src/test/ScriptECSTest.cpp +++ b/src/test/ScriptECSTest.cpp @@ -4,8 +4,8 @@ #include #include -#include #include +#include #include #include @@ -32,8 +32,7 @@ TEST_F(ScriptECSTest, GetOwnComponent) { TEST_F(ScriptECSTest, GetOwnComponents) { const unsigned COUNT = 4; - for (unsigned i = 0; i < COUNT; i++) - entity.add_component(); + for (unsigned i = 0; i < COUNT; i++) entity.add_component(); MyScript & script = this->script; RefVector components = script.get_components(); diff --git a/src/test/ScriptSceneTest.cpp b/src/test/ScriptSceneTest.cpp index 8e849c1..2568049 100644 --- a/src/test/ScriptSceneTest.cpp +++ b/src/test/ScriptSceneTest.cpp @@ -28,4 +28,3 @@ TEST_F(ScriptSceneTest, Default) { 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 b0b2546..acdae70 100644 --- a/src/test/ScriptTest.cpp +++ b/src/test/ScriptTest.cpp @@ -73,4 +73,3 @@ TEST_F(ScriptTest, UpdateInactive) { system.update(); } } - -- cgit v1.2.3 From 76f94a8cdd2945e26d716191b664eb906ebb9032 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 7 Dec 2024 14:36:08 +0100 Subject: add doxygen comments --- src/crepe/manager/ComponentManager.h | 40 +++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'src/crepe/manager') diff --git a/src/crepe/manager/ComponentManager.h b/src/crepe/manager/ComponentManager.h index e7eeaa2..19a8e81 100644 --- a/src/crepe/manager/ComponentManager.h +++ b/src/crepe/manager/ComponentManager.h @@ -155,18 +155,55 @@ public: RefVector get_components_by_tag(const std::string & tag) const; private: + /** + * \brief Get object IDs by predicate function + * + * This function calls the predicate function \c pred for all components matching type \c T, + * and adds their parent game_object_id to a \c std::set if the predicate returns true. + * + * \tparam T The type of the component to check the predicate against + * \param pred Predicate function + * + * \note The predicate function may be called for multiple components with the same \c + * game_object_id. In this case, the ID is added if *any* call returns \c true. + * + * \returns game_object_id for all components where the predicate returned true + */ template std::set get_objects_by_predicate(const std::function & pred) const; + + /** + * \brief Get components of type \c T for multiple game object IDs + * + * \tparam T The type of the components to return + * \param ids The object IDs + * + * \return All components matching type \c T and one of the IDs in \c ids + */ template RefVector get_components_by_ids(const std::set & ids) const; + /** + * \brief Get object IDs for objects with name \c name + * + * \param name Object name to match + * \returns Object IDs where Metadata::name is equal to \c name + */ std::set get_objects_by_name(const std::string & name) const; + /** + * \brief Get object IDs for objects with tag \c tag + * + * \param tag Object tag to match + * \returns Object IDs where Metadata::tag is equal to \c tag + */ std::set get_objects_by_tag(const std::string & tag) const; private: + //! By Component \c std::type_index (readability helper type) template using by_type = std::unordered_map; + //! By \c game_object_id index (readability helper type) template using by_id_index = std::vector; /** @@ -188,9 +225,6 @@ private: game_object_id_t next_id = 0; }; -// template <> -// RefVector ComponentManager::get_components_by_id(game_object_id_t id) const; - } // namespace crepe #include "ComponentManager.hpp" -- cgit v1.2.3 From f19f37ae3eff84161f86e62a26fbd8b68f8f91a9 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 7 Dec 2024 15:29:33 +0100 Subject: make SaveManager no longer a singleton --- src/crepe/manager/Mediator.h | 4 ++-- src/crepe/manager/SaveManager.cpp | 32 +++++++++++++--------------- src/crepe/manager/SaveManager.h | 27 ++++++++++-------------- src/example/CMakeLists.txt | 2 -- src/example/asset_manager.cpp | 36 -------------------------------- src/example/savemgr.cpp | 44 --------------------------------------- src/test/CMakeLists.txt | 1 + src/test/SaveManagerTest.cpp | 41 ++++++++++++++++++++++++++++++++++++ 8 files changed, 69 insertions(+), 118 deletions(-) delete mode 100644 src/example/asset_manager.cpp delete mode 100644 src/example/savemgr.cpp create mode 100644 src/test/SaveManagerTest.cpp (limited to 'src/crepe/manager') diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index 8094d80..6507a74 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -5,13 +5,13 @@ // TODO: remove these singletons: #include "../facade/SDLContext.h" #include "EventManager.h" -#include "SaveManager.h" #include "api/LoopTimer.h" namespace crepe { class ComponentManager; class SceneManager; +class SaveManager; /** * Struct to pass references to classes that would otherwise need to be singletons down to @@ -28,7 +28,7 @@ class SceneManager; struct Mediator { OptionalRef component_manager; OptionalRef scene_manager; - OptionalRef save_manager = SaveManager::get_instance(); + OptionalRef save_manager; OptionalRef event_manager = EventManager::get_instance(); OptionalRef sdl_context = SDLContext::get_instance(); OptionalRef timer = LoopTimer::get_instance(); diff --git a/src/crepe/manager/SaveManager.cpp b/src/crepe/manager/SaveManager.cpp index d4ed1c1..292e8fd 100644 --- a/src/crepe/manager/SaveManager.cpp +++ b/src/crepe/manager/SaveManager.cpp @@ -1,13 +1,24 @@ #include "../ValueBroker.h" #include "../api/Config.h" #include "../facade/DB.h" -#include "../util/Log.h" #include "SaveManager.h" using namespace std; using namespace crepe; +SaveManager::SaveManager(Mediator & mediator) : Manager(mediator) { + mediator.save_manager = *this; +} + +DB & SaveManager::get_db() { + if (this->db == nullptr) { + Config & cfg = Config::get_instance(); + this->db = make_unique(cfg.savemgr.location); + } + return *this->db; +} + template <> string SaveManager::serialize(const string & value) const noexcept { return value; @@ -90,22 +101,6 @@ int32_t SaveManager::deserialize(const string & value) const noexcept { return deserialize(value); } -SaveManager::SaveManager() { dbg_trace(); } - -SaveManager & SaveManager::get_instance() { - dbg_trace(); - static SaveManager instance; - return instance; -} - -DB & SaveManager::get_db() { - Config & cfg = Config::get_instance(); - // TODO: make this path relative to XDG_DATA_HOME on Linux and whatever the - // default equivalent is on Windows using some third party library - static DB db(cfg.savemgr.location); - return db; -} - bool SaveManager::has(const string & key) { DB & db = this->get_db(); return db.has(key); @@ -155,7 +150,8 @@ ValueBroker SaveManager::get(const string & key) { return { [this, key](const T & target) { this->set(key, target); }, [this, key, value]() mutable -> const T & { - value = this->deserialize(this->get_db().get(key)); + DB & db = this->get_db(); + value = this->deserialize(db.get(key)); return value; }, }; diff --git a/src/crepe/manager/SaveManager.h b/src/crepe/manager/SaveManager.h index 3d8c852..d13a97a 100644 --- a/src/crepe/manager/SaveManager.h +++ b/src/crepe/manager/SaveManager.h @@ -4,6 +4,8 @@ #include "../ValueBroker.h" +#include "Manager.h" + namespace crepe { class DB; @@ -18,7 +20,7 @@ class DB; * * The underlying database is a key-value store. */ -class SaveManager { +class SaveManager : public Manager { public: /** * \brief Get a read/write reference to a value and initialize it if it does not yet exist @@ -63,8 +65,8 @@ public: */ bool has(const std::string & key); -private: - SaveManager(); +public: + SaveManager(Mediator & mediator); virtual ~SaveManager() = default; private: @@ -90,25 +92,18 @@ private: T deserialize(const std::string & value) const noexcept; public: - // singleton - static SaveManager & get_instance(); SaveManager(const SaveManager &) = delete; SaveManager(SaveManager &&) = delete; SaveManager & operator=(const SaveManager &) = delete; SaveManager & operator=(SaveManager &&) = delete; +protected: + //! Create or return DB + virtual DB & get_db(); + private: - /** - * \brief Create an instance of DB and return its reference - * - * \returns DB instance - * - * This function exists because DB is a facade class, which can't directly be used in the API - * without workarounds - * - * TODO: better solution - */ - static DB & get_db(); + //! Database + std::unique_ptr db = nullptr; }; } // namespace crepe diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 8ef71bb..5a93b1f 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -16,8 +16,6 @@ function(add_example target_name) add_dependencies(examples ${target_name}) endfunction() -add_example(asset_manager) -add_example(savemgr) add_example(rendering_particle) add_example(game) add_example(button) diff --git a/src/example/asset_manager.cpp b/src/example/asset_manager.cpp deleted file mode 100644 index 917b547..0000000 --- a/src/example/asset_manager.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include -#include - -using namespace crepe; - -int main() { - - // this needs to be called before the asset manager otherwise the destructor of sdl is not in - // the right order - { Texture test("../asset/texture/img.png"); } - // FIXME: make it so the issue described by the above comment is not possible (i.e. the order - // in which internal classes are instantiated should not impact the way the engine works). - - auto & mgr = AssetManager::get_instance(); - - { - // TODO: [design] the Sound class can't be directly included by the user as it includes - // SoLoud headers. - auto bgm = mgr.cache("../mwe/audio/bgm.ogg"); - auto sfx1 = mgr.cache("../mwe/audio/sfx1.wav"); - auto sfx2 = mgr.cache("../mwe/audio/sfx2.wav"); - - auto img = mgr.cache("../asset/texture/img.png"); - auto img1 = mgr.cache("../asset/texture/second.png"); - } - - { - auto bgm = mgr.cache("../mwe/audio/bgm.ogg"); - auto sfx1 = mgr.cache("../mwe/audio/sfx1.wav"); - auto sfx2 = mgr.cache("../mwe/audio/sfx2.wav"); - - auto img = mgr.cache("../asset/texture/img.png"); - auto img1 = mgr.cache("../asset/texture/second.png"); - } -} diff --git a/src/example/savemgr.cpp b/src/example/savemgr.cpp deleted file mode 100644 index 65c4a34..0000000 --- a/src/example/savemgr.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/** \file - * - * Standalone example for usage of the save manager - */ - -#include -#include -#include -#include -#include - -using namespace crepe; - -// unrelated setup code -int _ = []() { - // make sure all log messages get printed - auto & cfg = Config::get_instance(); - cfg.log.level = Log::Level::TRACE; - - return 0; // satisfy compiler -}(); - -int main() { - const char * key = "mygame.test"; - - SaveManager & mgr = SaveManager::get_instance(); - - dbg_logf("has key = {}", mgr.has(key)); - ValueBroker prop = mgr.get(key, 0); - Proxy val = mgr.get(key, 0); - - dbg_logf("val = {}", mgr.get(key).get()); - prop.set(1); - dbg_logf("val = {}", mgr.get(key).get()); - val = 2; - dbg_logf("val = {}", mgr.get(key).get()); - mgr.set(key, 3); - dbg_logf("val = {}", mgr.get(key).get()); - - dbg_logf("has key = {}", mgr.has(key)); - assert(true == mgr.has(key)); - - return 0; -} diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index c9cbac5..734e3ee 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -17,4 +17,5 @@ target_sources(test_main PUBLIC ScriptEventTest.cpp ScriptSceneTest.cpp Profiling.cpp + SaveManagerTest.cpp ) diff --git a/src/test/SaveManagerTest.cpp b/src/test/SaveManagerTest.cpp new file mode 100644 index 0000000..a1efc33 --- /dev/null +++ b/src/test/SaveManagerTest.cpp @@ -0,0 +1,41 @@ +#include + +#include +#include +#include + +using namespace std; +using namespace crepe; +using namespace testing; + +class SaveManagerTest : public Test { + Mediator m; + class TestSaveManager : public SaveManager { + using SaveManager::SaveManager; + + // in-memory database for testing + DB db{}; + virtual DB & get_db() override { return this->db; } + }; + +public: + TestSaveManager mgr{m}; +}; + +TEST_F(SaveManagerTest, ReadWrite) { + ASSERT_FALSE(mgr.has("foo")); + mgr.set("foo", "bar"); + ASSERT_TRUE(mgr.has("foo")); + + ValueBroker value = mgr.get("foo"); + EXPECT_EQ(value.get(), "bar"); +} + +TEST_F(SaveManagerTest, DefaultValue) { + ValueBroker value = mgr.get("foo", 3); + + ASSERT_EQ(value.get(), 3); + value.set(5); + ASSERT_EQ(value.get(), 5); +} + -- cgit v1.2.3 From b5bf36aec3caa879d77457e3b2d1d29b6b625f27 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 7 Dec 2024 16:03:47 +0100 Subject: minor cleanup --- src/crepe/api/Script.h | 1 - src/crepe/manager/SaveManager.h | 7 ------- 2 files changed, 8 deletions(-) (limited to 'src/crepe/manager') diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 0d59ab6..3417bb4 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -2,7 +2,6 @@ #include -#include "../ValueBroker.h" #include "../manager/EventManager.h" #include "../manager/Mediator.h" #include "../system/CollisionSystem.h" diff --git a/src/crepe/manager/SaveManager.h b/src/crepe/manager/SaveManager.h index d13a97a..1b55a22 100644 --- a/src/crepe/manager/SaveManager.h +++ b/src/crepe/manager/SaveManager.h @@ -91,16 +91,9 @@ private: template T deserialize(const std::string & value) const noexcept; -public: - SaveManager(const SaveManager &) = delete; - SaveManager(SaveManager &&) = delete; - SaveManager & operator=(const SaveManager &) = delete; - SaveManager & operator=(SaveManager &&) = delete; - protected: //! Create or return DB virtual DB & get_db(); - private: //! Database std::unique_ptr db = nullptr; -- cgit v1.2.3 From 8ed0c3f425b02d4f064c455270288a6c4a359109 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 10 Dec 2024 20:10:58 +0100 Subject: use opaque type instead of forward declared DB for unique_ptr in SaveManager --- src/crepe/manager/SaveManager.cpp | 7 +++++-- src/crepe/manager/SaveManager.h | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'src/crepe/manager') diff --git a/src/crepe/manager/SaveManager.cpp b/src/crepe/manager/SaveManager.cpp index 292e8fd..39b92d4 100644 --- a/src/crepe/manager/SaveManager.cpp +++ b/src/crepe/manager/SaveManager.cpp @@ -14,9 +14,12 @@ SaveManager::SaveManager(Mediator & mediator) : Manager(mediator) { DB & SaveManager::get_db() { if (this->db == nullptr) { Config & cfg = Config::get_instance(); - this->db = make_unique(cfg.savemgr.location); + this->db = { + new DB(cfg.savemgr.location), + [](void * db){ delete static_cast(db); } + }; } - return *this->db; + return *static_cast(this->db.get()); } template <> diff --git a/src/crepe/manager/SaveManager.h b/src/crepe/manager/SaveManager.h index 1b55a22..27e625c 100644 --- a/src/crepe/manager/SaveManager.h +++ b/src/crepe/manager/SaveManager.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "../ValueBroker.h" @@ -96,7 +97,7 @@ protected: virtual DB & get_db(); private: //! Database - std::unique_ptr db = nullptr; + std::unique_ptr> db = nullptr; }; } // namespace crepe -- cgit v1.2.3 From bea86a6fcea272d7cc7fba44d6c489bd70587578 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 11 Dec 2024 13:27:24 +0100 Subject: Make format --- src/crepe/api/LoopManager.h | 2 +- src/crepe/manager/SaveManager.cpp | 6 ++---- src/crepe/manager/SaveManager.h | 5 +++-- src/crepe/system/AudioSystem.cpp | 3 +-- 4 files changed, 7 insertions(+), 9 deletions(-) (limited to 'src/crepe/manager') diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 33a61a3..700afe4 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -5,8 +5,8 @@ #include "../facade/SDLContext.h" #include "../manager/ComponentManager.h" #include "../manager/ResourceManager.h" -#include "../manager/SceneManager.h" #include "../manager/SaveManager.h" +#include "../manager/SceneManager.h" #include "../system/System.h" #include "LoopTimer.h" diff --git a/src/crepe/manager/SaveManager.cpp b/src/crepe/manager/SaveManager.cpp index 39b92d4..691ea2f 100644 --- a/src/crepe/manager/SaveManager.cpp +++ b/src/crepe/manager/SaveManager.cpp @@ -14,10 +14,8 @@ SaveManager::SaveManager(Mediator & mediator) : Manager(mediator) { DB & SaveManager::get_db() { if (this->db == nullptr) { Config & cfg = Config::get_instance(); - this->db = { - new DB(cfg.savemgr.location), - [](void * db){ delete static_cast(db); } - }; + this->db + = {new DB(cfg.savemgr.location), [](void * db) { delete static_cast(db); }}; } return *static_cast(this->db.get()); } diff --git a/src/crepe/manager/SaveManager.h b/src/crepe/manager/SaveManager.h index 27e625c..61a978d 100644 --- a/src/crepe/manager/SaveManager.h +++ b/src/crepe/manager/SaveManager.h @@ -1,7 +1,7 @@ #pragma once -#include #include +#include #include "../ValueBroker.h" @@ -95,9 +95,10 @@ private: protected: //! Create or return DB virtual DB & get_db(); + private: //! Database - std::unique_ptr> db = nullptr; + std::unique_ptr> db = nullptr; }; } // namespace crepe diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp index ddba268..b1aa0f8 100644 --- a/src/crepe/system/AudioSystem.cpp +++ b/src/crepe/system/AudioSystem.cpp @@ -30,8 +30,7 @@ void AudioSystem::diff_update(AudioSource & component, Sound & resource) { context.stop(component.voice); return; } - if (component.play_on_awake) - component.oneshot_play = true; + if (component.play_on_awake) component.oneshot_play = true; } if (!component.active) return; -- cgit v1.2.3