From be6a8cca9e20e1aa04af5f643a75fd8cf9da6bae Mon Sep 17 00:00:00 2001 From: Vladislav Khorev Date: Fri, 1 May 2026 21:00:51 +0300 Subject: [PATCH] working on texture manager --- src/Game.cpp | 2 +- src/Location.cpp | 10 +++---- src/UiManager.cpp | 21 +++++--------- src/dialogue/DialogueOverlay.cpp | 21 +++++--------- src/dialogue/DialogueOverlay.h | 3 -- src/items/GameObjectLoader.cpp | 4 +-- src/render/Renderer.h | 2 ++ src/render/TextureManager.cpp | 49 ++++++++++++++++++++++++++++++++ src/render/TextureManager.h | 19 +++++++++++++ 9 files changed, 92 insertions(+), 39 deletions(-) diff --git a/src/Game.cpp b/src/Game.cpp index 47cddd8..ddbbb67 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -95,7 +95,7 @@ namespace ZL renderer.shaderManager.AddShaderFromFiles("defaultColor", "resources/shaders/defaultColor.vertex", "resources/shaders/defaultColor_desktop.fragment", CONST_ZIP_FILE); renderer.shaderManager.AddShaderFromFiles("default", "resources/shaders/default.vertex", "resources/shaders/default_desktop.fragment", CONST_ZIP_FILE); #endif - loadingTexture = std::make_unique(CreateTextureDataFromPng("resources/loading.png", "")); + loadingTexture = renderer.textureManager.LoadFromPng("resources/loading.png", ""); float minDimension; float width = Environment::projectionWidth; diff --git a/src/Location.cpp b/src/Location.cpp index cc7e439..dbdca37 100644 --- a/src/Location.cpp +++ b/src/Location.cpp @@ -49,7 +49,7 @@ namespace ZL void Location::setup(const LocationSetup& params) { - roomTexture = std::make_unique(CreateTextureDataFromPng(params.roomTexturePath, CONST_ZIP_FILE)); + roomTexture = renderer.textureManager.LoadFromPng(params.roomTexturePath, CONST_ZIP_FILE); roomMesh.data = LoadFromTextFile02(params.roomMeshPath, CONST_ZIP_FILE); roomMesh.data.RotateByMatrix(Eigen::Quaternionf(Eigen::AngleAxisf(-M_PI * 0.5, Eigen::Vector3f::UnitY())).toRotationMatrix()); roomMesh.RefreshVBO(); @@ -62,9 +62,9 @@ namespace ZL //auto playerTexture = std::make_shared(CreateTextureDataFromPng("resources/w/gg/IMG_20260413_182354_992.png", CONST_ZIP_FILE)); - auto playerTexture = std::make_shared(CreateTextureDataFromPng("resources/w/gg/UniV_Grid_2K_Base_color.png", CONST_ZIP_FILE)); + auto playerTexture = renderer.textureManager.LoadFromPng("resources/w/gg/UniV_Grid_2K_Base_color.png", CONST_ZIP_FILE); - auto sparkTexture = std::make_shared(CreateTextureDataFromPng("resources/w/spark.png", CONST_ZIP_FILE)); + auto sparkTexture = renderer.textureManager.LoadFromPng("resources/w/spark.png", CONST_ZIP_FILE); player = std::make_unique(); /* @@ -90,7 +90,7 @@ namespace ZL - player->weaponTexture = std::make_shared(CreateTextureDataFromPng("resources/w/white.png", CONST_ZIP_FILE)); + player->weaponTexture = renderer.textureManager.LoadFromPng("resources/w/white.png", CONST_ZIP_FILE); //player->weaponMesh.AssignFrom(LoadFromTextFile02("resources/w/gg/knife001.txt", CONST_ZIP_FILE)); player->weaponMesh.data = LoadFromTextFile02("resources/w/gg/knife002.txt", CONST_ZIP_FILE); @@ -132,7 +132,7 @@ namespace ZL } } - auto sparkTexture2 = std::make_shared(CreateTextureDataFromPng("resources/w/star.png", CONST_ZIP_FILE)); + auto sparkTexture2 = renderer.textureManager.LoadFromPng("resources/w/star.png", CONST_ZIP_FILE); // Teleport zone visuals: an upward fountain of sparks parked at the // teleport position, so the player can spot the zone from a distance. diff --git a/src/UiManager.cpp b/src/UiManager.cpp index c5f4126..e2043e5 100644 --- a/src/UiManager.cpp +++ b/src/UiManager.cpp @@ -490,8 +490,7 @@ namespace ZL { std::string path = t[key].get(); try { std::cout << "UiManager: loading texture for button '" << btn->name << "' : " << path << " Zip file: " << zipFile << std::endl; - auto data = CreateTextureDataFromPng(path.c_str(), zipFile.c_str()); - return std::make_shared(data); + return renderer.textureManager.LoadFromPng(path, zipFile); } catch (const std::exception& e) { std::cerr << "UiManager: failed load texture " << path << " : " << e.what() << std::endl; @@ -523,8 +522,7 @@ namespace ZL { std::string path = t[key].get(); try { std::cout << "UiManager: --loading texture for slider '" << s->name << "' : " << path << " Zip file: " << zipFile << std::endl; - auto data = CreateTextureDataFromPng(path.c_str(), zipFile.c_str()); - return std::make_shared(data); + return renderer.textureManager.LoadFromPng(path, zipFile); } catch (const std::exception& e) { std::cerr << "UiManager: failed load texture " << path << " : " << e.what() << std::endl; @@ -674,8 +672,7 @@ namespace ZL { if (!texPath.empty()) { try { - auto data = CreateTextureDataFromPng(texPath.c_str(), zipFile.c_str()); - img->texture = std::make_shared(data); + img->texture = renderer.textureManager.LoadFromPng(texPath, zipFile); } catch (const std::exception& e) { std::cerr << "UiManager: failed load texture for StaticImage '" << img->name << "' : " << e.what() << std::endl; @@ -1058,14 +1055,10 @@ namespace ZL { s->vertical = vertical; try { - if (!trackPath.empty()) { - auto data = CreateTextureDataFromPng(trackPath.c_str(), zipFile.c_str()); - s->texTrack = std::make_shared(data); - } - if (!knobPath.empty()) { - auto data = CreateTextureDataFromPng(knobPath.c_str(), zipFile.c_str()); - s->texKnob = std::make_shared(data); - } + if (!trackPath.empty()) + s->texTrack = renderer.textureManager.LoadFromPng(trackPath, zipFile); + if (!knobPath.empty()) + s->texKnob = renderer.textureManager.LoadFromPng(knobPath, zipFile); } catch (const std::exception& e) { std::cerr << "UiManager: addSlider failed to load textures: " << e.what() << std::endl; diff --git a/src/dialogue/DialogueOverlay.cpp b/src/dialogue/DialogueOverlay.cpp index 8f5e87a..0bd134f 100644 --- a/src/dialogue/DialogueOverlay.cpp +++ b/src/dialogue/DialogueOverlay.cpp @@ -63,12 +63,12 @@ bool DialogueOverlay::init(Renderer& renderer, const std::string& zipFile) { rendererRef = &renderer; zipFilename = zipFile; - textboxTexture = std::make_shared(CreateTextureDataFromPng("resources/dialogue/textbox_bg.png", zipFile)); - portraitFrameTexture = std::make_shared(CreateTextureDataFromPng("resources/dialogue/portrait_frame.png", zipFile)); - choiceMainTexture = std::make_shared(CreateTextureDataFromPng("resources/dialogue/choice_main.png", zipFile)); - choiceOptionalTexture = std::make_shared(CreateTextureDataFromPng("resources/dialogue/choice_optional.png", zipFile)); - choiceSelectedTexture = std::make_shared(CreateTextureDataFromPng("resources/dialogue/choice_selected.png", zipFile)); - cutsceneSubtitleTexture = std::make_shared(CreateTextureDataFromPng("resources/dialogue/cutscene_subtitle_bg.png", zipFile)); + textboxTexture = renderer.textureManager.LoadFromPng("resources/dialogue/textbox_bg.png", zipFile); + portraitFrameTexture = renderer.textureManager.LoadFromPng("resources/dialogue/portrait_frame.png", zipFile); + choiceMainTexture = renderer.textureManager.LoadFromPng("resources/dialogue/choice_main.png", zipFile); + choiceOptionalTexture = renderer.textureManager.LoadFromPng("resources/dialogue/choice_optional.png", zipFile); + choiceSelectedTexture = renderer.textureManager.LoadFromPng("resources/dialogue/choice_selected.png", zipFile); + cutsceneSubtitleTexture = renderer.textureManager.LoadFromPng("resources/dialogue/cutscene_subtitle_bg.png", zipFile); nameRenderer = std::make_unique(); bodyRenderer = std::make_unique(); @@ -648,14 +648,7 @@ std::shared_ptr DialogueOverlay::loadTextureCached(const std::string& p return nullptr; } - auto it = textureCache.find(path); - if (it != textureCache.end()) { - return it->second; - } - - auto texture = std::make_shared(CreateTextureDataFromPng(path, zipFilename)); - textureCache[path] = texture; - return texture; + return rendererRef->textureManager.LoadFromPng(path, zipFilename); } void DialogueOverlay::drawQuad(Renderer& renderer, const TexturedQuad& quad, const std::shared_ptr& texture) const { diff --git a/src/dialogue/DialogueOverlay.h b/src/dialogue/DialogueOverlay.h index aae4a0b..eccb6ce 100644 --- a/src/dialogue/DialogueOverlay.h +++ b/src/dialogue/DialogueOverlay.h @@ -7,7 +7,6 @@ #include "UiManager.h" #include #include -#include #include namespace ZL::Dialogue { @@ -90,8 +89,6 @@ private: TexturedQuad skipProgressFillQuad; mutable std::vector choiceQuads; - std::unordered_map> textureCache; - void drawDialogue(Renderer& renderer, const PresentationModel& model); void drawCutscene(Renderer& renderer, const PresentationModel& model); diff --git a/src/items/GameObjectLoader.cpp b/src/items/GameObjectLoader.cpp index cb483b2..b837166 100644 --- a/src/items/GameObjectLoader.cpp +++ b/src/items/GameObjectLoader.cpp @@ -129,7 +129,7 @@ namespace ZL { // Load texture try { - gameObj.texture = std::make_shared(CreateTextureDataFromPng(objData.texturePath, zipPath.c_str())); + gameObj.texture = renderer.textureManager.LoadFromPng(objData.texturePath, zipPath); } catch (const std::exception& e) { std::cerr << "GameObjectLoader: Failed to load texture for '" << objData.name << "': " << e.what() << std::endl; @@ -202,7 +202,7 @@ namespace ZL { // Load texture try { - intObj.texture = std::make_shared(CreateTextureDataFromPng(objData.texturePath, zipPath.c_str())); + intObj.texture = renderer.textureManager.LoadFromPng(objData.texturePath, zipPath); } catch (const std::exception& e) { std::cerr << "GameObjectLoader: Failed to load texture for interactive '" << objData.name << "': " << e.what() << std::endl; diff --git a/src/render/Renderer.h b/src/render/Renderer.h index 3e040aa..51614fc 100644 --- a/src/render/Renderer.h +++ b/src/render/Renderer.h @@ -5,6 +5,7 @@ #include #include #include "ShaderManager.h" +#include "TextureManager.h" #include namespace ZL { @@ -104,6 +105,7 @@ namespace ZL { public: ShaderManager shaderManager; + TextureManager textureManager; void InitOpenGL(); diff --git a/src/render/TextureManager.cpp b/src/render/TextureManager.cpp index e348e7b..951f309 100644 --- a/src/render/TextureManager.cpp +++ b/src/render/TextureManager.cpp @@ -416,5 +416,54 @@ namespace ZL #endif + std::string TextureManager::MakeKey(const std::string& fileName, const std::string& zipFile) + { + return zipFile.empty() ? fileName : fileName + "|" + zipFile; + } + + std::shared_ptr TextureManager::LoadFromBmp24(const std::string& fileName, const std::string& zipFile) + { + std::string key = MakeKey(fileName, zipFile); + auto it = textureMap.find(key); + if (it != textureMap.end()) + return it->second; + auto tex = std::make_shared(CreateTextureDataFromBmp24(fileName, zipFile)); + textureMap[key] = tex; + return tex; + } + + std::shared_ptr TextureManager::LoadFromBmp32(const std::string& fileName, const std::string& zipFile) + { + std::string key = MakeKey(fileName, zipFile); + auto it = textureMap.find(key); + if (it != textureMap.end()) + return it->second; + auto tex = std::make_shared(CreateTextureDataFromBmp32(fileName, zipFile)); + textureMap[key] = tex; + return tex; + } + +#ifdef PNG_ENABLED + std::shared_ptr TextureManager::LoadFromPng(const std::string& fileName, const std::string& zipFile) + { + std::string key = MakeKey(fileName, zipFile); + auto it = textureMap.find(key); + if (it != textureMap.end()) + return it->second; + auto tex = std::make_shared(CreateTextureDataFromPng(fileName, zipFile)); + textureMap[key] = tex; + return tex; + } +#endif + + void TextureManager::Unload(const std::string& fileName, const std::string& zipFile) + { + textureMap.erase(MakeKey(fileName, zipFile)); + } + + void TextureManager::UnloadAll() + { + textureMap.clear(); + } } \ No newline at end of file diff --git a/src/render/TextureManager.h b/src/render/TextureManager.h index ea453b1..eb53ec4 100644 --- a/src/render/TextureManager.h +++ b/src/render/TextureManager.h @@ -2,6 +2,8 @@ #include "render/OpenGlExtensions.h" #include "utils/Utils.h" +#include +#include #if defined EMSCRIPTEN || defined __ANDROID__ #define PNG_ENABLED @@ -30,6 +32,7 @@ namespace ZL Format format = RGBA; MipmapType mipmap = GENERATE; }; + class Texture { size_t width = 0; @@ -59,5 +62,21 @@ namespace ZL TextureDataStruct CreateTextureDataFromPng(const std::string& fullFileName, const std::string& ZIPFileName = ""); #endif + class TextureManager + { + public: + std::shared_ptr LoadFromBmp24(const std::string& fileName, const std::string& zipFile = ""); + std::shared_ptr LoadFromBmp32(const std::string& fileName, const std::string& zipFile = ""); +#ifdef PNG_ENABLED + std::shared_ptr LoadFromPng(const std::string& fileName, const std::string& zipFile = ""); +#endif + + void Unload(const std::string& fileName, const std::string& zipFile = ""); + void UnloadAll(); + + private: + std::unordered_map> textureMap; + static std::string MakeKey(const std::string& fileName, const std::string& zipFile); + }; }