From b670f97ed1512655c7bd0463fc54b9f06106e40d Mon Sep 17 00:00:00 2001 From: Lactozilla Date: Fri, 4 Aug 2023 00:36:11 -0300 Subject: [PATCH] Add lighttable_t support --- src/lua_baselib.c | 1 + src/lua_colorlib.c | 33 ++++++++++++++++++++++++++++++++- src/lua_libs.h | 3 ++- src/r_defs.h | 3 +++ src/r_draw.c | 2 -- 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/lua_baselib.c b/src/lua_baselib.c index ecebae4ac..cdda48e8f 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -214,6 +214,7 @@ static const struct { {META_PATCH, "patch_t"}, {META_COLORMAP, "colormap"}, {META_EXTRACOLORMAP,"extracolormap_t"}, + {META_LIGHTTABLE, "lighttable_t"}, {META_CAMERA, "camera_t"}, {META_ACTION, "action"}, diff --git a/src/lua_colorlib.c b/src/lua_colorlib.c index ee7074e84..a963a6a6b 100644 --- a/src/lua_colorlib.c +++ b/src/lua_colorlib.c @@ -209,7 +209,7 @@ static int extracolormap_get(lua_State *L) lua_pushinteger(L, exc->fadeend); break; case extracolormap_colormap: - LUA_PushUserdata(L, exc->colormap, META_COLORMAP); + LUA_PushUserdata(L, exc->colormap, META_LIGHTTABLE); break; } return 1; @@ -321,6 +321,29 @@ static int extracolormap_set(lua_State *L) return 0; } +static int lighttable_get(lua_State *L) +{ + void **userdata; + + lighttable_t *table = *((lighttable_t **)luaL_checkudata(L, 1, META_LIGHTTABLE)); + UINT32 row = luaL_checkinteger(L, 2); + if (row < 1 || row > 34) + return luaL_error(L, "lighttable row %d out of range (1 - %d)", row, 34); + + userdata = lua_newuserdata(L, sizeof(void *)); + *userdata = &table[256 * (row - 1)]; + luaL_getmetatable(L, META_COLORMAP); + lua_setmetatable(L, -2); + + return 1; +} + +static int lighttable_len(lua_State *L) +{ + lua_pushinteger(L, NUM_PALETTE_ENTRIES); + return 1; +} + int LUA_ColorLib(lua_State *L) { luaL_newmetatable(L, META_EXTRACOLORMAP); @@ -331,5 +354,13 @@ int LUA_ColorLib(lua_State *L) lua_setfield(L, -2, "__newindex"); lua_pop(L, 1); + luaL_newmetatable(L, META_LIGHTTABLE); + lua_pushcfunction(L, lighttable_get); + lua_setfield(L, -2, "__index"); + + lua_pushcfunction(L, lighttable_len); + lua_setfield(L, -2, "__len"); + lua_pop(L, 1); + return 0; } diff --git a/src/lua_libs.h b/src/lua_libs.h index b520bb983..65d5acb1b 100644 --- a/src/lua_libs.h +++ b/src/lua_libs.h @@ -84,7 +84,8 @@ extern boolean mousegrabbedbylua; #define META_HUDINFO "HUDINFO_T*" #define META_PATCH "PATCH_T*" #define META_COLORMAP "COLORMAP" -#define META_EXTRACOLORMAP "EXTRACOLORMAP_T" +#define META_EXTRACOLORMAP "EXTRACOLORMAP_T*" +#define META_LIGHTTABLE "LIGHTTABLE_T*" #define META_CAMERA "CAMERA_T*" #define META_ACTION "ACTIONF_T*" diff --git a/src/r_defs.h b/src/r_defs.h index a9b9a4a08..1edbba473 100644 --- a/src/r_defs.h +++ b/src/r_defs.h @@ -53,6 +53,9 @@ typedef struct // Could even use more than 32 levels. typedef UINT8 lighttable_t; +#define NUM_PALETTE_ENTRIES 256 +#define DEFAULT_STARTTRANSCOLOR 96 + #define CMF_FADEFULLBRIGHTSPRITES 1 #define CMF_FOG 4 diff --git a/src/r_draw.c b/src/r_draw.c index df9e1a460..671767b21 100644 --- a/src/r_draw.c +++ b/src/r_draw.c @@ -132,8 +132,6 @@ UINT32 nflatxshift, nflatyshift, nflatshiftup, nflatmask; #define RAINBOW_TT_CACHE_INDEX (MAXSKINS + 4) #define BLINK_TT_CACHE_INDEX (MAXSKINS + 5) #define DASHMODE_TT_CACHE_INDEX (MAXSKINS + 6) -#define DEFAULT_STARTTRANSCOLOR 96 -#define NUM_PALETTE_ENTRIES 256 static UINT8 **translationtablecache[MAXSKINS + 7] = {NULL}; UINT8 skincolor_modified[MAXSKINCOLORS];