aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/manager/ResourceManager.h
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-29 12:21:26 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-29 12:21:26 +0100
commitc2ef6a36532c8c078fd7836325d6be277b946cbf (patch)
treebcac24106220ad626aca5dfcd3576e7d60ac3af2 /src/crepe/manager/ResourceManager.h
parent74bffd3e466c342ca80811146a536716fb6437cb (diff)
parent7a07c56d572a6f30d0aa611bd566197bc04c3b33 (diff)
merge `loek/scripts`
Diffstat (limited to 'src/crepe/manager/ResourceManager.h')
-rw-r--r--src/crepe/manager/ResourceManager.h48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/crepe/manager/ResourceManager.h b/src/crepe/manager/ResourceManager.h
new file mode 100644
index 0000000..e7e6abc
--- /dev/null
+++ b/src/crepe/manager/ResourceManager.h
@@ -0,0 +1,48 @@
+#pragma once
+
+#include <memory>
+#include <unordered_map>
+
+#include "../Resource.h"
+#include "../api/Asset.h"
+
+#include "Manager.h"
+
+namespace crepe {
+
+/**
+ * \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 : public Manager {
+public:
+ ResourceManager(Mediator & mediator);
+ virtual ~ResourceManager(); // dbg_trace
+
+private:
+ struct CacheEntry {
+ std::unique_ptr<Resource> resource = nullptr;
+ bool persistent = false;
+ };
+ //! A cache that holds all the assets, accessible by their file path, over multiple scenes.
+ std::unordered_map<const Asset, CacheEntry> resources;
+ CacheEntry & get_entry(const Asset & asset);
+
+public:
+ void set_persistent(const Asset & asset, bool persistent);
+
+ template <typename Resource>
+ Resource & get(const Asset & asset);
+
+ void clear();
+ void clear_all();
+};
+
+} // namespace crepe
+
+#include "ResourceManager.hpp"