-loadpal now loads a palette from disk and returns it in a table.

-setpal sets a palette from a table as the current palette.
This commit is contained in:
2022-10-05 18:51:26 +02:00
parent 8415841c5c
commit 035d82c1ec
3 changed files with 184 additions and 136 deletions

47
lua.cpp
View File

@@ -54,9 +54,51 @@ extern "C" {
static int cpp_loadpal(lua_State *L) {
const char* str = luaL_checkstring(L, 1);
loadpal(str);
uint32_t *pal = loadpal(str);
lua_createtable(L, 2, 0);
for (int i=0;i<=256;++i) {
uint32_t color = pal[i];
lua_createtable(L, 0, 3);
lua_pushinteger(L, (color>>16)&0xff);
lua_setfield(L, -2, "r");
lua_pushinteger(L, (color>>8)&0xff);
lua_setfield(L, -2, "g");
lua_pushinteger(L, color&0xff);
lua_setfield(L, -2, "b");
lua_rawseti(L, -2, i+1);
}
free(pal);
return 1;
}
static int cpp_setpal(lua_State *L) {
int r,g,b;
if (lua_istable(L, -1)) {
uint32_t pal[256];
const int len = lua_rawlen(L, -1);
for (int i=1;i<=len;++i) {
lua_rawgeti(L, -1, i);
lua_getfield(L, -1, "r");
r = luaL_checknumber(L, -1);
lua_pop(L, 1);
lua_getfield(L, -1, "g");
g = luaL_checknumber(L, -1);
lua_pop(L, 1);
lua_getfield(L, -1, "b");
b = luaL_checknumber(L, -1);
lua_pop(L, 1);
lua_pop(L, 1);
pal[i-1] = (r<<16)+(g<<8)+b;
}
setpal(pal);
}
return 0;
}
static int cpp_setcolor(lua_State *L) {
uint8_t index = luaL_checkinteger(L, 1);
uint8_t r = luaL_checkinteger(L, 2);
@@ -619,13 +661,12 @@ void push_lua_funcs() {
lua_pushcfunction(L,cpp_color); lua_setglobal(L, "color");
lua_pushcfunction(L,cpp_loadpal); lua_setglobal(L, "loadpal");
lua_pushcfunction(L,cpp_setpal); lua_setglobal(L, "setpal");
lua_pushcfunction(L,cpp_setcolor); lua_setglobal(L, "setcolor");
lua_pushcfunction(L,cpp_getcolor); lua_setglobal(L, "getcolor");
lua_pushcfunction(L,cpp_settrans); lua_setglobal(L, "settrans");
lua_pushcfunction(L,cpp_gettrans); lua_setglobal(L, "gettrans");
/*lua_pushcfunction(L,cpp_pal); lua_setglobal(L, "pal");
lua_pushcfunction(L,cpp_palt); lua_setglobal(L, "palt");*/
lua_pushcfunction(L,cpp_pset); lua_setglobal(L, "pset");
lua_pushcfunction(L,cpp_pget); lua_setglobal(L, "pget");
lua_pushcfunction(L,cpp_line); lua_setglobal(L, "line");

View File

@@ -292,7 +292,7 @@ void color(uint8_t color) {
ds::pen_color=color;
}
void loadpal(const char* filename) {
uint32_t *loadpal(const char* filename) {
FILE *f = fopen(filename, "rb");
if (f) {
fseek(f, 0, SEEK_END);
@@ -303,9 +303,15 @@ void loadpal(const char* filename) {
fclose(f);
uint32_t *pal = LoadPalette(buffer);
free(buffer);
memcpy(palette, pal, 1024);
free(pal);
return pal;
//memcpy(palette, pal, 1024);
//free(pal);
}
return NULL;
}
void setpal(uint32_t *pal) {
memcpy(palette, pal, 1024);
}
void setcolor(uint8_t index, uint32_t color) {

3
mini.h
View File

@@ -122,7 +122,8 @@ void setmap(uint8_t surface);
void cls(uint8_t color=0);
void color(uint8_t color=6);
void loadpal(const char* filename);
uint32_t *loadpal(const char* filename);
void setpal(uint32_t *pal);
void setcolor(uint8_t index, uint32_t color);
uint32_t getcolor(uint8_t index);
void settrans(uint8_t index);