From 4c0eabcc5c478c4d2073015e929468c27f4050b0 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Fri, 29 May 2026 20:37:15 +0200 Subject: [PATCH] - [NEW] Funcions per a apilar i desapilar surfaces target i source: surf.source.push(surface) surf.source.pop() surf.target.push(surface) surf.target.pop() --- source/mini/lua/lua.wrappers.cpp | 63 ++++++++++++++++++++++++++++---- source/mini/surf/surf.cpp | 20 ++++++++++ source/mini/surf/surf.h | 7 ++++ vscode/library.lua | 18 +++++++++ 4 files changed, 100 insertions(+), 8 deletions(-) diff --git a/source/mini/lua/lua.wrappers.cpp b/source/mini/lua/lua.wrappers.cpp index c0737de..a30d9f2 100644 --- a/source/mini/lua/lua.wrappers.cpp +++ b/source/mini/lua/lua.wrappers.cpp @@ -154,12 +154,12 @@ namespace mini static int target(lua_State *L) { const int numargs = lua_gettop(L); switch (numargs) { - case 0: { + case 1: { lua_pushinteger(L, mini::surf::target::get()); return 1; } - case 1: { - uint8_t surface = luaL_checkinteger(L, 1); + case 2: { + uint8_t surface = luaL_checkinteger(L, 2); mini::surf::target::set(surface); return 0; } @@ -168,15 +168,31 @@ namespace mini }; } + static int target_push(lua_State *L) { + const int numargs = lua_gettop(L); + if (numargs==1) { + uint8_t surface = luaL_checkinteger(L, 1); + mini::surf::target::push(surface); + return 0; + } else { + return luaL_error(L, "Function 'surface.target.push' Unexpected number of parameters."); + }; + } + + static int target_pop(lua_State *L) { + mini::surf::target::pop(); + return 0; + } + static int source(lua_State *L) { const int numargs = lua_gettop(L); switch (numargs) { - case 0: { + case 1: { lua_pushinteger(L, mini::surf::source::get()); return 1; } - case 1: { - uint8_t surface = luaL_checkinteger(L, 1); + case 2: { + uint8_t surface = luaL_checkinteger(L, 2); mini::surf::source::set(surface); return 0; } @@ -185,6 +201,22 @@ namespace mini }; } + static int source_push(lua_State *L) { + const int numargs = lua_gettop(L); + if (numargs==1) { + uint8_t surface = luaL_checkinteger(L, 1); + mini::surf::source::push(surface); + return 0; + } else { + return luaL_error(L, "Function 'surface.source.push' Unexpected number of parameters."); + }; + } + + static int source_pop(lua_State *L) { + mini::surf::source::pop(); + return 0; + } + static int cls(lua_State *L) { uint8_t color = luaL_optinteger(L, 1, 0); mini::surf::cls(color); @@ -1046,8 +1078,23 @@ namespace mini lua_pushcfunction(L,wrappers::surf::save); lua_setfield(L, -2, "save"); lua_pushcfunction(L,wrappers::surf::free); lua_setfield(L, -2, "free"); lua_pushcfunction(L,wrappers::surf::size); lua_setfield(L, -2, "size"); - lua_pushcfunction(L,wrappers::surf::target); lua_setfield(L, -2, "target"); - lua_pushcfunction(L,wrappers::surf::source); lua_setfield(L, -2, "source"); + + lua_newtable(L); + lua_pushcfunction(L, wrappers::surf::target_push);lua_setfield(L, -2, "push"); + lua_pushcfunction(L, wrappers::surf::target_pop); lua_setfield(L, -2, "pop"); + lua_newtable(L); + lua_pushcfunction(L, wrappers::surf::target); lua_setfield(L, -2, "__call"); + lua_setmetatable(L, -2); + lua_setfield(L, -2, "target"); + + lua_newtable(L); + lua_pushcfunction(L, wrappers::surf::source_push);lua_setfield(L, -2, "push"); + lua_pushcfunction(L, wrappers::surf::source_pop); lua_setfield(L, -2, "pop"); + lua_newtable(L); + lua_pushcfunction(L, wrappers::surf::source); lua_setfield(L, -2, "__call"); + lua_setmetatable(L, -2); + lua_setfield(L, -2, "source"); + lua_pushcfunction(L,wrappers::surf::cls); lua_setfield(L, -2, "cls"); lua_pushcfunction(L,wrappers::surf::clip); lua_setfield(L, -2, "clip"); diff --git a/source/mini/surf/surf.cpp b/source/mini/surf/surf.cpp index 87fe4b7..c489e1e 100644 --- a/source/mini/surf/surf.cpp +++ b/source/mini/surf/surf.cpp @@ -176,6 +176,16 @@ namespace mini uint8_t get() { return state.dest_surface; } + + void push(uint8_t surface) { + state.dest_stack.push(state.dest_surface); + state.dest_surface = surface; + } + + void pop() { + state.dest_surface = state.dest_stack.top(); + state.dest_stack.pop(); + } } namespace source { @@ -186,6 +196,16 @@ namespace mini uint8_t get() { return state.source_surface; } + + void push(uint8_t surface) { + state.source_stack.push(state.source_surface); + state.source_surface = surface; + } + + void pop() { + state.source_surface = state.source_stack.top(); + state.source_stack.pop(); + } } namespace clip { diff --git a/source/mini/surf/surf.h b/source/mini/surf/surf.h index 9350b2f..412123f 100644 --- a/source/mini/surf/surf.h +++ b/source/mini/surf/surf.h @@ -2,6 +2,7 @@ #include #include +#include namespace mini { @@ -27,6 +28,8 @@ namespace mini std::vector surfaces; int dest_surface = -1; int source_surface = -1; + std::stack dest_stack; + std::stack source_stack; }; extern state_t state; @@ -46,11 +49,15 @@ namespace mini namespace target { void set(uint8_t surface); uint8_t get(); + void push(uint8_t surface); + void pop(); } namespace source { void set(uint8_t surface); uint8_t get(); + void push(uint8_t surface); + void pop(); } namespace clip { diff --git a/vscode/library.lua b/vscode/library.lua index 76e7406..e1efd9a 100644 --- a/vscode/library.lua +++ b/vscode/library.lua @@ -38,6 +38,8 @@ function surf.free(surface) end ---Retrieve width and height of surface function surf.size(surface) end +surf.target = {} + ---@return number surface ---Get current target surface function surf.target() end @@ -46,6 +48,15 @@ function surf.target() end ---Set surface as target function surf.target(surface) end +---@param surface number +---push current target to the stack and set surface as target +function surf.target.push(surface) end + +---pop surface on the stack as current target +function surf.target.pop() end + +surf.source = {} + ---@return number surface ---Get current source surface function surf.source() end @@ -54,6 +65,13 @@ function surf.source() end ---Set surface as source function surf.source(surface) end +---@param surface number +---push current source to the stack and set surface as source +function surf.source.push(surface) end + +---pop surface on the stack and set it as current source +function surf.source.pop() end + ---Erase the current target surface with color 0. function surf.cls() end