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