From 213f947d0907858cace470736c15f87caa934591 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 14 Nov 2024 17:01:10 +0100 Subject: fix resource manager --- src/crepe/api/Asset.cpp | 4 ++++ src/crepe/api/Asset.h | 10 ++++++++++ src/crepe/api/ResourceManager.cpp | 6 +----- src/crepe/api/ResourceManager.h | 23 +++++++++++++++-------- src/crepe/api/ResourceManager.hpp | 25 ++++++++++++++----------- 5 files changed, 44 insertions(+), 24 deletions(-) diff --git a/src/crepe/api/Asset.cpp b/src/crepe/api/Asset.cpp index 8692c6c..1887814 100644 --- a/src/crepe/api/Asset.cpp +++ b/src/crepe/api/Asset.cpp @@ -48,3 +48,7 @@ string Asset::whereami() const noexcept { return path; } +size_t std::hash::operator()(const Asset & asset) const noexcept { + return std::hash{}(asset.get_path()); +}; + diff --git a/src/crepe/api/Asset.h b/src/crepe/api/Asset.h index f6e6782..0f6b0b3 100644 --- a/src/crepe/api/Asset.h +++ b/src/crepe/api/Asset.h @@ -1,6 +1,7 @@ #pragma once #include +#include namespace crepe { @@ -41,3 +42,12 @@ private: }; } // namespace crepe + +namespace std { + +template<> struct hash { + size_t operator()(const crepe::Asset & asset) const noexcept; +}; + +} + diff --git a/src/crepe/api/ResourceManager.cpp b/src/crepe/api/ResourceManager.cpp index 470e511..17fbd9b 100644 --- a/src/crepe/api/ResourceManager.cpp +++ b/src/crepe/api/ResourceManager.cpp @@ -9,9 +9,5 @@ ResourceManager & ResourceManager::get_instance() { return instance; } -ResourceManager::~ResourceManager() { - dbg_trace(); - this->asset_cache.clear(); -} - +ResourceManager::~ResourceManager() { dbg_trace(); } ResourceManager::ResourceManager() { dbg_trace(); } diff --git a/src/crepe/api/ResourceManager.h b/src/crepe/api/ResourceManager.h index 7a45493..a69a9fa 100644 --- a/src/crepe/api/ResourceManager.h +++ b/src/crepe/api/ResourceManager.h @@ -1,12 +1,15 @@ #pragma once -#include #include -#include #include +#include "Asset.h" +#include "Resource.h" + namespace crepe { +class Sound; + /** * \brief The ResourceManager is responsible for storing and managing assets over * multiple scenes. @@ -20,18 +23,18 @@ class ResourceManager { private: //! A cache that holds all the assets, accessible by their file path, over multiple scenes. - std::unordered_map asset_cache; + std::unordered_map> resources; private: - ResourceManager(); - virtual ~ResourceManager(); + ResourceManager(); // dbg_trace + virtual ~ResourceManager(); // dbg_trace -public: 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. * @@ -56,8 +59,12 @@ public: * cache. */ template - std::shared_ptr cache(const std::string & file_path, - bool reload = false); + T & cache(const Asset & asset); + + /** + * \brief Clear the resource cache + */ + void clear(); }; } // namespace crepe diff --git a/src/crepe/api/ResourceManager.hpp b/src/crepe/api/ResourceManager.hpp index 9cd4bcb..62cac20 100644 --- a/src/crepe/api/ResourceManager.hpp +++ b/src/crepe/api/ResourceManager.hpp @@ -1,24 +1,27 @@ #pragma once +#include +#include + #include "ResourceManager.h" namespace crepe { -template -std::shared_ptr ResourceManager::cache(const std::string & file_path, - bool reload) { - auto it = asset_cache.find(file_path); +template +T & ResourceManager::cache(const Asset & asset) { + using namespace std; + static_assert(is_base_of::value, "cache must recieve a derivative class of Resource"); - if (!reload && it != asset_cache.end()) { - return std::any_cast>(it->second); - } + if (!this->resources.contains(asset)) + this->resources[asset] = make_unique(asset); - std::shared_ptr new_asset - = std::make_shared(file_path.c_str()); + Resource * resource = this->resources.at(asset).get(); + T * concrete_resource = dynamic_cast(resource); - asset_cache[file_path] = new_asset; + if (concrete_resource == nullptr) + throw runtime_error(format("ResourceManager: mismatch between requested type and actual type of resource ({})", asset.get_path())); - return new_asset; + return *concrete_resource; } } // namespace crepe -- cgit v1.2.3