added cl_screen.c/h (eventually most 2D stuff should be moved here)
[divverent/darkplaces.git] / wad.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // wad.c
21
22 #include "quakedef.h"
23
24 static int                      wad_numlumps;
25 static lumpinfo_t       *wad_lumps;
26 static byte                     *wad_base = NULL;
27 static mempool_t        *wad_mempool = NULL;
28
29 void SwapPic (qpic_t *pic);
30
31 /*
32 ==================
33 W_CleanupName
34
35 Lowercases name and pads with spaces and a terminating 0 to the length of
36 lumpinfo_t->name.
37 Used so lumpname lookups can proceed rapidly by comparing 4 chars at a time
38 Space padding is so names can be printed nicely in tables.
39 Can safely be performed in place.
40 ==================
41 */
42 static void W_CleanupName (char *in, char *out)
43 {
44         int             i;
45         int             c;
46
47         for (i=0 ; i<16 ; i++ )
48         {
49                 c = in[i];
50                 if (!c)
51                         break;
52
53                 if (c >= 'A' && c <= 'Z')
54                         c += ('a' - 'A');
55                 out[i] = c;
56         }
57
58         for ( ; i< 16 ; i++ )
59                 out[i] = 0;
60 }
61
62
63
64 /*
65 ====================
66 W_LoadWadFile
67 ====================
68 */
69 void W_LoadWadFile (char *filename)
70 {
71         lumpinfo_t              *lump_p;
72         wadinfo_t               *header;
73         unsigned                i;
74         int                             infotableofs;
75         void                    *temp;
76
77         temp = COM_LoadFile (filename, false);
78         if (!temp)
79                 Sys_Error ("W_LoadWadFile: couldn't load %s", filename);
80
81         if (wad_mempool)
82                 Mem_FreePool(&wad_mempool);
83         wad_mempool = Mem_AllocPool(filename);
84         wad_base = Mem_Alloc(wad_mempool, loadsize);
85
86         memcpy(wad_base, temp, loadsize);
87         Mem_Free(temp);
88
89         header = (wadinfo_t *)wad_base;
90
91         if (memcmp(header->identification, "WAD2", 4))
92                 Sys_Error ("Wad file %s doesn't have WAD2 id\n",filename);
93
94         wad_numlumps = LittleLong(header->numlumps);
95         infotableofs = LittleLong(header->infotableofs);
96         wad_lumps = (lumpinfo_t *)(wad_base + infotableofs);
97
98         for (i=0, lump_p = wad_lumps ; i<wad_numlumps ; i++,lump_p++)
99         {
100                 lump_p->filepos = LittleLong(lump_p->filepos);
101                 lump_p->size = LittleLong(lump_p->size);
102                 W_CleanupName (lump_p->name, lump_p->name);
103                 if (lump_p->type == TYP_QPIC)
104                         SwapPic ( (qpic_t *)(wad_base + lump_p->filepos));
105         }
106 }
107
108 void *W_GetLumpName (char *name)
109 {
110         int             i;
111         lumpinfo_t      *lump;
112         char    clean[16];
113
114         W_CleanupName (name, clean);
115
116         for (lump = wad_lumps, i = 0;i < wad_numlumps;i++, lump++)
117                 if (!strcmp(clean, lump->name))
118                         return (void *)(wad_base + lump->filepos);
119
120         //Sys_Error ("W_GetLumpinfo: %s not found", name);
121         return NULL;
122 }
123
124 /*
125 =============================================================================
126
127 automatic byte swapping
128
129 =============================================================================
130 */
131
132 void SwapPic (qpic_t *pic)
133 {
134         pic->width = LittleLong(pic->width);
135         pic->height = LittleLong(pic->height);
136 }
137
138 // LordHavoc: added alternate WAD2/WAD3 system for HalfLife texture wads
139 #define TEXWAD_MAXIMAGES 16384
140 typedef struct
141 {
142         char name[16];
143         QFile *file;
144         int position;
145         int size;
146 } texwadlump_t;
147
148 static texwadlump_t texwadlump[TEXWAD_MAXIMAGES];
149
150 /*
151 ====================
152 W_LoadTextureWadFile
153 ====================
154 */
155 void W_LoadTextureWadFile (char *filename, int complain)
156 {
157         lumpinfo_t              *lumps, *lump_p;
158         wadinfo_t               header;
159         unsigned                i, j;
160         int                             infotableofs;
161         QFile                   *file;
162         int                             numlumps;
163
164         COM_FOpenFile (filename, &file, false, false);
165         if (!file)
166         {
167                 if (complain)
168                         Con_Printf ("W_LoadTextureWadFile: couldn't find %s", filename);
169                 return;
170         }
171
172         if (Qread(file, &header, sizeof(wadinfo_t)) != sizeof(wadinfo_t))
173         {Con_Printf ("W_LoadTextureWadFile: unable to read wad header");return;}
174
175         if(memcmp(header.identification, "WAD3", 4))
176         {Con_Printf ("W_LoadTextureWadFile: Wad file %s doesn't have WAD3 id\n",filename);return;}
177
178         numlumps = LittleLong(header.numlumps);
179         if (numlumps < 1 || numlumps > TEXWAD_MAXIMAGES)
180         {Con_Printf ("W_LoadTextureWadFile: invalid number of lumps (%i)\n", numlumps);return;}
181         infotableofs = LittleLong(header.infotableofs);
182         if (Qseek(file, infotableofs, SEEK_SET))
183         {Con_Printf ("W_LoadTextureWadFile: unable to seek to lump table");return;}
184         if (!(lumps = Mem_Alloc(tempmempool, sizeof(lumpinfo_t)*numlumps)))
185         {Con_Printf ("W_LoadTextureWadFile: unable to allocate temporary memory for lump table");return;}
186
187         if (Qread(file, lumps, sizeof(lumpinfo_t) * numlumps) != sizeof(lumpinfo_t) * numlumps)
188         {Con_Printf ("W_LoadTextureWadFile: unable to read lump table");return;}
189
190         for (i=0, lump_p = lumps ; i<numlumps ; i++,lump_p++)
191         {
192                 W_CleanupName (lump_p->name, lump_p->name);
193                 for (j = 0;j < TEXWAD_MAXIMAGES;j++)
194                 {
195                         if (texwadlump[j].name[0]) // occupied slot, check the name
196                         {
197                                 if (!strcmp(lump_p->name, texwadlump[j].name)) // name match, replace old one
198                                         break;
199                         }
200                         else // empty slot
201                                 break;
202                 }
203                 if (j >= TEXWAD_MAXIMAGES)
204                         break; // abort loading
205                 W_CleanupName (lump_p->name, texwadlump[j].name);
206                 texwadlump[j].file = file;
207                 texwadlump[j].position = LittleLong(lump_p->filepos);
208                 texwadlump[j].size = LittleLong(lump_p->disksize);
209         }
210         Mem_Free(lumps);
211         // leaves the file open
212 }
213
214 /*
215 byte hlpalette[768] =
216 {
217         0x00,0x00,0x00,0x0F,0x0F,0x0F,0x1F,0x1F,0x1F,0x2F,0x2F,0x2F,0x3F,0x3F,0x3F,0x4B,
218         0x4B,0x4B,0x5B,0x5B,0x5B,0x6B,0x6B,0x6B,0x7B,0x7B,0x7B,0x8B,0x8B,0x8B,0x9B,0x9B,
219         0x9B,0xAB,0xAB,0xAB,0xBB,0xBB,0xBB,0xCB,0xCB,0xCB,0xDB,0xDB,0xDB,0xEB,0xEB,0xEB,
220         0x0F,0x0B,0x07,0x17,0x0F,0x0B,0x1F,0x17,0x0B,0x27,0x1B,0x0F,0x2F,0x23,0x13,0x37,
221         0x2B,0x17,0x3F,0x2F,0x17,0x4B,0x37,0x1B,0x53,0x3B,0x1B,0x5B,0x43,0x1F,0x63,0x4B,
222         0x1F,0x6B,0x53,0x1F,0x73,0x57,0x1F,0x7B,0x5F,0x23,0x83,0x67,0x23,0x8F,0x6F,0x23,
223         0x0B,0x0B,0x0F,0x13,0x13,0x1B,0x1B,0x1B,0x27,0x27,0x27,0x33,0x2F,0x2F,0x3F,0x37,
224         0x37,0x4B,0x3F,0x3F,0x57,0x47,0x47,0x67,0x4F,0x4F,0x73,0x5B,0x5B,0x7F,0x63,0x63,
225         0x8B,0x6B,0x6B,0x97,0x73,0x73,0xA3,0x7B,0x7B,0xAF,0x83,0x83,0xBB,0x8B,0x8B,0xCB,
226         0x00,0x00,0x00,0x07,0x07,0x00,0x0B,0x0B,0x00,0x13,0x13,0x00,0x1B,0x1B,0x00,0x23,
227         0x23,0x00,0x2B,0x2B,0x07,0x2F,0x2F,0x07,0x37,0x37,0x07,0x3F,0x3F,0x07,0x47,0x47,
228         0x07,0x4B,0x4B,0x0B,0x53,0x53,0x0B,0x5B,0x5B,0x0B,0x63,0x63,0x0B,0x6B,0x6B,0x0F,
229         0x07,0x00,0x00,0x0F,0x00,0x00,0x17,0x00,0x00,0x1F,0x00,0x00,0x27,0x00,0x00,0x2F,
230         0x00,0x00,0x37,0x00,0x00,0x3F,0x00,0x00,0x47,0x00,0x00,0x4F,0x00,0x00,0x57,0x00,
231         0x00,0x5F,0x00,0x00,0x67,0x00,0x00,0x6F,0x00,0x00,0x77,0x00,0x00,0x7F,0x00,0x00,
232         0x13,0x13,0x00,0x1B,0x1B,0x00,0x23,0x23,0x00,0x2F,0x2B,0x00,0x37,0x2F,0x00,0x43,
233         0x37,0x00,0x4B,0x3B,0x07,0x57,0x43,0x07,0x5F,0x47,0x07,0x6B,0x4B,0x0B,0x77,0x53,
234         0x0F,0x83,0x57,0x13,0x8B,0x5B,0x13,0x97,0x5F,0x1B,0xA3,0x63,0x1F,0xAF,0x67,0x23,
235         0x23,0x13,0x07,0x2F,0x17,0x0B,0x3B,0x1F,0x0F,0x4B,0x23,0x13,0x57,0x2B,0x17,0x63,
236         0x2F,0x1F,0x73,0x37,0x23,0x7F,0x3B,0x2B,0x8F,0x43,0x33,0x9F,0x4F,0x33,0xAF,0x63,
237         0x2F,0xBF,0x77,0x2F,0xCF,0x8F,0x2B,0xDF,0xAB,0x27,0xEF,0xCB,0x1F,0xFF,0xF3,0x1B,
238         0x0B,0x07,0x00,0x1B,0x13,0x00,0x2B,0x23,0x0F,0x37,0x2B,0x13,0x47,0x33,0x1B,0x53,
239         0x37,0x23,0x63,0x3F,0x2B,0x6F,0x47,0x33,0x7F,0x53,0x3F,0x8B,0x5F,0x47,0x9B,0x6B,
240         0x53,0xA7,0x7B,0x5F,0xB7,0x87,0x6B,0xC3,0x93,0x7B,0xD3,0xA3,0x8B,0xE3,0xB3,0x97,
241         0xAB,0x8B,0xA3,0x9F,0x7F,0x97,0x93,0x73,0x87,0x8B,0x67,0x7B,0x7F,0x5B,0x6F,0x77,
242         0x53,0x63,0x6B,0x4B,0x57,0x5F,0x3F,0x4B,0x57,0x37,0x43,0x4B,0x2F,0x37,0x43,0x27,
243         0x2F,0x37,0x1F,0x23,0x2B,0x17,0x1B,0x23,0x13,0x13,0x17,0x0B,0x0B,0x0F,0x07,0x07,
244         0xBB,0x73,0x9F,0xAF,0x6B,0x8F,0xA3,0x5F,0x83,0x97,0x57,0x77,0x8B,0x4F,0x6B,0x7F,
245         0x4B,0x5F,0x73,0x43,0x53,0x6B,0x3B,0x4B,0x5F,0x33,0x3F,0x53,0x2B,0x37,0x47,0x23,
246         0x2B,0x3B,0x1F,0x23,0x2F,0x17,0x1B,0x23,0x13,0x13,0x17,0x0B,0x0B,0x0F,0x07,0x07,
247         0xDB,0xC3,0xBB,0xCB,0xB3,0xA7,0xBF,0xA3,0x9B,0xAF,0x97,0x8B,0xA3,0x87,0x7B,0x97,
248         0x7B,0x6F,0x87,0x6F,0x5F,0x7B,0x63,0x53,0x6B,0x57,0x47,0x5F,0x4B,0x3B,0x53,0x3F,
249         0x33,0x43,0x33,0x27,0x37,0x2B,0x1F,0x27,0x1F,0x17,0x1B,0x13,0x0F,0x0F,0x0B,0x07,
250         0x6F,0x83,0x7B,0x67,0x7B,0x6F,0x5F,0x73,0x67,0x57,0x6B,0x5F,0x4F,0x63,0x57,0x47,
251         0x5B,0x4F,0x3F,0x53,0x47,0x37,0x4B,0x3F,0x2F,0x43,0x37,0x2B,0x3B,0x2F,0x23,0x33,
252         0x27,0x1F,0x2B,0x1F,0x17,0x23,0x17,0x0F,0x1B,0x13,0x0B,0x13,0x0B,0x07,0x0B,0x07,
253         0xFF,0xF3,0x1B,0xEF,0xDF,0x17,0xDB,0xCB,0x13,0xCB,0xB7,0x0F,0xBB,0xA7,0x0F,0xAB,
254         0x97,0x0B,0x9B,0x83,0x07,0x8B,0x73,0x07,0x7B,0x63,0x07,0x6B,0x53,0x00,0x5B,0x47,
255         0x00,0x4B,0x37,0x00,0x3B,0x2B,0x00,0x2B,0x1F,0x00,0x1B,0x0F,0x00,0x0B,0x07,0x00,
256         0x00,0x00,0xFF,0x0B,0x0B,0xEF,0x13,0x13,0xDF,0x1B,0x1B,0xCF,0x23,0x23,0xBF,0x2B,
257         0x2B,0xAF,0x2F,0x2F,0x9F,0x2F,0x2F,0x8F,0x2F,0x2F,0x7F,0x2F,0x2F,0x6F,0x2F,0x2F,
258         0x5F,0x2B,0x2B,0x4F,0x23,0x23,0x3F,0x1B,0x1B,0x2F,0x13,0x13,0x1F,0x0B,0x0B,0x0F,
259         0x2B,0x00,0x00,0x3B,0x00,0x00,0x4B,0x07,0x00,0x5F,0x07,0x00,0x6F,0x0F,0x00,0x7F,
260         0x17,0x07,0x93,0x1F,0x07,0xA3,0x27,0x0B,0xB7,0x33,0x0F,0xC3,0x4B,0x1B,0xCF,0x63,
261         0x2B,0xDB,0x7F,0x3B,0xE3,0x97,0x4F,0xE7,0xAB,0x5F,0xEF,0xBF,0x77,0xF7,0xD3,0x8B,
262         0xA7,0x7B,0x3B,0xB7,0x9B,0x37,0xC7,0xC3,0x37,0xE7,0xE3,0x57,0x00,0xFF,0x00,0xAB,
263         0xE7,0xFF,0xD7,0xFF,0xFF,0x67,0x00,0x00,0x8B,0x00,0x00,0xB3,0x00,0x00,0xD7,0x00,
264         0x00,0xFF,0x00,0x00,0xFF,0xF3,0x93,0xFF,0xF7,0xC7,0xFF,0xFF,0xFF,0x9F,0x5B,0x53,
265 };
266 */
267
268 byte *W_ConvertWAD3Texture(miptex_t *tex)
269 {
270         byte *in, *data, *out, *pal;
271 //      int palsize;
272         int d, p;
273         in = (byte *)((int) tex + tex->offsets[0]);
274         data = out = Mem_Alloc(tempmempool, tex->width * tex->height * 4);
275         if (!data)
276                 return NULL;
277         image_width = tex->width;
278         image_height = tex->height;
279         pal = in + (((image_width * image_height) * 85) >> 6);
280 //      palsize = pal[1] * 0x100 + pal[0];
281 //      if (palsize >= 256)
282 //              palsize = 256;
283         pal += 2;
284         for (d = 0;d < image_width * image_height;d++)
285         {
286                 p = *in++;
287                 if (tex->name[0] == '{' && p == 255)
288                         out[0] = out[1] = out[2] = out[3] = 0;
289                 else
290                 {
291                         p *= 3;
292                         out[0] = pal[p];
293                         out[1] = pal[p+1];
294                         out[2] = pal[p+2];
295                         out[3] = 255;
296                 }
297                 out += 4;
298         }
299         return data;
300 }
301
302 byte *W_GetTexture(char *name)
303 {
304 //      int i, c, datasize;
305 //      short colorcount;
306 //      byte pal[256][3], *indata, *outdata, *data;
307         char texname[17];
308         int i, j;
309         QFile *file;
310         miptex_t *tex;
311         byte *data;
312         texname[16] = 0;
313         W_CleanupName (name, texname);
314         for (i = 0;i < TEXWAD_MAXIMAGES;i++)
315         {
316                 if (texwadlump[i].name[0])
317                 {
318                         if (!strcmp(texname, texwadlump[i].name)) // found it
319                         {
320                                 file = texwadlump[i].file;
321                                 if (Qseek(file, texwadlump[i].position, SEEK_SET))
322                                 {Con_Printf("W_GetTexture: corrupt WAD3 file");return NULL;}
323
324                                 tex = Mem_Alloc(tempmempool, texwadlump[i].size);
325                                 if (!tex)
326                                         return NULL;
327                                 if (Qread(file, tex, texwadlump[i].size) < texwadlump[i].size)
328                                 {Con_Printf("W_GetTexture: corrupt WAD3 file");return NULL;}
329
330                                 tex->width = LittleLong(tex->width);
331                                 tex->height = LittleLong(tex->height);
332                                 for (j = 0;j < MIPLEVELS;j++)
333                                         tex->offsets[j] = LittleLong(tex->offsets[j]);
334                                 data = W_ConvertWAD3Texture(tex);
335                                 Mem_Free(tex);
336                                 return data;
337                                 /*
338                                 image_width = LittleLong(t.width);
339                                 image_height = LittleLong(t.height);
340                                 if (matchwidth && image_width != matchwidth)
341                                         continue;
342                                 if (matchheight && image_height != matchheight)
343                                         continue;
344                                 if (image_width & 15 || image_height & 15)
345                                 {Con_Printf("W_GetTexture: corrupt WAD3 file");return NULL;}
346                                 // allocate space for expanded image,
347                                 // and load incoming image into upper area (overwritten as it expands)
348                                 if (!(data = outdata = Mem_Alloc(tempmempool, image_width*image_height*4)))
349                                 {Con_Printf("W_GetTexture: out of memory");return NULL;}
350                                 indata = outdata + image_width*image_height*3;
351                                 datasize = image_width*image_height*85/64;
352                                 // read the image data
353                                 if (Qseek(file, texwadlump[i].position + sizeof(t), SEEK_SET))
354                                 {Con_Printf("W_GetTexture: corrupt WAD3 file");return NULL;}
355                                 if (Qread(file, indata, image_width*image_height) != image_width*image_height)
356                                 {Con_Printf("W_GetTexture: corrupt WAD3 file");return NULL;}
357                                 // read the number of colors used (always 256)
358                                 if (Qseek(file, texwadlump[i].position + sizeof(t) + datasize, SEEK_SET))
359                                 {Con_Printf("W_GetTexture: corrupt WAD3 file");return NULL;}
360                                 if (Qread(file, &colorcount, 2) != 2)
361                                 {Con_Printf("W_GetTexture: corrupt WAD3 file");return NULL;}
362                                 if (texwadlump[i].size > (datasize + sizeof(t)))
363                                 {
364                                         colorcount = LittleShort(colorcount);
365                                         // sanity checking
366                                         if (colorcount < 0) colorcount = 0;
367                                         if (colorcount > 256) colorcount = 256;
368                                         // read the palette
369         //                              Qseek(file, texwadlump[i].position + sizeof(t) + datasize + 2, SEEK_SET);
370                                         if (Qread(file, &pal, 3 * colorcount) != 3 * colorcount)
371                                         {Con_Printf("W_GetTexture: corrupt WAD3 file");return NULL;}
372                                 }
373                                 else
374                                         memcpy(&pal, hlpalette, 768);
375                                 // expand the image to 32bit RGBA
376                                 for (i = 0;i < image_width*image_height;i++)
377                                 {
378                                         c = *indata++;
379                                         if (name[0] == '{' && c == 255)
380                                                 outdata[0] = outdata[1] = outdata[2] = outdata[3] = 0;
381                                         else
382                                         {
383                                                 outdata[0] = pal[c][0];
384                                                 outdata[1] = pal[c][1];
385                                                 outdata[2] = pal[c][2];
386                                                 outdata[3] = 255;
387                                         }
388                                         outdata += 4;
389                                 }
390                                 return data;
391                                 */
392                         }
393                 }
394                 else
395                         break;
396         }
397         image_width = image_height = 0;
398         return NULL;
399 }