From 1941ee3690d27030e66d3df2b9b332b2c4100124 Mon Sep 17 00:00:00 2001 From: sebastian Date: Sat, 14 Feb 2026 15:44:17 +0100 Subject: [PATCH] ADD: Include Textures in Assetmanager --- src/engine/renderer/loader/AssetManager.cpp | 15 +++++++++++++++ src/engine/renderer/loader/AssetManager.h | 3 +++ src/game/GameLayer.cpp | 4 ++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/engine/renderer/loader/AssetManager.cpp b/src/engine/renderer/loader/AssetManager.cpp index cf13451..002e2ad 100644 --- a/src/engine/renderer/loader/AssetManager.cpp +++ b/src/engine/renderer/loader/AssetManager.cpp @@ -68,6 +68,21 @@ std::shared_ptr AssetManager::getModelStageConfiguratio return nullptr; } +std::shared_ptr AssetManager::loadTexture(const std::string& name, const std::string &texturePath, Loader &loader) { + if (textures.contains(name)) { + return textures[name]; + } + + auto texture = std::make_shared(loader.loadTextureFromFile(texturePath)); + textures[name] = texture; + return texture; +} + +std::shared_ptr AssetManager::getTexture(const std::string &name) { + assert(textures.contains(name) && "Texture not found!"); + return textures.at(name); +} + json AssetManager::readJsonFile(const std::string &path) { if (fs::exists(path)) { diff --git a/src/engine/renderer/loader/AssetManager.h b/src/engine/renderer/loader/AssetManager.h index 49f538c..d0aa552 100644 --- a/src/engine/renderer/loader/AssetManager.h +++ b/src/engine/renderer/loader/AssetManager.h @@ -21,9 +21,12 @@ public: static void loadAsset(const std::string& assetPath, Loader& loader); static std::shared_ptr getModelStages(const std::string& modelName); static std::shared_ptr getModelStageConfiguration(const std::string& modelName, const std::string& stageName); + static std::shared_ptr loadTexture(const std::string &name, const std::string &texturePath, Loader &loader); + static std::shared_ptr getTexture(const std::string& texturePath); private: static inline std::unordered_map> models; static inline std::unordered_map> modelsWithStages; + static inline std::unordered_map> textures; static nlohmann::json readJsonFile(const std::string &path); }; diff --git a/src/game/GameLayer.cpp b/src/game/GameLayer.cpp index 51f4536..f742d78 100644 --- a/src/game/GameLayer.cpp +++ b/src/game/GameLayer.cpp @@ -58,12 +58,12 @@ void GameLayer::onAttach() AssetManager::loadAsset("assets/buildings/stone_mason/stone_mason.json", loader); AssetManager::loadAsset("assets/buildings/forest_hut/cabin.json", loader); - ModelTexture modelTexture = loader.loadTextureFromFile("assets/worldIcons/warning.png"); + auto modelTexture = AssetManager::loadTexture("warning", "assets/worldIcons/warning.png", loader); TransformComponent transformComponent(glm::vec3(0,0,0), glm::vec3(0), 1.0f); testEntity = BuildingFactory::create(*entityManager, TemporaryBuildingDefinitionFactory::createForestHutDefinition(), transformComponent, 0, 0); auto worldSpritePtr = std::make_shared(); - worldSpritePtr->texture = std::make_shared(modelTexture); + worldSpritePtr->texture = modelTexture; entityManager->addComponent(testEntity, worldSpritePtr); auto anim = std::make_shared();