Subversion Repositories eduke32

Rev

Rev 7139 | Rev 7359 | 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
 
6061 hendricks2 19
uint8_t *basepaltable[MAXBASEPALS] = { palette };
5789 terminx 20
uint8_t basepalreset=1;
21
uint8_t curbasepal;
22
int32_t globalblend;
23
 
24
uint32_t g_lastpalettesum = 0;
25
palette_t curpalette[256];                      // the current palette, unadjusted for brightness or tint
26
palette_t curpalettefaded[256];         // the current palette, adjusted for brightness and tint (ie. what gets sent to the card)
6061 hendricks2 27
palette_t palfadergb = { 0, 0, 0, 0 };
5789 terminx 28
char palfadedelta = 0;
29
uint8_t blackcol;
30
 
6514 hendricks2 31
int32_t realmaxshade;
32
float frealmaxshade;
33
 
5789 terminx 34
#if defined(USE_OPENGL)
35
palette_t palookupfog[MAXPALOOKUPS];
36
#endif
37
 
38
// For every pal number, whether tsprite pal should not be taken over from
39
// floor pal.
40
// NOTE: g_noFloorPal[0] is irrelevant as it's never checked.
41
int8_t g_noFloorPal[MAXPALOOKUPS];
42
 
43
int32_t curbrightness = 0, gammabrightness = 0;
44
 
6829 terminx 45
static void paletteSetFade(uint8_t offset);
5789 terminx 46
 
6827 terminx 47
#ifdef USE_OPENGL
48
void fullscreen_tint_gl(uint8_t r, uint8_t g, uint8_t b, uint8_t f)
49
{
50
    glMatrixMode(GL_PROJECTION);
51
    glPushMatrix();
52
    glLoadIdentity();
53
    glMatrixMode(GL_MODELVIEW);
54
    glPushMatrix();
55
    glLoadIdentity();
56
 
57
    glDisable(GL_DEPTH_TEST);
58
    glDisable(GL_ALPHA_TEST);
59
    glDisable(GL_TEXTURE_2D);
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
{
146
    initfastcolorlookup_scale(30, 59, 11);
147
    initfastcolorlookup_gridvectors();
148
 
5888 hendricks2 149
#ifdef USE_OPENGL
7130 terminx 150
    for (auto & x : glblend)
151
        x = defaultglblend;
5888 hendricks2 152
#endif
153
 
5789 terminx 154
    int32_t fil;
155
    if ((fil = kopen4load("palette.dat", 0)) == -1)
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
 
167
    initfastcolorlookup_palette(palette);
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.
240
            if (kread_and_test(fil, &transluc[256*i + i], 256-i-1))
241
                return kclose(fil);
242
 
243
            // Duplicate the entries below the diagonal.
5829 terminx 244
            for (bssize_t j=0; j<i; j++)
5789 terminx 245
                transluc[256*i + j] = transluc[256*j + i];
246
        }
247
    }
248
    else
249
    {
250
        if (kread_and_test(fil, transluc, 65536))
251
            return kclose(fil);
252
    }
253
 
254
    paletteloaded |= PALETTE_TRANSLUC;
255
 
256
 
257
    // additional blending tables
258
 
259
    uint8_t magic[12];
260
    if (!kread_and_test(fil, magic, sizeof(magic)) && !Bmemcmp(magic, "MoreBlendTab", sizeof(magic)))
261
    {
262
        uint8_t addblendtabs;
263
        if (kread_and_test(fil, &addblendtabs, 1))
264
        {
265
            initprintf("Warning: failed reading additional blending table count\n");
266
            return kclose(fil);
267
        }
268
 
269
        uint8_t blendnum;
270
        char *tab = (char *) Xmalloc(256*256);
5829 terminx 271
        for (bssize_t i=0; i<addblendtabs; i++)
5789 terminx 272
        {
273
            if (kread_and_test(fil, &blendnum, 1))
274
            {
275
                initprintf("Warning: failed reading additional blending table index\n");
276
                Bfree(tab);
277
                return kclose(fil);
278
            }
279
 
6829 terminx 280
            if (paletteGetBlendTable(blendnum) != NULL)
5789 terminx 281
                initprintf("Warning: duplicate blending table index %3d encountered\n", blendnum);
282
 
283
            if (kread_and_test(fil, tab, 256*256))
284
            {
285
                initprintf("Warning: failed reading additional blending table\n");
286
                Bfree(tab);
287
                return kclose(fil);
288
            }
289
 
6829 terminx 290
            paletteSetBlendTable(blendnum, tab);
5789 terminx 291
        }
292
        Bfree(tab);
293
 
294
        // Read log2 of count of alpha blending tables.
295
        uint8_t lognumalphatabs;
296
        if (!kread_and_test(fil, &lognumalphatabs, 1))
297
        {
298
            if (!(lognumalphatabs >= 1 && lognumalphatabs <= 7))
299
                initprintf("invalid lognumalphatabs value, must be in [1 .. 7]\n");
300
            else
301
                numalphatabs = 1<<lognumalphatabs;
302
        }
303
    }
304
 
305
    kclose(fil);
306
}
307
 
308
uint32_t PaletteIndexFullbrights[8];
309
 
6829 terminx 310
void palettePostLoadTables(void)
5789 terminx 311
{
312
    globalpal = 0;
5872 hendricks2 313
 
5789 terminx 314
    globalpalwritten = palookup[0];
315
    setpalookupaddress(globalpalwritten);
316
 
317
    fixtransluscence(FP_OFF(blendtable[0]));
318
 
6513 hendricks2 319
    char const * const palookup0 = palookup[0];
320
 
5789 terminx 321
#ifdef DEBUG_TILESIZY_512
322
    // Bump shade 1 by 16.
5829 terminx 323
    for (bssize_t i=256; i<512; i++)
6513 hendricks2 324
        palookup0[i] = palookup0[i+(16<<8)];
5789 terminx 325
#endif
326
 
6831 terminx 327
    blackcol = paletteGetClosestColor(0, 0, 0);
328
    whitecol = paletteGetClosestColor(255, 255, 255);
329
    redcol = paletteGetClosestColor(255, 0, 0);
5789 terminx 330
 
6461 terminx 331
    for (size_t i = 0; i<16; i++)
332
    {
333
        palette_t *edcol = (palette_t *) &vgapal16[4*i];
334
        editorcolors[i] = getclosestcol_lim(edcol->b, edcol->g, edcol->r, 239);
335
    }
336
 
5789 terminx 337
    // Bmemset(PaletteIndexFullbrights, 0, sizeof(PaletteIndexFullbrights));
5829 terminx 338
    for (bssize_t c = 0; c < 255; ++c) // skipping transparent color
5789 terminx 339
    {
6513 hendricks2 340
        uint8_t const index = palookup0[c];
341
        rgb24_t const & color = *(rgb24_t *)&palette[index*3];
5789 terminx 342
 
6513 hendricks2 343
        // don't consider #000000 fullbright
344
        if (EDUKE32_PREDICT_FALSE(color.r == 0 && color.g == 0 && color.b == 0))
345
            continue;
5789 terminx 346
 
6513 hendricks2 347
        for (size_t s = c + 256, s_end = 256*numshades; s < s_end; s += 256)
348
            if (EDUKE32_PREDICT_FALSE(palookup0[s] != index))
5789 terminx 349
                goto PostLoad_NotFullbright;
350
 
351
        SetPaletteIndexFullbright(c);
352
 
6513 hendricks2 353
        PostLoad_NotFullbright: ;
5789 terminx 354
    }
6514 hendricks2 355
 
6592 hendricks2 356
    if (realmaxshade == 0)
6514 hendricks2 357
    {
6592 hendricks2 358
        uint8_t const * const blackcolor = &palette[blackcol*3];
359
        size_t s;
360
        for (s = numshades < 2 ? 0 : numshades-2; s > 0; --s)
6514 hendricks2 361
        {
6592 hendricks2 362
            for (size_t c = s*256, c_end = c+255; c < c_end; ++c) // skipping transparent color
363
            {
364
                uint8_t const index = palookup0[c];
365
                uint8_t const * const color = &palette[index*3];
366
                if (!IsPaletteIndexFullbright(index) && memcmp(blackcolor, color, sizeof(rgb24_t)))
367
                    goto PostLoad_FoundShade;
368
            }
6514 hendricks2 369
        }
6592 hendricks2 370
        PostLoad_FoundShade: ;
371
        frealmaxshade = (float)(realmaxshade = s+1);
6514 hendricks2 372
    }
5789 terminx 373
}
374
 
6829 terminx 375
void paletteFixTranslucencyMask(void)
5789 terminx 376
{
7283 terminx 377
    for (auto thispalookup : palookup)
5789 terminx 378
    {
379
        if (thispalookup == NULL)
380
            continue;
381
 
5829 terminx 382
        for (bssize_t j=0; j<numshades; j++)
5789 terminx 383
            thispalookup[(j<<8) + 255] = 255;
384
    }
385
 
386
    // fix up translucency table so that transluc(255,x)
387
    // and transluc(x,255) is black instead of purple.
7283 terminx 388
    for (auto transluc : blendtable)
5789 terminx 389
    {
390
        if (transluc == NULL)
391
            continue;
392
 
5829 terminx 393
        for (bssize_t j=0; j<255; j++)
5789 terminx 394
        {
395
            transluc[(255<<8) + j] = transluc[(blackcol<<8) + j];
396
            transluc[255 + (j<<8)] = transluc[blackcol + (j<<8)];
397
        }
398
        transluc[(255<<8) + 255] = transluc[(blackcol<<8) + blackcol];
399
    }
400
}
401
 
402
// Load LOOKUP.DAT, which contains lookup tables and additional base palettes.
403
//
404
// <fp>: kopen4load file handle
405
//
406
// Returns:
407
//  - on success, 0
408
//  - on error, -1 (didn't read enough data)
409
//  - -2: error, we already wrote an error message ourselves
6829 terminx 410
int32_t paletteLoadLookupTable(int32_t fp)
5789 terminx 411
{
412
    uint8_t numlookups;
413
    char remapbuf[256];
414
 
415
    if (kread_and_test(fp, &numlookups, 1))
416
        return -1;
417
 
5829 terminx 418
    for (bssize_t j=0; j<numlookups; j++)
5789 terminx 419
    {
420
        uint8_t palnum;
421
 
422
        if (kread_and_test(fp, &palnum, 1))
423
            return -1;
424
 
425
        if (palnum >= 256-RESERVEDPALS)
426
        {
427
            initprintf("ERROR: attempt to load lookup at reserved pal %d\n", palnum);
428
            return -2;
429
        }
430
 
431
        if (kread_and_test(fp, remapbuf, 256))
432
            return -1;
433
 
6829 terminx 434
        paletteMakeLookupTable(palnum, remapbuf, 0, 0, 0, 0);
5789 terminx 435
    }
436
 
437
    return 0;
438
}
439
 
6829 terminx 440
void paletteSetupDefaultFog(void)
5789 terminx 441
{
442
    // Find a gap of four consecutive unused pal numbers to generate fog shade
443
    // tables.
5829 terminx 444
    for (bssize_t j=1; j<=255-3; j++)
5789 terminx 445
        if (!palookup[j] && !palookup[j+1] && !palookup[j+2] && !palookup[j+3])
446
        {
6829 terminx 447
            paletteMakeLookupTable(j, NULL, 60, 60, 60, 1);
448
            paletteMakeLookupTable(j+1, NULL, 60, 0, 0, 1);
449
            paletteMakeLookupTable(j+2, NULL, 0, 60, 0, 1);
450
            paletteMakeLookupTable(j+3, NULL, 0, 0, 60, 1);
5789 terminx 451
 
452
            break;
453
        }
454
}
455
 
6829 terminx 456
void palettePostLoadLookups(void)
5789 terminx 457
{
458
    // Alias remaining unused pal numbers to the base shade table.
5829 terminx 459
    for (bssize_t j=1; j<MAXPALOOKUPS; j++)
5789 terminx 460
    {
461
        // If an existing lookup is identical to #0, free it.
462
        if (palookup[j] && palookup[j] != palookup[0] && !Bmemcmp(palookup[0], palookup[j], 256*numshades))
6829 terminx 463
            paletteFreeLookupTable(j);
5789 terminx 464
 
465
        if (!palookup[j])
6829 terminx 466
            paletteMakeLookupTable(j, NULL, 0, 0, 0, 1);
5789 terminx 467
    }
468
}
469
 
470
static int32_t palookup_isdefault(int32_t palnum)  // KEEPINSYNC engine.lua
471
{
472
    return (palookup[palnum] == NULL || (palnum!=0 && palookup[palnum] == palookup[0]));
473
}
474
 
475
static void maybe_alloc_palookup(int32_t palnum)
476
{
477
    if (palookup_isdefault(palnum))
478
    {
479
        alloc_palookup(palnum);
480
        if (palookup[palnum] == NULL)
481
            Bexit(1);
482
    }
483
}
484
 
6829 terminx 485
void paletteSetBlendTable(int32_t blend, const char *tab)
5789 terminx 486
{
487
    if (blendtable[blend] == NULL)
488
        blendtable[blend] = (char *) Xmalloc(256*256);
489
 
490
    Bmemcpy(blendtable[blend], tab, 256*256);
491
}
6829 terminx 492
void paletteFreeBlendTable(int32_t const blend)
5789 terminx 493
{
494
    DO_FREE_AND_NULL(blendtable[blend]);
495
}
496
 
497
#ifdef LUNATIC
6829 terminx 498
const char *(paletteGetBlendTable) (int32_t blend)
5789 terminx 499
{
500
    return blendtable[blend];
501
}
502
#endif
503
 
5888 hendricks2 504
#ifdef USE_OPENGL
505
glblend_t const nullglblend =
506
{
507
    {
508
        { 1.f, BLENDFACTOR_ONE, BLENDFACTOR_ZERO, 0 },
509
        { 1.f, BLENDFACTOR_ONE, BLENDFACTOR_ZERO, 0 },
510
    },
511
};
512
glblend_t const defaultglblend =
513
{
514
    {
515
        { 2.f/3.f, BLENDFACTOR_SRC_ALPHA, BLENDFACTOR_ONE_MINUS_SRC_ALPHA, 0 },
516
        { 1.f/3.f, BLENDFACTOR_SRC_ALPHA, BLENDFACTOR_ONE_MINUS_SRC_ALPHA, 0 },
517
    },
518
};
519
 
520
glblend_t glblend[MAXBLENDTABS];
521
 
522
void handle_blend(uint8_t enable, uint8_t blend, uint8_t def)
523
{
524
    static GLenum const blendFuncTokens[NUMBLENDFACTORS] =
525
    {
526
        GL_ZERO,
527
        GL_ONE,
528
        GL_SRC_COLOR,
529
        GL_ONE_MINUS_SRC_COLOR,
530
        GL_SRC_ALPHA,
531
        GL_ONE_MINUS_SRC_ALPHA,
532
        GL_DST_ALPHA,
533
        GL_ONE_MINUS_DST_ALPHA,
534
        GL_DST_COLOR,
535
        GL_ONE_MINUS_DST_COLOR,
536
    };
537
 
538
    if (!enable)
539
    {
6656 pogokeen 540
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
5888 hendricks2 541
        return;
542
    }
543
 
544
    glblenddef_t const * const glbdef = glblend[blend].def + def;
6656 pogokeen 545
    glBlendFunc(blendFuncTokens[glbdef->src], blendFuncTokens[glbdef->dst]);
5888 hendricks2 546
}
547
#endif
548
 
6829 terminx 549
int32_t paletteSetLookupTable(int32_t palnum, const uint8_t *shtab)
5789 terminx 550
{
551
    if (numshades != 32)
552
        return -1;
553
 
554
    if (shtab != NULL)
555
    {
556
        maybe_alloc_palookup(palnum);
557
        Bmemcpy(palookup[palnum], shtab, 256*numshades);
558
    }
559
 
560
    return 0;
561
}
6829 terminx 562
 
563
void paletteFreeLookupTable(int32_t const palnum)
5789 terminx 564
{
565
    if (palnum == 0 && palookup[palnum] != NULL)
566
    {
5829 terminx 567
        for (bssize_t i = 1; i < MAXPALOOKUPS; i++)
5789 terminx 568
            if (palookup[i] == palookup[palnum])
569
                palookup[i] = NULL;
570
 
6438 terminx 571
        ALIGNED_FREE_AND_NULL(palookup[palnum]);
5789 terminx 572
    }
573
    else if (palookup[palnum] == palookup[0])
574
        palookup[palnum] = NULL;
575
    else
6438 terminx 576
        ALIGNED_FREE_AND_NULL(palookup[palnum]);
5789 terminx 577
}
578
 
579
//
580
// makepalookup
581
//
6829 terminx 582
void paletteMakeLookupTable(int32_t palnum, const char *remapbuf, uint8_t r, uint8_t g, uint8_t b, char noFloorPal)
5789 terminx 583
{
584
    int32_t i, j;
585
 
6061 hendricks2 586
    static char idmap[256] = { 1 };
5789 terminx 587
 
588
    if (paletteloaded == 0)
589
        return;
590
 
591
    // NOTE: palnum==0 is allowed
592
    if ((unsigned) palnum >= MAXPALOOKUPS)
593
        return;
594
 
595
    g_noFloorPal[palnum] = noFloorPal;
596
 
597
    if (remapbuf==NULL)
598
    {
599
        if ((r|g|b) == 0)
600
        {
601
            palookup[palnum] = palookup[0];  // Alias to base shade table!
602
            return;
603
        }
604
 
605
        if (idmap[0]==1)  // init identity map
606
            for (i=0; i<256; i++)
607
                idmap[i] = i;
608
 
609
        remapbuf = idmap;
610
    }
611
 
612
    maybe_alloc_palookup(palnum);
613
 
614
    if ((r|g|b) == 0)
615
    {
616
        // "black fog"/visibility case -- only remap color indices
617
 
618
        for (j=0; j<numshades; j++)
619
            for (i=0; i<256; i++)
620
            {
621
                const char *src = palookup[0];
622
                palookup[palnum][256*j + i] = src[256*j + remapbuf[i]];
623
            }
624
    }
625
    else
626
    {
627
        // colored fog case
628
 
629
        char *ptr2 = palookup[palnum];
630
 
631
        for (i=0; i<numshades; i++)
632
        {
633
            int32_t palscale = divscale16(i, numshades-1);
634
 
635
            for (j=0; j<256; j++)
636
            {
637
                const char *ptr = (const char *) &palette[remapbuf[j]*3];
6831 terminx 638
                *ptr2++ = paletteGetClosestColor(ptr[0] + mulscale16(r-ptr[0], palscale),
5789 terminx 639
                    ptr[1] + mulscale16(g-ptr[1], palscale),
640
                    ptr[2] + mulscale16(b-ptr[2], palscale));
641
            }
642
        }
643
    }
644
 
645
#if defined(USE_OPENGL)
646
    palookupfog[palnum].r = r;
647
    palookupfog[palnum].g = g;
648
    palookupfog[palnum].b = b;
649
#endif
650
}
651
 
652
//
653
// setbasepal
654
//
6829 terminx 655
void paletteSetColorTable(int32_t id, uint8_t const * const table)
5789 terminx 656
{
657
    if (basepaltable[id] == NULL)
658
        basepaltable[id] = (uint8_t *) Xmalloc(768);
659
 
660
    Bmemcpy(basepaltable[id], table, 768);
6776 pogokeen 661
 
6915 pogokeen 662
#ifdef USE_OPENGL
6932 pogokeen 663
    if (videoGetRenderMode() >= REND_POLYMOST)
6919 pogokeen 664
    {
665
        uploadbasepalette(id);
666
    }
6915 pogokeen 667
#endif
5789 terminx 668
}
6829 terminx 669
 
670
void paletteFreeColorTable(int32_t const id)
5789 terminx 671
{
672
    if (id == 0)
673
        Bmemset(basepaltable[id], 0, 768);
674
    else
675
        DO_FREE_AND_NULL(basepaltable[id]);
676
}
677
 
678
//
679
// setbrightness
680
//
681
// flags:
682
//  1: don't setpalette(),  DON'T USE THIS FLAG!
683
//  2: don't gltexinvalidateall()
684
//  4: don't calc curbrightness from dabrightness,  DON'T USE THIS FLAG!
685
//  8: don't gltexinvalidate8()
686
// 16: don't reset palfade*
6829 terminx 687
void videoSetPalette(char dabrightness, uint8_t dapalid, uint8_t flags)
5789 terminx 688
{
5884 terminx 689
    int32_t i, j;
5789 terminx 690
    const uint8_t *dapal;
691
 
692
#ifdef USE_OPENGL
693
    int32_t paldidchange;
694
#endif
695
    int32_t palsumdidchange;
696
    //    uint32_t lastbright = curbrightness;
697
 
698
    Bassert((flags&4)==0);
699
 
700
    if (/*(unsigned)dapalid >= MAXBASEPALS ||*/ basepaltable[dapalid] == NULL)
701
        dapalid = 0;
702
#ifdef USE_OPENGL
703
    paldidchange = (curbasepal != dapalid || basepalreset);
704
#endif
705
    curbasepal = dapalid;
706
    basepalreset = 0;
707
 
708
    dapal = basepaltable[curbasepal];
709
 
710
    if (!(flags&4))
711
    {
712
        curbrightness = clamp(dabrightness, 0, 15);
713
        //        if (lastbright != (unsigned)curbrightness)
714
        //            vid_gamma = 1.0 + ((float)curbrightness / 10.0);
715
    }
716
 
6828 terminx 717
    videoSetGamma();
5884 terminx 718
    j = !gammabrightness ? curbrightness : 0;
5789 terminx 719
 
720
    for (i=0; i<256; i++)
721
    {
722
        // save palette without any brightness adjustment
723
        curpalette[i].r = dapal[i*3+0];
724
        curpalette[i].g = dapal[i*3+1];
725
        curpalette[i].b = dapal[i*3+2];
726
        curpalette[i].f = 0;
727
 
728
        // brightness adjust the palette
729
        curpalettefaded[i].b = britable[j][curpalette[i].b];
730
        curpalettefaded[i].g = britable[j][curpalette[i].g];
731
        curpalettefaded[i].r = britable[j][curpalette[i].r];
732
        curpalettefaded[i].f = 0;
733
    }
734
 
735
    if ((flags&16) && palfadedelta)  // keep the fade
6829 terminx 736
        paletteSetFade(palfadedelta>>2);
5789 terminx 737
 
6829 terminx 738
    static uint32_t lastpalettesum=0;
739
    uint32_t newpalettesum = XXH32((uint8_t *) curpalettefaded, sizeof(curpalettefaded), sizeof(curpalettefaded));
5789 terminx 740
 
6829 terminx 741
    palsumdidchange = (newpalettesum != lastpalettesum);
5789 terminx 742
 
6829 terminx 743
    if (palsumdidchange || newpalettesum != g_lastpalettesum)
744
    {
745
        //            if ((flags&1) == 0)
746
        videoUpdatePalette(0, 256);
5789 terminx 747
    }
748
 
6829 terminx 749
    g_lastpalettesum = lastpalettesum = newpalettesum;
750
 
5789 terminx 751
#ifdef USE_OPENGL
6829 terminx 752
    if (videoGetRenderMode() >= REND_POLYMOST)
5789 terminx 753
    {
754
        // Only reset the textures if the corresponding preserve flags are clear and
755
        // either (a) the new palette is different to the last, or (b) the brightness
756
        // changed and we couldn't set it using hardware gamma.
757
 
758
        // XXX: no-HW-gamma OpenGL platforms will exhibit bad performance with
759
        // simultaneous basepal and tint changes?
5884 terminx 760
        const int32_t doinvalidate = (paldidchange || (palsumdidchange && !gammabrightness));
5789 terminx 761
 
762
        if (!(flags&2) && doinvalidate)
6776 pogokeen 763
            gltexinvalidatetype(INVALIDATE_ALL_NON_INDEXED);
5789 terminx 764
        if (!(flags&8) && doinvalidate)
6776 pogokeen 765
            gltexinvalidatetype(INVALIDATE_ART_NON_INDEXED);
5789 terminx 766
#ifdef POLYMER
6829 terminx 767
        if ((videoGetRenderMode() == REND_POLYMER) && doinvalidate)
5789 terminx 768
            polymer_texinvalidate();
769
#endif
770
    }
771
#endif
772
 
773
    if ((flags&16)==0)
774
    {
775
        palfadergb.r = palfadergb.g = palfadergb.b = 0;
776
        palfadedelta = 0;
777
    }
778
}
779
 
6829 terminx 780
palette_t paletteGetColor(int32_t col)
5789 terminx 781
{
782
    if (!gammabrightness)
783
    {
5884 terminx 784
        palette_t const p = { britable[curbrightness][curpalette[col].r], britable[curbrightness][curpalette[col].g],
785
                              britable[curbrightness][curpalette[col].b], 0 };
5789 terminx 786
        return p;
787
    }
788
 
789
    return curpalette[col];
790
}
791
 
6829 terminx 792
static void paletteSetFade(uint8_t offset)
5789 terminx 793
{
6829 terminx 794
    for (native_t i=0; i<256; i++)
5789 terminx 795
    {
6829 terminx 796
        palette_t const p = paletteGetColor(i);
5789 terminx 797
 
6829 terminx 798
        curpalettefaded[i].b = p.b + (((palfadergb.b - p.b) * offset) >> 8);
799
        curpalettefaded[i].g = p.g + (((palfadergb.g - p.g) * offset) >> 8);
800
        curpalettefaded[i].r = p.r + (((palfadergb.r - p.r) * offset) >> 8);
5789 terminx 801
        curpalettefaded[i].f = 0;
802
    }
803
}
804
 
805
//#define DEBUG_PALETTEFADE
806
 
807
//
808
// setpalettefade
809
//
6829 terminx 810
void videoFadePalette(uint8_t r, uint8_t g, uint8_t b, uint8_t offset)
5789 terminx 811
{
812
    palfadergb.r = r;
813
    palfadergb.g = g;
814
    palfadergb.b = b;
815
#ifdef DEBUG_PALETTEFADE
816
    if (offset)
817
        offset = max(offset, 128);
818
#endif
819
    palfadedelta = offset;
820
 
6829 terminx 821
    paletteSetFade(offset);
5789 terminx 822
 
6829 terminx 823
    static uint32_t lastpalettesum=0;
824
    uint32_t newpalettesum = XXH32((uint8_t *) curpalettefaded, sizeof(curpalettefaded), sizeof(curpalettefaded));
5789 terminx 825
 
6829 terminx 826
    if (newpalettesum != lastpalettesum || newpalettesum != g_lastpalettesum)
6932 pogokeen 827
    {
6829 terminx 828
        videoUpdatePalette(0, 256);
6932 pogokeen 829
    }
5789 terminx 830
 
6829 terminx 831
    g_lastpalettesum = lastpalettesum = newpalettesum;
5789 terminx 832
}