From 6aa64a6e27c23fabf67334d1756ce6b49b68f396 Mon Sep 17 00:00:00 2001 From: mridul-outscal Date: Tue, 1 Oct 2024 16:24:17 +0530 Subject: [PATCH 1/6] created a solution --- RM_Test/RM_Test.sln | 22 +++++ RM_Test/RM_Test.vcxproj | 162 ++++++++++++++++++++++++++++++++ RM_Test/RM_Test.vcxproj.filters | 22 +++++ 3 files changed, 206 insertions(+) create mode 100644 RM_Test/RM_Test.sln create mode 100644 RM_Test/RM_Test.vcxproj create mode 100644 RM_Test/RM_Test.vcxproj.filters diff --git a/RM_Test/RM_Test.sln b/RM_Test/RM_Test.sln new file mode 100644 index 0000000..5dacbc1 --- /dev/null +++ b/RM_Test/RM_Test.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "RM_Test", "RM_Test.vcxproj", "{D9098475-14D1-4F6A-9665-A2513CE60411}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D9098475-14D1-4F6A-9665-A2513CE60411}.Debug|Win32.ActiveCfg = Debug|Win32 + {D9098475-14D1-4F6A-9665-A2513CE60411}.Debug|Win32.Build.0 = Debug|Win32 + {D9098475-14D1-4F6A-9665-A2513CE60411}.Debug|x64.ActiveCfg = Debug|x64 + {D9098475-14D1-4F6A-9665-A2513CE60411}.Debug|x64.Build.0 = Debug|x64 + {D9098475-14D1-4F6A-9665-A2513CE60411}.Release|Win32.ActiveCfg = Release|Win32 + {D9098475-14D1-4F6A-9665-A2513CE60411}.Release|Win32.Build.0 = Release|Win32 + {D9098475-14D1-4F6A-9665-A2513CE60411}.Release|x64.ActiveCfg = Release|x64 + {D9098475-14D1-4F6A-9665-A2513CE60411}.Release|x64.Build.0 = Release|x64 + EndGlobalSection +EndGlobal diff --git a/RM_Test/RM_Test.vcxproj b/RM_Test/RM_Test.vcxproj new file mode 100644 index 0000000..cf63558 --- /dev/null +++ b/RM_Test/RM_Test.vcxproj @@ -0,0 +1,162 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {D9098475-14D1-4F6A-9665-A2513CE60411} + Win32Proj + RM_Test + 10.0 + + + + x64 + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + NotUsing + Level3 + Disabled + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + + + + + NotUsing + Level3 + Disabled + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + + + + + NotUsing + Level3 + MaxSpeed + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + true + true + + + + + NotUsing + Level3 + MaxSpeed + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/RM_Test/RM_Test.vcxproj.filters b/RM_Test/RM_Test.vcxproj.filters new file mode 100644 index 0000000..d5fffd7 --- /dev/null +++ b/RM_Test/RM_Test.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file From 90db8846ae9ee9d83c855bc60f719a340167a357 Mon Sep 17 00:00:00 2001 From: mridul-outscal Date: Tue, 1 Oct 2024 16:24:29 +0530 Subject: [PATCH 2/6] created main.cpp --- RM_Test/main.cpp | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 RM_Test/main.cpp diff --git a/RM_Test/main.cpp b/RM_Test/main.cpp new file mode 100644 index 0000000..942c05d --- /dev/null +++ b/RM_Test/main.cpp @@ -0,0 +1,6 @@ + +int main() +{ + + return 0; +} From 885838d3feee94421b74f35251a56cdafd5dbeb9 Mon Sep 17 00:00:00 2001 From: mridul-outscal Date: Mon, 7 Oct 2024 10:43:07 +0530 Subject: [PATCH 3/6] placeholder game loop created. --- RM_Test/GameLoop.cpp | 27 +++++++++++++++++++++++++++ RM_Test/GameLoop.h | 14 ++++++++++++++ RM_Test/RM_Test.vcxproj | 4 ++++ RM_Test/main.cpp | 8 +++++--- 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 RM_Test/GameLoop.cpp create mode 100644 RM_Test/GameLoop.h diff --git a/RM_Test/GameLoop.cpp b/RM_Test/GameLoop.cpp new file mode 100644 index 0000000..9f65b05 --- /dev/null +++ b/RM_Test/GameLoop.cpp @@ -0,0 +1,27 @@ +#include "GameLoop.h" +#include + +void GameLoop::initialize() { + std::cout << "Initializing game resources..." << std::endl; +} + +void GameLoop::update() { + std::cout << "Updating game logic..." << std::endl; +} + +void GameLoop::render() { + std::cout << "Rendering game..." << std::endl; +} + +void GameLoop::shutdown() { + std::cout << "Shutting down the game..." << std::endl; +} + +void GameLoop::run() { + while (isRunning) { + update(); + render(); + isRunning = false; // For now, we'll shut down after one iteration + } + shutdown(); +} diff --git a/RM_Test/GameLoop.h b/RM_Test/GameLoop.h new file mode 100644 index 0000000..a45bcc9 --- /dev/null +++ b/RM_Test/GameLoop.h @@ -0,0 +1,14 @@ +#pragma once + +class GameLoop { +public: + void initialize(); + void update(); + void render(); + void shutdown(); + void run(); + +private: + // ResourceManager* resource_manager = nullptr; + bool isRunning = true; +}; diff --git a/RM_Test/RM_Test.vcxproj b/RM_Test/RM_Test.vcxproj index cf63558..f1b39f5 100644 --- a/RM_Test/RM_Test.vcxproj +++ b/RM_Test/RM_Test.vcxproj @@ -154,8 +154,12 @@ + + + + diff --git a/RM_Test/main.cpp b/RM_Test/main.cpp index 942c05d..0d96ac6 100644 --- a/RM_Test/main.cpp +++ b/RM_Test/main.cpp @@ -1,6 +1,8 @@ +#include "GameLoop.h" -int main() -{ - +int main() { + GameLoop gameLoop; + gameLoop.initialize(); + gameLoop.run(); // Start the game loop return 0; } From 44dab6ca405481e30a28d2d534f7194cd0f5d9e4 Mon Sep 17 00:00:00 2001 From: mridul-outscal Date: Mon, 7 Oct 2024 11:30:21 +0530 Subject: [PATCH 4/6] Create base and child asset classes --- RM_Test/Asset.cpp | 10 ++++++++++ RM_Test/Asset.h | 21 +++++++++++++++++++++ RM_Test/RM_Test.vcxproj | 6 ++++++ RM_Test/SoundAsset.cpp | 11 +++++++++++ RM_Test/SoundAsset.h | 11 +++++++++++ RM_Test/TextureAsset.cpp | 11 +++++++++++ RM_Test/TextureAsset.h | 11 +++++++++++ 7 files changed, 81 insertions(+) create mode 100644 RM_Test/Asset.cpp create mode 100644 RM_Test/Asset.h create mode 100644 RM_Test/SoundAsset.cpp create mode 100644 RM_Test/SoundAsset.h create mode 100644 RM_Test/TextureAsset.cpp create mode 100644 RM_Test/TextureAsset.h diff --git a/RM_Test/Asset.cpp b/RM_Test/Asset.cpp new file mode 100644 index 0000000..488497e --- /dev/null +++ b/RM_Test/Asset.cpp @@ -0,0 +1,10 @@ +#include "Asset.h" + +// Constructor implementation +Asset::Asset(const std::string& assetId) : id(assetId) {} + +// Virtual destructor implementation +Asset::~Asset() = default; + +// Getter implementation +std::string Asset::getId() const { return id; } diff --git a/RM_Test/Asset.h b/RM_Test/Asset.h new file mode 100644 index 0000000..8197784 --- /dev/null +++ b/RM_Test/Asset.h @@ -0,0 +1,21 @@ +#pragma once +#include + +class Asset { +protected: + // Unique identifier for the asset + std::string id; + +public: + // Constructor with an asset identifier + Asset(const std::string& assetId); + + // Virtual destructor to ensure proper cleanup in derived classes + virtual ~Asset(); + + // Pure virtual function for loading the asset + virtual void load() = 0; + + // Getter for the asset ID + std::string getId() const; +}; \ No newline at end of file diff --git a/RM_Test/RM_Test.vcxproj b/RM_Test/RM_Test.vcxproj index f1b39f5..aee73c1 100644 --- a/RM_Test/RM_Test.vcxproj +++ b/RM_Test/RM_Test.vcxproj @@ -154,11 +154,17 @@ + + + + + + diff --git a/RM_Test/SoundAsset.cpp b/RM_Test/SoundAsset.cpp new file mode 100644 index 0000000..eb81c12 --- /dev/null +++ b/RM_Test/SoundAsset.cpp @@ -0,0 +1,11 @@ +#include "SoundAsset.h" +#include + +// Constructor implementation +SoundAsset::SoundAsset(const std::string& assetId) : Asset(assetId) {} + +// Implement the load function specific to sounds +void SoundAsset::load() { + std::cout << "Loading sound: " << id << std::endl; + // Simulate sound file loading +} \ No newline at end of file diff --git a/RM_Test/SoundAsset.h b/RM_Test/SoundAsset.h new file mode 100644 index 0000000..b312a6c --- /dev/null +++ b/RM_Test/SoundAsset.h @@ -0,0 +1,11 @@ +#pragma once +#include "Asset.h" + +class SoundAsset : public Asset { +public: + // Constructor + SoundAsset(const std::string& assetId); + + // Override the load function specific to sounds + void load() override; +}; \ No newline at end of file diff --git a/RM_Test/TextureAsset.cpp b/RM_Test/TextureAsset.cpp new file mode 100644 index 0000000..05be287 --- /dev/null +++ b/RM_Test/TextureAsset.cpp @@ -0,0 +1,11 @@ +#include "TextureAsset.h" +#include + +// Constructor implementation +TextureAsset::TextureAsset(const std::string& assetId) : Asset(assetId) {} + +// Implement the load function specific to textures +void TextureAsset::load() { + // Simulate texture loading from file + std::cout << "Loading texture: " << id << std::endl; +} \ No newline at end of file diff --git a/RM_Test/TextureAsset.h b/RM_Test/TextureAsset.h new file mode 100644 index 0000000..cec379b --- /dev/null +++ b/RM_Test/TextureAsset.h @@ -0,0 +1,11 @@ +#pragma once +#include "Asset.h" + +class TextureAsset : public Asset { +public: + // Constructor + TextureAsset(const std::string& assetId); + + // Override the load function specific to textures + void load() override; +}; \ No newline at end of file From 4bfe6c379405e44e3d39d8f0f712ad1697c14713 Mon Sep 17 00:00:00 2001 From: mridul-outscal Date: Mon, 7 Oct 2024 11:55:09 +0530 Subject: [PATCH 5/6] Created Resource Manager but it only works for Texture resources --- RM_Test/GameLoop.cpp | 4 ++++ RM_Test/GameLoop.h | 3 ++- RM_Test/RM_Test.vcxproj | 2 ++ RM_Test/ResourceManager.cpp | 17 +++++++++++++++++ RM_Test/ResourceManager.h | 15 +++++++++++++++ 5 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 RM_Test/ResourceManager.cpp create mode 100644 RM_Test/ResourceManager.h diff --git a/RM_Test/GameLoop.cpp b/RM_Test/GameLoop.cpp index 9f65b05..9810f79 100644 --- a/RM_Test/GameLoop.cpp +++ b/RM_Test/GameLoop.cpp @@ -3,6 +3,9 @@ void GameLoop::initialize() { std::cout << "Initializing game resources..." << std::endl; + resource_manager = new ResourceManager(); + resource_manager->load("player_texture"); + resource_manager->load("enemy_texture"); } void GameLoop::update() { @@ -15,6 +18,7 @@ void GameLoop::render() { void GameLoop::shutdown() { std::cout << "Shutting down the game..." << std::endl; + delete resource_manager; } void GameLoop::run() { diff --git a/RM_Test/GameLoop.h b/RM_Test/GameLoop.h index a45bcc9..43400d6 100644 --- a/RM_Test/GameLoop.h +++ b/RM_Test/GameLoop.h @@ -1,4 +1,5 @@ #pragma once +#include "ResourceManager.h" class GameLoop { public: @@ -9,6 +10,6 @@ class GameLoop { void run(); private: - // ResourceManager* resource_manager = nullptr; + ResourceManager* resource_manager = nullptr; bool isRunning = true; }; diff --git a/RM_Test/RM_Test.vcxproj b/RM_Test/RM_Test.vcxproj index aee73c1..2798fd8 100644 --- a/RM_Test/RM_Test.vcxproj +++ b/RM_Test/RM_Test.vcxproj @@ -157,12 +157,14 @@ + + diff --git a/RM_Test/ResourceManager.cpp b/RM_Test/ResourceManager.cpp new file mode 100644 index 0000000..d955ae7 --- /dev/null +++ b/RM_Test/ResourceManager.cpp @@ -0,0 +1,17 @@ +#include "ResourceManager.h" + +TextureAsset* ResourceManager::load(const std::string& assetId) { + if (assets.find(assetId) != assets.end()) { + return assets[assetId]; + } + TextureAsset* texture = new TextureAsset(assetId); + texture->load(); + assets[assetId] = texture; + return texture; +} + +ResourceManager::~ResourceManager() { + for (auto& pair : assets) { + delete pair.second; + } +} \ No newline at end of file diff --git a/RM_Test/ResourceManager.h b/RM_Test/ResourceManager.h new file mode 100644 index 0000000..a8a99d2 --- /dev/null +++ b/RM_Test/ResourceManager.h @@ -0,0 +1,15 @@ +#pragma once +#include +#include "TextureAsset.h" + +class ResourceManager { +private: + std::map assets; + +public: + TextureAsset* load(const std::string& assetId); + TextureAsset* get(const std::string& assetId); + ~ResourceManager(); +}; + + From 57d58143bf5c4ec560e57174b5b2d80538b7f6b5 Mon Sep 17 00:00:00 2001 From: mridul-outscal Date: Tue, 8 Oct 2024 16:31:23 +0530 Subject: [PATCH 6/6] Generic Resource Manager has been created. --- RM_Test/GameLoop.cpp | 6 +++-- RM_Test/RM_Test.vcxproj | 1 + RM_Test/ResourceManager.cpp | 31 ++++++++++++++---------- RM_Test/ResourceManager.h | 20 ++++++++++++---- RM_Test/ResourceManager.inl | 48 +++++++++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 18 deletions(-) create mode 100644 RM_Test/ResourceManager.inl diff --git a/RM_Test/GameLoop.cpp b/RM_Test/GameLoop.cpp index 9810f79..deeef20 100644 --- a/RM_Test/GameLoop.cpp +++ b/RM_Test/GameLoop.cpp @@ -1,11 +1,13 @@ #include "GameLoop.h" #include +#include "SoundAsset.h" + void GameLoop::initialize() { std::cout << "Initializing game resources..." << std::endl; resource_manager = new ResourceManager(); - resource_manager->load("player_texture"); - resource_manager->load("enemy_texture"); + resource_manager->loadResource("player_texture"); + resource_manager->loadResource("background_music"); } void GameLoop::update() { diff --git a/RM_Test/RM_Test.vcxproj b/RM_Test/RM_Test.vcxproj index 2798fd8..2502ae7 100644 --- a/RM_Test/RM_Test.vcxproj +++ b/RM_Test/RM_Test.vcxproj @@ -165,6 +165,7 @@ + diff --git a/RM_Test/ResourceManager.cpp b/RM_Test/ResourceManager.cpp index d955ae7..c1eccc7 100644 --- a/RM_Test/ResourceManager.cpp +++ b/RM_Test/ResourceManager.cpp @@ -1,17 +1,24 @@ #include "ResourceManager.h" +#include -TextureAsset* ResourceManager::load(const std::string& assetId) { - if (assets.find(assetId) != assets.end()) { - return assets[assetId]; +// Destructor: Clean up all resources +ResourceManager::~ResourceManager() { unloadAllResources(); } + +// Unload a specific resource by its ID +void ResourceManager::unloadResource(const std::string& id) +{ + auto iterator = assets.find(id); + if (iterator != assets.end()) + { + delete iterator->second; // Delete the resource + assets.erase(iterator); // Remove it from the map + std::cout << "Resource " << id << " unloaded." << std::endl; } - TextureAsset* texture = new TextureAsset(assetId); - texture->load(); - assets[assetId] = texture; - return texture; } -ResourceManager::~ResourceManager() { - for (auto& pair : assets) { - delete pair.second; - } -} \ No newline at end of file +// Unload all resources +void ResourceManager::unloadAllResources() +{ + assets.clear(); // Clear the map + std::cout << "All resources unloaded." << std::endl; +} diff --git a/RM_Test/ResourceManager.h b/RM_Test/ResourceManager.h index a8a99d2..b574c8e 100644 --- a/RM_Test/ResourceManager.h +++ b/RM_Test/ResourceManager.h @@ -4,12 +4,24 @@ class ResourceManager { private: - std::map assets; + std::map assets; public: - TextureAsset* load(const std::string& assetId); - TextureAsset* get(const std::string& assetId); + ResourceManager() = default; ~ResourceManager(); -}; + template + T* findResource(const std::string& id); + template + T* createResource(const std::string& id); + template + T* loadResource(const std::string& id); + template + T* getResource(const std::string& id); + + // Non-template methods for unloading resources + void unloadResource(const std::string& id); + void unloadAllResources(); +}; +#include "ResourceManager.inl" \ No newline at end of file diff --git a/RM_Test/ResourceManager.inl b/RM_Test/ResourceManager.inl new file mode 100644 index 0000000..9fb5133 --- /dev/null +++ b/RM_Test/ResourceManager.inl @@ -0,0 +1,48 @@ +#pragma once +#include + +// Load a resource or create it if it doesn't exist +template +T* ResourceManager::loadResource(const std::string& id) +{ + // Check if the resource is already loaded + auto existingResource = findResource(id); + + if (existingResource) + return existingResource; // Return the already loaded resource + + // Create a new resource if not found + return createResource(id); +} + +// Public function to get an already loaded resource by id +template +T* ResourceManager::getResource(const std::string& id) +{ + auto resource = findResource(id); + + if (resource == nullptr) + std::cout << "Resource not found: " << id; // Resource doesn't exist + + return resource; +} + +// Find a resource in the map +template +T* ResourceManager::findResource(const std::string& id) { + auto iterator = assets.find(id); + if (iterator != assets.end()) { + // Use dynamic_cast to ensure type safety + return dynamic_cast(iterator->second); + } + return nullptr; +} + +// Create and store a new resource in the map +template +T* ResourceManager::createResource(const std::string& id) { + T* resource = new T(id); // Create the resource + resource->load(); // Assuming T has a load() function + assets[id] = resource; // Add the resource to the map + return resource; +}