allow shirt/pants/scoreboard colors to be pulled from an extra palette file (r_colorm...
[divverent/darkplaces.git] / palette.c
1
2 #include "quakedef.h"
3
4 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"};
5
6 unsigned int palette_complete[256];
7 unsigned int palette_font[256];
8 unsigned int palette_alpha[256];
9 unsigned int palette_nocolormap[256];
10 unsigned int palette_nocolormapnofullbrights[256];
11 unsigned int palette_nofullbrights[256];
12 unsigned int palette_onlyfullbrights[256];
13 unsigned int palette_pantsaswhite[256];
14 unsigned int palette_shirtaswhite[256];
15 unsigned int palette_transparent[256];
16 unsigned int palette_pantscolormap[16];
17 unsigned int palette_shirtcolormap[16];
18 unsigned int palette_pantsscoreboard[16];
19 unsigned int palette_shirtscoreboard[16];
20
21 // 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.
22 unsigned char host_quakepal[768] =
23 {
24 // marked: colormap colors: cb = (e->render.colormap & 0xF0);cb += (cb >= 128 && cb < 224) ? 4 : 12;
25 // 0x0*
26         0,0,0,        15,15,15,     31,31,31,     47,47,47,     63,63,63,     75,75,75,     91,91,91,     107,107,107,
27         123,123,123,  139,139,139,  155,155,155,  171,171,171,  187,187,187,  203,203,203,  219,219,219,  235,235,235,
28 // 0x1*                                                   0 ^
29         15,11,7,      23,15,11,     31,23,11,     39,27,15,     47,35,19,     55,43,23,     63,47,23,     75,55,27,
30         83,59,27,     91,67,31,     99,75,31,     107,83,31,    115,87,31,    123,95,35,    131,103,35,   143,111,35,
31 // 0x2*                                                   1 ^
32         11,11,15,     19,19,27,     27,27,39,     39,39,51,     47,47,63,     55,55,75,     63,63,87,     71,71,103,
33         79,79,115,    91,91,127,    99,99,139,    107,107,151,  115,115,163,  123,123,175,  131,131,187,  139,139,203,
34 // 0x3*                                                   2 ^
35         0,0,0,        7,7,0,        11,11,0,      19,19,0,      27,27,0,      35,35,0,      43,43,7,      47,47,7,
36         55,55,7,      63,63,7,      71,71,7,      75,75,11,     83,83,11,     91,91,11,     99,99,11,     107,107,15,
37 // 0x4*                                                   3 ^
38         7,0,0,        15,0,0,       23,0,0,       31,0,0,       39,0,0,       47,0,0,       55,0,0,       63,0,0,
39         71,0,0,       79,0,0,       87,0,0,       95,0,0,       103,0,0,      111,0,0,      119,0,0,      127,0,0,
40 // 0x5*                                                   4 ^
41         19,19,0,      27,27,0,      35,35,0,      47,43,0,      55,47,0,      67,55,0,      75,59,7,      87,67,7,
42         95,71,7,      107,75,11,    119,83,15,    131,87,19,    139,91,19,    151,95,27,    163,99,31,    175,103,35,
43 // 0x6*                                                   5 ^
44         35,19,7,      47,23,11,     59,31,15,     75,35,19,     87,43,23,     99,47,31,     115,55,35,    127,59,43,
45         143,67,51,    159,79,51,    175,99,47,    191,119,47,   207,143,43,   223,171,39,   239,203,31,   255,243,27,
46 // 0x7*                                                   6 ^
47         11,7,0,       27,19,0,      43,35,15,     55,43,19,     71,51,27,     83,55,35,     99,63,43,     111,71,51,
48         127,83,63,    139,95,71,    155,107,83,   167,123,95,   183,135,107,  195,147,123,  211,163,139,  227,179,151,
49 // 0x8*                                                   7 ^        v 8
50         171,139,163,  159,127,151,  147,115,135,  139,103,123,  127,91,111,   119,83,99,    107,75,87,    95,63,75,
51         87,55,67,     75,47,55,     67,39,47,     55,31,35,     43,23,27,     35,19,19,     23,11,11,     15,7,7,
52 // 0x9*                                                   9 v
53         187,115,159,  175,107,143,  163,95,131,   151,87,119,   139,79,107,   127,75,95,    115,67,83,    107,59,75,
54         95,51,63,     83,43,55,     71,35,43,     59,31,35,     47,23,27,     35,19,19,     23,11,11,     15,7,7,
55 // 0xA*                                                  10 v
56         219,195,187,  203,179,167,  191,163,155,  175,151,139,  163,135,123,  151,123,111,  135,111,95,   123,99,83,
57         107,87,71,    95,75,59,     83,63,51,     67,51,39,     55,43,31,     39,31,23,     27,19,15,     15,11,7,
58 // 0xB*                                                  11 v
59         111,131,123,  103,123,111,  95,115,103,   87,107,95,    79,99,87,     71,91,79,     63,83,71,     55,75,63,
60         47,67,55,     43,59,47,     35,51,39,     31,43,31,     23,35,23,     15,27,19,     11,19,11,     7,11,7,
61 // 0xC*                                                  12 v
62         255,243,27,   239,223,23,   219,203,19,   203,183,15,   187,167,15,   171,151,11,   155,131,7,    139,115,7,
63         123,99,7,     107,83,0,     91,71,0,      75,55,0,      59,43,0,      43,31,0,      27,15,0,      11,7,0,
64 // 0xD*                                                  13 v
65         0,0,255,      11,11,239,    19,19,223,    27,27,207,    35,35,191,    43,43,175,    47,47,159,    47,47,143,
66         47,47,127,    47,47,111,    47,47,95,     43,43,79,     35,35,63,     27,27,47,     19,19,31,     11,11,15,
67 // 0xE*
68         43,0,0,       59,0,0,       75,7,0,       95,7,0,       111,15,0,     127,23,7,     147,31,7,     163,39,11,
69         183,51,15,    195,75,27,    207,99,43,    219,127,59,   227,151,79,   231,171,95,   239,191,119,  247,211,139,
70 // 0xF*                                                  14 ^
71         167,123,59,   183,155,55,   199,195,55,   231,227,87,   127,191,255,  171,231,255,  215,255,255,  103,0,0,
72         139,0,0,      179,0,0,      215,0,0,      255,0,0,      255,243,147,  255,247,199,  255,255,255,  159,91,83
73 }; //                                                    15 ^
74
75 void Palette_SetupSpecialPalettes(void)
76 {
77         int i;
78         int fullbright_start, fullbright_end;
79         int pants_start, pants_end;
80         int shirt_start, shirt_end;
81         int reversed_start, reversed_end;
82         int transparentcolor;
83         unsigned char *colormap;
84         fs_offset_t filesize;
85
86         colormap = FS_LoadFile("gfx/colormap.lmp", tempmempool, true, &filesize);
87         if (colormap && filesize >= 16385)
88                 fullbright_start = 256 - colormap[16384];
89         else
90                 fullbright_start = 256;
91         if (colormap)
92                 Mem_Free(colormap);
93         fullbright_end = 256;
94         pants_start = 96;
95         pants_end = 112;
96         shirt_start = 16;
97         shirt_end = 32;
98         reversed_start = 128;
99         reversed_end = 224;
100         transparentcolor = 255;
101
102         for (i = 0;i < 256;i++)
103                 palette_transparent[i] = palette_complete[i];
104         palette_transparent[transparentcolor] = 0;
105
106         for (i = 0;i < fullbright_start;i++)
107                 palette_nofullbrights[i] = palette_complete[i];
108         for (i = fullbright_start;i < fullbright_end;i++)
109                 palette_nofullbrights[i] = palette_complete[0];
110
111         for (i = 0;i < 256;i++)
112                 palette_onlyfullbrights[i] = palette_complete[0];
113         for (i = fullbright_start;i < fullbright_end;i++)
114                 palette_onlyfullbrights[i] = palette_complete[i];
115
116         for (i = 0;i < 256;i++)
117                 palette_nocolormapnofullbrights[i] = palette_complete[i];
118         for (i = pants_start;i < pants_end;i++)
119                 palette_nocolormapnofullbrights[i] = palette_complete[0];
120         for (i = shirt_start;i < shirt_end;i++)
121                 palette_nocolormapnofullbrights[i] = palette_complete[0];
122         for (i = fullbright_start;i < fullbright_end;i++)
123                 palette_nocolormapnofullbrights[i] = palette_complete[0];
124
125         for (i = 0;i < 256;i++)
126                 palette_nocolormap[i] = palette_complete[i];
127         for (i = pants_start;i < pants_end;i++)
128                 palette_nocolormap[i] = palette_complete[0];
129         for (i = shirt_start;i < shirt_end;i++)
130                 palette_nocolormap[i] = palette_complete[0];
131
132         for (i = 0;i < 256;i++)
133                 palette_pantsaswhite[i] = palette_complete[0];
134         for (i = pants_start;i < pants_end;i++)
135         {
136                 if (i >= reversed_start && i < reversed_end)
137                         palette_pantsaswhite[i] = palette_complete[15 - (i - pants_start)];
138                 else
139                         palette_pantsaswhite[i] = palette_complete[i - pants_start];
140         }
141
142         for (i = 0;i < 256;i++)
143                 palette_shirtaswhite[i] = palette_complete[0];
144         for (i = shirt_start;i < shirt_end;i++)
145         {
146                 if (i >= reversed_start && i < reversed_end)
147                         palette_shirtaswhite[i] = palette_complete[15 - (i - shirt_start)];
148                 else
149                         palette_shirtaswhite[i] = palette_complete[i - shirt_start];
150         }
151
152         for (i = 0;i < 256;i++)
153                 palette_alpha[i] = 0xFFFFFFFF;
154         palette_alpha[transparentcolor] = 0;
155
156         for (i = 0;i < 256;i++)
157                 palette_font[i] = palette_complete[i];
158         palette_font[0] = 0;
159 }
160
161 void BuildGammaTable8(float prescale, float gamma, float scale, float base, unsigned char *out, int rampsize)
162 {
163         int i, adjusted;
164         double invgamma;
165
166         invgamma = 1.0 / gamma;
167         prescale /= (double) (rampsize - 1);
168         for (i = 0;i < rampsize;i++)
169         {
170                 adjusted = (int) (255.0 * (pow((double) i * prescale, invgamma) * scale + base) + 0.5);
171                 out[i] = bound(0, adjusted, 255);
172         }
173 }
174
175 void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out, int rampsize)
176 {
177         int i, adjusted;
178         double invgamma;
179
180         invgamma = 1.0 / gamma;
181         prescale /= (double) (rampsize - 1);
182         for (i = 0;i < rampsize;i++)
183         {
184                 adjusted = (int) (65535.0 * (pow((double) i * prescale, invgamma) * scale + base) + 0.5);
185                 out[i] = bound(0, adjusted, 65535);
186         }
187 }
188
189 void Palette_Shutdown(void)
190 {
191 }
192
193 void Palette_NewMap(void)
194 {
195 }
196
197 void Palette_Load(void)
198 {
199         int i;
200         float gamma, scale, base;
201         fs_offset_t filesize;
202         unsigned char *in, *out, *palfile;
203         unsigned char texturegammaramp[256];
204
205         gamma = 1;
206         scale = 1;
207         base = 0;
208 // COMMANDLINEOPTION: Client: -texgamma <number> sets the quake palette gamma, allowing you to make quake textures brighter/darker, not recommended
209         i = COM_CheckParm("-texgamma");
210         if (i)
211                 gamma = atof(com_argv[i + 1]);
212 // COMMANDLINEOPTION: Client: -texcontrast <number> sets the quake palette contrast, allowing you to make quake textures brighter/darker, not recommended
213         i = COM_CheckParm("-texcontrast");
214         if (i)
215                 scale = atof(com_argv[i + 1]);
216 // COMMANDLINEOPTION: Client: -texbrightness <number> sets the quake palette brightness (brightness of black), allowing you to make quake textures brighter/darker, not recommended
217         i = COM_CheckParm("-texbrightness");
218         if (i)
219                 base = atof(com_argv[i + 1]);
220         gamma = bound(0.01, gamma, 10.0);
221         scale = bound(0.01, scale, 10.0);
222         base = bound(0, base, 0.95);
223
224         BuildGammaTable8(1.0f, gamma, scale, base, texturegammaramp, 256);
225
226         palfile = (unsigned char *)FS_LoadFile ("gfx/palette.lmp", tempmempool, false, &filesize);
227         if (palfile && filesize >= 768)
228                 in = palfile;
229         else
230         {
231                 Con_DPrint("Couldn't load gfx/palette.lmp, falling back on internal palette\n");
232                 in = host_quakepal;
233         }
234         out = (unsigned char *) palette_complete; // palette is accessed as 32bit for speed reasons, but is created as 8bit bytes
235         for (i = 0;i < 256;i++)
236         {
237                 *out++ = texturegammaramp[*in++];
238                 *out++ = texturegammaramp[*in++];
239                 *out++ = texturegammaramp[*in++];
240                 *out++ = 255;
241         }
242         if (palfile)
243                 Mem_Free(palfile);
244
245 if(*r_colormap_palette.string)
246         palfile = (unsigned char *)FS_LoadFile (r_colormap_palette.string, tempmempool, false, &filesize);
247 else
248         palfile = NULL;
249
250 in = palfile;
251 if (palfile && filesize >= 48)
252 {
253         out = (unsigned char *) palette_shirtcolormap;
254         for (i = 0;i < 16;i++)
255         {
256                 *out++ = texturegammaramp[*in++];
257                 *out++ = texturegammaramp[*in++];
258                 *out++ = texturegammaramp[*in++];
259                 *out++ = 255;
260         }
261 }
262 else
263         for(i = 0; i < 16; ++i)
264                 palette_shirtcolormap[i] = palette_complete[(i << 4) | ((i >= 8 && i <= 13) ? 0x04 : 0x0C)];
265
266 if(palfile && filesize >= 48 + 48)
267 {
268         out = (unsigned char *) palette_shirtscoreboard;
269         for (i = 0;i < 16;i++)
270         {
271                 *out++ = texturegammaramp[*in++];
272                 *out++ = texturegammaramp[*in++];
273                 *out++ = texturegammaramp[*in++];
274                 *out++ = 255;
275         }
276 }
277 else
278         for(i = 0; i < 16; ++i)
279                 palette_shirtscoreboard[i] = palette_complete[(i << 4) | 0x08];
280
281 if (palfile && filesize >= 48 + 48 + 48)
282 {
283         out = (unsigned char *) palette_pantscolormap;
284         for (i = 0;i < 16;i++)
285         {
286                 *out++ = texturegammaramp[*in++];
287                 *out++ = texturegammaramp[*in++];
288                 *out++ = texturegammaramp[*in++];
289                 *out++ = 255;
290         }
291 }
292 else
293         memcpy(palette_pantscolormap, palette_shirtcolormap, sizeof(palette_pantscolormap));
294
295 if (palfile && filesize >= 48 + 48 + 48 + 48)
296 {
297         out = (unsigned char *) palette_pantsscoreboard;
298         for (i = 0;i < 16;i++)
299         {
300                 *out++ = texturegammaramp[*in++];
301                 *out++ = texturegammaramp[*in++];
302                 *out++ = texturegammaramp[*in++];
303                 *out++ = 255;
304         }
305 }
306 else
307         memcpy(palette_pantsscoreboard, palette_shirtscoreboard, sizeof(palette_pantsscoreboard));
308
309 if(palfile)
310         Mem_Free(palfile);
311
312         Palette_SetupSpecialPalettes();
313 }
314
315 void Palette_Init(void)
316 {
317         R_RegisterModule("Palette", Palette_Load, Palette_Shutdown, Palette_NewMap);
318         Cvar_RegisterVariable(&r_colormap_palette);
319         Palette_Load();
320 }