From ae6403a205746e72edd384695714d43164143a3d Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 23 May 2020 18:18:36 +0200 Subject: [PATCH] - added a level of abstraction to the lookup table code to ease later refactoring. --- source/build/include/palette.h | 19 ++++++++++++++++++- source/build/src/defs.cpp | 12 ++++++------ source/build/src/palette.cpp | 28 ++++++++++++---------------- source/build/src/polymost.cpp | 6 +++--- source/core/textures/buildtiles.cpp | 2 +- source/rr/src/common.cpp | 4 ++-- source/rr/src/rrdh.cpp | 6 +++--- source/sw/src/colormap.cpp | 1 + 8 files changed, 46 insertions(+), 32 deletions(-) diff --git a/source/build/include/palette.h b/source/build/include/palette.h index ad3c082f8..ae3557f5e 100644 --- a/source/build/include/palette.h +++ b/source/build/include/palette.h @@ -29,6 +29,23 @@ #define BRIGHTPAL (MAXPALOOKUPS) extern FString LookupTables[MAXPALOOKUPS]; +inline const uint8_t *paletteGetLookupTable(int num) +{ + return (const uint8_t*)LookupTables[num].GetChars(); +} + +inline void paletteCopyLookupTable(int dest, int src) +{ + LookupTables[dest] = LookupTables[src]; +} +inline bool paletteCheckLookupTable(int num) +{ + return LookupTables[num].Len() > 0; +} +inline void paletteClearLookupTable(int num) +{ + LookupTables[num] = ""; +} enum { @@ -47,7 +64,7 @@ struct palette_t extern PalEntry palfadergb; -void paletteMakeLookupTable(int32_t palnum, const char *remapbuf, uint8_t r, uint8_t g, uint8_t b, char noFloorPal); +void paletteMakeLookupTable(int32_t palnum, const uint8_t *remapbuf, uint8_t r, uint8_t g, uint8_t b, char noFloorPal); void paletteSetColorTable(int32_t id, uint8_t const* table, bool notransparency, bool twodonly); int32_t paletteSetLookupTable(int32_t palnum, const uint8_t *shtab); diff --git a/source/build/src/defs.cpp b/source/build/src/defs.cpp index 15cfe93df..574a445b1 100644 --- a/source/build/src/defs.cpp +++ b/source/build/src/defs.cpp @@ -2040,7 +2040,7 @@ static int32_t defsparser(scriptfile *script) // NOTE: all palookups are initialized, i.e. non-NULL! // NOTE2: aliasing (pal==remappal) is OK - paletteMakeLookupTable(pal, LookupTables[remappal].GetChars(), red<<2, green<<2, blue<<2, + paletteMakeLookupTable(pal, paletteGetLookupTable(remappal), red<<2, green<<2, blue<<2, remappal==0 ? 1 : (nofloorpal == -1 ? g_noFloorPal[remappal] : nofloorpal)); } break; @@ -2839,7 +2839,7 @@ static int32_t defsparser(scriptfile *script) break; } - paletteMakeLookupTable(id, (char*)palookupbuf.Data(), 0,0,0, g_noFloorPal[id]); + paletteMakeLookupTable(id, palookupbuf.Data(), 0,0,0, g_noFloorPal[id]); } break; } @@ -2862,8 +2862,8 @@ static int32_t defsparser(scriptfile *script) break; } - if (LookupTables[source].IsNotEmpty() || id > 0) // do not overwrite the base with an empty table. - LookupTables[id] = LookupTables[source]; + if (paletteCheckLookupTable(source) || id > 0) // do not overwrite the base with an empty table. + paletteCopyLookupTable(id, source); didLoadShade = 1; break; } @@ -2986,7 +2986,7 @@ static int32_t defsparser(scriptfile *script) } case T_UNDEF: { - LookupTables[id] = ""; + paletteClearLookupTable(id); didLoadShade = 0; if (id == 0) paletteloaded &= ~PALETTE_SHADE; @@ -3341,7 +3341,7 @@ static int32_t defsparser(scriptfile *script) } for (bssize_t i = id0; i <= id1; i++) - LookupTables[i] = ""; + paletteClearLookupTable(i); if (id0 == 0) paletteloaded &= ~PALETTE_SHADE; diff --git a/source/build/src/palette.cpp b/source/build/src/palette.cpp index 50f9bc05f..6ba85fbfc 100644 --- a/source/build/src/palette.cpp +++ b/source/build/src/palette.cpp @@ -138,11 +138,9 @@ void paletteLoadFromDisk(void) // Read base shade table (lookuptables 0). int length = numshades * 256; - auto p = LookupTables[0].LockNewBuffer(length); - auto count = fil.Read(p, length); - LookupTables->UnlockBuffer(); - if (count != length) - return; + auto buffer = fil.Read(length); + if (buffer.Size() != length) return; + LookupTables[0] = FString((char*)buffer.Data(), length); paletteloaded |= PALETTE_SHADE; paletteloaded |= PALETTE_TRANSLUC; @@ -158,7 +156,7 @@ void palettePostLoadTables(void) { globalpal = 0; - auto lookup = (const uint8_t*)LookupTables[0].GetChars(); + auto lookup = paletteGetLookupTable(0); ImageHelpers::SetPalette(GPalette.BaseColors); for (int c = 0; c < 255; ++c) // skipping transparent color @@ -211,7 +209,7 @@ void paletteFixTranslucencyMask(void) int32_t paletteLoadLookupTable(FileReader &fp) { - char remapbuf[256]; + uint8_t remapbuf[256]; int numlookups = fp.ReadUInt8(); if (numlookups < 1) return -1; @@ -244,7 +242,7 @@ void paletteSetupDefaultFog(void) { for (int j = 1; j <= 255 - 3; j++) { - if (!LookupTables[j].IsEmpty() && !LookupTables[j + 1].IsEmpty() && !LookupTables[j + 2].IsEmpty() && !LookupTables[j + 3].IsEmpty()) + if (LookupTables[j].IsEmpty() && LookupTables[j + 1].IsEmpty() && LookupTables[j + 2].IsEmpty() && LookupTables[j + 3].IsEmpty()) { paletteMakeLookupTable(j, NULL, 60, 60, 60, 1); paletteMakeLookupTable(j + 1, NULL, 60, 0, 0, 1); @@ -272,7 +270,7 @@ void palettePostLoadLookups(void) { if (!LookupTables[l].IsEmpty()) { - const uint8_t* lookup = (uint8_t*)LookupTables[l].GetChars(); + const uint8_t* lookup = paletteGetLookupTable(l); FRemapTable remap; for (int i = 0; i < numpalettes; i++) { @@ -306,9 +304,7 @@ int32_t paletteSetLookupTable(int32_t palnum, const uint8_t *shtab) if (shtab != NULL) { int length = numshades * 256; - auto p = LookupTables[palnum].LockNewBuffer(length); - memcpy(p, shtab, length); - LookupTables->UnlockBuffer(); + LookupTables[palnum] = FString((const char*)shtab, length); } return 0; @@ -320,9 +316,9 @@ int32_t paletteSetLookupTable(int32_t palnum, const uint8_t *shtab) // //========================================================================== -void paletteMakeLookupTable(int32_t palnum, const char *remapbuf, uint8_t r, uint8_t g, uint8_t b, char noFloorPal) +void paletteMakeLookupTable(int32_t palnum, const uint8_t *remapbuf, uint8_t r, uint8_t g, uint8_t b, char noFloorPal) { - char idmap[256]; + uint8_t idmap[256]; // NOTE: palnum==0 is allowed if (paletteloaded == 0 || (unsigned)palnum >= MAXPALOOKUPS) @@ -334,7 +330,7 @@ void paletteMakeLookupTable(int32_t palnum, const char *remapbuf, uint8_t r, uin { if (r == 0 || g == 0 || b == 0) { - LookupTables[palnum] = ""; // clear this entry so that later it can be filled with the base remap. + paletteClearLookupTable(palnum); return; } @@ -348,7 +344,7 @@ void paletteMakeLookupTable(int32_t palnum, const char *remapbuf, uint8_t r, uin { // "black fog"/visibility case -- only remap color indices - auto src = (const uint8_t*)LookupTables[0].GetChars(); + auto src = paletteGetLookupTable(0); for (int j = 0; j < numshades; j++) for (int i = 0; i < 256; i++) diff --git a/source/build/src/polymost.cpp b/source/build/src/polymost.cpp index c2f725636..50e3795fd 100644 --- a/source/build/src/polymost.cpp +++ b/source/build/src/polymost.cpp @@ -233,7 +233,7 @@ static void polymost_glinit() } for (int palookupnum = 0; palookupnum < MAXPALOOKUPS; ++palookupnum) { - GLInterface.SetPalswapData(palookupnum, (uint8_t*)LookupTables[palookupnum].GetChars(), numshades+1, palookupfog[palookupnum]); + GLInterface.SetPalswapData(palookupnum, paletteGetLookupTable(palookupnum), numshades+1, palookupfog[palookupnum]); } } @@ -406,7 +406,7 @@ static void polymost_drawpoly(vec2f_t const * const dpxy, int32_t const n, int32 return; } - if (LookupTables[globalpal].IsEmpty()) + if (!paletteCheckLookupTable(globalpal)) globalpal = 0; //Load texture (globalpicnum) @@ -4601,7 +4601,7 @@ static void polymost_precache(int32_t dapicnum, int32_t dapalnum, int32_t datype // while sprites are clamped if (videoGetRenderMode() < REND_POLYMOST) return; - if ((dapalnum < (MAXPALOOKUPS - RESERVEDPALS)) && (LookupTables[dapalnum].IsEmpty())) return;//dapalnum = 0; + if ((dapalnum < (MAXPALOOKUPS - RESERVEDPALS)) && (!paletteCheckLookupTable(dapalnum))) return;//dapalnum = 0; //Printf("precached %d %d type %d\n", dapicnum, dapalnum, datype); hicprecaching = 1; diff --git a/source/core/textures/buildtiles.cpp b/source/core/textures/buildtiles.cpp index 089c98ce0..27e143d3e 100644 --- a/source/core/textures/buildtiles.cpp +++ b/source/core/textures/buildtiles.cpp @@ -555,7 +555,7 @@ void tileCopy(int tile, int source, int pal, int xoffset, int yoffset, int flags if (pal != -1) { - auto remap = (const uint8_t*)LookupTables[pal].GetChars(); + auto remap = paletteGetLookupTable(pal); for (auto& pixel : buffer) { pixel = remap[pixel]; diff --git a/source/rr/src/common.cpp b/source/rr/src/common.cpp index 0c5757ea9..667021a4f 100644 --- a/source/rr/src/common.cpp +++ b/source/rr/src/common.cpp @@ -146,7 +146,7 @@ void G_LoadLookups(void) if (RR) { - char table[256]; + uint8_t table[256]; for (bssize_t i = 0; i < 256; i++) table[i] = i; for (bssize_t i = 0; i < 32; i++) @@ -186,7 +186,7 @@ void G_LoadLookups(void) { paletteMakeLookupTable(50, NULL, 12 * 4, 12 * 4, 12 * 4, 0); paletteMakeLookupTable(51, NULL, 12 * 4, 12 * 4, 12 * 4, 0); - paletteMakeLookupTable(54, LookupTables[8].GetChars(), 32 * 4, 32 * 4, 32 * 4, 0); + paletteMakeLookupTable(54, paletteGetLookupTable(8), 32 * 4, 32 * 4, 32 * 4, 0); } } } diff --git a/source/rr/src/rrdh.cpp b/source/rr/src/rrdh.cpp index d501e2a2a..62706aaf6 100644 --- a/source/rr/src/rrdh.cpp +++ b/source/rr/src/rrdh.cpp @@ -4320,10 +4320,10 @@ void ghdeploy_plrtouchedsprite(short a1, short a2) short word_2BE990[68]; short word_2BEA18; -char byte_2BE350[256]; void sub_59C20(void) { + uint8_t table[256]; int i; word_2BEA18 = 0; tileDelete(7059); @@ -4334,8 +4334,8 @@ void sub_59C20(void) word_2BE990[word_2BEA18++] = i; } for (i = 0; i < 256; i++) - byte_2BE350[i] = i; - paletteMakeLookupTable(2, byte_2BE350, 10*4, 10*4, 24*4, 0); + table[i] = i; + paletteMakeLookupTable(2, table, 10*4, 10*4, 24*4, 0); } int dword_2BEA20, dword_2BEA24; diff --git a/source/sw/src/colormap.cpp b/source/sw/src/colormap.cpp index 8a4c99b41..2c6eea60c 100644 --- a/source/sw/src/colormap.cpp +++ b/source/sw/src/colormap.cpp @@ -218,6 +218,7 @@ InitPalette(void) unsigned int i; short play; + uint8_t tempbuf[256]; // // Dive palettes