From 4ea2887d7fa41cd753b46b05de6920b9d84d3fbc Mon Sep 17 00:00:00 2001 From: Lactozilla Date: Fri, 4 Aug 2023 00:31:51 -0300 Subject: [PATCH 1/9] Lua colorlib: extracolormap support --- src/CMakeLists.txt | 1 + src/lua_baselib.c | 22 +++ src/lua_colorlib.c | 335 +++++++++++++++++++++++++++++++++++++++++++++ src/lua_libs.h | 2 + src/lua_maplib.c | 5 + src/lua_script.c | 1 + src/m_misc.c | 14 ++ src/m_misc.h | 3 + src/p_local.h | 4 + src/p_map.c | 32 +++++ src/r_data.c | 55 ++++---- src/r_data.h | 1 + src/r_things.c | 42 +----- 13 files changed, 457 insertions(+), 60 deletions(-) create mode 100644 src/lua_colorlib.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b926b3b7a..92cc11637 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -105,6 +105,7 @@ add_executable(SRB2SDL2 MACOSX_BUNDLE WIN32 lua_blockmaplib.c lua_hudlib.c lua_hudlib_drawlist.c + lua_colorlib.c lua_inputlib.c ) diff --git a/src/lua_baselib.c b/src/lua_baselib.c index 915669bb2..ecebae4ac 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -213,6 +213,7 @@ static const struct { {META_HUDINFO, "hudinfo_t"}, {META_PATCH, "patch_t"}, {META_COLORMAP, "colormap"}, + {META_EXTRACOLORMAP,"extracolormap_t"}, {META_CAMERA, "camera_t"}, {META_ACTION, "action"}, @@ -1928,6 +1929,26 @@ static int lib_pCeilingzAtPos(lua_State *L) return 1; } +static int lib_pGetSectorColormapAt(lua_State *L) +{ + sector_t *sector = NULL; + if (!lua_isnone(L, 1) && lua_isuserdata(L, 1)) + sector = *((sector_t **)luaL_checkudata(L, 1, META_SECTOR)); + fixed_t x = luaL_checkfixed(L, 2); + fixed_t y = luaL_checkfixed(L, 3); + fixed_t z = luaL_checkfixed(L, 4); + INLEVEL + if (!sector) + return LUA_ErrInvalid(L, "sector_t"); + extracolormap_t *exc; + if (sector) + exc = P_GetColormapFromSectorAt(sector, x, y, z); + else + exc = P_GetSectorColormapAt(x, y, z); + LUA_PushUserdata(L, exc, META_EXTRACOLORMAP); + return 1; +} + static int lib_pDoSpring(lua_State *L) { mobj_t *spring = *((mobj_t **)luaL_checkudata(L, 1, META_MOBJ)); @@ -4151,6 +4172,7 @@ static luaL_Reg lib[] = { {"P_RadiusAttack",lib_pRadiusAttack}, {"P_FloorzAtPos",lib_pFloorzAtPos}, {"P_CeilingzAtPos",lib_pCeilingzAtPos}, + {"P_GetSectorColormapAt",lib_pGetSectorColormapAt}, {"P_DoSpring",lib_pDoSpring}, {"P_TryCameraMove", lib_pTryCameraMove}, {"P_TeleportCameraMove", lib_pTeleportCameraMove}, diff --git a/src/lua_colorlib.c b/src/lua_colorlib.c new file mode 100644 index 000000000..ee7074e84 --- /dev/null +++ b/src/lua_colorlib.c @@ -0,0 +1,335 @@ +// SONIC ROBO BLAST 2 +//----------------------------------------------------------------------------- +// Copyright (C) 2021-2022 by "Lactozilla". +// Copyright (C) 2014-2023 by Sonic Team Junior. +// +// This program is free software distributed under the +// terms of the GNU General Public License, version 2. +// See the 'LICENSE' file for more details. +//----------------------------------------------------------------------------- +/// \file lua_colorlib.c +/// \brief color and colormap libraries for Lua scripting + +#include "doomdef.h" +#include "fastcmp.h" +#include "r_data.h" + +#include "lua_script.h" +#include "lua_libs.h" + +static UINT8 GetRGBAColorsFromTable(lua_State *L, UINT32 index, UINT8 *rgba, boolean useAlpha) +{ + UINT8 num = 0; + + lua_pushnil(L); + + while (lua_next(L, index)) { + lua_Integer i = 0; + const char *field = NULL; + if (lua_isnumber(L, -2)) + i = lua_tointeger(L, -2); + else + field = luaL_checkstring(L, -2); + +#define CHECKFIELD(p, c) (i == p || (field && fastcmp(field, c))) +#define RGBASET(p, c) { \ + INT32 color = luaL_checkinteger(L, -1); \ + if (color < 0 || color > 255) \ + luaL_error(L, c " channel %d out of range (0 - 255)", color); \ + rgba[p] = (UINT8)color; \ + num++; \ + } + + if (CHECKFIELD(1, "r")) { + RGBASET(0, "red color"); + } else if (CHECKFIELD(2, "g")) { + RGBASET(1, "green color"); + } else if (CHECKFIELD(3, "b")) { + RGBASET(2, "blue color"); + } else if (useAlpha && CHECKFIELD(4, "a")) { + RGBASET(3, "alpha"); + } + +#undef CHECKFIELD +#undef RGBASET + + lua_pop(L, 1); + } + + return num; +} + +#define IS_HEX_CHAR(x) ((x >= '0' && x <= '9') || (x >= 'a' && x <= 'f') || (x >= 'A' && x <= 'F')) +#define ARE_HEX_CHARS(str, i) IS_HEX_CHAR(str[i]) && IS_HEX_CHAR(str[i + 1]) + +static UINT32 hex2int(char x) +{ + if (x >= '0' && x <= '9') + return x - '0'; + else if (x >= 'a' && x <= 'f') + return x - 'a' + 10; + else if (x >= 'A' && x <= 'F') + return x - 'A' + 10; + + return 0; +} + +static UINT8 ParseHTMLColor(const char *str, UINT8 *rgba, size_t numc) +{ + const char *hex = str; + + if (hex[0] == '#') + hex++; + else if (!IS_HEX_CHAR(hex[0])) + return 0; + + size_t len = strlen(hex); + + if (len == 3) + { + // Shorthand like #09C + for (unsigned i = 0; i < 3; i++) + { + if (!IS_HEX_CHAR(hex[i])) + return 0; + + UINT32 hx = hex2int(hex[i]); + *rgba++ = (hx * 16) + hx; + } + + return 3; + } + else if (len == 6 || len == 8) + { + if (numc != 4) + len = 6; + + // A triplet like #0099CC + for (unsigned i = 0; i < len; i += 2) + { + if (!ARE_HEX_CHARS(hex, i)) + return false; + + *rgba++ = (hex2int(hex[i]) * 16) + hex2int(hex[i + 1]); + } + + return len; + } + + return 0; +} + +///////////////////////// +// extracolormap userdata +///////////////////////// + +enum extracolormap_e { + extracolormap_r = 0, + extracolormap_g, + extracolormap_b, + extracolormap_a, + extracolormap_rgba, + extracolormap_fade_r, + extracolormap_fade_g, + extracolormap_fade_b, + extracolormap_fade_a, + extracolormap_fade_rgba, + extracolormap_fade_start, + extracolormap_fade_end, + extracolormap_colormap +}; + +static const char *const extracolormap_opt[] = { + "r", + "g", + "b", + "a", + "rgba", + "fade_r", + "fade_g", + "fade_b", + "fade_a", + "fade_rgba", + "fade_start", + "fade_end", + "colormap", + NULL}; + +#define ALPHA_SCALE_FACTOR 102 // (255 / 25) * 10 +#define SCALE_ALPHA_UP(alpha) (((alpha) * ALPHA_SCALE_FACTOR) / 10) +#define SCALE_ALPHA_DOWN(alpha) (((alpha) * 10) / ALPHA_SCALE_FACTOR) + +static int extracolormap_get(lua_State *L) +{ + extracolormap_t *exc = *((extracolormap_t **)luaL_checkudata(L, 1, META_EXTRACOLORMAP)); + enum extracolormap_e field = luaL_checkoption(L, 2, NULL, extracolormap_opt); + + switch (field) + { + case extracolormap_r: + lua_pushinteger(L, R_GetRgbaR(exc->rgba)); + break; + case extracolormap_g: + lua_pushinteger(L, R_GetRgbaG(exc->rgba)); + break; + case extracolormap_b: + lua_pushinteger(L, R_GetRgbaB(exc->rgba)); + break; + case extracolormap_a: + lua_pushinteger(L, SCALE_ALPHA_UP(R_GetRgbaA(exc->rgba))); + break; + case extracolormap_rgba: + lua_pushinteger(L, R_GetRgbaR(exc->rgba)); + lua_pushinteger(L, R_GetRgbaG(exc->rgba)); + lua_pushinteger(L, R_GetRgbaB(exc->rgba)); + lua_pushinteger(L, SCALE_ALPHA_UP(R_GetRgbaA(exc->rgba))); + return 4; + case extracolormap_fade_r: + lua_pushinteger(L, R_GetRgbaR(exc->fadergba)); + break; + case extracolormap_fade_g: + lua_pushinteger(L, R_GetRgbaG(exc->fadergba)); + break; + case extracolormap_fade_b: + lua_pushinteger(L, R_GetRgbaB(exc->fadergba)); + break; + case extracolormap_fade_a: + lua_pushinteger(L, SCALE_ALPHA_UP(R_GetRgbaA(exc->fadergba))); + break; + case extracolormap_fade_rgba: + lua_pushinteger(L, R_GetRgbaR(exc->fadergba)); + lua_pushinteger(L, R_GetRgbaG(exc->fadergba)); + lua_pushinteger(L, R_GetRgbaB(exc->fadergba)); + lua_pushinteger(L, SCALE_ALPHA_UP(R_GetRgbaA(exc->fadergba))); + return 4; + case extracolormap_fade_start: + lua_pushinteger(L, exc->fadestart); + break; + case extracolormap_fade_end: + lua_pushinteger(L, exc->fadeend); + break; + case extracolormap_colormap: + LUA_PushUserdata(L, exc->colormap, META_COLORMAP); + break; + } + return 1; +} + +static void GetExtraColormapRGBA(lua_State *L, UINT8 *rgba) +{ + rgba[3] = SCALE_ALPHA_UP(rgba[3]); + + if (lua_type(L, 3) == LUA_TSTRING) + { + const char *str = lua_tostring(L, 3); + UINT8 parsed = ParseHTMLColor(str, rgba, 4); + if (!parsed) + luaL_error(L, "Malformed HTML color '%s'", str); + } + else + GetRGBAColorsFromTable(L, 3, rgba, true); + + rgba[3] = SCALE_ALPHA_DOWN(rgba[3]); +} + +static int extracolormap_set(lua_State *L) +{ + extracolormap_t *exc = *((extracolormap_t **)luaL_checkudata(L, 1, META_EXTRACOLORMAP)); + enum extracolormap_e field = luaL_checkoption(L, 2, NULL, extracolormap_opt); + + UINT8 r = R_GetRgbaR(exc->rgba); + UINT8 g = R_GetRgbaG(exc->rgba); + UINT8 b = R_GetRgbaB(exc->rgba); + UINT8 a = R_GetRgbaA(exc->rgba); + + UINT8 fr = R_GetRgbaR(exc->fadergba); + UINT8 fg = R_GetRgbaG(exc->fadergba); + UINT8 fb = R_GetRgbaB(exc->fadergba); + UINT8 fa = R_GetRgbaA(exc->fadergba); + + UINT8 rgba[4]; + + INT32 old_rgba = exc->rgba, old_fade_rgba = exc->fadergba; // It's not unsigned? + UINT8 old_fade_start = exc->fadestart, old_fade_end = exc->fadeend; + +#define val luaL_checkinteger(L, 3) + + switch(field) + { + case extracolormap_r: + exc->rgba = R_PutRgbaRGBA(val, g, b, a); + break; + case extracolormap_g: + exc->rgba = R_PutRgbaRGBA(r, val, b, a); + break; + case extracolormap_b: + exc->rgba = R_PutRgbaRGBA(r, g, val, a); + break; + case extracolormap_a: + exc->rgba = R_PutRgbaRGBA(r, g, b, SCALE_ALPHA_DOWN(val)); + break; + case extracolormap_rgba: + rgba[0] = r; + rgba[1] = g; + rgba[2] = b; + rgba[3] = a; + GetExtraColormapRGBA(L, rgba); + exc->rgba = R_PutRgbaRGBA(rgba[0], rgba[1], rgba[2], rgba[3]); + break; + case extracolormap_fade_r: + exc->fadergba = R_PutRgbaRGBA(val, fg, fb, fa); + break; + case extracolormap_fade_g: + exc->fadergba = R_PutRgbaRGBA(fr, val, fb, fa); + break; + case extracolormap_fade_b: + exc->fadergba = R_PutRgbaRGBA(fr, fg, val, fa); + break; + case extracolormap_fade_a: + exc->fadergba = R_PutRgbaRGBA(fr, fg, fb, SCALE_ALPHA_DOWN(val)); + break; + case extracolormap_fade_rgba: + rgba[0] = fr; + rgba[1] = fg; + rgba[2] = fb; + rgba[3] = fa; + GetExtraColormapRGBA(L, rgba); + exc->fadergba = R_PutRgbaRGBA(rgba[0], rgba[1], rgba[2], rgba[3]); + break; + case extracolormap_fade_start: + if (val > 31) + return luaL_error(L, "fade start %d out of range (0 - 31)", val); + exc->fadestart = val; + break; + case extracolormap_fade_end: + if (val > 31) + return luaL_error(L, "fade end %d out of range (0 - 31)", val); + exc->fadeend = val; + break; + case extracolormap_colormap: + return luaL_error(L, LUA_QL("extracolormap_t") " field " LUA_QS " should not be set directly.", extracolormap_opt[field]); + } + +#undef val + + if (exc->rgba != old_rgba + || exc->fadergba != old_fade_rgba + || exc->fadestart != old_fade_start + || exc->fadeend != old_fade_end) + R_GenerateLightTable(exc, true); + + return 0; +} + +int LUA_ColorLib(lua_State *L) +{ + luaL_newmetatable(L, META_EXTRACOLORMAP); + lua_pushcfunction(L, extracolormap_get); + lua_setfield(L, -2, "__index"); + + lua_pushcfunction(L, extracolormap_set); + lua_setfield(L, -2, "__newindex"); + lua_pop(L, 1); + + return 0; +} diff --git a/src/lua_libs.h b/src/lua_libs.h index 7f8d21f38..b520bb983 100644 --- a/src/lua_libs.h +++ b/src/lua_libs.h @@ -84,6 +84,7 @@ extern boolean mousegrabbedbylua; #define META_HUDINFO "HUDINFO_T*" #define META_PATCH "PATCH_T*" #define META_COLORMAP "COLORMAP" +#define META_EXTRACOLORMAP "EXTRACOLORMAP_T" #define META_CAMERA "CAMERA_T*" #define META_ACTION "ACTIONF_T*" @@ -111,4 +112,5 @@ int LUA_TagLib(lua_State *L); int LUA_PolyObjLib(lua_State *L); int LUA_BlockmapLib(lua_State *L); int LUA_HudLib(lua_State *L); +int LUA_ColorLib(lua_State *L); int LUA_InputLib(lua_State *L); diff --git a/src/lua_maplib.c b/src/lua_maplib.c index 3d95cdb75..440d1fce4 100644 --- a/src/lua_maplib.c +++ b/src/lua_maplib.c @@ -57,6 +57,7 @@ enum sector_e { sector_ffloors, sector_fslope, sector_cslope, + sector_extracolormap, sector_flags, sector_specialflags, sector_damagetype, @@ -95,6 +96,7 @@ static const char *const sector_opt[] = { "ffloors", "f_slope", "c_slope", + "extracolormap", "flags", "specialflags", "damagetype", @@ -751,6 +753,9 @@ static int sector_get(lua_State *L) case sector_cslope: // c_slope LUA_PushUserdata(L, sector->c_slope, META_SLOPE); return 1; + case sector_extracolormap: // extra_colormap + LUA_PushUserdata(L, sector->extra_colormap, META_EXTRACOLORMAP); + return 1; case sector_flags: // flags lua_pushinteger(L, sector->flags); return 1; diff --git a/src/lua_script.c b/src/lua_script.c index 6a5982006..8b081c587 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -58,6 +58,7 @@ static lua_CFunction liblist[] = { LUA_PolyObjLib, // polyobj_t LUA_BlockmapLib, // blockmap stuff LUA_HudLib, // HUD stuff + LUA_ColorLib, // general color functions LUA_InputLib, // inputs NULL }; diff --git a/src/m_misc.c b/src/m_misc.c index f547f5c41..68bc52055 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -2805,3 +2805,17 @@ boolean M_IsStringEmpty(const char *s) return true; } + +// Rounds off floating numbers and checks for 0 - 255 bounds +int M_RoundUp(double number) +{ + if (number > 255.0l) + return 255; + if (number < 0.0l) + return 0; + + if ((int)number <= (int)(number - 0.5f)) + return (int)number + 1; + + return (int)number; +} diff --git a/src/m_misc.h b/src/m_misc.h index 8cad7ba9a..753991e70 100644 --- a/src/m_misc.h +++ b/src/m_misc.h @@ -112,6 +112,9 @@ boolean M_IsStringEmpty(const char *s); // counting bits, for weapon ammo code, usually FUNCMATH UINT8 M_CountBits(UINT32 num, UINT8 size); +// Rounds off floating numbers and checks for 0 - 255 bounds +int M_RoundUp(double number); + #include "w_wad.h" extern char configfile[MAX_WADPATH]; diff --git a/src/p_local.h b/src/p_local.h index 563e257d8..7110d3494 100644 --- a/src/p_local.h +++ b/src/p_local.h @@ -438,6 +438,10 @@ boolean PIT_PushableMoved(mobj_t *thing); boolean P_DoSpring(mobj_t *spring, mobj_t *object); +INT32 P_GetSectorLightAt(sector_t *sector, fixed_t x, fixed_t y, fixed_t z); +extracolormap_t *P_GetColormapFromSectorAt(sector_t *sector, fixed_t x, fixed_t y, fixed_t z); +extracolormap_t *P_GetSectorColormapAt(fixed_t x, fixed_t y, fixed_t z); + // // P_SETUP // diff --git a/src/p_map.c b/src/p_map.c index 132a3cf85..9ffe238e9 100644 --- a/src/p_map.c +++ b/src/p_map.c @@ -5067,3 +5067,35 @@ fixed_t P_CeilingzAtPos(fixed_t x, fixed_t y, fixed_t z, fixed_t height) return ceilingz; } + +INT32 P_GetSectorLightAt(sector_t *sector, fixed_t x, fixed_t y, fixed_t z) +{ + if (!sector->numlights) + return -1; + + INT32 light = sector->numlights - 1; + + // R_GetPlaneLight won't work on sloped lights! + for (INT32 lightnum = 1; lightnum < sector->numlights; lightnum++) { + fixed_t h = P_GetLightZAt(§or->lightlist[lightnum], x, y); + if (h <= z) { + light = lightnum - 1; + break; + } + } + + return light; +} + +extracolormap_t *P_GetColormapFromSectorAt(sector_t *sector, fixed_t x, fixed_t y, fixed_t z) +{ + if (sector->numlights) + return *sector->lightlist[P_GetSectorLightAt(sector, x, y, z)].extra_colormap; + else + return sector->extra_colormap; +} + +extracolormap_t *P_GetSectorColormapAt(fixed_t x, fixed_t y, fixed_t z) +{ + return P_GetColormapFromSectorAt(R_PointInSubsector(x, y)->sector, x, y, z); +} diff --git a/src/r_data.c b/src/r_data.c index 4b7492f90..1795e7727 100644 --- a/src/r_data.c +++ b/src/r_data.c @@ -692,9 +692,26 @@ extracolormap_t *R_ColormapForName(char *name) // static double deltas[256][3], map[256][3]; -static int RoundUp(double number); +static colorlookup_t lighttable_lut; + +static UINT8 LightTableNearest(UINT8 r, UINT8 g, UINT8 b) +{ + return NearestColor(r, g, b); +} + +static UINT8 LightTableNearest_LUT(UINT8 r, UINT8 g, UINT8 b) +{ + return GetColorLUT(&lighttable_lut, r, g, b); +} lighttable_t *R_CreateLightTable(extracolormap_t *extra_colormap) +{ + extra_colormap->colormap = Z_MallocAlign((256 * 34) + 10, PU_LEVEL, NULL, 8); + R_GenerateLightTable(extra_colormap, false); + return extra_colormap->colormap; +} + +void R_GenerateLightTable(extracolormap_t *extra_colormap, boolean uselookup) { double cmaskr, cmaskg, cmaskb, cdestr, cdestg, cdestb; double maskamt = 0, othermask = 0; @@ -711,7 +728,6 @@ lighttable_t *R_CreateLightTable(extracolormap_t *extra_colormap) UINT8 fadestart = extra_colormap->fadestart, fadedist = extra_colormap->fadeend - extra_colormap->fadestart; - lighttable_t *lighttable = NULL; size_t i; ///////////////////// @@ -753,6 +769,16 @@ lighttable_t *R_CreateLightTable(extracolormap_t *extra_colormap) int p; char *colormap_p; + UINT8 (*NearestColorFunc)(UINT8, UINT8, UINT8); + + if (uselookup) + { + InitColorLUT(&lighttable_lut, pMasterPalette, false); + NearestColorFunc = LightTableNearest_LUT; + } + else + NearestColorFunc = LightTableNearest; + // Initialise the map and delta arrays // map[i] stores an RGB color (as double) for index i, // which is then converted to SRB2's palette later @@ -783,8 +809,7 @@ lighttable_t *R_CreateLightTable(extracolormap_t *extra_colormap) // Now allocate memory for the actual colormap array itself! // aligned on 8 bit for asm code - colormap_p = Z_MallocAlign((256 * 34) + 10, PU_LEVEL, NULL, 8); - lighttable = (UINT8 *)colormap_p; + colormap_p = (char *)extra_colormap->colormap; // Calculate the palette index for each palette index, for each light level // (as well as the two unused colormap lines we inherited from Doom) @@ -792,9 +817,9 @@ lighttable_t *R_CreateLightTable(extracolormap_t *extra_colormap) { for (i = 0; i < 256; i++) { - *colormap_p = NearestColor((UINT8)RoundUp(map[i][0]), - (UINT8)RoundUp(map[i][1]), - (UINT8)RoundUp(map[i][2])); + *colormap_p = NearestColorFunc((UINT8)M_RoundUp(map[i][0]), + (UINT8)M_RoundUp(map[i][1]), + (UINT8)M_RoundUp(map[i][2])); colormap_p++; if ((UINT32)p < fadestart) @@ -818,8 +843,6 @@ lighttable_t *R_CreateLightTable(extracolormap_t *extra_colormap) } } } - - return lighttable; } extracolormap_t *R_CreateColormapFromLinedef(char *p1, char *p2, char *p3) @@ -1133,20 +1156,6 @@ UINT8 NearestPaletteColor(UINT8 r, UINT8 g, UINT8 b, RGBA_t *palette) return (UINT8)bestcolor; } -// Rounds off floating numbers and checks for 0 - 255 bounds -static int RoundUp(double number) -{ - if (number > 255.0l) - return 255; - if (number < 0.0l) - return 0; - - if ((int)number <= (int)(number - 0.5f)) - return (int)number + 1; - - return (int)number; -} - #ifdef EXTRACOLORMAPLUMPS const char *R_NameForColormap(extracolormap_t *extra_colormap) { diff --git a/src/r_data.h b/src/r_data.h index ef5c967e5..364f85b6d 100644 --- a/src/r_data.h +++ b/src/r_data.h @@ -92,6 +92,7 @@ typedef enum TMCF_OVERRIDE = 1<<13, } textmapcolormapflags_t; +void R_GenerateLightTable(extracolormap_t *extra_colormap, boolean uselookup); lighttable_t *R_CreateLightTable(extracolormap_t *extra_colormap); extracolormap_t * R_CreateColormapFromLinedef(char *p1, char *p2, char *p3); extracolormap_t* R_CreateColormap(INT32 rgba, INT32 fadergba, UINT8 fadestart, UINT8 fadeend, UINT8 flags); diff --git a/src/r_things.c b/src/r_things.c index 90b80dda8..a0b49072e 100644 --- a/src/r_things.c +++ b/src/r_things.c @@ -1306,8 +1306,8 @@ static void R_ProjectDropShadow(mobj_t *thing, vissprite_t *vis, fixed_t scale, patch_t *patch; fixed_t xscale, yscale, shadowxscale, shadowyscale, shadowskew, x1, x2; INT32 heightsec, phs; - INT32 light = 0; - fixed_t scalemul; UINT8 trans; + fixed_t scalemul; + UINT8 trans; fixed_t floordiff; fixed_t groundz; pslope_t *groundslope; @@ -1425,27 +1425,7 @@ static void R_ProjectDropShadow(mobj_t *thing, vissprite_t *vis, fixed_t scale, if (thing->renderflags & RF_NOCOLORMAPS) shadow->extra_colormap = NULL; else - { - if (thing->subsector->sector->numlights) - { - INT32 lightnum; - light = thing->subsector->sector->numlights - 1; - - // R_GetPlaneLight won't work on sloped lights! - for (lightnum = 1; lightnum < thing->subsector->sector->numlights; lightnum++) { - fixed_t h = P_GetLightZAt(&thing->subsector->sector->lightlist[lightnum], interp.x, interp.y); - if (h <= shadow->gzt) { - light = lightnum - 1; - break; - } - } - } - - if (thing->subsector->sector->numlights) - shadow->extra_colormap = *thing->subsector->sector->lightlist[light].extra_colormap; - else - shadow->extra_colormap = thing->subsector->sector->extra_colormap; - } + shadow->extra_colormap = P_GetColormapFromSectorAt(thing->subsector->sector, interp.x, interp.y, shadow->gzt); shadow->transmap = R_GetTranslucencyTable(trans + 1); shadow->colormap = scalelight[0][0]; // full dark! @@ -2132,21 +2112,9 @@ static void R_ProjectSprite(mobj_t *thing) if (thing->subsector->sector->numlights) { - INT32 lightnum; - fixed_t top = (splat) ? gz : gzt; - light = thing->subsector->sector->numlights - 1; - - // R_GetPlaneLight won't work on sloped lights! - for (lightnum = 1; lightnum < thing->subsector->sector->numlights; lightnum++) { - fixed_t h = P_GetLightZAt(&thing->subsector->sector->lightlist[lightnum], interp.x, interp.y); - if (h <= top) { - light = lightnum - 1; - break; - } - } - //light = R_GetPlaneLight(thing->subsector->sector, gzt, false); - lightnum = (*thing->subsector->sector->lightlist[light].lightlevel >> LIGHTSEGSHIFT); + light = P_GetSectorLightAt(thing->subsector->sector, interp.x, interp.y, splat ? gz : gzt); + INT32 lightnum = (*thing->subsector->sector->lightlist[light].lightlevel >> LIGHTSEGSHIFT); if (lightnum < 0) spritelights = scalelight[0]; else if (lightnum >= LIGHTLEVELS) From b670f97ed1512655c7bd0463fc54b9f06106e40d Mon Sep 17 00:00:00 2001 From: Lactozilla Date: Fri, 4 Aug 2023 00:36:11 -0300 Subject: [PATCH 2/9] 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]; From ec8757d716e196d5d0f226a1fbfdef6827d287b5 Mon Sep 17 00:00:00 2001 From: Lactozilla Date: Fri, 4 Aug 2023 05:00:46 -0300 Subject: [PATCH 3/9] Extend extracolormap alpha range --- src/hardware/hw_defs.h | 3 -- src/hardware/hw_main.c | 8 ++--- src/hardware/hw_md2.c | 3 -- src/hardware/r_opengl/r_opengl.c | 2 +- src/lua_colorlib.c | 20 ++++-------- src/r_data.c | 54 ++++++++++++++++---------------- 6 files changed, 38 insertions(+), 52 deletions(-) diff --git a/src/hardware/hw_defs.h b/src/hardware/hw_defs.h index 74c4ed7d2..b02f2824a 100644 --- a/src/hardware/hw_defs.h +++ b/src/hardware/hw_defs.h @@ -276,9 +276,6 @@ struct FSurfaceInfo }; typedef struct FSurfaceInfo FSurfaceInfo; -#define GL_DEFAULTMIX 0x00000000 -#define GL_DEFAULTFOG 0xFF000000 - //Hurdler: added for backward compatibility enum hwdsetspecialstate { diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c index 36ff86abd..568161425 100644 --- a/src/hardware/hw_main.c +++ b/src/hardware/hw_main.c @@ -186,8 +186,8 @@ void HWR_Lighting(FSurfaceInfo *Surface, INT32 light_level, extracolormap_t *col RGBA_t poly_color, tint_color, fade_color; poly_color.rgba = 0xFFFFFFFF; - tint_color.rgba = (colormap != NULL) ? (UINT32)colormap->rgba : GL_DEFAULTMIX; - fade_color.rgba = (colormap != NULL) ? (UINT32)colormap->fadergba : GL_DEFAULTFOG; + tint_color.rgba = (colormap != NULL) ? (UINT32)colormap->rgba : 0x00000000; + fade_color.rgba = (colormap != NULL) ? (UINT32)colormap->fadergba : 0xFF000000; // Crappy backup coloring if you can't do shaders if (!HWR_UseShader()) @@ -201,7 +201,7 @@ void HWR_Lighting(FSurfaceInfo *Surface, INT32 light_level, extracolormap_t *col blue = (float)poly_color.s.blue; // 48 is just an arbritrary value that looked relatively okay. - tint_alpha = (float)(sqrt(tint_color.s.alpha) * 48) / 255.0f; + tint_alpha = (float)(sqrt((float)tint_color.s.alpha / 10.2) * 48) / 255.0f; // 8 is roughly the brightness of the "close" color in Software, and 16 the brightness of the "far" color. // 8 is too bright for dark levels, and 16 is too dark for bright levels. @@ -242,7 +242,7 @@ UINT8 HWR_FogBlockAlpha(INT32 light, extracolormap_t *colormap) // Let's see if RGBA_t realcolor, surfcolor; INT32 alpha; - realcolor.rgba = (colormap != NULL) ? colormap->rgba : GL_DEFAULTMIX; + realcolor.rgba = (colormap != NULL) ? colormap->rgba : 0x00000000; if (cv_glshaders.value && gl_shadersavailable) { diff --git a/src/hardware/hw_md2.c b/src/hardware/hw_md2.c index d005f0037..6d1f12664 100644 --- a/src/hardware/hw_md2.c +++ b/src/hardware/hw_md2.c @@ -1140,9 +1140,6 @@ static void HWR_GetBlendedTexture(patch_t *patch, patch_t *blendpatch, INT32 ski Z_ChangeTag(newMipmap->data, PU_HWRMODELTEXTURE_UNLOCKED); } -#define NORMALFOG 0x00000000 -#define FADEFOG 0x19000000 - static boolean HWR_AllowModel(mobj_t *mobj) { // Signpost overlay. Not needed. diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c index 71cb5ca70..6c0c90fc5 100644 --- a/src/hardware/r_opengl/r_opengl.c +++ b/src/hardware/r_opengl/r_opengl.c @@ -697,7 +697,7 @@ static GLRGBAFloat shader_defaultcolor = {1.0f, 1.0f, 1.0f, 1.0f}; #define GLSL_SOFTWARE_TINT_EQUATION \ "if (tint_color.a > 0.0) {\n" \ "float color_bright = sqrt((base_color.r * base_color.r) + (base_color.g * base_color.g) + (base_color.b * base_color.b));\n" \ - "float strength = sqrt(9.0 * tint_color.a);\n" \ + "float strength = sqrt(tint_color.a);\n" \ "final_color.r = clamp((color_bright * (tint_color.r * strength)) + (base_color.r * (1.0 - strength)), 0.0, 1.0);\n" \ "final_color.g = clamp((color_bright * (tint_color.g * strength)) + (base_color.g * (1.0 - strength)), 0.0, 1.0);\n" \ "final_color.b = clamp((color_bright * (tint_color.b * strength)) + (base_color.b * (1.0 - strength)), 0.0, 1.0);\n" \ diff --git a/src/lua_colorlib.c b/src/lua_colorlib.c index a963a6a6b..1aed69836 100644 --- a/src/lua_colorlib.c +++ b/src/lua_colorlib.c @@ -155,10 +155,6 @@ static const char *const extracolormap_opt[] = { "colormap", NULL}; -#define ALPHA_SCALE_FACTOR 102 // (255 / 25) * 10 -#define SCALE_ALPHA_UP(alpha) (((alpha) * ALPHA_SCALE_FACTOR) / 10) -#define SCALE_ALPHA_DOWN(alpha) (((alpha) * 10) / ALPHA_SCALE_FACTOR) - static int extracolormap_get(lua_State *L) { extracolormap_t *exc = *((extracolormap_t **)luaL_checkudata(L, 1, META_EXTRACOLORMAP)); @@ -176,13 +172,13 @@ static int extracolormap_get(lua_State *L) lua_pushinteger(L, R_GetRgbaB(exc->rgba)); break; case extracolormap_a: - lua_pushinteger(L, SCALE_ALPHA_UP(R_GetRgbaA(exc->rgba))); + lua_pushinteger(L, R_GetRgbaA(exc->rgba)); break; case extracolormap_rgba: lua_pushinteger(L, R_GetRgbaR(exc->rgba)); lua_pushinteger(L, R_GetRgbaG(exc->rgba)); lua_pushinteger(L, R_GetRgbaB(exc->rgba)); - lua_pushinteger(L, SCALE_ALPHA_UP(R_GetRgbaA(exc->rgba))); + lua_pushinteger(L, R_GetRgbaA(exc->rgba)); return 4; case extracolormap_fade_r: lua_pushinteger(L, R_GetRgbaR(exc->fadergba)); @@ -194,13 +190,13 @@ static int extracolormap_get(lua_State *L) lua_pushinteger(L, R_GetRgbaB(exc->fadergba)); break; case extracolormap_fade_a: - lua_pushinteger(L, SCALE_ALPHA_UP(R_GetRgbaA(exc->fadergba))); + lua_pushinteger(L, R_GetRgbaA(exc->fadergba)); break; case extracolormap_fade_rgba: lua_pushinteger(L, R_GetRgbaR(exc->fadergba)); lua_pushinteger(L, R_GetRgbaG(exc->fadergba)); lua_pushinteger(L, R_GetRgbaB(exc->fadergba)); - lua_pushinteger(L, SCALE_ALPHA_UP(R_GetRgbaA(exc->fadergba))); + lua_pushinteger(L, R_GetRgbaA(exc->fadergba)); return 4; case extracolormap_fade_start: lua_pushinteger(L, exc->fadestart); @@ -217,8 +213,6 @@ static int extracolormap_get(lua_State *L) static void GetExtraColormapRGBA(lua_State *L, UINT8 *rgba) { - rgba[3] = SCALE_ALPHA_UP(rgba[3]); - if (lua_type(L, 3) == LUA_TSTRING) { const char *str = lua_tostring(L, 3); @@ -228,8 +222,6 @@ static void GetExtraColormapRGBA(lua_State *L, UINT8 *rgba) } else GetRGBAColorsFromTable(L, 3, rgba, true); - - rgba[3] = SCALE_ALPHA_DOWN(rgba[3]); } static int extracolormap_set(lua_State *L) @@ -266,7 +258,7 @@ static int extracolormap_set(lua_State *L) exc->rgba = R_PutRgbaRGBA(r, g, val, a); break; case extracolormap_a: - exc->rgba = R_PutRgbaRGBA(r, g, b, SCALE_ALPHA_DOWN(val)); + exc->rgba = R_PutRgbaRGBA(r, g, b, val); break; case extracolormap_rgba: rgba[0] = r; @@ -286,7 +278,7 @@ static int extracolormap_set(lua_State *L) exc->fadergba = R_PutRgbaRGBA(fr, fg, val, fa); break; case extracolormap_fade_a: - exc->fadergba = R_PutRgbaRGBA(fr, fg, fb, SCALE_ALPHA_DOWN(val)); + exc->fadergba = R_PutRgbaRGBA(fr, fg, fb, val); break; case extracolormap_fade_rgba: rgba[0] = fr; diff --git a/src/r_data.c b/src/r_data.c index 1795e7727..e2b74da40 100644 --- a/src/r_data.c +++ b/src/r_data.c @@ -438,7 +438,7 @@ extracolormap_t *R_CreateDefaultColormap(boolean lighttable) exc->fadeend = 31; exc->flags = 0; exc->rgba = 0; - exc->fadergba = 0x19000000; + exc->fadergba = 0xFF000000; exc->colormap = lighttable ? R_CreateLightTable(exc) : NULL; #ifdef EXTRACOLORMAPLUMPS exc->lump = LUMPERROR; @@ -553,7 +553,7 @@ boolean R_CheckDefaultColormapByValues(boolean checkrgba, boolean checkfadergba, && !flags) ) && (!checkrgba ? true : rgba == 0) - && (!checkfadergba ? true : fadergba == 0x19000000) + && (!checkfadergba ? true : (unsigned)fadergba == 0xFF000000) #ifdef EXTRACOLORMAPLUMPS && lump == LUMPERROR && extra_colormap->lumpname[0] == 0 @@ -654,7 +654,7 @@ extracolormap_t *R_ColormapForName(char *name) if (lump == LUMPERROR) I_Error("R_ColormapForName: Cannot find colormap lump %.8s\n", name); - exc = R_GetColormapFromListByValues(0, 0x19000000, 0, 31, 0, lump); + exc = R_GetColormapFromListByValues(0, 0xFF000000, 0, 31, 0, lump); if (exc) return exc; @@ -674,7 +674,7 @@ extracolormap_t *R_ColormapForName(char *name) exc->fadeend = 31; exc->flags = 0; exc->rgba = 0; - exc->fadergba = 0x19000000; + exc->fadergba = 0xFF000000; R_AddColormapToList(exc); @@ -737,7 +737,7 @@ void R_GenerateLightTable(extracolormap_t *extra_colormap, boolean uselookup) cmaskg = cg; cmaskb = cb; - maskamt = (double)(ca/24.0l); + maskamt = (double)(ca/255.0l); othermask = 1 - maskamt; maskamt /= 0xff; @@ -753,7 +753,7 @@ void R_GenerateLightTable(extracolormap_t *extra_colormap, boolean uselookup) cdestb = cfb; // fade alpha unused in software - // maskamt = (double)(cfa/24.0l); + // maskamt = (double)(cfa/255.0l); // othermask = 1 - maskamt; // maskamt /= 0xff; @@ -851,7 +851,7 @@ extracolormap_t *R_CreateColormapFromLinedef(char *p1, char *p2, char *p3) UINT8 cr = 0, cg = 0, cb = 0, ca = 0, cfr = 0, cfg = 0, cfb = 0, cfa = 25; UINT32 fadestart = 0, fadeend = 31; UINT8 flags = 0; - INT32 rgba = 0, fadergba = 0x19000000; + INT32 rgba = 0, fadergba = 0xFF000000; #define HEX2INT(x) (UINT32)(x >= '0' && x <= '9' ? x - '0' : x >= 'a' && x <= 'f' ? x - 'a' + 10 : x >= 'A' && x <= 'F' ? x - 'A' + 10 : 0) #define ALPHA2INT(x) (x >= 'a' && x <= 'z' ? x - 'a' : x >= 'A' && x <= 'Z' ? x - 'A' : x >= '0' && x <= '9' ? 25 : 0) @@ -859,13 +859,13 @@ extracolormap_t *R_CreateColormapFromLinedef(char *p1, char *p2, char *p3) // Get base colormap value // First alpha-only, then full value if (p1[0] >= 'a' && p1[0] <= 'z' && !p1[1]) - ca = (p1[0] - 'a'); + ca = ((p1[0] - 'a') * 102) / 10; else if (p1[0] == '#' && p1[1] >= 'a' && p1[1] <= 'z' && !p1[2]) - ca = (p1[1] - 'a'); + ca = ((p1[1] - 'a') * 102) / 10; else if (p1[0] >= 'A' && p1[0] <= 'Z' && !p1[1]) - ca = (p1[0] - 'A'); + ca = ((p1[0] - 'A') * 102) / 10; else if (p1[0] == '#' && p1[1] >= 'A' && p1[1] <= 'Z' && !p1[2]) - ca = (p1[1] - 'A'); + ca = ((p1[1] - 'A') * 102) / 10; else if (p1[0] == '#') { // For each subsequent value, the value before it must exist @@ -881,20 +881,20 @@ extracolormap_t *R_CreateColormapFromLinedef(char *p1, char *p2, char *p3) cb = ((HEX2INT(p1[5]) * 16) + HEX2INT(p1[6])); if (p1[7] >= 'a' && p1[7] <= 'z') - ca = (p1[7] - 'a'); + ca = ((p1[7] - 'a') * 102) / 10; else if (p1[7] >= 'A' && p1[7] <= 'Z') - ca = (p1[7] - 'A'); + ca = ((p1[7] - 'A') * 102) / 10; else - ca = 25; + ca = 255; } else - ca = 25; + ca = 255; } else - ca = 25; + ca = 255; } else - ca = 25; + ca = 255; } #define NUMFROMCHAR(c) (c >= '0' && c <= '9' ? c - '0' : 0) @@ -924,13 +924,13 @@ extracolormap_t *R_CreateColormapFromLinedef(char *p1, char *p2, char *p3) // Get fade (dark) colormap value // First alpha-only, then full value if (p3[0] >= 'a' && p3[0] <= 'z' && !p3[1]) - cfa = (p3[0] - 'a'); + cfa = ((p3[0] - 'a') * 102) / 10; else if (p3[0] == '#' && p3[1] >= 'a' && p3[1] <= 'z' && !p3[2]) - cfa = (p3[1] - 'a'); + cfa = ((p3[1] - 'a') * 102) / 10; else if (p3[0] >= 'A' && p3[0] <= 'Z' && !p3[1]) - cfa = (p3[0] - 'A'); + cfa = ((p3[0] - 'A') * 102) / 10; else if (p3[0] == '#' && p3[1] >= 'A' && p3[1] <= 'Z' && !p3[2]) - cfa = (p3[1] - 'A'); + cfa = ((p3[1] - 'A') * 102) / 10; else if (p3[0] == '#') { // For each subsequent value, the value before it must exist @@ -946,20 +946,20 @@ extracolormap_t *R_CreateColormapFromLinedef(char *p1, char *p2, char *p3) cfb = ((HEX2INT(p3[5]) * 16) + HEX2INT(p3[6])); if (p3[7] >= 'a' && p3[7] <= 'z') - cfa = (p3[7] - 'a'); + cfa = ((p3[7] - 'a') * 102) / 10; else if (p3[7] >= 'A' && p3[7] <= 'Z') - cfa = (p3[7] - 'A'); + cfa = ((p3[7] - 'A') * 102) / 10; else - cfa = 25; + cfa = 255; } else - cfa = 25; + cfa = 255; } else - cfa = 25; + cfa = 255; } else - cfa = 25; + cfa = 255; } #undef ALPHA2INT #undef HEX2INT From a48030d02dc6d26cfc8a6a145cdbfa4a625631ce Mon Sep 17 00:00:00 2001 From: Lactozilla Date: Fri, 4 Aug 2023 05:08:22 -0300 Subject: [PATCH 4/9] Support number as accepted value for "rgba" or "fade_rgba" fields --- src/lua_colorlib.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/lua_colorlib.c b/src/lua_colorlib.c index 1aed69836..312b32571 100644 --- a/src/lua_colorlib.c +++ b/src/lua_colorlib.c @@ -220,8 +220,26 @@ static void GetExtraColormapRGBA(lua_State *L, UINT8 *rgba) if (!parsed) luaL_error(L, "Malformed HTML color '%s'", str); } - else + else if (lua_type(L, 3) == LUA_TTABLE) GetRGBAColorsFromTable(L, 3, rgba, true); + else + { + UINT32 colors = lua_tointeger(L, 3); + if (colors > 0xFFFFFF) + { + rgba[0] = (colors >> 24) & 0xFF; + rgba[1] = (colors >> 16) & 0xFF; + rgba[2] = (colors >> 8) & 0xFF; + rgba[3] = colors & 0xFF; + } + else + { + rgba[0] = (colors >> 16) & 0xFF; + rgba[1] = (colors >> 8) & 0xFF; + rgba[2] = colors & 0xFF; + rgba[3] = 0xFF; + } + } } static int extracolormap_set(lua_State *L) From 0e2b6a94234ac0fe01263b4ede5f77bff6119faa Mon Sep 17 00:00:00 2001 From: Lactozilla Date: Fri, 4 Aug 2023 05:13:56 -0300 Subject: [PATCH 5/9] Rename fields --- src/lua_colorlib.c | 83 ++++++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/src/lua_colorlib.c b/src/lua_colorlib.c index 312b32571..f4a641744 100644 --- a/src/lua_colorlib.c +++ b/src/lua_colorlib.c @@ -124,32 +124,32 @@ static UINT8 ParseHTMLColor(const char *str, UINT8 *rgba, size_t numc) ///////////////////////// enum extracolormap_e { - extracolormap_r = 0, - extracolormap_g, - extracolormap_b, - extracolormap_a, - extracolormap_rgba, - extracolormap_fade_r, - extracolormap_fade_g, - extracolormap_fade_b, - extracolormap_fade_a, - extracolormap_fade_rgba, + extracolormap_red = 0, + extracolormap_green, + extracolormap_blue, + extracolormap_alpha, + extracolormap_color, + extracolormap_fade_red, + extracolormap_fade_green, + extracolormap_fade_blue, + extracolormap_fade_alpha, + extracolormap_fade_color, extracolormap_fade_start, extracolormap_fade_end, extracolormap_colormap }; static const char *const extracolormap_opt[] = { - "r", - "g", - "b", - "a", - "rgba", - "fade_r", - "fade_g", - "fade_b", - "fade_a", - "fade_rgba", + "red", + "green", + "blue", + "alpha", + "color", + "fade_red", + "fade_green", + "fade_blue", + "fade_alpha", + "fade_color", "fade_start", "fade_end", "colormap", @@ -162,37 +162,37 @@ static int extracolormap_get(lua_State *L) switch (field) { - case extracolormap_r: + case extracolormap_red: lua_pushinteger(L, R_GetRgbaR(exc->rgba)); break; - case extracolormap_g: + case extracolormap_green: lua_pushinteger(L, R_GetRgbaG(exc->rgba)); break; - case extracolormap_b: + case extracolormap_blue: lua_pushinteger(L, R_GetRgbaB(exc->rgba)); break; - case extracolormap_a: + case extracolormap_alpha: lua_pushinteger(L, R_GetRgbaA(exc->rgba)); break; - case extracolormap_rgba: + case extracolormap_color: lua_pushinteger(L, R_GetRgbaR(exc->rgba)); lua_pushinteger(L, R_GetRgbaG(exc->rgba)); lua_pushinteger(L, R_GetRgbaB(exc->rgba)); lua_pushinteger(L, R_GetRgbaA(exc->rgba)); return 4; - case extracolormap_fade_r: + case extracolormap_fade_red: lua_pushinteger(L, R_GetRgbaR(exc->fadergba)); break; - case extracolormap_fade_g: + case extracolormap_fade_green: lua_pushinteger(L, R_GetRgbaG(exc->fadergba)); break; - case extracolormap_fade_b: + case extracolormap_fade_blue: lua_pushinteger(L, R_GetRgbaB(exc->fadergba)); break; - case extracolormap_fade_a: + case extracolormap_fade_alpha: lua_pushinteger(L, R_GetRgbaA(exc->fadergba)); break; - case extracolormap_fade_rgba: + case extracolormap_fade_color: lua_pushinteger(L, R_GetRgbaR(exc->fadergba)); lua_pushinteger(L, R_GetRgbaG(exc->fadergba)); lua_pushinteger(L, R_GetRgbaB(exc->fadergba)); @@ -205,6 +205,9 @@ static int extracolormap_get(lua_State *L) lua_pushinteger(L, exc->fadeend); break; case extracolormap_colormap: + // I'm not sure if making the colormap available makes sense. + // It's a read-only field, only used by one of the renderers, and + // the only way to manipulate it is by modifying the other fields. LUA_PushUserdata(L, exc->colormap, META_LIGHTTABLE); break; } @@ -266,19 +269,19 @@ static int extracolormap_set(lua_State *L) switch(field) { - case extracolormap_r: + case extracolormap_red: exc->rgba = R_PutRgbaRGBA(val, g, b, a); break; - case extracolormap_g: + case extracolormap_green: exc->rgba = R_PutRgbaRGBA(r, val, b, a); break; - case extracolormap_b: + case extracolormap_blue: exc->rgba = R_PutRgbaRGBA(r, g, val, a); break; - case extracolormap_a: + case extracolormap_alpha: exc->rgba = R_PutRgbaRGBA(r, g, b, val); break; - case extracolormap_rgba: + case extracolormap_color: rgba[0] = r; rgba[1] = g; rgba[2] = b; @@ -286,19 +289,19 @@ static int extracolormap_set(lua_State *L) GetExtraColormapRGBA(L, rgba); exc->rgba = R_PutRgbaRGBA(rgba[0], rgba[1], rgba[2], rgba[3]); break; - case extracolormap_fade_r: + case extracolormap_fade_red: exc->fadergba = R_PutRgbaRGBA(val, fg, fb, fa); break; - case extracolormap_fade_g: + case extracolormap_fade_green: exc->fadergba = R_PutRgbaRGBA(fr, val, fb, fa); break; - case extracolormap_fade_b: + case extracolormap_fade_blue: exc->fadergba = R_PutRgbaRGBA(fr, fg, val, fa); break; - case extracolormap_fade_a: + case extracolormap_fade_alpha: exc->fadergba = R_PutRgbaRGBA(fr, fg, fb, val); break; - case extracolormap_fade_rgba: + case extracolormap_fade_color: rgba[0] = fr; rgba[1] = fg; rgba[2] = fb; From a54d3a274a49297b22a8a3da95bd78a1914aa86c Mon Sep 17 00:00:00 2001 From: Lactozilla Date: Fri, 4 Aug 2023 14:29:26 -0300 Subject: [PATCH 6/9] Remove assignment by table --- src/lua_colorlib.c | 59 +++++----------------------------------------- 1 file changed, 6 insertions(+), 53 deletions(-) diff --git a/src/lua_colorlib.c b/src/lua_colorlib.c index f4a641744..1e6e82333 100644 --- a/src/lua_colorlib.c +++ b/src/lua_colorlib.c @@ -17,48 +17,6 @@ #include "lua_script.h" #include "lua_libs.h" -static UINT8 GetRGBAColorsFromTable(lua_State *L, UINT32 index, UINT8 *rgba, boolean useAlpha) -{ - UINT8 num = 0; - - lua_pushnil(L); - - while (lua_next(L, index)) { - lua_Integer i = 0; - const char *field = NULL; - if (lua_isnumber(L, -2)) - i = lua_tointeger(L, -2); - else - field = luaL_checkstring(L, -2); - -#define CHECKFIELD(p, c) (i == p || (field && fastcmp(field, c))) -#define RGBASET(p, c) { \ - INT32 color = luaL_checkinteger(L, -1); \ - if (color < 0 || color > 255) \ - luaL_error(L, c " channel %d out of range (0 - 255)", color); \ - rgba[p] = (UINT8)color; \ - num++; \ - } - - if (CHECKFIELD(1, "r")) { - RGBASET(0, "red color"); - } else if (CHECKFIELD(2, "g")) { - RGBASET(1, "green color"); - } else if (CHECKFIELD(3, "b")) { - RGBASET(2, "blue color"); - } else if (useAlpha && CHECKFIELD(4, "a")) { - RGBASET(3, "alpha"); - } - -#undef CHECKFIELD -#undef RGBASET - - lua_pop(L, 1); - } - - return num; -} - #define IS_HEX_CHAR(x) ((x >= '0' && x <= '9') || (x >= 'a' && x <= 'f') || (x >= 'A' && x <= 'F')) #define ARE_HEX_CHARS(str, i) IS_HEX_CHAR(str[i]) && IS_HEX_CHAR(str[i + 1]) @@ -205,29 +163,24 @@ static int extracolormap_get(lua_State *L) lua_pushinteger(L, exc->fadeend); break; case extracolormap_colormap: - // I'm not sure if making the colormap available makes sense. - // It's a read-only field, only used by one of the renderers, and - // the only way to manipulate it is by modifying the other fields. LUA_PushUserdata(L, exc->colormap, META_LIGHTTABLE); break; } return 1; } -static void GetExtraColormapRGBA(lua_State *L, UINT8 *rgba) +static void GetExtraColormapRGBA(lua_State *L, UINT8 *rgba, int arg) { - if (lua_type(L, 3) == LUA_TSTRING) + if (lua_type(L, arg) == LUA_TSTRING) { - const char *str = lua_tostring(L, 3); + const char *str = lua_tostring(L, arg); UINT8 parsed = ParseHTMLColor(str, rgba, 4); if (!parsed) luaL_error(L, "Malformed HTML color '%s'", str); } - else if (lua_type(L, 3) == LUA_TTABLE) - GetRGBAColorsFromTable(L, 3, rgba, true); else { - UINT32 colors = lua_tointeger(L, 3); + UINT32 colors = lua_tointeger(L, arg); if (colors > 0xFFFFFF) { rgba[0] = (colors >> 24) & 0xFF; @@ -286,7 +239,7 @@ static int extracolormap_set(lua_State *L) rgba[1] = g; rgba[2] = b; rgba[3] = a; - GetExtraColormapRGBA(L, rgba); + GetExtraColormapRGBA(L, rgba, 3); exc->rgba = R_PutRgbaRGBA(rgba[0], rgba[1], rgba[2], rgba[3]); break; case extracolormap_fade_red: @@ -306,7 +259,7 @@ static int extracolormap_set(lua_State *L) rgba[1] = fg; rgba[2] = fb; rgba[3] = fa; - GetExtraColormapRGBA(L, rgba); + GetExtraColormapRGBA(L, rgba, 3); exc->fadergba = R_PutRgbaRGBA(rgba[0], rgba[1], rgba[2], rgba[3]); break; case extracolormap_fade_start: From 9d650b0ddca510f61d2d9f948bffb948175f5ba0 Mon Sep 17 00:00:00 2001 From: Alam Ed Arias Date: Wed, 25 Oct 2023 07:26:34 -0400 Subject: [PATCH 7/9] Makefile: add lua_colorlib.c --- src/Sourcefile | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Sourcefile b/src/Sourcefile index 6ed1f3b4c..7beb98c9e 100644 --- a/src/Sourcefile +++ b/src/Sourcefile @@ -94,3 +94,4 @@ lua_blockmaplib.c lua_hudlib.c lua_hudlib_drawlist.c lua_inputlib.c +lua_colorlib.c From 563ce141ce0406a915cee9260033a209d53181ab Mon Sep 17 00:00:00 2001 From: Lactozilla Date: Tue, 21 Nov 2023 21:15:09 -0300 Subject: [PATCH 8/9] Fix P_GetSectorColormapAt --- src/lua_baselib.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/lua_baselib.c b/src/lua_baselib.c index 97bc9319c..3074187a3 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -2025,14 +2025,18 @@ static int lib_pCeilingzAtPos(lua_State *L) static int lib_pGetSectorColormapAt(lua_State *L) { + boolean has_sector = false; sector_t *sector = NULL; - if (!lua_isnone(L, 1) && lua_isuserdata(L, 1)) + if (!lua_isnoneornil(L, 1)) + { + has_sector = true; sector = *((sector_t **)luaL_checkudata(L, 1, META_SECTOR)); + } fixed_t x = luaL_checkfixed(L, 2); fixed_t y = luaL_checkfixed(L, 3); fixed_t z = luaL_checkfixed(L, 4); INLEVEL - if (!sector) + if (has_sector && !sector) return LUA_ErrInvalid(L, "sector_t"); extracolormap_t *exc; if (sector) From 46bd7344c5b17b72e1cbe647f3e0649be8c54abe Mon Sep 17 00:00:00 2001 From: Lactozilla Date: Thu, 23 Nov 2023 13:39:24 -0300 Subject: [PATCH 9/9] Rename 'sector.extracolormap' to just 'sector.colormap' --- src/lua_maplib.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lua_maplib.c b/src/lua_maplib.c index 2cc4ec2a2..1d85b786f 100644 --- a/src/lua_maplib.c +++ b/src/lua_maplib.c @@ -57,7 +57,7 @@ enum sector_e { sector_ffloors, sector_fslope, sector_cslope, - sector_extracolormap, + sector_colormap, sector_flags, sector_specialflags, sector_damagetype, @@ -96,7 +96,7 @@ static const char *const sector_opt[] = { "ffloors", "f_slope", "c_slope", - "extracolormap", + "colormap", "flags", "specialflags", "damagetype", @@ -753,7 +753,7 @@ static int sector_get(lua_State *L) case sector_cslope: // c_slope LUA_PushUserdata(L, sector->c_slope, META_SLOPE); return 1; - case sector_extracolormap: // extra_colormap + case sector_colormap: // extra_colormap LUA_PushUserdata(L, sector->extra_colormap, META_EXTRACOLORMAP); return 1; case sector_flags: // flags