diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-26 20:03:41 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-26 20:03:41 +0100 |
commit | a685e5f743786cc6499e7ce8973bb78a83d101f7 (patch) | |
tree | 21d7aeb768912bef8f98436d13acb4423cffd8f0 /src/crepe | |
parent | 92e3fbda73128e65a31b3760b3fa4bd0147debe3 (diff) |
big WIP
Diffstat (limited to 'src/crepe')
-rw-r--r-- | src/crepe/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/ResourceManager.cpp | 36 | ||||
-rw-r--r-- | src/crepe/ResourceManager.h (renamed from src/crepe/api/ResourceManager.h) | 49 | ||||
-rw-r--r-- | src/crepe/api/AudioSource.h | 1 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/api/Config.h | 27 | ||||
-rw-r--r-- | src/crepe/api/ResourceManager.cpp | 41 | ||||
-rw-r--r-- | src/crepe/system/AudioSystem.cpp | 2 | ||||
-rw-r--r-- | src/crepe/system/AudioSystem.h | 4 | ||||
-rw-r--r-- | src/crepe/util/Log.cpp | 2 | ||||
-rw-r--r-- | src/crepe/util/Log.h | 16 |
11 files changed, 89 insertions, 93 deletions
diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index df15b8f..0313dfa 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 + ResourceManager.cpp Resource.cpp ) @@ -13,6 +14,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Collider.h ValueBroker.h ValueBroker.hpp + ResourceManager.h Resource.h ) diff --git a/src/crepe/ResourceManager.cpp b/src/crepe/ResourceManager.cpp new file mode 100644 index 0000000..111b9e0 --- /dev/null +++ b/src/crepe/ResourceManager.cpp @@ -0,0 +1,36 @@ +#include <stdexcept> + +#include "util/Log.h" + +#include "ResourceManager.h" + +using namespace crepe; +using namespace std; + +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/ResourceManager.h index efdd5c5..26a86a8 100644 --- a/src/crepe/api/ResourceManager.h +++ b/src/crepe/ResourceManager.h @@ -3,12 +3,13 @@ #include <memory> #include <unordered_map> -#include "Asset.h" +#include "api/Asset.h" + +#include "Component.h" #include "Resource.h" namespace crepe { -class Sound; /** * \brief The ResourceManager is responsible for storing and managing assets over @@ -20,27 +21,19 @@ class Sound; * 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: +public: ResourceManager(); // dbg_trace virtual ~ResourceManager(); // dbg_trace - ResourceManager(const ResourceManager &) = delete; - ResourceManager(ResourceManager &&) = delete; - ResourceManager & operator=(const ResourceManager &) = delete; - ResourceManager & operator=(ResourceManager &&) = delete; +private: + template <typename Resource> + Resource & get_internal(const Component & component, const Asset & asset); -public: - /** - * \brief Retrieves the singleton instance of the ResourceManager. - * - * \return A reference to the single instance of the ResourceManager. - */ - static ResourceManager & get_instance(); + template <typename Resource> + const Asset & get_source(const Component & component) const; + + //! 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; public: /** @@ -58,12 +51,26 @@ public: * Otherwise, the concrete resource will be instantiated and added to the * cache. */ - template <typename T> - T & cache(const Asset & asset); + template <typename Resource> + void cache(const Asset & asset, bool persistent = false); + + template <typename Component> + void cache(const Component & component, bool persistent = false); + + // void resman.cache<Resource>(Asset, Lifetime); + // void resman.cache(Component, Asset, Lifetime); + + template <typename Resource, typename Component> + Resource & get(const Component & component); //! Clear the resource cache void clear(); }; +class Sound; +class AudioSource; +template <> +Sound & ResourceManager::get(const AudioSource & component); + } // namespace crepe diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 1264790..0950129 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -3,6 +3,7 @@ #include "../Component.h" #include "../types.h" +#include "GameObject.h" #include "Asset.h" namespace crepe { diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index a2e21fa..ad82924 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -7,7 +7,6 @@ target_sources(crepe PUBLIC Transform.cpp Color.cpp Texture.cpp - ResourceManager.cpp Sprite.cpp SaveManager.cpp Config.cpp @@ -39,7 +38,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Vector2.hpp Color.h Texture.h - ResourceManager.h SaveManager.h Scene.h Metadata.h diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 0c9d116..5bd6913 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -11,35 +11,12 @@ namespace crepe { * modified *before* execution is handed over from the game programmer to the engine (i.e. the * main loop is started). */ -class Config final { -public: +struct Config final { //! Retrieve handle to global Config instance static Config & get_instance(); -private: - Config() = default; - ~Config() = default; - Config(const Config &) = default; - Config(Config &&) = default; - Config & operator=(const Config &) = default; - Config & operator=(Config &&) = default; - -public: //! Logging-related settings - 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; + Log::Config log; //! Save manager struct { diff --git a/src/crepe/api/ResourceManager.cpp b/src/crepe/api/ResourceManager.cpp deleted file mode 100644 index 7877ed9..0000000 --- a/src/crepe/api/ResourceManager.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#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/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp index b7ac1f2..6c30b85 100644 --- a/src/crepe/system/AudioSystem.cpp +++ b/src/crepe/system/AudioSystem.cpp @@ -47,7 +47,7 @@ void AudioSystem::update() { * using the same underlying instance of Sound (esp. w/ * play/pause/retrigger behavior). */ - Sound & sound = this->resman.cache<Sound>(component.source); + // Sound & sound = this->resource_manager.get<Sound>(component); // TODO: lots of state diffing } diff --git a/src/crepe/system/AudioSystem.h b/src/crepe/system/AudioSystem.h index d0b4f9a..d3d5aeb 100644 --- a/src/crepe/system/AudioSystem.h +++ b/src/crepe/system/AudioSystem.h @@ -1,7 +1,7 @@ #pragma once #include "../facade/SoundContext.h" -#include "../api/ResourceManager.h" +#include "../ResourceManager.h" #include "System.h" @@ -14,7 +14,7 @@ public: private: SoundContext context {}; - ResourceManager & resman = ResourceManager::get_instance(); + ResourceManager resource_manager {}; }; } // namespace crepe diff --git a/src/crepe/util/Log.cpp b/src/crepe/util/Log.cpp index 84d80a8..bc86c7e 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 = Config::get_instance(); + auto & cfg = crepe::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 fc0bb3a..914145a 100644 --- a/src/crepe/util/Log.h +++ b/src/crepe/util/Log.h @@ -77,6 +77,22 @@ 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 |