From 734033250dcb9a085b07cafbe1a7bdd84b63aa14 Mon Sep 17 00:00:00 2001 From: SwitchKaze Date: Tue, 14 Jul 2020 17:09:31 -0500 Subject: [PATCH] Fix skincolor-related memory leak --- src/dehacked.c | 1 + src/lua_infolib.c | 10 +++++----- src/r_draw.c | 11 +++++++++++ src/r_draw.h | 3 +++ 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/dehacked.c b/src/dehacked.c index 8510d7a3d..2e98a854c 100644 --- a/src/dehacked.c +++ b/src/dehacked.c @@ -851,6 +851,7 @@ static void readskincolor(MYFILE *f, INT32 num) if ((tmp = strtok(NULL,",")) == NULL) break; } + skincolor_modified[num] = true; } else if (fastcmp(word, "INVCOLOR")) { diff --git a/src/lua_infolib.c b/src/lua_infolib.c index fe0ca759f..b3e92f833 100644 --- a/src/lua_infolib.c +++ b/src/lua_infolib.c @@ -26,8 +26,8 @@ #include "lua_libs.h" #include "lua_hud.h" // hud_running errors -extern CV_PossibleValue_t Color_cons_t[MAXSKINCOLORS+1]; -extern void R_FlushTranslationColormapCache(void); +extern CV_PossibleValue_t Color_cons_t[]; +extern UINT8 skincolor_modified[]; boolean LUA_CallAction(const char *action, mobj_t *actor); state_t *astate; @@ -1559,7 +1559,7 @@ static int lib_setSkinColor(lua_State *L) else for (j=0; jramp[j] = (*((UINT8 **)luaL_checkudata(L, 3, META_COLORRAMP)))[j]; - R_FlushTranslationColormapCache(); + skincolor_modified[cnum] = true; } else if (i == 3 || (str && fastcmp(str,"invcolor"))) { UINT16 v = (UINT16)luaL_checkinteger(L, 3); if (v >= numskincolors) @@ -1644,7 +1644,7 @@ static int skincolor_set(lua_State *L) else for (i=0; iramp[i] = (*((UINT8 **)luaL_checkudata(L, 3, META_COLORRAMP)))[i]; - R_FlushTranslationColormapCache(); + skincolor_modified[info-skincolors] = true; } else if (fastcmp(field,"invcolor")) { UINT16 v = (UINT16)luaL_checkinteger(L, 3); if (v >= numskincolors) @@ -1698,7 +1698,7 @@ static int colorramp_set(lua_State *L) if (hud_running) return luaL_error(L, "Do not alter skincolor_t in HUD rendering code!"); colorramp[n] = i; - R_FlushTranslationColormapCache(); + skincolor_modified[cnum] = true; return 0; } diff --git a/src/r_draw.c b/src/r_draw.c index 6492d9d36..2b798c3bf 100644 --- a/src/r_draw.c +++ b/src/r_draw.c @@ -134,6 +134,7 @@ UINT32 nflatxshift, nflatyshift, nflatshiftup, nflatmask; #define NUM_PALETTE_ENTRIES 256 static UINT8** translationtablecache[MAXSKINS + 7] = {NULL}; +UINT8 skincolor_modified[MAXSKINCOLORS]; CV_PossibleValue_t Color_cons_t[MAXSKINCOLORS+1]; @@ -362,6 +363,7 @@ UINT8* R_GetTranslationColormap(INT32 skinnum, skincolornum_t color, UINT8 flags { UINT8* ret; INT32 skintableindex; + INT32 i; // Adjust if we want the default colormap switch (skinnum) @@ -385,6 +387,15 @@ UINT8* R_GetTranslationColormap(INT32 skinnum, skincolornum_t color, UINT8 flags // Get colormap ret = translationtablecache[skintableindex][color]; + + // Rebuild the cache if necessary + if (skincolor_modified[color]) + { + for (i = 0; i < (INT32)(sizeof(translationtablecache) / sizeof(translationtablecache[0])); i++) + if (translationtablecache[i] && translationtablecache[i][color]) + R_GenerateTranslationColormap(translationtablecache[i][color], i>=MAXSKINS ? MAXSKINS-i-1 : i, color); + skincolor_modified[color] = false; + } } else ret = NULL; diff --git a/src/r_draw.h b/src/r_draw.h index 329c4974a..1ca22f18a 100644 --- a/src/r_draw.h +++ b/src/r_draw.h @@ -117,6 +117,9 @@ void R_FlushTranslationColormapCache(void); UINT16 R_GetColorByName(const char *name); UINT16 R_GetSuperColorByName(const char *name); +// Color ramp modification should force a recache +extern UINT8 skincolor_modified[]; + // Custom player skin translation void R_InitViewBuffer(INT32 width, INT32 height); void R_InitViewBorder(void);