From ffd968a26826108ca9fb23d46ed5789260852eb7 Mon Sep 17 00:00:00 2001 From: Ritabrata Das Date: Wed, 9 Sep 2026 21:07:21 +0530 Subject: intial commit --- lib/assetManager.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 lib/assetManager.cpp (limited to 'lib/assetManager.cpp') diff --git a/lib/assetManager.cpp b/lib/assetManager.cpp new file mode 100644 index 0000000..9c60f9c --- /dev/null +++ b/lib/assetManager.cpp @@ -0,0 +1,60 @@ +#include "assetManager.hpp" + +#include +#include + +assetManager::assetManager() = default; + +assetManager::~assetManager() +{ + cleanup(); +} + +void assetManager::init(const std::string& dirPath) +{ + if (!std::filesystem::exists(dirPath) || !std::filesystem::is_directory(dirPath)) { + std::cerr << "assetmanager: directory not found: " << dirPath << std::endl; + return; + } + + for (const auto& entry : std::filesystem::recursive_directory_iterator(dirPath)) { + if (entry.is_regular_file()) { + std::string ext = entry.path().extension().string(); + + if (ext == ".png" || ext == ".jpg" || ext == ".jpeg") { + std::string key = entry.path().stem().string(); + std::string fullPath = entry.path().string(); + + Texture2D tex = LoadTexture(fullPath.c_str()); + + if (tex.id != 0) { + textures[key] = tex; + std::cout << "success loading texture: " << key << std::endl; + } else { + std::cerr << "failed to load texture: " << key << std::endl; + } + } + } + } +} + +Texture2D assetManager::load(const std::string& texName) +{ + const auto tex = textures.find(texName); + if (tex != textures.end()) { + return tex->second; + } + + std::cerr << "asset not found: " << texName << std::endl; + return Texture2D{0}; +} + +void assetManager::cleanup() +{ + for (auto& pair : textures) { + if (pair.second.id != 0) { + UnloadTexture(pair.second); + } + } + textures.clear(); +} -- cgit v1.2.3