Subversion Repositories eduke32

Rev

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