add canvas:create_texture(...)

This commit is contained in:
MihailRis 2025-03-17 02:45:21 +03:00
parent e1ee1d8a31
commit f9c5bd5e4d
3 changed files with 31 additions and 0 deletions

View File

@ -72,6 +72,11 @@ public:
assets[typeid(T)][name].reset(asset.release());
}
template <class T>
void store(std::shared_ptr<T> asset, const std::string& name) {
assets[typeid(T)][name] = std::move(asset);
}
template <class T>
T* get(const std::string& name) const {
const auto& mapIter = assets.find(typeid(T));

View File

@ -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:

View File

@ -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<LuaCanvas>(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<std::string, lua_CFunction> methods {
{"at", lua::wrap<l_at>},
{"set", lua::wrap<l_set>},
@ -173,6 +192,7 @@ static std::unordered_map<std::string, lua_CFunction> methods {
{"blit", lua::wrap<l_blit>},
{"clear", lua::wrap<l_clear>},
{"update", lua::wrap<l_update>},
{"create_texture", lua::wrap<l_create_texture>},
{"_set_data", lua::wrap<l_set_data>},
};