diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-10-23 13:49:20 +0200 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-10-23 13:49:20 +0200 |
commit | eaa05e7a981b0f581f5393882e4753d9294a3dba (patch) | |
tree | 4f55cbacf8a70229ec523a45848fe89915d707d5 /src/crepe/api/AssetManager.h | |
parent | f1857fc2d4ddec71b3f0395903f8446cf96b8d0c (diff) |
rendering and asset_manager poc
:
Diffstat (limited to 'src/crepe/api/AssetManager.h')
-rw-r--r-- | src/crepe/api/AssetManager.h | 47 |
1 files changed, 20 insertions, 27 deletions
diff --git a/src/crepe/api/AssetManager.h b/src/crepe/api/AssetManager.h index a646d95..1b8b86f 100644 --- a/src/crepe/api/AssetManager.h +++ b/src/crepe/api/AssetManager.h @@ -2,56 +2,49 @@ +#include <any> #include <memory> #include <string> #include <unordered_map> #include <utility> -#include "api/baseResource.h" - namespace crepe::api{ -class ResourceManager{ +class AssetManager{ private: + std::unordered_map< std::string, std::any> asset_cache; - std::unordered_map< std::string, std::unique_ptr<BaseResource>> m_resources; - - -protected: - ResourceManager() = default; - ~ResourceManager(); +private: + AssetManager(); + virtual ~AssetManager(); public: - ResourceManager(const ResourceManager &) = delete; - ResourceManager(ResourceManager &&) = delete; - ResourceManager &operator=(const ResourceManager &) = delete; - ResourceManager &operator=(ResourceManager &&) = delete; + AssetManager(const AssetManager &) = delete; + AssetManager(AssetManager &&) = delete; + AssetManager &operator=(const AssetManager &) = delete; + AssetManager &operator=(AssetManager &&) = delete; - static ResourceManager& get_instance(); + static AssetManager& get_instance(); public: - template<typename T> - T* Load(const std::string& file_path){ - - if (m_resources.find(file_path) != m_resources.end()) { - return static_cast<T*>(m_resources[file_path].get()); - } + template<typename asset> + std::shared_ptr<asset> cache(const std::string& file_path, bool reload = false){ + auto it = asset_cache.find(file_path); - auto resource = std::make_unique<T>(file_path.c_str()); - if (resource) { - m_resources[file_path] = std::move(resource); - return static_cast<T*>(m_resources[file_path].get() ); + if (!reload && it != asset_cache.end()) { + return std::any_cast<std::shared_ptr<asset>>(it->second); } - return nullptr; - } + std::shared_ptr<asset> new_asset = std::make_shared<asset>(file_path.c_str()); - void Unload(const std::string& file_path); + asset_cache[file_path] = new_asset; + return new_asset; + } }; } |