From f9c5bd5e4d2087d32a999cc457a1085db9d40f81 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 17 Mar 2025 02:45:21 +0300 Subject: [PATCH 1/3] add canvas:create_texture(...) --- src/assets/Assets.hpp | 5 +++++ src/logic/scripting/lua/lua_custom_types.hpp | 6 ++++++ .../lua/usertypes/lua_type_canvas.cpp | 20 +++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/src/assets/Assets.hpp b/src/assets/Assets.hpp index 2642e005..c126079d 100644 --- a/src/assets/Assets.hpp +++ b/src/assets/Assets.hpp @@ -72,6 +72,11 @@ public: assets[typeid(T)][name].reset(asset.release()); } + template + void store(std::shared_ptr asset, const std::string& name) { + assets[typeid(T)][name] = std::move(asset); + } + template T* get(const std::string& name) const { const auto& mapIter = assets.find(typeid(T)); diff --git a/src/logic/scripting/lua/lua_custom_types.hpp b/src/logic/scripting/lua/lua_custom_types.hpp index a844b1f1..e126ee72 100644 --- a/src/logic/scripting/lua/lua_custom_types.hpp +++ b/src/logic/scripting/lua/lua_custom_types.hpp @@ -118,6 +118,12 @@ namespace lua { return mTexture != nullptr; } + auto shareTexture() const { + return mTexture; + } + + void createTexture(); + static int createMetatable(lua::State*); inline static std::string TYPENAME = "Canvas"; private: diff --git a/src/logic/scripting/lua/usertypes/lua_type_canvas.cpp b/src/logic/scripting/lua/usertypes/lua_type_canvas.cpp index a4292f56..18352b19 100644 --- a/src/logic/scripting/lua/usertypes/lua_type_canvas.cpp +++ b/src/logic/scripting/lua/usertypes/lua_type_canvas.cpp @@ -4,6 +4,8 @@ #include "graphics/core/Texture.hpp" #include "logic/scripting/lua/lua_custom_types.hpp" #include "logic/scripting/lua/lua_util.hpp" +#include "engine/Engine.hpp" +#include "assets/Assets.hpp" using namespace lua; @@ -13,6 +15,10 @@ LuaCanvas::LuaCanvas( : mTexture(std::move(inTexture)), mData(std::move(inData)) { } +void LuaCanvas::createTexture() { + mTexture = Texture::from(mData.get()); +} + union RGBA { struct { uint8_t r, g, b, a; @@ -166,6 +172,19 @@ static int l_update(State* L) { return 0; } +static int l_create_texture(State* L) { + if (auto canvas = touserdata(L, 1)) { + if (!canvas->hasTexture()) { + canvas->createTexture(); + } + if (canvas->hasTexture()) { + std::string name = require_string(L, 2); + scripting::engine->getAssets()->store(canvas->shareTexture(), name); + } + } + return 0; +} + static std::unordered_map methods { {"at", lua::wrap}, {"set", lua::wrap}, @@ -173,6 +192,7 @@ static std::unordered_map methods { {"blit", lua::wrap}, {"clear", lua::wrap}, {"update", lua::wrap}, + {"create_texture", lua::wrap}, {"_set_data", lua::wrap}, }; From 5b49c72d12ca883446db606bb1c6aa50ca433818 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 17 Mar 2025 03:05:10 +0300 Subject: [PATCH 2/3] update doc/*/scripting/ui.md --- doc/en/scripting/ui.md | 1 + doc/ru/scripting/ui.md | 1 + 2 files changed, 2 insertions(+) diff --git a/doc/en/scripting/ui.md b/doc/en/scripting/ui.md index c6da89c4..a88a80a6 100644 --- a/doc/en/scripting/ui.md +++ b/doc/en/scripting/ui.md @@ -184,6 +184,7 @@ Here, *color* can be specified in the following ways: | data:clear(*color*) | fills the canvas with the specified RGBA color | | data:update() | applies changes to the canvas and uploads it to the GPU | | data:set_data(data: table) | replaces pixel data (width * height * 4 numbers) | +| data:create_texture(name: str) | creates and shares texture to renderer | ## Inventory diff --git a/doc/ru/scripting/ui.md b/doc/ru/scripting/ui.md index 62697c8f..107f5139 100644 --- a/doc/ru/scripting/ui.md +++ b/doc/ru/scripting/ui.md @@ -185,6 +185,7 @@ document["worlds-panel"]:clear() | data:clear(*цвет*) | заполняет холст указанным RGBA цветом | | data:update() | применяет изменения и загружает холст в видеопамять | | data:set_data(data: table) | заменяет данные пикселей (ширина * высота * 4 чисел) | +| data:create_texture(name: str) | создаёт и делится текстурой с рендерером | ## Inventory (inventory) From 3a203bd4b009ea3b182dcf591be5f94d2a7def72 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 17 Mar 2025 03:46:40 +0300 Subject: [PATCH 3/3] disable mip-mapping for canvas texture --- src/assets/assetload_funcs.cpp | 2 +- src/graphics/core/GLTexture.cpp | 12 +++++++++--- src/graphics/core/GLTexture.hpp | 2 +- src/graphics/core/Texture.hpp | 2 +- src/graphics/render/PrecipitationRenderer.cpp | 2 +- .../scripting/lua/usertypes/lua_type_canvas.cpp | 1 + 6 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/assets/assetload_funcs.cpp b/src/assets/assetload_funcs.cpp index f8bcd84d..bad2bbb5 100644 --- a/src/assets/assetload_funcs.cpp +++ b/src/assets/assetload_funcs.cpp @@ -165,7 +165,7 @@ assetload::postfunc assetload::font( textures.emplace_back(nullptr); } else { auto texture = Texture::from(page.get()); - texture->setMipMapping(false); + texture->setMipMapping(false, true); textures.emplace_back(std::move(texture)); } } diff --git a/src/graphics/core/GLTexture.cpp b/src/graphics/core/GLTexture.cpp index a446a88c..f3d752d1 100644 --- a/src/graphics/core/GLTexture.cpp +++ b/src/graphics/core/GLTexture.cpp @@ -71,14 +71,20 @@ void GLTexture::setNearestFilter() { glBindTexture(GL_TEXTURE_2D, 0); } -void GLTexture::setMipMapping(bool flag) { +void GLTexture::setMipMapping(bool flag, bool pixelated) { bind(); if (flag) { glTexParameteri( - GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST + GL_TEXTURE_2D, + GL_TEXTURE_MIN_FILTER, + pixelated ? GL_NEAREST : GL_LINEAR_MIPMAP_NEAREST ); } else { - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri( + GL_TEXTURE_2D, + GL_TEXTURE_MIN_FILTER, + pixelated ? GL_NEAREST : GL_LINEAR + ); } glBindTexture(GL_TEXTURE_2D, 0); } diff --git a/src/graphics/core/GLTexture.hpp b/src/graphics/core/GLTexture.hpp index 85d7f85d..64fb5489 100644 --- a/src/graphics/core/GLTexture.hpp +++ b/src/graphics/core/GLTexture.hpp @@ -18,7 +18,7 @@ public: virtual void reload(const ImageData& image) override; - virtual void setMipMapping(bool flag) override; + virtual void setMipMapping(bool flag, bool pixelated) override; virtual std::unique_ptr readData() override; virtual uint getId() const override; diff --git a/src/graphics/core/Texture.hpp b/src/graphics/core/Texture.hpp index bc7b73f0..483c93da 100644 --- a/src/graphics/core/Texture.hpp +++ b/src/graphics/core/Texture.hpp @@ -34,7 +34,7 @@ public: virtual uint getId() const = 0; - virtual void setMipMapping(bool flag) = 0; + virtual void setMipMapping(bool flag, bool pixelated) = 0; static std::unique_ptr from(const ImageData* image); }; diff --git a/src/graphics/render/PrecipitationRenderer.cpp b/src/graphics/render/PrecipitationRenderer.cpp index 2b626921..1b138fc8 100644 --- a/src/graphics/render/PrecipitationRenderer.cpp +++ b/src/graphics/render/PrecipitationRenderer.cpp @@ -114,7 +114,7 @@ void PrecipitationRenderer::render( batch->begin(); auto& texture = assets.require(weather.fall.texture); - texture.setMipMapping(false); + texture.setMipMapping(false, true); batch->setTexture(&texture, {}); const struct { diff --git a/src/logic/scripting/lua/usertypes/lua_type_canvas.cpp b/src/logic/scripting/lua/usertypes/lua_type_canvas.cpp index 18352b19..ec086904 100644 --- a/src/logic/scripting/lua/usertypes/lua_type_canvas.cpp +++ b/src/logic/scripting/lua/usertypes/lua_type_canvas.cpp @@ -17,6 +17,7 @@ LuaCanvas::LuaCanvas( void LuaCanvas::createTexture() { mTexture = Texture::from(mData.get()); + mTexture->setMipMapping(false, true); } union RGBA {