From 45d31579d22162915077cc5eea0f4786a028d8ba Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Wed, 19 Feb 2025 10:40:14 +0100 Subject: [PATCH] - [NEW] tilemap.new(x,y) - [CHG] tilemap.new i tilemap.load alliberen la surface anterior (si hi havia) del mapa --- lua.cpp | 13 ++++++++++++- mini.cpp | 2 ++ vscode/library.lua | 7 ++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lua.cpp b/lua.cpp index 489c877..6297d3d 100644 --- a/lua.cpp +++ b/lua.cpp @@ -140,10 +140,20 @@ extern "C" { // map // =============================================== + static int cpp_map_new(lua_State *L) { + int w = luaL_checknumber(L, 1); + int h = luaL_checknumber(L, 2); + uint8_t s = newsurf(w, h); + uint8_t old = getmap(); if (old) freesurf(old); + setmap(s); + return 0; + } static int cpp_map_load(lua_State *L) { const char* str = luaL_checkstring(L, 1); - setmap(loadsurf(str)); + uint8_t s = loadsurf(str); + uint8_t old = getmap(); if (old) freesurf(old); + setmap(s); return 0; } @@ -833,6 +843,7 @@ void push_lua_funcs() { lua_setglobal(L, "surface"); lua_newtable(L); + lua_pushcfunction(L,cpp_map_new); lua_setfield(L, -2, "new"); lua_pushcfunction(L,cpp_map_load); lua_setfield(L, -2, "load"); lua_pushcfunction(L,cpp_map_save); lua_setfield(L, -2, "save"); lua_pushcfunction(L,cpp_map_set); lua_setfield(L, -2, "set"); diff --git a/mini.cpp b/mini.cpp index 4cbc3fd..6ab7655 100644 --- a/mini.cpp +++ b/mini.cpp @@ -1022,11 +1022,13 @@ void tvline(int x, int y0, int y1, float mx, float my, float mdx, float mdy) { } uint8_t mget(int celx, int cely) { + if (!map_surface) return 0; if (celx < 0 || celx > (map_surface->w-1) || cely < 0 || cely > (map_surface->h-1)) return 0; return TILES(celx, cely); } void mset(int celx, int cely, uint8_t snum) { + if (!map_surface) return; if (celx < 0 || celx > (map_surface->w-1) || cely < 0 || cely > (map_surface->h-1)) return; TILES(celx, cely) = snum; } diff --git a/vscode/library.lua b/vscode/library.lua index 2d735b1..c6aa405 100644 --- a/vscode/library.lua +++ b/vscode/library.lua @@ -69,6 +69,11 @@ function surface.getPixel(x, y) end ---@class tilemap tilemap = {} +---@param w number +---@param h number +---Create new map specifying width and height +function tilemap.new(w, h) end + ---@param filename string ---@return number surface ---Load a tilemap from a file and set it as current tilemap @@ -403,7 +408,7 @@ function window.setResolution(w, h) end config = {} ---@param key string ----@param value string +---@param value any ---Sets the value of a key in the configuration file function config.setKey(key, value) end