- Esta tot fet una marranà, en mig de la conversió

This commit is contained in:
2025-02-16 21:48:07 +01:00
parent 40e98737ce
commit 29a90f4b46
7 changed files with 402 additions and 344 deletions

113
lua.cpp
View File

@@ -24,13 +24,42 @@ extern "C" {
return 1;
}
static int cpp_surf_save(lua_State *L) {
uint8_t surface = luaL_checkinteger(L, 1);
const char* str = luaL_checkstring(L, 2);
int r,g,b;
uint8_t pal[256*3];
uint8_t *p=pal;
if (lua_istable(L, -1)) {
const int len = SDL_min(256, lua_rawlen(L, -1));
for (int i=1;i<=len;++i) {
lua_rawgeti(L, -1, i);
lua_getfield(L, -1, "r");
*(p++) = luaL_checknumber(L, -1);
lua_pop(L, 1);
lua_getfield(L, -1, "g");
*(p++) = luaL_checknumber(L, -1);
lua_pop(L, 1);
lua_getfield(L, -1, "b");
*(p++) = luaL_checknumber(L, -1);
lua_pop(L, 1);
lua_pop(L, 1);
//pal[i-1] = (r<<16)+(g<<8)+b;
}
}
savesurf(surface, str, pal);
return 0;
}
static int cpp_surf_free(lua_State *L) {
uint8_t surface = luaL_checkinteger(L, 1);
freesurf(surface);
return 0;
}
static int cpp_surf_size(lua_State *L) {
static int cpp_surf_getSize(lua_State *L) {
uint8_t surface = luaL_checkinteger(L, 1);
lua_pushinteger(L, surfw(surface));
lua_pushinteger(L, surfh(surface));
@@ -43,9 +72,10 @@ extern "C" {
static int cpp_pal_load(lua_State *L) {
const char* str = luaL_checkstring(L, 1);
uint32_t *pal = loadpal(str);
uint16_t size;
uint32_t *pal = loadpal(str, &size);
lua_createtable(L, 2, 0);
for (int i=0;i<=256;++i) {
for (int i=0;i<size;++i) {
uint32_t color = pal[i];
lua_createtable(L, 0, 3);
@@ -69,8 +99,8 @@ extern "C" {
if (lua_istable(L, -1)) {
uint32_t pal[256];
const int len = SDL_min(256, lua_rawlen(L, -1));
for (int i=1;i<=len;++i) {
lua_rawgeti(L, -1, i);
for (int i=0;i<len;++i) {
lua_rawgeti(L, -1, i+1);
lua_getfield(L, -1, "r");
r = luaL_checknumber(L, -1);
lua_pop(L, 1);
@@ -81,31 +111,31 @@ extern "C" {
b = luaL_checknumber(L, -1);
lua_pop(L, 1);
lua_pop(L, 1);
pal[i-1] = (r<<16)+(g<<8)+b;
pal[i] = (r<<16)+(g<<8)+b;
}
setpal(pal);
}
return 0;
}
static int cpp_pal_color(lua_State *L) {
if (lua_gettop(L) >= 1) {
uint8_t index = luaL_checkinteger(L, 1);
if (lua_gettop(L) > 1) {
uint8_t r = luaL_checkinteger(L, 2);
uint8_t g = luaL_optinteger(L, 3, 0);
uint8_t b = luaL_optinteger(L, 4, 0);
uint32_t color = (r<<16) + (g<<8) + b;
setcolor(index, color);
return 0;
} else {
uint32_t color = getcolor(index);
lua_pushinteger(L, (color>>16)&0xff);
lua_pushinteger(L, (color>>8)&0xff);
lua_pushinteger(L, color&0xff);
return 3;
}
}
static int cpp_pal_getColor(lua_State *L) {
if (lua_gettop(L) != 1) return luaL_error(L, "Function 'mini.palette.getColor' Unexpected number of parameters.");
uint8_t index = luaL_checkinteger(L, 1);
uint32_t color = getcolor(index);
lua_pushinteger(L, (color>>16)&0xff);
lua_pushinteger(L, (color>>8)&0xff);
lua_pushinteger(L, color&0xff);
return 3;
}
static int cpp_pal_setColor(lua_State *L) {
if (lua_gettop(L) != 4) return luaL_error(L, "Function 'mini.palette.setColor' Unexpected number of parameters.");
uint8_t index = luaL_checkinteger(L, 1);
uint8_t r = luaL_checkinteger(L, 2);
uint8_t g = luaL_optinteger(L, 3, 0);
uint8_t b = luaL_optinteger(L, 4, 0);
uint32_t color = (r<<16) + (g<<8) + b;
setcolor(index, color);
return 0;
}
@@ -774,17 +804,35 @@ void push_lua_funcs() {
lua_newtable(L);
lua_pushcfunction(L,cpp_surf_new); lua_setfield(L, -2, "new");
lua_pushcfunction(L,cpp_surf_load); lua_setfield(L, -2, "load");
lua_pushcfunction(L,cpp_surf_save); lua_setfield(L, -2, "save");
lua_pushcfunction(L,cpp_surf_free); lua_setfield(L, -2, "free");
lua_pushcfunction(L,cpp_surf_size); lua_setfield(L, -2, "size");
lua_setglobal(L, "surf");
lua_pushcfunction(L,cpp_surf_getSize); lua_setfield(L, -2, "getSize");
// setTarget
// cls
// setPixel
// getPixel
lua_setglobal(L, "surface");
map.load
map.save
map.draw
map.getTile
map.setTile
lua_newtable(L);
lua_pushcfunction(L,cpp_pal_load); lua_setfield(L, -2, "load");
lua_pushcfunction(L,cpp_pal_set); lua_setfield(L, -2, "set");
lua_pushcfunction(L,cpp_pal_color); lua_setfield(L, -2, "color");
lua_pushcfunction(L,cpp_pal_trans); lua_setfield(L, -2, "trans");
lua_pushcfunction(L,cpp_pal_sub); lua_setfield(L, -2, "sub");
lua_setglobal(L, "pal");
lua_pushcfunction(L,cpp_pal_load); lua_setfield(L, -2, "load");
lua_pushcfunction(L,cpp_pal_set); lua_setfield(L, -2, "set");
lua_pushcfunction(L,cpp_pal_getColor); lua_setfield(L, -2, "getColor");
lua_pushcfunction(L,cpp_pal_setColor); lua_setfield(L, -2, "setColor");
lua_pushcfunction(L,cpp_pal_trans); lua_setfield(L, -2, "trans");
lua_pushcfunction(L,cpp_pal_sub); lua_setfield(L, -2, "sub");
lua_setglobal(L, "palette");
subpalette.resetAll
subpalette.set
subpalette.reset
subpalette.setRange
subpalette.resetRange
lua_newtable(L);
lua_pushcfunction(L,cpp_draw_dest); lua_setfield(L, -2, "dest");
@@ -999,6 +1047,7 @@ void push_lua_funcs() {
lua_pushinteger(L, 20); lua_setfield(L, -2, "TOUCHPAD");
lua_setglobal(L, "pad");
}
int MiniLoader(lua_State *L) {