Subversion Repositories eduke32

Rev

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

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