Disabled vsync when doing a timedemo.
[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
21
22 #include "quakedef.h"
23 #include "image.h"
24 #include "wad.h"
25
26
27 void SwapPic (qpic_t *pic);
28
29
30 /*
31 ==================
32 W_CleanupName
33
34 Lowercases name and pads with spaces and a terminating 0 to the length of
35 lumpinfo_t->name.
36 Used so lumpname lookups can proceed rapidly by comparing 4 chars at a time
37 Space padding is so names can be printed nicely in tables.
38 Can safely be performed in place.
39 ==================
40 */
41 static void W_CleanupName (char *in, char *out)
42 {
43         int             i;
44         int             c;
45
46         for (i=0 ; i<16 ; i++ )
47         {
48                 c = in[i];
49                 if (!c)
50                         break;
51
52                 if (c >= 'A' && c <= 'Z')
53                         c += ('a' - 'A');
54                 out[i] = c;
55         }
56
57         for ( ; i< 16 ; i++ )
58                 out[i] = 0;
59 }
60
61 void *W_GetLumpName(char *name)
62 {
63         int i;
64         lumpinfo_t *lump;
65         char clean[16];
66         wadinfo_t *header;
67         int infotableofs;
68         void *temp;
69         static int wad_loaded = false;
70         static int wad_numlumps = 0;
71         static lumpinfo_t *wad_lumps = NULL;
72         static qbyte *wad_base = NULL;
73         static mempool_t *wad_mempool = NULL;
74
75         W_CleanupName (name, clean);
76
77         if (!wad_loaded)
78         {
79                 wad_loaded = true;
80                 if ((temp = FS_LoadFile ("gfx.wad", tempmempool, false)))
81                 {
82                         if (memcmp(temp, "WAD2", 4))
83                                 Con_Print("gfx.wad doesn't have WAD2 id\n");
84                         else
85                         {
86                                 wad_mempool = Mem_AllocPool("gfx.wad", 0, NULL);
87                                 wad_base = Mem_Alloc(wad_mempool, fs_filesize);
88
89                                 memcpy(wad_base, temp, fs_filesize);
90                                 Mem_Free(temp);
91
92                                 header = (wadinfo_t *)wad_base;
93                                 wad_numlumps = LittleLong(header->numlumps);
94                                 infotableofs = LittleLong(header->infotableofs);
95                                 wad_lumps = (lumpinfo_t *)(wad_base + infotableofs);
96
97                                 for (i=0, lump = wad_lumps ; i<wad_numlumps ; i++,lump++)
98                                 {
99                                         lump->filepos = LittleLong(lump->filepos);
100                                         lump->size = LittleLong(lump->size);
101                                         W_CleanupName (lump->name, lump->name);
102                                         if (lump->type == TYP_QPIC)
103                                                 SwapPic ( (qpic_t *)(wad_base + lump->filepos));
104                                 }
105                         }
106                 }
107         }
108
109         for (lump = wad_lumps, i = 0;i < wad_numlumps;i++, lump++)
110                 if (!strcmp(clean, lump->name))
111                         return (void *)(wad_base + lump->filepos);
112
113         if (wad_base)
114                 Con_DPrintf("W_GetLumpByName(\"%s\"): couldn't find file in gfx.wad\n", name);
115         else
116                 Con_DPrintf("W_GetLumpByName(\"%s\"): couldn't load gfx.wad\n", name);
117         return NULL;
118 }
119
120 /*
121 =============================================================================
122
123 automatic byte swapping
124
125 =============================================================================
126 */
127
128 void SwapPic (qpic_t *pic)
129 {
130         pic->width = LittleLong(pic->width);
131         pic->height = LittleLong(pic->height);
132 }
133
134 // LordHavoc: added alternate WAD2/WAD3 system for HalfLife texture wads
135 #define TEXWAD_MAXIMAGES 16384
136 typedef struct
137 {
138         char name[16];
139         qfile_t *file;
140         int position;
141         int size;
142 } texwadlump_t;
143
144 static texwadlump_t texwadlump[TEXWAD_MAXIMAGES];
145
146 /*
147 ====================
148 W_LoadTextureWadFile
149 ====================
150 */
151 void W_LoadTextureWadFile (char *filename, int complain)
152 {
153         lumpinfo_t              *lumps, *lump_p;
154         wadinfo_t               header;
155         int                             i, j;
156         int                             infotableofs;
157         qfile_t                 *file;
158         int                             numlumps;
159
160         file = FS_Open (filename, "rb", false);
161         if (!file)
162         {
163                 if (complain)
164                         Con_Printf("W_LoadTextureWadFile: couldn't find %s\n", filename);
165                 return;
166         }
167
168         if (FS_Read(file, &header, sizeof(wadinfo_t)) != sizeof(wadinfo_t))
169         {Con_Print("W_LoadTextureWadFile: unable to read wad header\n");return;}
170
171         if(memcmp(header.identification, "WAD3", 4))
172         {Con_Printf("W_LoadTextureWadFile: Wad file %s doesn't have WAD3 id\n",filename);return;}
173
174         numlumps = LittleLong(header.numlumps);
175         if (numlumps < 1 || numlumps > TEXWAD_MAXIMAGES)
176         {Con_Printf("W_LoadTextureWadFile: invalid number of lumps (%i)\n", numlumps);return;}
177         infotableofs = LittleLong(header.infotableofs);
178         if (FS_Seek (file, infotableofs, SEEK_SET))
179         {Con_Print("W_LoadTextureWadFile: unable to seek to lump table\n");return;}
180         if (!(lumps = Mem_Alloc(tempmempool, sizeof(lumpinfo_t)*numlumps)))
181         {Con_Print("W_LoadTextureWadFile: unable to allocate temporary memory for lump table\n");return;}
182
183         if (FS_Read(file, lumps, sizeof(lumpinfo_t) * numlumps) != sizeof(lumpinfo_t) * (size_t)numlumps)
184         {Con_Print("W_LoadTextureWadFile: unable to read lump table\n");return;}
185
186         for (i=0, lump_p = lumps ; i<numlumps ; i++,lump_p++)
187         {
188                 W_CleanupName (lump_p->name, lump_p->name);
189                 for (j = 0;j < TEXWAD_MAXIMAGES;j++)
190                 {
191                         if (texwadlump[j].name[0]) // occupied slot, check the name
192                         {
193                                 if (!strcmp(lump_p->name, texwadlump[j].name)) // name match, replace old one
194                                         break;
195                         }
196                         else // empty slot
197                                 break;
198                 }
199                 if (j >= TEXWAD_MAXIMAGES)
200                         break; // abort loading
201                 W_CleanupName (lump_p->name, texwadlump[j].name);
202                 texwadlump[j].file = file;
203                 texwadlump[j].position = LittleLong(lump_p->filepos);
204                 texwadlump[j].size = LittleLong(lump_p->disksize);
205         }
206         Mem_Free(lumps);
207         // leaves the file open
208 }
209
210
211 qbyte *W_ConvertWAD3Texture(miptex_t *tex)
212 {
213         qbyte *in, *data, *out, *pal;
214         int d, p;
215
216         in = (qbyte *)tex + tex->offsets[0];
217         data = out = Mem_Alloc(tempmempool, tex->width * tex->height * 4);
218         if (!data)
219                 return NULL;
220         image_width = tex->width;
221         image_height = tex->height;
222         pal = in + (((image_width * image_height) * 85) >> 6);
223         pal += 2;
224         for (d = 0;d < image_width * image_height;d++)
225         {
226                 p = *in++;
227                 if (tex->name[0] == '{' && p == 255)
228                         out[0] = out[1] = out[2] = out[3] = 0;
229                 else
230                 {
231                         p *= 3;
232                         out[0] = pal[p];
233                         out[1] = pal[p+1];
234                         out[2] = pal[p+2];
235                         out[3] = 255;
236                 }
237                 out += 4;
238         }
239         return data;
240 }
241
242 qbyte *W_GetTexture(char *name)
243 {
244         char texname[17];
245         int i, j;
246         qfile_t *file;
247         miptex_t *tex;
248         qbyte *data;
249
250         texname[16] = 0;
251         W_CleanupName (name, texname);
252         for (i = 0;i < TEXWAD_MAXIMAGES;i++)
253         {
254                 if (texwadlump[i].name[0])
255                 {
256                         if (!strcmp(texname, texwadlump[i].name)) // found it
257                         {
258                                 file = texwadlump[i].file;
259                                 if (FS_Seek(file, texwadlump[i].position, SEEK_SET))
260                                 {Con_Print("W_GetTexture: corrupt WAD3 file\n");return NULL;}
261
262                                 tex = Mem_Alloc(tempmempool, texwadlump[i].size);
263                                 if (!tex)
264                                         return NULL;
265                                 if (FS_Read(file, tex, texwadlump[i].size) < (size_t)texwadlump[i].size)
266                                 {Con_Print("W_GetTexture: corrupt WAD3 file\n");return NULL;}
267
268                                 tex->width = LittleLong(tex->width);
269                                 tex->height = LittleLong(tex->height);
270                                 for (j = 0;j < MIPLEVELS;j++)
271                                         tex->offsets[j] = LittleLong(tex->offsets[j]);
272                                 data = W_ConvertWAD3Texture(tex);
273                                 Mem_Free(tex);
274                                 return data;
275                         }
276                 }
277                 else
278                         break;
279         }
280         image_width = image_height = 0;
281         return NULL;
282 }
283