aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-14 17:01:10 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-14 17:01:10 +0100
commit213f947d0907858cace470736c15f87caa934591 (patch)
tree2d15878761eefb84e5ccdfa75a91646d9fab4aa2
parent431b0bd7c6c502b42bb5be5488371d8c475e7024 (diff)
fix resource manager
-rw-r--r--src/crepe/api/Asset.cpp4
-rw-r--r--src/crepe/api/Asset.h10
-rw-r--r--src/crepe/api/ResourceManager.cpp6
-rw-r--r--src/crepe/api/ResourceManager.h23
-rw-r--r--src/crepe/api/ResourceManager.hpp25
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<const Asset>::operator()(const Asset & asset) const noexcept {
+ return std::hash<string>{}(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 <string>
+#include <unordered_map>
namespace crepe {
@@ -41,3 +42,12 @@ private:
};
} // namespace crepe
+
+namespace std {
+
+template<> struct hash<const crepe::Asset> {
+ 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 <any>
#include <memory>
-#include <string>
#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.
@@ -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<std::string, std::any> asset_cache;
+ std::unordered_map<const Asset, std::unique_ptr<Resource>> 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 <typename T>
- std::shared_ptr<T> 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 <stdexcept>
+#include <format>
+
#include "ResourceManager.h"
namespace crepe {
-template <typename asset>
-std::shared_ptr<asset> ResourceManager::cache(const std::string & file_path,
- bool reload) {
- auto it = asset_cache.find(file_path);
+template <typename T>
+T & ResourceManager::cache(const Asset & asset) {
+ using namespace std;
+ static_assert(is_base_of<Resource, T>::value, "cache must recieve a derivative class of Resource");
- if (!reload && it != asset_cache.end()) {
- return std::any_cast<std::shared_ptr<asset>>(it->second);
- }
+ if (!this->resources.contains(asset))
+ this->resources[asset] = make_unique<T>(asset);
- std::shared_ptr<asset> new_asset
- = std::make_shared<asset>(file_path.c_str());
+ Resource * resource = this->resources.at(asset).get();
+ T * concrete_resource = dynamic_cast<T *>(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