- added a level of abstraction to the lookup table code to ease later refactoring.

This commit is contained in:
Christoph Oelckers 2020-05-23 18:18:36 +02:00
parent f929419a0a
commit ae6403a205
8 changed files with 46 additions and 32 deletions

View file

@ -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);

View file

@ -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;

View file

@ -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++)

View file

@ -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;

View file

@ -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];

View file

@ -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);
}
}
}

View file

@ -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;

View file

@ -218,6 +218,7 @@ InitPalette(void)
unsigned int i;
short play;
uint8_t tempbuf[256];
//
// Dive palettes