Subversion Repositories eduke32

Rev

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