]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - palette.c
Add an in_releaseall command for debugging/working around stuck keys.
[xonotic/darkplaces.git] / palette.c
index f87a834d5ee1d7d28cebc0d3eef484f390a15696..f9d68bf5eeac36becb16ade92747602aff0e8e81 100644 (file)
--- a/palette.c
+++ b/palette.c
@@ -1,21 +1,36 @@
 
 #include "quakedef.h"
+#include "image.h"
 
-unsigned int palette_complete[256];
-unsigned int palette_font[256];
-unsigned int palette_alpha[256];
-unsigned int palette_nocolormap[256];
-unsigned int palette_nocolormapnofullbrights[256];
-unsigned int palette_nofullbrights[256];
-unsigned int palette_onlyfullbrights[256];
-unsigned int palette_pantsaswhite[256];
-unsigned int palette_shirtaswhite[256];
-unsigned int palette_transparent[256];
+cvar_t r_colormap_palette = {0, "r_colormap_palette", "gfx/colormap_palette.lmp", "name of a palette lmp file to override the shirt/pants colors of player models. It consists of 16 shirt colors, 16 scoreboard shirt colors, 16 pants colors and 16 scoreboard pants colors"};
+
+unsigned char palette_rgb[256][3];
+unsigned char palette_rgb_pantscolormap[16][3];
+unsigned char palette_rgb_shirtcolormap[16][3];
+unsigned char palette_rgb_pantsscoreboard[16][3];
+unsigned char palette_rgb_shirtscoreboard[16][3];
+
+unsigned int palette_bgra_complete[256];
+unsigned int palette_bgra_font[256];
+unsigned int palette_bgra_alpha[256];
+unsigned int palette_bgra_nocolormap[256];
+unsigned int palette_bgra_nocolormapnofullbrights[256];
+unsigned int palette_bgra_nofullbrights[256];
+unsigned int palette_bgra_nofullbrights_transparent[256];
+unsigned int palette_bgra_onlyfullbrights[256];
+unsigned int palette_bgra_onlyfullbrights_transparent[256];
+unsigned int palette_bgra_pantsaswhite[256];
+unsigned int palette_bgra_shirtaswhite[256];
+unsigned int palette_bgra_transparent[256];
+unsigned int palette_bgra_embeddedpic[256];
+unsigned char palette_featureflags[256];
+
+unsigned int q2palette_bgra_complete[256];
 
 // John Carmack said the quake palette.lmp can be considered public domain because it is not an important asset to id, so I include it here as a fallback if no external palette file is found.
 unsigned char host_quakepal[768] =
 {
-// marked: colormap colors: cb = (e->render.colormap & 0xF0);cb += (cb >= 128 && cb < 224) ? 4 : 12;
+// marked: colormap colors: cb = (colormap & 0xF0);cb += (cb >= 128 && cb < 224) ? 4 : 12;
 // 0x0*
        0,0,0,        15,15,15,     31,31,31,     47,47,47,     63,63,63,     75,75,75,     91,91,91,     107,107,107,
        123,123,123,  139,139,139,  155,155,155,  171,171,171,  187,187,187,  203,203,203,  219,219,219,  235,235,235,
@@ -66,7 +81,7 @@ unsigned char host_quakepal[768] =
        139,0,0,      179,0,0,      215,0,0,      255,0,0,      255,243,147,  255,247,199,  255,255,255,  159,91,83
 }; //                                                    15 ^
 
-void Palette_SetupSpecialPalettes(void)
+static void Palette_SetupSpecialPalettes(void)
 {
        int i;
        int fullbright_start, fullbright_end;
@@ -76,6 +91,12 @@ void Palette_SetupSpecialPalettes(void)
        int transparentcolor;
        unsigned char *colormap;
        fs_offset_t filesize;
+       union
+       {
+               int i;
+               unsigned char b[4];
+       }
+       u;
 
        colormap = FS_LoadFile("gfx/colormap.lmp", tempmempool, true, &filesize);
        if (colormap && filesize >= 16385)
@@ -94,99 +115,167 @@ void Palette_SetupSpecialPalettes(void)
        transparentcolor = 255;
 
        for (i = 0;i < 256;i++)
-               palette_transparent[i] = palette_complete[i];
-       palette_transparent[transparentcolor] = 0;
+               palette_featureflags[i] = PALETTEFEATURE_STANDARD;
+       for (i = reversed_start;i < reversed_end;i++)
+               palette_featureflags[i] = PALETTEFEATURE_REVERSED;
+       for (i = pants_start;i < pants_end;i++)
+               palette_featureflags[i] = PALETTEFEATURE_PANTS;
+       for (i = shirt_start;i < shirt_end;i++)
+               palette_featureflags[i] = PALETTEFEATURE_SHIRT;
+       for (i = fullbright_start;i < fullbright_end;i++)
+               palette_featureflags[i] = PALETTEFEATURE_GLOW;
+       palette_featureflags[0] = PALETTEFEATURE_ZERO;
+       palette_featureflags[transparentcolor] = PALETTEFEATURE_TRANSPARENT;
+
+       for (i = 0;i < 256;i++)
+               palette_bgra_transparent[i] = palette_bgra_complete[i];
+       palette_bgra_transparent[transparentcolor] = 0;
 
        for (i = 0;i < fullbright_start;i++)
-               palette_nofullbrights[i] = palette_complete[i];
+               palette_bgra_nofullbrights[i] = palette_bgra_complete[i];
        for (i = fullbright_start;i < fullbright_end;i++)
-               palette_nofullbrights[i] = palette_complete[0];
+               palette_bgra_nofullbrights[i] = palette_bgra_complete[0];
+       for (i = 0;i < 256;i++)
+               palette_bgra_nofullbrights_transparent[i] = palette_bgra_nofullbrights[i];
+       palette_bgra_nofullbrights_transparent[transparentcolor] = 0;
 
        for (i = 0;i < 256;i++)
-               palette_onlyfullbrights[i] = palette_complete[0];
+               palette_bgra_onlyfullbrights[i] = 0;
        for (i = fullbright_start;i < fullbright_end;i++)
-               palette_onlyfullbrights[i] = palette_complete[i];
+               palette_bgra_onlyfullbrights[i] = palette_bgra_complete[i];
+       for (i = 0;i < 256;i++)
+               palette_bgra_onlyfullbrights_transparent[i] = palette_bgra_onlyfullbrights[i];
+       palette_bgra_onlyfullbrights_transparent[transparentcolor] = 0;
 
        for (i = 0;i < 256;i++)
-               palette_nocolormapnofullbrights[i] = palette_complete[i];
+               palette_bgra_nocolormapnofullbrights[i] = palette_bgra_complete[i];
        for (i = pants_start;i < pants_end;i++)
-               palette_nocolormapnofullbrights[i] = palette_complete[0];
+               palette_bgra_nocolormapnofullbrights[i] = 0;
        for (i = shirt_start;i < shirt_end;i++)
-               palette_nocolormapnofullbrights[i] = palette_complete[0];
+               palette_bgra_nocolormapnofullbrights[i] = 0;
        for (i = fullbright_start;i < fullbright_end;i++)
-               palette_nocolormapnofullbrights[i] = palette_complete[0];
+               palette_bgra_nocolormapnofullbrights[i] = 0;
 
        for (i = 0;i < 256;i++)
-               palette_nocolormap[i] = palette_complete[i];
+               palette_bgra_nocolormap[i] = palette_bgra_complete[i];
        for (i = pants_start;i < pants_end;i++)
-               palette_nocolormap[i] = palette_complete[0];
+               palette_bgra_nocolormap[i] = 0;
        for (i = shirt_start;i < shirt_end;i++)
-               palette_nocolormap[i] = palette_complete[0];
+               palette_bgra_nocolormap[i] = 0;
 
        for (i = 0;i < 256;i++)
-               palette_pantsaswhite[i] = palette_complete[0];
+               palette_bgra_pantsaswhite[i] = 0;
        for (i = pants_start;i < pants_end;i++)
        {
                if (i >= reversed_start && i < reversed_end)
-                       palette_pantsaswhite[i] = palette_complete[15 - (i - pants_start)];
+                       palette_bgra_pantsaswhite[i] = palette_bgra_complete[15 - (i - pants_start)];
                else
-                       palette_pantsaswhite[i] = palette_complete[i - pants_start];
+                       palette_bgra_pantsaswhite[i] = palette_bgra_complete[i - pants_start];
        }
 
        for (i = 0;i < 256;i++)
-               palette_shirtaswhite[i] = palette_complete[0];
+               palette_bgra_shirtaswhite[i] = 0;
        for (i = shirt_start;i < shirt_end;i++)
        {
                if (i >= reversed_start && i < reversed_end)
-                       palette_shirtaswhite[i] = palette_complete[15 - (i - shirt_start)];
+                       palette_bgra_shirtaswhite[i] = palette_bgra_complete[15 - (i - shirt_start)];
                else
-                       palette_shirtaswhite[i] = palette_complete[i - shirt_start];
+                       palette_bgra_shirtaswhite[i] = palette_bgra_complete[i - shirt_start];
        }
 
        for (i = 0;i < 256;i++)
-               palette_alpha[i] = 0xFFFFFFFF;
-       palette_alpha[transparentcolor] = 0;
+               palette_bgra_alpha[i] = 0xFFFFFFFF;
+       u.i = 0xFFFFFFFF;
+       u.b[3] = 0;
+       palette_bgra_alpha[transparentcolor] = u.i;
 
        for (i = 0;i < 256;i++)
-               palette_font[i] = palette_complete[i];
-       palette_font[0] = 0;
+               palette_bgra_font[i] = palette_bgra_complete[i];
+       palette_bgra_font[0] = 0;
 }
 
-void BuildGammaTable8(float prescale, float gamma, float scale, float base, unsigned char *out, int rampsize)
+static void Palette_LoadQ2Colormap(void)
+{
+       fs_offset_t filesize;
+       unsigned char * q2colormapfile = FS_LoadFile("pics/colormap.pcx", tempmempool, true, &filesize);
+       if (q2colormapfile && filesize >= 768)
+       {
+               unsigned char q2palette_rgb[256][3];
+               unsigned char *out = (unsigned char *) q2palette_bgra_complete; // palette is accessed as 32bit for speed reasons, but is created as 8bit bytes
+               int i;
+               LoadPCX_PaletteOnly(q2colormapfile, filesize, q2palette_rgb[0]);
+               // this stops at color 255 because it is a pink transparent color that we don't actually want to preserve color on.
+               for (i = 0;i < 255;i++)
+               {
+                       out[i*4+2] = q2palette_rgb[i][0];
+                       out[i*4+1] = q2palette_rgb[i][1];
+                       out[i*4+0] = q2palette_rgb[i][2];
+                       out[i*4+3] = 255;
+               }
+               Mem_Free(q2colormapfile);
+       }
+}
+
+void BuildGammaTable8(float prescale, float gamma, float scale, float base, float contrastboost, unsigned char *out, int rampsize)
 {
        int i, adjusted;
        double invgamma;
+       double t, d;
 
        invgamma = 1.0 / gamma;
        prescale /= (double) (rampsize - 1);
        for (i = 0;i < rampsize;i++)
        {
-               adjusted = (int) (255.0 * (pow((double) i * prescale, invgamma) * scale + base) + 0.5);
+               t = i * prescale;
+               d = ((contrastboost - 1) * t + 1);
+               if(d == 0)
+                       t = 0; // we could just as well assume 1 here, depending on which side of the division by zero we want to be
+               else
+                       t = contrastboost * t / d;
+               adjusted = (int) (255.0 * (pow(t, invgamma) * scale + base) + 0.5);
                out[i] = bound(0, adjusted, 255);
        }
 }
 
-void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out, int rampsize)
+void BuildGammaTable16(float prescale, float gamma, float scale, float base, float contrastboost, unsigned short *out, int rampsize)
 {
        int i, adjusted;
        double invgamma;
+       double t;
 
        invgamma = 1.0 / gamma;
        prescale /= (double) (rampsize - 1);
        for (i = 0;i < rampsize;i++)
        {
-               adjusted = (int) (65535.0 * (pow((double) i * prescale, invgamma) * scale + base) + 0.5);
+               t = i * prescale;
+               t = contrastboost * t / ((contrastboost - 1) * t + 1);
+               adjusted = (int) (65535.0 * (pow(t, invgamma) * scale + base) + 0.5);
                out[i] = bound(0, adjusted, 65535);
        }
 }
 
-void Palette_Init(void)
+static void Palette_Shutdown(void)
+{
+}
+
+static void Palette_NewMap(void)
+{
+}
+
+static void Palette_Load(void)
 {
        int i;
+       unsigned char *out;
        float gamma, scale, base;
        fs_offset_t filesize;
-       unsigned char *in, *out, *palfile;
+       unsigned char *palfile;
        unsigned char texturegammaramp[256];
+       union
+       {
+               unsigned char b[4];
+               unsigned int i;
+       }
+       bgra;
 
        gamma = 1;
        scale = 1;
@@ -207,27 +296,76 @@ void Palette_Init(void)
        scale = bound(0.01, scale, 10.0);
        base = bound(0, base, 0.95);
 
-       BuildGammaTable8(1.0f, gamma, scale, base, texturegammaramp, 256);
+       BuildGammaTable8(1.0f, gamma, scale, base, 1, texturegammaramp, 256);
 
        palfile = (unsigned char *)FS_LoadFile ("gfx/palette.lmp", tempmempool, false, &filesize);
        if (palfile && filesize >= 768)
-               in = palfile;
+               memcpy(palette_rgb, palfile, 768);
        else
        {
                Con_DPrint("Couldn't load gfx/palette.lmp, falling back on internal palette\n");
-               in = host_quakepal;
+               memcpy(palette_rgb, host_quakepal, 768);
        }
-       out = (unsigned char *) palette_complete; // palette is accessed as 32bit for speed reasons, but is created as 8bit bytes
+       if (palfile)
+               Mem_Free(palfile);
+
+       out = (unsigned char *) palette_bgra_complete; // palette is accessed as 32bit for speed reasons, but is created as 8bit bytes
        for (i = 0;i < 256;i++)
        {
-               *out++ = texturegammaramp[*in++];
-               *out++ = texturegammaramp[*in++];
-               *out++ = texturegammaramp[*in++];
-               *out++ = 255;
+               out[i*4+2] = texturegammaramp[palette_rgb[i][0]];
+               out[i*4+1] = texturegammaramp[palette_rgb[i][1]];
+               out[i*4+0] = texturegammaramp[palette_rgb[i][2]];
+               out[i*4+3] = 255;
        }
-       if (palfile)
+
+       if(*r_colormap_palette.string)
+               palfile = (unsigned char *)FS_LoadFile (r_colormap_palette.string, tempmempool, false, &filesize);
+       else
+               palfile = NULL;
+
+       if (palfile && filesize >= 48*2)
+       {
+               memcpy(palette_rgb_shirtcolormap[0], palfile, 48);
+               memcpy(palette_rgb_shirtscoreboard[0], palfile + 48, 48);
+       }
+       else
+       {
+               for(i = 0;i < 16;i++)
+               {
+                       VectorCopy(palette_rgb[(i << 4) | ((i >= 8 && i <= 13) ? 0x04 : 0x0C)], palette_rgb_shirtcolormap[i]);
+                       VectorCopy(palette_rgb[(i << 4) | 0x08], palette_rgb_shirtscoreboard[i]);
+               }
+       }
+
+       if (palfile && filesize >= 48*4)
+       {
+               memcpy(palette_rgb_pantscolormap[0], palfile + 48*2, 48);
+               memcpy(palette_rgb_pantsscoreboard[0], palfile + 48*3, 48);
+       }
+       else
+       {
+               memcpy(palette_rgb_pantscolormap, palette_rgb_shirtcolormap, sizeof(palette_rgb_pantscolormap));
+               memcpy(palette_rgb_pantsscoreboard, palette_rgb_shirtscoreboard, sizeof(palette_rgb_pantsscoreboard));
+       }
+
+       if(palfile)
                Mem_Free(palfile);
 
+       memset(palette_bgra_embeddedpic, 0, sizeof(palette_bgra_embeddedpic));
+       for (i = '1';i <= '7';i++)
+       {
+               Vector4Set(bgra.b, 255, 255, 255, (i - '0') * 255 / 7);
+               palette_bgra_embeddedpic[i] = bgra.i;
+       }
+
        Palette_SetupSpecialPalettes();
+
+       Palette_LoadQ2Colormap();
 }
 
+void Palette_Init(void)
+{
+       R_RegisterModule("Palette", Palette_Load, Palette_Shutdown, Palette_NewMap, NULL, NULL);
+       Cvar_RegisterVariable(&r_colormap_palette);
+       Palette_Load();
+}