]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - wad.c
renamed varray_ arrays to rsurface_array_, and they are no longer used outside the...
[xonotic/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 (const 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(const char *name)
62 {
63         int i;
64         fs_offset_t filesize;
65         lumpinfo_t *lump;
66         char clean[16];
67         wadinfo_t *header;
68         int infotableofs;
69         static int wad_loaded = false;
70         static int wad_numlumps = 0;
71         static lumpinfo_t *wad_lumps = NULL;
72         static unsigned char *wad_base = NULL;
73
74         W_CleanupName (name, clean);
75
76         if (!wad_loaded)
77         {
78                 wad_loaded = true;
79                 if ((wad_base = FS_LoadFile ("gfx.wad", cls.permanentmempool, false, &filesize)))
80                 {
81                         if (memcmp(wad_base, "WAD2", 4))
82                         {
83                                 Con_Print("gfx.wad doesn't have WAD2 id\n");
84                                 Mem_Free(wad_base);
85                                 wad_base = NULL;
86                         }
87                         else
88                         {
89                                 header = (wadinfo_t *)wad_base;
90                                 wad_numlumps = LittleLong(header->numlumps);
91                                 infotableofs = LittleLong(header->infotableofs);
92                                 wad_lumps = (lumpinfo_t *)(wad_base + infotableofs);
93
94                                 for (i=0, lump = wad_lumps ; i<wad_numlumps ; i++,lump++)
95                                 {
96                                         lump->filepos = LittleLong(lump->filepos);
97                                         lump->size = LittleLong(lump->size);
98                                         W_CleanupName (lump->name, lump->name);
99                                         if (lump->type == TYP_QPIC)
100                                                 SwapPic ( (qpic_t *)(wad_base + lump->filepos));
101                                 }
102                         }
103                 }
104         }
105
106         for (lump = wad_lumps, i = 0;i < wad_numlumps;i++, lump++)
107                 if (!strcmp(clean, lump->name))
108                         return (void *)(wad_base + lump->filepos);
109
110         if (wad_base)
111                 Con_DPrintf("W_GetLumpByName(\"%s\"): couldn't find file in gfx.wad\n", name);
112         else
113                 Con_DPrintf("W_GetLumpByName(\"%s\"): couldn't load gfx.wad\n", name);
114         return NULL;
115 }
116
117 /*
118 =============================================================================
119
120 automatic byte swapping
121
122 =============================================================================
123 */
124
125 void SwapPic (qpic_t *pic)
126 {
127         pic->width = LittleLong(pic->width);
128         pic->height = LittleLong(pic->height);
129 }
130
131 // LordHavoc: added alternate WAD2/WAD3 system for HalfLife texture wads
132 #define TEXWAD_MAXIMAGES 16384
133 typedef struct texwadlump_s
134 {
135         char name[16];
136         qfile_t *file;
137         int position;
138         int size;
139 } texwadlump_t;
140
141 static texwadlump_t texwadlump[TEXWAD_MAXIMAGES];
142
143 /*
144 ====================
145 W_LoadTextureWadFile
146 ====================
147 */
148 void W_LoadTextureWadFile (char *filename, int complain)
149 {
150         lumpinfo_t              *lumps, *lump_p;
151         wadinfo_t               header;
152         int                             i, j;
153         int                             infotableofs;
154         qfile_t                 *file;
155         int                             numlumps;
156
157         file = FS_Open (filename, "rb", false, false);
158         if (!file)
159         {
160                 if (complain)
161                         Con_Printf("W_LoadTextureWadFile: couldn't find %s\n", filename);
162                 return;
163         }
164
165         if (FS_Read(file, &header, sizeof(wadinfo_t)) != sizeof(wadinfo_t))
166         {Con_Print("W_LoadTextureWadFile: unable to read wad header\n");return;}
167
168         if(memcmp(header.identification, "WAD3", 4))
169         {Con_Printf("W_LoadTextureWadFile: Wad file %s doesn't have WAD3 id\n",filename);return;}
170
171         numlumps = LittleLong(header.numlumps);
172         if (numlumps < 1 || numlumps > TEXWAD_MAXIMAGES)
173         {Con_Printf("W_LoadTextureWadFile: invalid number of lumps (%i)\n", numlumps);return;}
174         infotableofs = LittleLong(header.infotableofs);
175         if (FS_Seek (file, infotableofs, SEEK_SET))
176         {Con_Print("W_LoadTextureWadFile: unable to seek to lump table\n");return;}
177         if (!(lumps = (lumpinfo_t *)Mem_Alloc(tempmempool, sizeof(lumpinfo_t)*numlumps)))
178         {Con_Print("W_LoadTextureWadFile: unable to allocate temporary memory for lump table\n");return;}
179
180         if (FS_Read(file, lumps, sizeof(lumpinfo_t) * numlumps) != (fs_offset_t)sizeof(lumpinfo_t) * numlumps)
181         {Con_Print("W_LoadTextureWadFile: unable to read lump table\n");return;}
182
183         for (i=0, lump_p = lumps ; i<numlumps ; i++,lump_p++)
184         {
185                 W_CleanupName (lump_p->name, lump_p->name);
186                 for (j = 0;j < TEXWAD_MAXIMAGES;j++)
187                 {
188                         if (texwadlump[j].name[0]) // occupied slot, check the name
189                         {
190                                 if (!strcmp(lump_p->name, texwadlump[j].name)) // name match, replace old one
191                                         break;
192                         }
193                         else // empty slot
194                                 break;
195                 }
196                 if (j >= TEXWAD_MAXIMAGES)
197                         break; // abort loading
198                 W_CleanupName (lump_p->name, texwadlump[j].name);
199                 texwadlump[j].file = file;
200                 texwadlump[j].position = LittleLong(lump_p->filepos);
201                 texwadlump[j].size = LittleLong(lump_p->disksize);
202         }
203         Mem_Free(lumps);
204         // leaves the file open
205 }
206
207
208 unsigned char *W_ConvertWAD3Texture(miptex_t *tex)
209 {
210         unsigned char *in, *data, *out, *pal;
211         int d, p;
212
213         in = (unsigned char *)tex + tex->offsets[0];
214         data = out = (unsigned char *)Mem_Alloc(tempmempool, tex->width * tex->height * 4);
215         if (!data)
216                 return NULL;
217         image_width = tex->width;
218         image_height = tex->height;
219         pal = in + (((image_width * image_height) * 85) >> 6);
220         pal += 2;
221         for (d = 0;d < image_width * image_height;d++)
222         {
223                 p = *in++;
224                 if (tex->name[0] == '{' && p == 255)
225                         out[0] = out[1] = out[2] = out[3] = 0;
226                 else
227                 {
228                         p *= 3;
229                         out[0] = pal[p];
230                         out[1] = pal[p+1];
231                         out[2] = pal[p+2];
232                         out[3] = 255;
233                 }
234                 out += 4;
235         }
236         return data;
237 }
238
239 unsigned char *W_GetTexture(char *name)
240 {
241         char texname[17];
242         int i, j;
243         qfile_t *file;
244         miptex_t *tex;
245         unsigned char *data;
246
247         texname[16] = 0;
248         W_CleanupName (name, texname);
249         for (i = 0;i < TEXWAD_MAXIMAGES;i++)
250         {
251                 if (texwadlump[i].name[0])
252                 {
253                         if (!strcmp(texname, texwadlump[i].name)) // found it
254                         {
255                                 file = texwadlump[i].file;
256                                 if (FS_Seek(file, texwadlump[i].position, SEEK_SET))
257                                 {Con_Print("W_GetTexture: corrupt WAD3 file\n");return NULL;}
258
259                                 tex = (miptex_t *)Mem_Alloc(tempmempool, texwadlump[i].size);
260                                 if (!tex)
261                                         return NULL;
262                                 if (FS_Read(file, tex, texwadlump[i].size) < texwadlump[i].size)
263                                 {Con_Print("W_GetTexture: corrupt WAD3 file\n");return NULL;}
264
265                                 tex->width = LittleLong(tex->width);
266                                 tex->height = LittleLong(tex->height);
267                                 for (j = 0;j < MIPLEVELS;j++)
268                                         tex->offsets[j] = LittleLong(tex->offsets[j]);
269                                 data = W_ConvertWAD3Texture(tex);
270                                 Mem_Free(tex);
271                                 return data;
272                         }
273                 }
274                 else
275                         break;
276         }
277         image_width = image_height = 0;
278         return NULL;
279 }
280