From 2d13805218eb34b6e06205c1b65c341ebcdad504 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 1 Oct 2024 11:03:50 +0200 Subject: changed resource manager to singleton --- resource-manager/main.cpp | 6 ++---- resource-manager/resource_manager.cpp | 11 +++++++++++ resource-manager/resource_manager.h | 24 ++++++++++++++++++++---- 3 files changed, 33 insertions(+), 8 deletions(-) (limited to 'resource-manager') diff --git a/resource-manager/main.cpp b/resource-manager/main.cpp index 6b3a23e..0148555 100644 --- a/resource-manager/main.cpp +++ b/resource-manager/main.cpp @@ -15,8 +15,6 @@ #include #include #include -#include -#include int main() { SDL_Init(SDL_INIT_VIDEO); @@ -32,8 +30,8 @@ int main() { SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0); - - ResourceManager * rm = new ResourceManager(); + + ResourceManager* rm = ResourceManager::get_instance(); ResourceConverter resource_converter; Texture* img = rm->Load("../img.png"); diff --git a/resource-manager/resource_manager.cpp b/resource-manager/resource_manager.cpp index 1412592..aee0ce2 100644 --- a/resource-manager/resource_manager.cpp +++ b/resource-manager/resource_manager.cpp @@ -5,6 +5,15 @@ #include "resource.h" #include +ResourceManager* ResourceManager::m_single_resource_manager = nullptr; + +ResourceManager* ResourceManager::get_instance(){ + if (m_single_resource_manager == nullptr) { + m_single_resource_manager = new ResourceManager(); + } + return m_single_resource_manager; +} + ResourceManager::ResourceManager(){ } @@ -14,6 +23,8 @@ ResourceManager::~ResourceManager(){ for(auto pair : m_resources){ delete pair.second; } + + delete m_single_resource_manager; } diff --git a/resource-manager/resource_manager.h b/resource-manager/resource_manager.h index ea9c80a..c155559 100644 --- a/resource-manager/resource_manager.h +++ b/resource-manager/resource_manager.h @@ -14,11 +14,29 @@ using namespace crepe; class ResourceManager{ -public: +private: + + static ResourceManager* m_single_resource_manager; + std::unordered_map m_resources; + + + +protected: ResourceManager(); ~ResourceManager(); - + +public: + ResourceManager(const ResourceManager &) = delete; + ResourceManager(ResourceManager &&) = delete; + ResourceManager &operator=(const ResourceManager &) = delete; + ResourceManager &operator=(ResourceManager &&) = delete; + + static ResourceManager* get_instance(); + + + +public: template T* Load(const Constants::FILE_PATH& file_path){ @@ -35,8 +53,6 @@ public: void Unload(const Constants::FILE_PATH& file_path); -private: - std::unordered_map m_resources; }; -- cgit v1.2.3