From 4f377b2056a56338120b4150a609f4ba90d28c35 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 1 Nov 2024 02:13:21 +0300 Subject: [PATCH] refactor textures access --- src/assets/assets_util.cpp | 24 +++++++++++++++++++++ src/assets/assets_util.hpp | 21 ++++++++++++++++++ src/graphics/core/Batch2D.cpp | 2 +- src/graphics/core/Batch2D.hpp | 4 ++-- src/graphics/core/Cubemap.cpp | 4 ++-- src/graphics/core/Cubemap.hpp | 4 ++-- src/graphics/core/GLTexture.cpp | 4 ++-- src/graphics/core/GLTexture.hpp | 4 ++-- src/graphics/core/Texture.hpp | 4 ++-- src/graphics/render/ModelBatch.cpp | 25 ++++++---------------- src/graphics/render/ModelBatch.hpp | 4 ++-- src/graphics/render/WorldRenderer.cpp | 3 +-- src/graphics/ui/elements/InventoryView.cpp | 20 ++++++----------- 13 files changed, 74 insertions(+), 49 deletions(-) create mode 100644 src/assets/assets_util.cpp create mode 100644 src/assets/assets_util.hpp diff --git a/src/assets/assets_util.cpp b/src/assets/assets_util.cpp new file mode 100644 index 00000000..af7a90e2 --- /dev/null +++ b/src/assets/assets_util.cpp @@ -0,0 +1,24 @@ +#include "assets_util.hpp" + +#include "assets/Assets.hpp" +#include "graphics/core/Atlas.hpp" +#include "graphics/core/Texture.hpp" + +util::TextureRegion util::getTextureRegion( + const Assets& assets, const std::string& name, const std::string& fallback +) { + size_t sep = name.find(':'); + if (sep == std::string::npos) { + return {assets.get(name), UVRegion(0,0,1,1)}; + } else { + auto atlas = assets.get(name.substr(0, sep)); + if (atlas) { + if (auto reg = atlas->getIf(name.substr(sep+1))) { + return {atlas->getTexture(), *reg}; + } else if (!fallback.empty()){ + return util::getTextureRegion(assets, fallback, ""); + } + } + } + return {nullptr, UVRegion()}; +} diff --git a/src/assets/assets_util.hpp b/src/assets/assets_util.hpp new file mode 100644 index 00000000..69972e7b --- /dev/null +++ b/src/assets/assets_util.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include + +#include "maths/UVRegion.hpp" + +class Assets; +class Texture; + +namespace util { + struct TextureRegion { + const Texture* texture; + UVRegion region; + }; + + TextureRegion getTextureRegion( + const Assets& assets, + const std::string& name, + const std::string& fallback + ); +} diff --git a/src/graphics/core/Batch2D.cpp b/src/graphics/core/Batch2D.cpp index d9cc0ec2..13b48586 100644 --- a/src/graphics/core/Batch2D.cpp +++ b/src/graphics/core/Batch2D.cpp @@ -75,7 +75,7 @@ void Batch2D::vertex( buffer[index++] = a; } -void Batch2D::texture(Texture* new_texture){ +void Batch2D::texture(const Texture* new_texture){ if (currentTexture == new_texture) { return; } diff --git a/src/graphics/core/Batch2D.hpp b/src/graphics/core/Batch2D.hpp index 4e38ffe5..2877f5bd 100644 --- a/src/graphics/core/Batch2D.hpp +++ b/src/graphics/core/Batch2D.hpp @@ -17,7 +17,7 @@ class Batch2D : public Flushable { std::unique_ptr blank; size_t index; glm::vec4 color; - Texture* currentTexture; + const Texture* currentTexture; DrawPrimitive primitive = DrawPrimitive::triangle; UVRegion region {0.0f, 0.0f, 1.0f, 1.0f}; @@ -40,7 +40,7 @@ public: ~Batch2D(); void begin(); - void texture(Texture* texture); + void texture(const Texture* texture); void untexture(); void setRegion(UVRegion region); void sprite(float x, float y, float w, float h, const UVRegion& region, glm::vec4 tint); diff --git a/src/graphics/core/Cubemap.cpp b/src/graphics/core/Cubemap.cpp index 5da69e3e..3267b25e 100644 --- a/src/graphics/core/Cubemap.cpp +++ b/src/graphics/core/Cubemap.cpp @@ -30,10 +30,10 @@ Cubemap::Cubemap(uint width, uint height, ImageFormat imageFormat) } } -void Cubemap::bind(){ +void Cubemap::bind() const { glBindTexture(GL_TEXTURE_CUBE_MAP, id); } -void Cubemap::unbind() { +void Cubemap::unbind() const { glBindTexture(GL_TEXTURE_CUBE_MAP, 0); } diff --git a/src/graphics/core/Cubemap.hpp b/src/graphics/core/Cubemap.hpp index 72d29a21..3a80bda8 100644 --- a/src/graphics/core/Cubemap.hpp +++ b/src/graphics/core/Cubemap.hpp @@ -7,6 +7,6 @@ class Cubemap : public GLTexture { public: Cubemap(uint width, uint height, ImageFormat format); - virtual void bind() override; - virtual void unbind() override; + virtual void bind() const override; + virtual void unbind() const override; }; diff --git a/src/graphics/core/GLTexture.cpp b/src/graphics/core/GLTexture.cpp index 9572f415..4781536e 100644 --- a/src/graphics/core/GLTexture.cpp +++ b/src/graphics/core/GLTexture.cpp @@ -33,11 +33,11 @@ GLTexture::~GLTexture() { glDeleteTextures(1, &id); } -void GLTexture::bind(){ +void GLTexture::bind() const { glBindTexture(GL_TEXTURE_2D, id); } -void GLTexture::unbind() { +void GLTexture::unbind() const { glBindTexture(GL_TEXTURE_2D, 0); } diff --git a/src/graphics/core/GLTexture.hpp b/src/graphics/core/GLTexture.hpp index 7f9e8f22..b5f82384 100644 --- a/src/graphics/core/GLTexture.hpp +++ b/src/graphics/core/GLTexture.hpp @@ -10,8 +10,8 @@ public: GLTexture(const ubyte* data, uint width, uint height, ImageFormat format); virtual ~GLTexture(); - virtual void bind() override; - virtual void unbind() override; + virtual void bind() const override; + virtual void unbind() const override; virtual void reload(const ubyte* data); void setNearestFilter(); diff --git a/src/graphics/core/Texture.hpp b/src/graphics/core/Texture.hpp index 4b3f1651..ba5b066e 100644 --- a/src/graphics/core/Texture.hpp +++ b/src/graphics/core/Texture.hpp @@ -17,8 +17,8 @@ public: virtual ~Texture() {} - virtual void bind() = 0; - virtual void unbind() = 0; + virtual void bind() const = 0; + virtual void unbind() const = 0; virtual void reload(const ImageData& image) = 0; diff --git a/src/graphics/render/ModelBatch.cpp b/src/graphics/render/ModelBatch.cpp index 16f9393f..1d8ce2f1 100644 --- a/src/graphics/render/ModelBatch.cpp +++ b/src/graphics/render/ModelBatch.cpp @@ -1,5 +1,6 @@ #include "ModelBatch.hpp" +#include "assets/assets_util.hpp" #include "graphics/core/Mesh.hpp" #include "graphics/core/Model.hpp" #include "graphics/core/Atlas.hpp" @@ -144,7 +145,7 @@ void ModelBatch::setLightsOffset(const glm::vec3& offset) { void ModelBatch::setTexture(const std::string& name, const texture_names_map* varTextures) { - if (name.at(0) == '$') { + if (varTextures && name.at(0) == '$') { const auto& found = varTextures->find(name); if (found == varTextures->end()) { return setTexture(nullptr); @@ -152,25 +153,13 @@ void ModelBatch::setTexture(const std::string& name, return setTexture(found->second, varTextures); } } - size_t sep = name.find(':'); - if (sep == std::string::npos) { - setTexture(assets->get(name)); - } else { - auto atlas = assets->get(name.substr(0, sep)); - if (atlas == nullptr) { - setTexture(nullptr); - } else { - setTexture(atlas->getTexture()); - if (auto reg = atlas->getIf(name.substr(sep+1))) { - region = *reg; - } else { - setTexture("blocks:notfound", varTextures); - } - } - } + + auto textureRegion = util::getTextureRegion(*assets, name, "blocks:notfound"); + setTexture(textureRegion.texture); + region = textureRegion.region; } -void ModelBatch::setTexture(Texture* texture) { +void ModelBatch::setTexture(const Texture* texture) { if (texture == nullptr) { texture = blank.get(); } diff --git a/src/graphics/render/ModelBatch.hpp b/src/graphics/render/ModelBatch.hpp index eeff0e7e..523eb300 100644 --- a/src/graphics/render/ModelBatch.hpp +++ b/src/graphics/render/ModelBatch.hpp @@ -31,7 +31,7 @@ class ModelBatch { Assets* assets; Chunks* chunks; - Texture* texture = nullptr; + const Texture* texture = nullptr; UVRegion region {0.0f, 0.0f, 1.0f, 1.0f}; const EngineSettings* settings; glm::vec3 lightsOffset {}; @@ -72,7 +72,7 @@ class ModelBatch { bool backlight); void setTexture(const std::string& name, const texture_names_map* varTextures); - void setTexture(Texture* texture); + void setTexture(const Texture* texture); void flush(); struct DrawEntry { diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index 2929eaf0..266a8c2c 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -376,13 +376,12 @@ void WorldRenderer::renderHands(const Camera& camera, const Assets& assets) { matrix = matrix * glm::translate(glm::mat4(1.0f), offset); // render - texture_names_map map = {}; modelBatch->setLightsOffset(camera.position); modelBatch->draw( matrix, glm::vec3(1.0f), assets.get(def.modelName), - &map + nullptr ); Window::clearDepth(); setupWorldShader(entityShader, hudcam, engine->getSettings(), 0.0f); diff --git a/src/graphics/ui/elements/InventoryView.cpp b/src/graphics/ui/elements/InventoryView.cpp index 991a9d5b..c9aefc96 100644 --- a/src/graphics/ui/elements/InventoryView.cpp +++ b/src/graphics/ui/elements/InventoryView.cpp @@ -1,6 +1,7 @@ #include "InventoryView.hpp" #include "assets/Assets.hpp" +#include "assets/assets_util.hpp" #include "content/Content.hpp" #include "frontend/LevelFrontend.hpp" #include "frontend/locale.hpp" @@ -174,22 +175,13 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) { break; } case ItemIconType::SPRITE: { - size_t index = item.icon.find(':'); - std::string name = item.icon.substr(index+1); - UVRegion region(0.0f, 0.0, 1.0f, 1.0f); - if (index == std::string::npos) { - batch->texture(assets->get(name)); - } else { - std::string atlasname = item.icon.substr(0, index); - auto atlas = assets->get(atlasname); - if (atlas && atlas->has(name)) { - region = atlas->get(name); - batch->texture(atlas->getTexture()); - } - } + auto textureRegion = + util::getTextureRegion(*assets, item.icon, "blocks:notfound"); + + batch->texture(textureRegion.texture); batch->rect( pos.x, pos.y, slotSize, slotSize, - 0, 0, 0, region, false, true, tint); + 0, 0, 0, textureRegion.region, false, true, tint); break; } }