Subversion Repositories eduke32

Rev

Rev 8487 | 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
 
6461 terminx 333
    for (size_t i = 0; i<16; i++)
334
    {
335
        palette_t *edcol = (palette_t *) &vgapal16[4*i];
8488 hendricks2 336
        editorcolors[i] = paletteGetClosestColorUpToIndex(edcol->b, edcol->g, edcol->r, 239);
6461 terminx 337
    }
338
 
5789 terminx 339
    // Bmemset(PaletteIndexFullbrights, 0, sizeof(PaletteIndexFullbrights));
5829 terminx 340
    for (bssize_t c = 0; c < 255; ++c) // skipping transparent color
5789 terminx 341
    {
6513 hendricks2 342
        uint8_t const index = palookup0[c];
343
        rgb24_t const & color = *(rgb24_t *)&palette[index*3];
5789 terminx 344
 
6513 hendricks2 345
        // don't consider #000000 fullbright
346
        if (EDUKE32_PREDICT_FALSE(color.r == 0 && color.g == 0 && color.b == 0))
347
            continue;
5789 terminx 348
 
6513 hendricks2 349
        for (size_t s = c + 256, s_end = 256*numshades; s < s_end; s += 256)
350
            if (EDUKE32_PREDICT_FALSE(palookup0[s] != index))
5789 terminx 351
                goto PostLoad_NotFullbright;
352
 
353
        SetPaletteIndexFullbright(c);
354
 
6513 hendricks2 355
        PostLoad_NotFullbright: ;
5789 terminx 356
    }
6514 hendricks2 357
 
6592 hendricks2 358
    if (realmaxshade == 0)
6514 hendricks2 359
    {
6592 hendricks2 360
        uint8_t const * const blackcolor = &palette[blackcol*3];
361
        size_t s;
362
        for (s = numshades < 2 ? 0 : numshades-2; s > 0; --s)
6514 hendricks2 363
        {
6592 hendricks2 364
            for (size_t c = s*256, c_end = c+255; c < c_end; ++c) // skipping transparent color
365
            {
366
                uint8_t const index = palookup0[c];
367
                uint8_t const * const color = &palette[index*3];
368
                if (!IsPaletteIndexFullbright(index) && memcmp(blackcolor, color, sizeof(rgb24_t)))
369
                    goto PostLoad_FoundShade;
370
            }
6514 hendricks2 371
        }
6592 hendricks2 372
        PostLoad_FoundShade: ;
373
        frealmaxshade = (float)(realmaxshade = s+1);
6514 hendricks2 374
    }
5789 terminx 375
}
376
 
6829 terminx 377
void paletteFixTranslucencyMask(void)
5789 terminx 378
{
7283 terminx 379
    for (auto thispalookup : palookup)
5789 terminx 380
    {
381
        if (thispalookup == NULL)
382
            continue;
383
 
5829 terminx 384
        for (bssize_t j=0; j<numshades; j++)
5789 terminx 385
            thispalookup[(j<<8) + 255] = 255;
386
    }
387
 
388
    // fix up translucency table so that transluc(255,x)
389
    // and transluc(x,255) is black instead of purple.
7283 terminx 390
    for (auto transluc : blendtable)
5789 terminx 391
    {
392
        if (transluc == NULL)
393
            continue;
394
 
5829 terminx 395
        for (bssize_t j=0; j<255; j++)
5789 terminx 396
        {
397
            transluc[(255<<8) + j] = transluc[(blackcol<<8) + j];
398
            transluc[255 + (j<<8)] = transluc[blackcol + (j<<8)];
399
        }
400
        transluc[(255<<8) + 255] = transluc[(blackcol<<8) + blackcol];
401
    }
402
}
403
 
404
// Load LOOKUP.DAT, which contains lookup tables and additional base palettes.
405
//
406
// <fp>: kopen4load file handle
407
//
408
// Returns:
409
//  - on success, 0
410
//  - on error, -1 (didn't read enough data)
411
//  - -2: error, we already wrote an error message ourselves
7359 hendricks2 412
int32_t paletteLoadLookupTable(buildvfs_kfd fp)
5789 terminx 413
{
414
    uint8_t numlookups;
415
    char remapbuf[256];
416
 
417
    if (kread_and_test(fp, &numlookups, 1))
418
        return -1;
419
 
5829 terminx 420
    for (bssize_t j=0; j<numlookups; j++)
5789 terminx 421
    {
422
        uint8_t palnum;
423
 
424
        if (kread_and_test(fp, &palnum, 1))
425
            return -1;
426
 
427
        if (palnum >= 256-RESERVEDPALS)
428
        {
429
            initprintf("ERROR: attempt to load lookup at reserved pal %d\n", palnum);
430
            return -2;
431
        }
432
 
433
        if (kread_and_test(fp, remapbuf, 256))
434
            return -1;
435
 
6829 terminx 436
        paletteMakeLookupTable(palnum, remapbuf, 0, 0, 0, 0);
5789 terminx 437
    }
438
 
439
    return 0;
440
}
441
 
6829 terminx 442
void paletteSetupDefaultFog(void)
5789 terminx 443
{
444
    // Find a gap of four consecutive unused pal numbers to generate fog shade
445
    // tables.
5829 terminx 446
    for (bssize_t j=1; j<=255-3; j++)
5789 terminx 447
        if (!palookup[j] && !palookup[j+1] && !palookup[j+2] && !palookup[j+3])
448
        {
6829 terminx 449
            paletteMakeLookupTable(j, NULL, 60, 60, 60, 1);
450
            paletteMakeLookupTable(j+1, NULL, 60, 0, 0, 1);
451
            paletteMakeLookupTable(j+2, NULL, 0, 60, 0, 1);
452
            paletteMakeLookupTable(j+3, NULL, 0, 0, 60, 1);
5789 terminx 453
 
454
            break;
455
        }
456
}
457
 
6829 terminx 458
void palettePostLoadLookups(void)
5789 terminx 459
{
460
    // Alias remaining unused pal numbers to the base shade table.
5829 terminx 461
    for (bssize_t j=1; j<MAXPALOOKUPS; j++)
5789 terminx 462
    {
463
        // If an existing lookup is identical to #0, free it.
464
        if (palookup[j] && palookup[j] != palookup[0] && !Bmemcmp(palookup[0], palookup[j], 256*numshades))
6829 terminx 465
            paletteFreeLookupTable(j);
5789 terminx 466
 
467
        if (!palookup[j])
6829 terminx 468
            paletteMakeLookupTable(j, NULL, 0, 0, 0, 1);
5789 terminx 469
    }
470
}
471
 
472
static int32_t palookup_isdefault(int32_t palnum)  // KEEPINSYNC engine.lua
473
{
474
    return (palookup[palnum] == NULL || (palnum!=0 && palookup[palnum] == palookup[0]));
475
}
476
 
477
static void maybe_alloc_palookup(int32_t palnum)
478
{
479
    if (palookup_isdefault(palnum))
480
    {
481
        alloc_palookup(palnum);
482
        if (palookup[palnum] == NULL)
8191 terminx 483
            fatal_exit("NULL palette!\n");
5789 terminx 484
    }
485
}
486
 
6829 terminx 487
void paletteSetBlendTable(int32_t blend, const char *tab)
5789 terminx 488
{
489
    if (blendtable[blend] == NULL)
490
        blendtable[blend] = (char *) Xmalloc(256*256);
491
 
492
    Bmemcpy(blendtable[blend], tab, 256*256);
493
}
6829 terminx 494
void paletteFreeBlendTable(int32_t const blend)
5789 terminx 495
{
496
    DO_FREE_AND_NULL(blendtable[blend]);
497
}
498
 
499
#ifdef LUNATIC
6829 terminx 500
const char *(paletteGetBlendTable) (int32_t blend)
5789 terminx 501
{
502
    return blendtable[blend];
503
}
504
#endif
505
 
5888 hendricks2 506
#ifdef USE_OPENGL
507
glblend_t const nullglblend =
508
{
509
    {
510
        { 1.f, BLENDFACTOR_ONE, BLENDFACTOR_ZERO, 0 },
511
        { 1.f, BLENDFACTOR_ONE, BLENDFACTOR_ZERO, 0 },
512
    },
513
};
514
glblend_t const defaultglblend =
515
{
516
    {
517
        { 2.f/3.f, BLENDFACTOR_SRC_ALPHA, BLENDFACTOR_ONE_MINUS_SRC_ALPHA, 0 },
518
        { 1.f/3.f, BLENDFACTOR_SRC_ALPHA, BLENDFACTOR_ONE_MINUS_SRC_ALPHA, 0 },
519
    },
520
};
521
 
522
glblend_t glblend[MAXBLENDTABS];
523
 
524
void handle_blend(uint8_t enable, uint8_t blend, uint8_t def)
525
{
526
    static GLenum const blendFuncTokens[NUMBLENDFACTORS] =
527
    {
528
        GL_ZERO,
529
        GL_ONE,
530
        GL_SRC_COLOR,
531
        GL_ONE_MINUS_SRC_COLOR,
532
        GL_SRC_ALPHA,
533
        GL_ONE_MINUS_SRC_ALPHA,
534
        GL_DST_ALPHA,
535
        GL_ONE_MINUS_DST_ALPHA,
536
        GL_DST_COLOR,
537
        GL_ONE_MINUS_DST_COLOR,
538
    };
539
 
540
    if (!enable)
541
    {
6656 pogokeen 542
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
5888 hendricks2 543
        return;
544
    }
545
 
546
    glblenddef_t const * const glbdef = glblend[blend].def + def;
6656 pogokeen 547
    glBlendFunc(blendFuncTokens[glbdef->src], blendFuncTokens[glbdef->dst]);
5888 hendricks2 548
}
549
#endif
550
 
6829 terminx 551
int32_t paletteSetLookupTable(int32_t palnum, const uint8_t *shtab)
5789 terminx 552
{
553
    if (shtab != NULL)
554
    {
555
        maybe_alloc_palookup(palnum);
556
        Bmemcpy(palookup[palnum], shtab, 256*numshades);
557
    }
558
 
559
    return 0;
560
}
6829 terminx 561
 
562
void paletteFreeLookupTable(int32_t const palnum)
5789 terminx 563
{
564
    if (palnum == 0 && palookup[palnum] != NULL)
565
    {
5829 terminx 566
        for (bssize_t i = 1; i < MAXPALOOKUPS; i++)
5789 terminx 567
            if (palookup[i] == palookup[palnum])
568
                palookup[i] = NULL;
569
 
6438 terminx 570
        ALIGNED_FREE_AND_NULL(palookup[palnum]);
5789 terminx 571
    }
572
    else if (palookup[palnum] == palookup[0])
573
        palookup[palnum] = NULL;
574
    else
6438 terminx 575
        ALIGNED_FREE_AND_NULL(palookup[palnum]);
5789 terminx 576
}
577
 
578
//
579
// makepalookup
580
//
6829 terminx 581
void paletteMakeLookupTable(int32_t palnum, const char *remapbuf, uint8_t r, uint8_t g, uint8_t b, char noFloorPal)
5789 terminx 582
{
583
    int32_t i, j;
584
 
6061 hendricks2 585
    static char idmap[256] = { 1 };
5789 terminx 586
 
587
    if (paletteloaded == 0)
588
        return;
589
 
590
    // NOTE: palnum==0 is allowed
591
    if ((unsigned) palnum >= MAXPALOOKUPS)
592
        return;
593
 
594
    g_noFloorPal[palnum] = noFloorPal;
595
 
596
    if (remapbuf==NULL)
597
    {
598
        if ((r|g|b) == 0)
599
        {
600
            palookup[palnum] = palookup[0];  // Alias to base shade table!
601
            return;
602
        }
603
 
604
        if (idmap[0]==1)  // init identity map
605
            for (i=0; i<256; i++)
606
                idmap[i] = i;
607
 
608
        remapbuf = idmap;
609
    }
610
 
611
    maybe_alloc_palookup(palnum);
612
 
613
    if ((r|g|b) == 0)
614
    {
615
        // "black fog"/visibility case -- only remap color indices
616
 
617
        for (j=0; j<numshades; j++)
618
            for (i=0; i<256; i++)
619
            {
620
                const char *src = palookup[0];
621
                palookup[palnum][256*j + i] = src[256*j + remapbuf[i]];
622
            }
623
    }
624
    else
625
    {
626
        // colored fog case
627
 
628
        char *ptr2 = palookup[palnum];
629
 
630
        for (i=0; i<numshades; i++)
631
        {
632
            int32_t palscale = divscale16(i, numshades-1);
633
 
634
            for (j=0; j<256; j++)
635
            {
636
                const char *ptr = (const char *) &palette[remapbuf[j]*3];
6831 terminx 637
                *ptr2++ = paletteGetClosestColor(ptr[0] + mulscale16(r-ptr[0], palscale),
5789 terminx 638
                    ptr[1] + mulscale16(g-ptr[1], palscale),
639
                    ptr[2] + mulscale16(b-ptr[2], palscale));
640
            }
641
        }
642
    }
643
 
644
#if defined(USE_OPENGL)
645
    palookupfog[palnum].r = r;
646
    palookupfog[palnum].g = g;
647
    palookupfog[palnum].b = b;
648
#endif
649
}
650
 
651
//
652
// setbasepal
653
//
6829 terminx 654
void paletteSetColorTable(int32_t id, uint8_t const * const table)
5789 terminx 655
{
656
    if (basepaltable[id] == NULL)
657
        basepaltable[id] = (uint8_t *) Xmalloc(768);
658
 
659
    Bmemcpy(basepaltable[id], table, 768);
6776 pogokeen 660
 
6915 pogokeen 661
#ifdef USE_OPENGL
6932 pogokeen 662
    if (videoGetRenderMode() >= REND_POLYMOST)
6919 pogokeen 663
    {
664
        uploadbasepalette(id);
665
    }
6915 pogokeen 666
#endif
5789 terminx 667
}
6829 terminx 668
 
669
void paletteFreeColorTable(int32_t const id)
5789 terminx 670
{
671
    if (id == 0)
672
        Bmemset(basepaltable[id], 0, 768);
673
    else
674
        DO_FREE_AND_NULL(basepaltable[id]);
675
}
676
 
677
//
678
// setbrightness
679
//
680
// flags:
681
//  1: don't setpalette(),  DON'T USE THIS FLAG!
682
//  2: don't gltexinvalidateall()
683
//  4: don't calc curbrightness from dabrightness,  DON'T USE THIS FLAG!
684
//  8: don't gltexinvalidate8()
685
// 16: don't reset palfade*
6829 terminx 686
void videoSetPalette(char dabrightness, uint8_t dapalid, uint8_t flags)
5789 terminx 687
{
5884 terminx 688
    int32_t i, j;
5789 terminx 689
    const uint8_t *dapal;
690
 
691
#ifdef USE_OPENGL
692
    int32_t paldidchange;
693
#endif
694
    int32_t palsumdidchange;
695
    //    uint32_t lastbright = curbrightness;
696
 
8300 hendricks2 697
    // Bassert((flags&4)==0); // What is so bad about this flag?
5789 terminx 698
 
699
    if (/*(unsigned)dapalid >= MAXBASEPALS ||*/ basepaltable[dapalid] == NULL)
700
        dapalid = 0;
701
#ifdef USE_OPENGL
702
    paldidchange = (curbasepal != dapalid || basepalreset);
703
#endif
704
    curbasepal = dapalid;
705
    basepalreset = 0;
706
 
707
    dapal = basepaltable[curbasepal];
708
 
709
    if (!(flags&4))
710
    {
711
        curbrightness = clamp(dabrightness, 0, 15);
712
        //        if (lastbright != (unsigned)curbrightness)
713
        //            vid_gamma = 1.0 + ((float)curbrightness / 10.0);
714
    }
715
 
6828 terminx 716
    videoSetGamma();
5884 terminx 717
    j = !gammabrightness ? curbrightness : 0;
5789 terminx 718
 
719
    for (i=0; i<256; i++)
720
    {
721
        // save palette without any brightness adjustment
722
        curpalette[i].r = dapal[i*3+0];
723
        curpalette[i].g = dapal[i*3+1];
724
        curpalette[i].b = dapal[i*3+2];
725
        curpalette[i].f = 0;
726
 
727
        // brightness adjust the palette
728
        curpalettefaded[i].b = britable[j][curpalette[i].b];
729
        curpalettefaded[i].g = britable[j][curpalette[i].g];
730
        curpalettefaded[i].r = britable[j][curpalette[i].r];
731
        curpalettefaded[i].f = 0;
732
    }
733
 
734
    if ((flags&16) && palfadedelta)  // keep the fade
6829 terminx 735
        paletteSetFade(palfadedelta>>2);
5789 terminx 736
 
6829 terminx 737
    static uint32_t lastpalettesum=0;
738
    uint32_t newpalettesum = XXH32((uint8_t *) curpalettefaded, sizeof(curpalettefaded), sizeof(curpalettefaded));
5789 terminx 739
 
6829 terminx 740
    palsumdidchange = (newpalettesum != lastpalettesum);
5789 terminx 741
 
6829 terminx 742
    if (palsumdidchange || newpalettesum != g_lastpalettesum)
743
    {
744
        //            if ((flags&1) == 0)
745
        videoUpdatePalette(0, 256);
5789 terminx 746
    }
747
 
6829 terminx 748
    g_lastpalettesum = lastpalettesum = newpalettesum;
749
 
5789 terminx 750
#ifdef USE_OPENGL
6829 terminx 751
    if (videoGetRenderMode() >= REND_POLYMOST)
5789 terminx 752
    {
753
        // Only reset the textures if the corresponding preserve flags are clear and
754
        // either (a) the new palette is different to the last, or (b) the brightness
755
        // changed and we couldn't set it using hardware gamma.
756
 
757
        // XXX: no-HW-gamma OpenGL platforms will exhibit bad performance with
758
        // simultaneous basepal and tint changes?
5884 terminx 759
        const int32_t doinvalidate = (paldidchange || (palsumdidchange && !gammabrightness));
5789 terminx 760
 
761
        if (!(flags&2) && doinvalidate)
6776 pogokeen 762
            gltexinvalidatetype(INVALIDATE_ALL_NON_INDEXED);
5789 terminx 763
        if (!(flags&8) && doinvalidate)
6776 pogokeen 764
            gltexinvalidatetype(INVALIDATE_ART_NON_INDEXED);
5789 terminx 765
#ifdef POLYMER
6829 terminx 766
        if ((videoGetRenderMode() == REND_POLYMER) && doinvalidate)
5789 terminx 767
            polymer_texinvalidate();
768
#endif
769
    }
770
#endif
771
 
772
    if ((flags&16)==0)
773
    {
774
        palfadergb.r = palfadergb.g = palfadergb.b = 0;
775
        palfadedelta = 0;
776
    }
777
}
778
 
6829 terminx 779
palette_t paletteGetColor(int32_t col)
5789 terminx 780
{
781
    if (!gammabrightness)
782
    {
5884 terminx 783
        palette_t const p = { britable[curbrightness][curpalette[col].r], britable[curbrightness][curpalette[col].g],
784
                              britable[curbrightness][curpalette[col].b], 0 };
5789 terminx 785
        return p;
786
    }
787
 
788
    return curpalette[col];
789
}
790
 
6829 terminx 791
static void paletteSetFade(uint8_t offset)
5789 terminx 792
{
6829 terminx 793
    for (native_t i=0; i<256; i++)
5789 terminx 794
    {
6829 terminx 795
        palette_t const p = paletteGetColor(i);
5789 terminx 796
 
6829 terminx 797
        curpalettefaded[i].b = p.b + (((palfadergb.b - p.b) * offset) >> 8);
798
        curpalettefaded[i].g = p.g + (((palfadergb.g - p.g) * offset) >> 8);
799
        curpalettefaded[i].r = p.r + (((palfadergb.r - p.r) * offset) >> 8);
5789 terminx 800
        curpalettefaded[i].f = 0;
801
    }
802
}
803
 
804
//#define DEBUG_PALETTEFADE
805
 
806
//
807
// setpalettefade
808
//
6829 terminx 809
void videoFadePalette(uint8_t r, uint8_t g, uint8_t b, uint8_t offset)
5789 terminx 810
{
811
    palfadergb.r = r;
812
    palfadergb.g = g;
813
    palfadergb.b = b;
814
#ifdef DEBUG_PALETTEFADE
815
    if (offset)
816
        offset = max(offset, 128);
817
#endif
818
    palfadedelta = offset;
819
 
6829 terminx 820
    paletteSetFade(offset);
5789 terminx 821
 
6829 terminx 822
    static uint32_t lastpalettesum=0;
823
    uint32_t newpalettesum = XXH32((uint8_t *) curpalettefaded, sizeof(curpalettefaded), sizeof(curpalettefaded));
5789 terminx 824
 
6829 terminx 825
    if (newpalettesum != lastpalettesum || newpalettesum != g_lastpalettesum)
6932 pogokeen 826
    {
6829 terminx 827
        videoUpdatePalette(0, 256);
6932 pogokeen 828
    }
5789 terminx 829
 
6829 terminx 830
    g_lastpalettesum = lastpalettesum = newpalettesum;
5789 terminx 831
}