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