Subversion Repositories eduke32

Rev

Rev 8490 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7139 hendricks2 1
// "Build Engine & Tools" Copyright (c) 1993-1997 Ken Silverman
2
// Ken Silverman's official web site: "http://www.advsys.net/ken"
3
// See the included license file "BUILDLIC.TXT" for license info.
4
//
5
// This file has been modified from Ken Silverman's original release
6
// by Jonathon Fowler (jf@jonof.id.au)
7
// by the EDuke32 team (development@voidpoint.com)
8
 
5789 terminx 9
#include "compat.h"
10
#include "build.h"
5872 hendricks2 11
#include "engine_priv.h"
5862 hendricks2 12
#include "baselayer.h"
5789 terminx 13
#include "colmatch.h"
14
#include "cache1d.h"
15
#include "palette.h"
16
#include "a.h"
17
#include "xxhash.h"
18
 
7359 hendricks2 19
#include "vfs.h"
20
 
6061 hendricks2 21
uint8_t *basepaltable[MAXBASEPALS] = { palette };
5789 terminx 22
uint8_t basepalreset=1;
23
uint8_t curbasepal;
24
int32_t globalblend;
25
 
26
uint32_t g_lastpalettesum = 0;
27
palette_t curpalette[256];                      // the current palette, unadjusted for brightness or tint
28
palette_t curpalettefaded[256];         // the current palette, adjusted for brightness and tint (ie. what gets sent to the card)
6061 hendricks2 29
palette_t palfadergb = { 0, 0, 0, 0 };
5789 terminx 30
char palfadedelta = 0;
31
 
6514 hendricks2 32
int32_t realmaxshade;
33
float frealmaxshade;
34
 
5789 terminx 35
#if defined(USE_OPENGL)
36
palette_t palookupfog[MAXPALOOKUPS];
37
#endif
38
 
39
// For every pal number, whether tsprite pal should not be taken over from
40
// floor pal.
41
// NOTE: g_noFloorPal[0] is irrelevant as it's never checked.
42
int8_t g_noFloorPal[MAXPALOOKUPS];
43
 
44
int32_t curbrightness = 0, gammabrightness = 0;
45
 
6829 terminx 46
static void paletteSetFade(uint8_t offset);
5789 terminx 47
 
6827 terminx 48
#ifdef USE_OPENGL
49
void fullscreen_tint_gl(uint8_t r, uint8_t g, uint8_t b, uint8_t f)
50
{
51
    glMatrixMode(GL_PROJECTION);
52
    glPushMatrix();
53
    glLoadIdentity();
54
    glMatrixMode(GL_MODELVIEW);
55
    glPushMatrix();
56
    glLoadIdentity();
57
 
58
    glDisable(GL_DEPTH_TEST);
59
    glDisable(GL_ALPHA_TEST);
60
    polymost_setFogEnabled(false);
61
 
62
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
63
    glEnable(GL_BLEND);
64
    glColor4ub(r, g, b, f);
65
 
66
    polymost_useColorOnly(true);
67
    glBegin(GL_TRIANGLES);
68
    glVertex2f(-2.5f, 1.f);
69
    glVertex2f(2.5f, 1.f);
70
    glVertex2f(.0f, -2.5f);
71
    glEnd();
72
    polymost_useColorOnly(false);
73
 
74
    glPopMatrix();
75
    glMatrixMode(GL_PROJECTION);
76
    glPopMatrix();
77
}
78
#endif
79
 
6829 terminx 80
void videoFadeToBlack(int32_t moreopaquep)
5789 terminx 81
{
82
#ifdef USE_OPENGL
6829 terminx 83
    if (videoGetRenderMode() >= REND_POLYMOST)
5789 terminx 84
        fullscreen_tint_gl(0, 0, 0, moreopaquep ? 168 : 84);
85
    else
86
#endif
87
    {
88
        Bassert(!offscreenrendering);
89
 
6828 terminx 90
        videoBeginDrawing();
6829 terminx 91
        char *const p = (char *) frameplace;
92
        const char *const trans = paletteGetBlendTable(0);
93
        const int32_t shiftamnt = ((!!moreopaquep)*8);
94
        const int32_t dimprod = xdim*ydim;
95
        int32_t i = 0;
5789 terminx 96
 
97
#ifdef CLASSIC_SLICE_BY_4
6829 terminx 98
        for (; i<dimprod-4; i+=4)
99
        {
100
            p[i] = trans[p[i]<<shiftamnt];
101
            p[i+1] = trans[p[i+1]<<shiftamnt];
102
            p[i+2] = trans[p[i+2]<<shiftamnt];
103
            p[i+3] = trans[p[i+3]<<shiftamnt];
104
        }
5789 terminx 105
#endif
106
 
6829 terminx 107
        for (; i<dimprod; i++)
108
            p[i] = trans[p[i]<<shiftamnt];
6828 terminx 109
        videoEndDrawing();
5789 terminx 110
    }
111
}
112
 
113
void setup_blend(int32_t blend, int32_t doreverse)
114
{
115
    if (blendtable[blend] == NULL)
116
        blend = 0;
117
 
118
    if (globalblend != blend)
119
    {
120
        globalblend = blend;
6829 terminx 121
        fixtransluscence(FP_OFF(paletteGetBlendTable(blend)));
5789 terminx 122
    }
123
 
124
    if (doreverse)
125
        settransreverse();
126
    else
127
        settransnormal();
128
}
129
 
130
static void alloc_palookup(int32_t pal)
131
{
132
    // The asm functions vlineasm1, mvlineasm1 (maybe others?) access the next
133
    // palookup[...] shade entry for tilesizy==512 tiles.
134
    // See DEBUG_TILESIZY_512 and the comment in a.nasm: vlineasm1.
6438 terminx 135
    palookup[pal] = (char *) Xaligned_alloc(16, (numshades + 1) * 256);
136
    memset(palookup[pal], 0, (numshades + 1) * 256);
5789 terminx 137
}
138
 
139
static void maybe_alloc_palookup(int32_t palnum);
140
 
141
//
142
// loadpalette (internal)
143
//
6829 terminx 144
void paletteLoadFromDisk(void)
5789 terminx 145
{
8488 hendricks2 146
    paletteInitClosestColorScale(30, 59, 11);
147
    paletteInitClosestColorGrid();
5789 terminx 148
 
5888 hendricks2 149
#ifdef USE_OPENGL
7130 terminx 150
    for (auto & x : glblend)
151
        x = defaultglblend;
5888 hendricks2 152
#endif
153
 
7359 hendricks2 154
    buildvfs_kfd fil;
155
    if ((fil = kopen4load("palette.dat", 0)) == buildvfs_kfd_invalid)
5789 terminx 156
        return;
157
 
158
 
159
    // PALETTE_MAIN
160
 
161
    if (kread_and_test(fil, palette, 768))
162
        return kclose(fil);
163
 
7130 terminx 164
    for (unsigned char & k : palette)
165
        k <<= 2;
5789 terminx 166
 
8488 hendricks2 167
    paletteInitClosestColorMap(palette);
5789 terminx 168
 
169
    paletteloaded |= PALETTE_MAIN;
170
 
171
 
172
    // PALETTE_SHADES
173
 
174
    if (kread_and_test(fil, &numshades, 2))
175
        return kclose(fil);
176
    numshades = B_LITTLE16(numshades);
177
 
178
    if (numshades <= 1)
179
    {
180
        initprintf("Warning: Invalid number of shades in \"palette.dat\"!\n");
181
        numshades = 0;
182
        return kclose(fil);
183
    }
184
 
185
    // Auto-detect LameDuke. Its PALETTE.DAT doesn't have a 'numshades' 16-bit
186
    // int after the base palette, but starts directly with the shade tables.
187
    // Thus, the first two bytes will be 00 01, which is 256 if read as
188
    // little-endian int16_t.
189
    int32_t lamedukep = 0;
190
    if (numshades >= 256)
191
    {
192
        static char const * const seekfail = "Warning: klseek() failed in loadpalette()!\n";
193
 
194
        uint16_t temp;
195
        if (kread_and_test(fil, &temp, 2))
196
            return kclose(fil);
197
        temp = B_LITTLE16(temp);
198
        if (temp == 770 || numshades > 256) // 02 03
199
        {
200
            if (klseek(fil, -4, BSEEK_CUR) < 0)
201
            {
202
                initputs(seekfail);
203
                return kclose(fil);
204
            }
205
 
206
            numshades = 32;
207
            lamedukep = 1;
208
        }
209
        else
210
        {
211
            if (klseek(fil, -2, BSEEK_CUR) < 0)
212
            {
213
                initputs(seekfail);
214
                return kclose(fil);
215
            }
216
        }
217
    }
218
 
219
    // Read base shade table (palookup 0).
220
    maybe_alloc_palookup(0);
221
    if (kread_and_test(fil, palookup[0], numshades<<8))
222
        return kclose(fil);
223
 
224
    paletteloaded |= PALETTE_SHADE;
225
 
226
 
227
    // PALETTE_TRANSLUC
228
 
229
    char * const transluc = blendtable[0] = (char *) Xcalloc(256, 256);
230
 
231
    // Read translucency (blending) table.
232
    if (lamedukep)
233
    {
5829 terminx 234
        for (bssize_t i=0; i<255; i++)
5789 terminx 235
        {
236
            // NOTE: LameDuke's table doesn't have the last row or column (i==255).
237
 
238
            // Read the entries above and on the diagonal, if the table is
239
            // thought as being row-major.
8436 hendricks2 240
            if (kread_and_test(fil, &transluc[256*i + i + 1], 255-i))
5789 terminx 241
                return kclose(fil);
242
 
243
            // Duplicate the entries below the diagonal.
8436 hendricks2 244
            for (bssize_t j=i+1; j<256; j++)
245
                transluc[256*j + i] = transluc[256*i + j];
5789 terminx 246
        }
8436 hendricks2 247
        for (bssize_t i=0; i<256; i++)
248
            transluc[256*i + i] = i;
5789 terminx 249
    }
250
    else
251
    {
252
        if (kread_and_test(fil, transluc, 65536))
253
            return kclose(fil);
254
    }
255
 
256
    paletteloaded |= PALETTE_TRANSLUC;
257
 
258
 
259
    // additional blending tables
260
 
261
    uint8_t magic[12];
262
    if (!kread_and_test(fil, magic, sizeof(magic)) && !Bmemcmp(magic, "MoreBlendTab", sizeof(magic)))
263
    {
264
        uint8_t addblendtabs;
265
        if (kread_and_test(fil, &addblendtabs, 1))
266
        {
267
            initprintf("Warning: failed reading additional blending table count\n");
268
            return kclose(fil);
269
        }
270
 
271
        uint8_t blendnum;
272
        char *tab = (char *) Xmalloc(256*256);
5829 terminx 273
        for (bssize_t i=0; i<addblendtabs; i++)
5789 terminx 274
        {
275
            if (kread_and_test(fil, &blendnum, 1))
276
            {
277
                initprintf("Warning: failed reading additional blending table index\n");
7705 terminx 278
                Xfree(tab);
5789 terminx 279
                return kclose(fil);
280
            }
281
 
6829 terminx 282
            if (paletteGetBlendTable(blendnum) != NULL)
5789 terminx 283
                initprintf("Warning: duplicate blending table index %3d encountered\n", blendnum);
284
 
285
            if (kread_and_test(fil, tab, 256*256))
286
            {
287
                initprintf("Warning: failed reading additional blending table\n");
7705 terminx 288
                Xfree(tab);
5789 terminx 289
                return kclose(fil);
290
            }
291
 
6829 terminx 292
            paletteSetBlendTable(blendnum, tab);
5789 terminx 293
        }
7705 terminx 294
        Xfree(tab);
5789 terminx 295
 
296
        // Read log2 of count of alpha blending tables.
297
        uint8_t lognumalphatabs;
298
        if (!kread_and_test(fil, &lognumalphatabs, 1))
299
        {
300
            if (!(lognumalphatabs >= 1 && lognumalphatabs <= 7))
301
                initprintf("invalid lognumalphatabs value, must be in [1 .. 7]\n");
302
            else
303
                numalphatabs = 1<<lognumalphatabs;
304
        }
305
    }
306
 
307
    kclose(fil);
308
}
309
 
8487 hendricks2 310
uint8_t PaletteIndexFullbrights[32];
5789 terminx 311
 
6829 terminx 312
void palettePostLoadTables(void)
5789 terminx 313
{
314
    globalpal = 0;
5872 hendricks2 315
 
5789 terminx 316
    globalpalwritten = palookup[0];
317
    setpalookupaddress(globalpalwritten);
318
 
319
    fixtransluscence(FP_OFF(blendtable[0]));
320
 
6513 hendricks2 321
    char const * const palookup0 = palookup[0];
322
 
5789 terminx 323
#ifdef DEBUG_TILESIZY_512
324
    // Bump shade 1 by 16.
5829 terminx 325
    for (bssize_t i=256; i<512; i++)
6513 hendricks2 326
        palookup0[i] = palookup0[i+(16<<8)];
5789 terminx 327
#endif
328
 
6831 terminx 329
    blackcol = paletteGetClosestColor(0, 0, 0);
330
    whitecol = paletteGetClosestColor(255, 255, 255);
331
    redcol = paletteGetClosestColor(255, 0, 0);
5789 terminx 332
 
333
    // Bmemset(PaletteIndexFullbrights, 0, sizeof(PaletteIndexFullbrights));
5829 terminx 334
    for (bssize_t c = 0; c < 255; ++c) // skipping transparent color
5789 terminx 335
    {
6513 hendricks2 336
        uint8_t const index = palookup0[c];
337
        rgb24_t const & color = *(rgb24_t *)&palette[index*3];
5789 terminx 338
 
6513 hendricks2 339
        // don't consider #000000 fullbright
340
        if (EDUKE32_PREDICT_FALSE(color.r == 0 && color.g == 0 && color.b == 0))
341
            continue;
5789 terminx 342
 
6513 hendricks2 343
        for (size_t s = c + 256, s_end = 256*numshades; s < s_end; s += 256)
344
            if (EDUKE32_PREDICT_FALSE(palookup0[s] != index))
5789 terminx 345
                goto PostLoad_NotFullbright;
346
 
347
        SetPaletteIndexFullbright(c);
348
 
6513 hendricks2 349
        PostLoad_NotFullbright: ;
5789 terminx 350
    }
6514 hendricks2 351
 
6592 hendricks2 352
    if (realmaxshade == 0)
6514 hendricks2 353
    {
6592 hendricks2 354
        uint8_t const * const blackcolor = &palette[blackcol*3];
355
        size_t s;
356
        for (s = numshades < 2 ? 0 : numshades-2; s > 0; --s)
6514 hendricks2 357
        {
6592 hendricks2 358
            for (size_t c = s*256, c_end = c+255; c < c_end; ++c) // skipping transparent color
359
            {
360
                uint8_t const index = palookup0[c];
361
                uint8_t const * const color = &palette[index*3];
362
                if (!IsPaletteIndexFullbright(index) && memcmp(blackcolor, color, sizeof(rgb24_t)))
363
                    goto PostLoad_FoundShade;
364
            }
6514 hendricks2 365
        }
6592 hendricks2 366
        PostLoad_FoundShade: ;
367
        frealmaxshade = (float)(realmaxshade = s+1);
6514 hendricks2 368
    }
8490 hendricks2 369
 
370
    for (size_t i = 0; i<256; i++)
371
    {
372
        if (editorcolorsdef[i])
373
            continue;
374
 
375
        palette_t *edcol = (palette_t *) &vgapal16[4*i];
8491 hendricks2 376
        editorcolors[i] = paletteGetClosestColorWithBlacklist(edcol->b, edcol->g, edcol->r, 254, PaletteIndexFullbrights);
8490 hendricks2 377
    }
5789 terminx 378
}
379
 
6829 terminx 380
void paletteFixTranslucencyMask(void)
5789 terminx 381
{
7283 terminx 382
    for (auto thispalookup : palookup)
5789 terminx 383
    {
384
        if (thispalookup == NULL)
385
            continue;
386
 
5829 terminx 387
        for (bssize_t j=0; j<numshades; j++)
5789 terminx 388
            thispalookup[(j<<8) + 255] = 255;
389
    }
390
 
391
    // fix up translucency table so that transluc(255,x)
392
    // and transluc(x,255) is black instead of purple.
7283 terminx 393
    for (auto transluc : blendtable)
5789 terminx 394
    {
395
        if (transluc == NULL)
396
            continue;
397
 
5829 terminx 398
        for (bssize_t j=0; j<255; j++)
5789 terminx 399
        {
400
            transluc[(255<<8) + j] = transluc[(blackcol<<8) + j];
401
            transluc[255 + (j<<8)] = transluc[blackcol + (j<<8)];
402
        }
403
        transluc[(255<<8) + 255] = transluc[(blackcol<<8) + blackcol];
404
    }
405
}
406
 
407
// Load LOOKUP.DAT, which contains lookup tables and additional base palettes.
408
//
409
// <fp>: kopen4load file handle
410
//
411
// Returns:
412
//  - on success, 0
413
//  - on error, -1 (didn't read enough data)
414
//  - -2: error, we already wrote an error message ourselves
7359 hendricks2 415
int32_t paletteLoadLookupTable(buildvfs_kfd fp)
5789 terminx 416
{
417
    uint8_t numlookups;
418
    char remapbuf[256];
419
 
420
    if (kread_and_test(fp, &numlookups, 1))
421
        return -1;
422
 
5829 terminx 423
    for (bssize_t j=0; j<numlookups; j++)
5789 terminx 424
    {
425
        uint8_t palnum;
426
 
427
        if (kread_and_test(fp, &palnum, 1))
428
            return -1;
429
 
430
        if (palnum >= 256-RESERVEDPALS)
431
        {
432
            initprintf("ERROR: attempt to load lookup at reserved pal %d\n", palnum);
433
            return -2;
434
        }
435
 
436
        if (kread_and_test(fp, remapbuf, 256))
437
            return -1;
438
 
6829 terminx 439
        paletteMakeLookupTable(palnum, remapbuf, 0, 0, 0, 0);
5789 terminx 440
    }
441
 
442
    return 0;
443
}
444
 
6829 terminx 445
void paletteSetupDefaultFog(void)
5789 terminx 446
{
447
    // Find a gap of four consecutive unused pal numbers to generate fog shade
448
    // tables.
5829 terminx 449
    for (bssize_t j=1; j<=255-3; j++)
5789 terminx 450
        if (!palookup[j] && !palookup[j+1] && !palookup[j+2] && !palookup[j+3])
451
        {
6829 terminx 452
            paletteMakeLookupTable(j, NULL, 60, 60, 60, 1);
453
            paletteMakeLookupTable(j+1, NULL, 60, 0, 0, 1);
454
            paletteMakeLookupTable(j+2, NULL, 0, 60, 0, 1);
455
            paletteMakeLookupTable(j+3, NULL, 0, 0, 60, 1);
5789 terminx 456
 
457
            break;
458
        }
459
}
460
 
6829 terminx 461
void palettePostLoadLookups(void)
5789 terminx 462
{
463
    // Alias remaining unused pal numbers to the base shade table.
5829 terminx 464
    for (bssize_t j=1; j<MAXPALOOKUPS; j++)
5789 terminx 465
    {
466
        // If an existing lookup is identical to #0, free it.
467
        if (palookup[j] && palookup[j] != palookup[0] && !Bmemcmp(palookup[0], palookup[j], 256*numshades))
6829 terminx 468
            paletteFreeLookupTable(j);
5789 terminx 469
 
470
        if (!palookup[j])
6829 terminx 471
            paletteMakeLookupTable(j, NULL, 0, 0, 0, 1);
5789 terminx 472
    }
473
}
474
 
475
static int32_t palookup_isdefault(int32_t palnum)  // KEEPINSYNC engine.lua
476
{
477
    return (palookup[palnum] == NULL || (palnum!=0 && palookup[palnum] == palookup[0]));
478
}
479
 
480
static void maybe_alloc_palookup(int32_t palnum)
481
{
482
    if (palookup_isdefault(palnum))
483
    {
484
        alloc_palookup(palnum);
485
        if (palookup[palnum] == NULL)
8191 terminx 486
            fatal_exit("NULL palette!\n");
5789 terminx 487
    }
488
}
489
 
6829 terminx 490
void paletteSetBlendTable(int32_t blend, const char *tab)
5789 terminx 491
{
492
    if (blendtable[blend] == NULL)
493
        blendtable[blend] = (char *) Xmalloc(256*256);
494
 
495
    Bmemcpy(blendtable[blend], tab, 256*256);
496
}
6829 terminx 497
void paletteFreeBlendTable(int32_t const blend)
5789 terminx 498
{
499
    DO_FREE_AND_NULL(blendtable[blend]);
500
}
501
 
502
#ifdef LUNATIC
6829 terminx 503
const char *(paletteGetBlendTable) (int32_t blend)
5789 terminx 504
{
505
    return blendtable[blend];
506
}
507
#endif
508
 
5888 hendricks2 509
#ifdef USE_OPENGL
510
glblend_t const nullglblend =
511
{
512
    {
513
        { 1.f, BLENDFACTOR_ONE, BLENDFACTOR_ZERO, 0 },
514
        { 1.f, BLENDFACTOR_ONE, BLENDFACTOR_ZERO, 0 },
515
    },
516
};
517
glblend_t const defaultglblend =
518
{
519
    {
520
        { 2.f/3.f, BLENDFACTOR_SRC_ALPHA, BLENDFACTOR_ONE_MINUS_SRC_ALPHA, 0 },
521
        { 1.f/3.f, BLENDFACTOR_SRC_ALPHA, BLENDFACTOR_ONE_MINUS_SRC_ALPHA, 0 },
522
    },
523
};
524
 
525
glblend_t glblend[MAXBLENDTABS];
526
 
527
void handle_blend(uint8_t enable, uint8_t blend, uint8_t def)
528
{
529
    static GLenum const blendFuncTokens[NUMBLENDFACTORS] =
530
    {
531
        GL_ZERO,
532
        GL_ONE,
533
        GL_SRC_COLOR,
534
        GL_ONE_MINUS_SRC_COLOR,
535
        GL_SRC_ALPHA,
536
        GL_ONE_MINUS_SRC_ALPHA,
537
        GL_DST_ALPHA,
538
        GL_ONE_MINUS_DST_ALPHA,
539
        GL_DST_COLOR,
540
        GL_ONE_MINUS_DST_COLOR,
541
    };
542
 
543
    if (!enable)
544
    {
6656 pogokeen 545
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
5888 hendricks2 546
        return;
547
    }
548
 
549
    glblenddef_t const * const glbdef = glblend[blend].def + def;
6656 pogokeen 550
    glBlendFunc(blendFuncTokens[glbdef->src], blendFuncTokens[glbdef->dst]);
5888 hendricks2 551
}
552
#endif
553
 
6829 terminx 554
int32_t paletteSetLookupTable(int32_t palnum, const uint8_t *shtab)
5789 terminx 555
{
556
    if (shtab != NULL)
557
    {
558
        maybe_alloc_palookup(palnum);
559
        Bmemcpy(palookup[palnum], shtab, 256*numshades);
560
    }
561
 
562
    return 0;
563
}
6829 terminx 564
 
565
void paletteFreeLookupTable(int32_t const palnum)
5789 terminx 566
{
567
    if (palnum == 0 && palookup[palnum] != NULL)
568
    {
5829 terminx 569
        for (bssize_t i = 1; i < MAXPALOOKUPS; i++)
5789 terminx 570
            if (palookup[i] == palookup[palnum])
571
                palookup[i] = NULL;
572
 
6438 terminx 573
        ALIGNED_FREE_AND_NULL(palookup[palnum]);
5789 terminx 574
    }
575
    else if (palookup[palnum] == palookup[0])
576
        palookup[palnum] = NULL;
577
    else
6438 terminx 578
        ALIGNED_FREE_AND_NULL(palookup[palnum]);
5789 terminx 579
}
580
 
581
//
582
// makepalookup
583
//
6829 terminx 584
void paletteMakeLookupTable(int32_t palnum, const char *remapbuf, uint8_t r, uint8_t g, uint8_t b, char noFloorPal)
5789 terminx 585
{
586
    int32_t i, j;
587
 
6061 hendricks2 588
    static char idmap[256] = { 1 };
5789 terminx 589
 
590
    if (paletteloaded == 0)
591
        return;
592
 
593
    // NOTE: palnum==0 is allowed
594
    if ((unsigned) palnum >= MAXPALOOKUPS)
595
        return;
596
 
597
    g_noFloorPal[palnum] = noFloorPal;
598
 
599
    if (remapbuf==NULL)
600
    {
601
        if ((r|g|b) == 0)
602
        {
603
            palookup[palnum] = palookup[0];  // Alias to base shade table!
604
            return;
605
        }
606
 
607
        if (idmap[0]==1)  // init identity map
608
            for (i=0; i<256; i++)
609
                idmap[i] = i;
610
 
611
        remapbuf = idmap;
612
    }
613
 
614
    maybe_alloc_palookup(palnum);
615
 
616
    if ((r|g|b) == 0)
617
    {
618
        // "black fog"/visibility case -- only remap color indices
619
 
620
        for (j=0; j<numshades; j++)
621
            for (i=0; i<256; i++)
622
            {
623
                const char *src = palookup[0];
624
                palookup[palnum][256*j + i] = src[256*j + remapbuf[i]];
625
            }
626
    }
627
    else
628
    {
629
        // colored fog case
630
 
631
        char *ptr2 = palookup[palnum];
632
 
633
        for (i=0; i<numshades; i++)
634
        {
635
            int32_t palscale = divscale16(i, numshades-1);
636
 
637
            for (j=0; j<256; j++)
638
            {
639
                const char *ptr = (const char *) &palette[remapbuf[j]*3];
6831 terminx 640
                *ptr2++ = paletteGetClosestColor(ptr[0] + mulscale16(r-ptr[0], palscale),
5789 terminx 641
                    ptr[1] + mulscale16(g-ptr[1], palscale),
642
                    ptr[2] + mulscale16(b-ptr[2], palscale));
643
            }
644
        }
645
    }
646
 
647
#if defined(USE_OPENGL)
648
    palookupfog[palnum].r = r;
649
    palookupfog[palnum].g = g;
650
    palookupfog[palnum].b = b;
651
#endif
652
}
653
 
654
//
655
// setbasepal
656
//
6829 terminx 657
void paletteSetColorTable(int32_t id, uint8_t const * const table)
5789 terminx 658
{
659
    if (basepaltable[id] == NULL)
660
        basepaltable[id] = (uint8_t *) Xmalloc(768);
661
 
662
    Bmemcpy(basepaltable[id], table, 768);
6776 pogokeen 663
 
6915 pogokeen 664
#ifdef USE_OPENGL
6932 pogokeen 665
    if (videoGetRenderMode() >= REND_POLYMOST)
6919 pogokeen 666
    {
667
        uploadbasepalette(id);
668
    }
6915 pogokeen 669
#endif
5789 terminx 670
}
6829 terminx 671
 
672
void paletteFreeColorTable(int32_t const id)
5789 terminx 673
{
674
    if (id == 0)
675
        Bmemset(basepaltable[id], 0, 768);
676
    else
677
        DO_FREE_AND_NULL(basepaltable[id]);
678
}
679
 
680
//
681
// setbrightness
682
//
683
// flags:
684
//  1: don't setpalette(),  DON'T USE THIS FLAG!
685
//  2: don't gltexinvalidateall()
686
//  4: don't calc curbrightness from dabrightness,  DON'T USE THIS FLAG!
687
//  8: don't gltexinvalidate8()
688
// 16: don't reset palfade*
6829 terminx 689
void videoSetPalette(char dabrightness, uint8_t dapalid, uint8_t flags)
5789 terminx 690
{
5884 terminx 691
    int32_t i, j;
5789 terminx 692
    const uint8_t *dapal;
693
 
694
#ifdef USE_OPENGL
695
    int32_t paldidchange;
696
#endif
697
    int32_t palsumdidchange;
698
    //    uint32_t lastbright = curbrightness;
699
 
8300 hendricks2 700
    // Bassert((flags&4)==0); // What is so bad about this flag?
5789 terminx 701
 
702
    if (/*(unsigned)dapalid >= MAXBASEPALS ||*/ basepaltable[dapalid] == NULL)
703
        dapalid = 0;
704
#ifdef USE_OPENGL
705
    paldidchange = (curbasepal != dapalid || basepalreset);
706
#endif
707
    curbasepal = dapalid;
708
    basepalreset = 0;
709
 
710
    dapal = basepaltable[curbasepal];
711
 
712
    if (!(flags&4))
713
    {
714
        curbrightness = clamp(dabrightness, 0, 15);
715
        //        if (lastbright != (unsigned)curbrightness)
716
        //            vid_gamma = 1.0 + ((float)curbrightness / 10.0);
717
    }
718
 
6828 terminx 719
    videoSetGamma();
5884 terminx 720
    j = !gammabrightness ? curbrightness : 0;
5789 terminx 721
 
722
    for (i=0; i<256; i++)
723
    {
724
        // save palette without any brightness adjustment
725
        curpalette[i].r = dapal[i*3+0];
726
        curpalette[i].g = dapal[i*3+1];
727
        curpalette[i].b = dapal[i*3+2];
728
        curpalette[i].f = 0;
729
 
730
        // brightness adjust the palette
731
        curpalettefaded[i].b = britable[j][curpalette[i].b];
732
        curpalettefaded[i].g = britable[j][curpalette[i].g];
733
        curpalettefaded[i].r = britable[j][curpalette[i].r];
734
        curpalettefaded[i].f = 0;
735
    }
736
 
737
    if ((flags&16) && palfadedelta)  // keep the fade
6829 terminx 738
        paletteSetFade(palfadedelta>>2);
5789 terminx 739
 
6829 terminx 740
    static uint32_t lastpalettesum=0;
741
    uint32_t newpalettesum = XXH32((uint8_t *) curpalettefaded, sizeof(curpalettefaded), sizeof(curpalettefaded));
5789 terminx 742
 
6829 terminx 743
    palsumdidchange = (newpalettesum != lastpalettesum);
5789 terminx 744
 
6829 terminx 745
    if (palsumdidchange || newpalettesum != g_lastpalettesum)
746
    {
747
        //            if ((flags&1) == 0)
748
        videoUpdatePalette(0, 256);
5789 terminx 749
    }
750
 
6829 terminx 751
    g_lastpalettesum = lastpalettesum = newpalettesum;
752
 
5789 terminx 753
#ifdef USE_OPENGL
6829 terminx 754
    if (videoGetRenderMode() >= REND_POLYMOST)
5789 terminx 755
    {
756
        // Only reset the textures if the corresponding preserve flags are clear and
757
        // either (a) the new palette is different to the last, or (b) the brightness
758
        // changed and we couldn't set it using hardware gamma.
759
 
760
        // XXX: no-HW-gamma OpenGL platforms will exhibit bad performance with
761
        // simultaneous basepal and tint changes?
5884 terminx 762
        const int32_t doinvalidate = (paldidchange || (palsumdidchange && !gammabrightness));
5789 terminx 763
 
764
        if (!(flags&2) && doinvalidate)
6776 pogokeen 765
            gltexinvalidatetype(INVALIDATE_ALL_NON_INDEXED);
5789 terminx 766
        if (!(flags&8) && doinvalidate)
6776 pogokeen 767
            gltexinvalidatetype(INVALIDATE_ART_NON_INDEXED);
5789 terminx 768
#ifdef POLYMER
6829 terminx 769
        if ((videoGetRenderMode() == REND_POLYMER) && doinvalidate)
5789 terminx 770
            polymer_texinvalidate();
771
#endif
772
    }
773
#endif
774
 
775
    if ((flags&16)==0)
776
    {
777
        palfadergb.r = palfadergb.g = palfadergb.b = 0;
778
        palfadedelta = 0;
779
    }
780
}
781
 
6829 terminx 782
palette_t paletteGetColor(int32_t col)
5789 terminx 783
{
784
    if (!gammabrightness)
785
    {
5884 terminx 786
        palette_t const p = { britable[curbrightness][curpalette[col].r], britable[curbrightness][curpalette[col].g],
787
                              britable[curbrightness][curpalette[col].b], 0 };
5789 terminx 788
        return p;
789
    }
790
 
791
    return curpalette[col];
792
}
793
 
6829 terminx 794
static void paletteSetFade(uint8_t offset)
5789 terminx 795
{
6829 terminx 796
    for (native_t i=0; i<256; i++)
5789 terminx 797
    {
6829 terminx 798
        palette_t const p = paletteGetColor(i);
5789 terminx 799
 
6829 terminx 800
        curpalettefaded[i].b = p.b + (((palfadergb.b - p.b) * offset) >> 8);
801
        curpalettefaded[i].g = p.g + (((palfadergb.g - p.g) * offset) >> 8);
802
        curpalettefaded[i].r = p.r + (((palfadergb.r - p.r) * offset) >> 8);
5789 terminx 803
        curpalettefaded[i].f = 0;
804
    }
805
}
806
 
807
//#define DEBUG_PALETTEFADE
808
 
809
//
810
// setpalettefade
811
//
6829 terminx 812
void videoFadePalette(uint8_t r, uint8_t g, uint8_t b, uint8_t offset)
5789 terminx 813
{
814
    palfadergb.r = r;
815
    palfadergb.g = g;
816
    palfadergb.b = b;
817
#ifdef DEBUG_PALETTEFADE
818
    if (offset)
819
        offset = max(offset, 128);
820
#endif
821
    palfadedelta = offset;
822
 
6829 terminx 823
    paletteSetFade(offset);
5789 terminx 824
 
6829 terminx 825
    static uint32_t lastpalettesum=0;
826
    uint32_t newpalettesum = XXH32((uint8_t *) curpalettefaded, sizeof(curpalettefaded), sizeof(curpalettefaded));
5789 terminx 827
 
6829 terminx 828
    if (newpalettesum != lastpalettesum || newpalettesum != g_lastpalettesum)
6932 pogokeen 829
    {
6829 terminx 830
        videoUpdatePalette(0, 256);
6932 pogokeen 831
    }
5789 terminx 832
 
6829 terminx 833
    g_lastpalettesum = lastpalettesum = newpalettesum;
5789 terminx 834
}