From 71c754b03938f0167393cd2599d6542654a86e92 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 30 Jun 2024 22:25:31 +0300 Subject: [PATCH] fix Image for non-existing textures in atlas --- src/graphics/core/Atlas.cpp | 8 ++++++++ src/graphics/core/Atlas.hpp | 2 ++ src/graphics/ui/elements/Image.cpp | 19 +++++++++++-------- src/logic/scripting/lua/libworld.cpp | 2 +- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/graphics/core/Atlas.cpp b/src/graphics/core/Atlas.cpp index 6067d6d6..7d8f7a31 100644 --- a/src/graphics/core/Atlas.cpp +++ b/src/graphics/core/Atlas.cpp @@ -34,6 +34,14 @@ const UVRegion& Atlas::get(const std::string& name) const { return regions.at(name); } +std::optional Atlas::getIf(const std::string& name) const { + const auto& found = regions.find(name); + if (found == regions.end()) { + return std::nullopt; + } + return found->second; +} + Texture* Atlas::getTexture() const { return texture.get(); } diff --git a/src/graphics/core/Atlas.hpp b/src/graphics/core/Atlas.hpp index 4561251b..8e8a7b33 100644 --- a/src/graphics/core/Atlas.hpp +++ b/src/graphics/core/Atlas.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include "../../maths/UVRegion.hpp" #include "../../typedefs.hpp" @@ -31,6 +32,7 @@ public: bool has(const std::string& name) const; const UVRegion& get(const std::string& name) const; + std::optional getIf(const std::string& name) const; Texture* getTexture() const; ImageData* getImage() const; diff --git a/src/graphics/ui/elements/Image.cpp b/src/graphics/ui/elements/Image.cpp index 11c61470..b6006c72 100644 --- a/src/graphics/ui/elements/Image.cpp +++ b/src/graphics/ui/elements/Image.cpp @@ -30,14 +30,17 @@ void Image::draw(const DrawContext* pctx, Assets* assets) { } else { auto atlasName = this->texture.substr(0, separator); if (auto atlas = assets->get(atlasName)) { - texture = atlas->getTexture(); - batch->texture(atlas->getTexture()); - auto& region = atlas->get(this->texture.substr(separator+1)); - batch->setRegion(region); - if (autoresize) { - setSize(glm::vec2( - texture->getWidth()*region.getWidth(), - texture->getHeight()*region.getHeight())); + if (auto region = atlas->getIf(this->texture.substr(separator+1))) { + texture = atlas->getTexture(); + batch->texture(atlas->getTexture()); + batch->setRegion(*region); + if (autoresize) { + setSize(glm::vec2( + texture->getWidth()*region->getWidth(), + texture->getHeight()*region->getHeight())); + } + } else { + batch->texture(nullptr); } } } diff --git a/src/logic/scripting/lua/libworld.cpp b/src/logic/scripting/lua/libworld.cpp index 75110ca0..642c85d8 100644 --- a/src/logic/scripting/lua/libworld.cpp +++ b/src/logic/scripting/lua/libworld.cpp @@ -26,7 +26,7 @@ static int l_world_get_list(lua::State* L) { lua::setfield(L, "name"); auto assets = engine->getAssets(); - std::string icon = "world:"+name+".icon"; + std::string icon = "world#"+name+".icon"; if (!AssetsLoader::loadExternalTexture(assets, icon, { worlds[i]/fs::path("icon.png"), worlds[i]/fs::path("preview.png")