From f9c5bd5e4d2087d32a999cc457a1085db9d40f81 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 17 Mar 2025 02:45:21 +0300 Subject: [PATCH] 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}, };