2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
24 static int wad_numlumps;
25 static lumpinfo_t *wad_lumps;
26 static qbyte *wad_base = NULL;
27 static mempool_t *wad_mempool = NULL;
29 void SwapPic (qpic_t *pic);
35 Lowercases name and pads with spaces and a terminating 0 to the length of
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.
42 static void W_CleanupName (char *in, char *out)
47 for (i=0 ; i<16 ; i++ )
53 if (c >= 'A' && c <= 'Z')
69 void W_LoadWadFile (char *filename)
77 temp = COM_LoadFile (filename, false);
79 Sys_Error ("W_LoadWadFile: couldn't load %s", filename);
82 Mem_FreePool(&wad_mempool);
83 wad_mempool = Mem_AllocPool(filename);
84 wad_base = Mem_Alloc(wad_mempool, loadsize);
86 memcpy(wad_base, temp, loadsize);
89 header = (wadinfo_t *)wad_base;
91 if (memcmp(header->identification, "WAD2", 4))
92 Sys_Error ("Wad file %s doesn't have WAD2 id\n",filename);
94 wad_numlumps = LittleLong(header->numlumps);
95 infotableofs = LittleLong(header->infotableofs);
96 wad_lumps = (lumpinfo_t *)(wad_base + infotableofs);
98 for (i=0, lump_p = wad_lumps ; i<wad_numlumps ; i++,lump_p++)
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));
108 void *W_GetLumpName (char *name)
114 W_CleanupName (name, clean);
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);
124 =============================================================================
126 automatic byte swapping
128 =============================================================================
131 void SwapPic (qpic_t *pic)
133 pic->width = LittleLong(pic->width);
134 pic->height = LittleLong(pic->height);
137 // LordHavoc: added alternate WAD2/WAD3 system for HalfLife texture wads
138 #define TEXWAD_MAXIMAGES 16384
147 static texwadlump_t texwadlump[TEXWAD_MAXIMAGES];
154 void W_LoadTextureWadFile (char *filename, int complain)
156 lumpinfo_t *lumps, *lump_p;
163 COM_FOpenFile (filename, &file, false, false);
167 Con_Printf ("W_LoadTextureWadFile: couldn't find %s", filename);
171 if (Qread(file, &header, sizeof(wadinfo_t)) != sizeof(wadinfo_t))
172 {Con_Printf ("W_LoadTextureWadFile: unable to read wad header");return;}
174 if(memcmp(header.identification, "WAD3", 4))
175 {Con_Printf ("W_LoadTextureWadFile: Wad file %s doesn't have WAD3 id\n",filename);return;}
177 numlumps = LittleLong(header.numlumps);
178 if (numlumps < 1 || numlumps > TEXWAD_MAXIMAGES)
179 {Con_Printf ("W_LoadTextureWadFile: invalid number of lumps (%i)\n", numlumps);return;}
180 infotableofs = LittleLong(header.infotableofs);
181 if (Qseek(file, infotableofs, SEEK_SET))
182 {Con_Printf ("W_LoadTextureWadFile: unable to seek to lump table");return;}
183 if (!(lumps = Mem_Alloc(tempmempool, sizeof(lumpinfo_t)*numlumps)))
184 {Con_Printf ("W_LoadTextureWadFile: unable to allocate temporary memory for lump table");return;}
186 if (Qread(file, lumps, sizeof(lumpinfo_t) * numlumps) != sizeof(lumpinfo_t) * numlumps)
187 {Con_Printf ("W_LoadTextureWadFile: unable to read lump table");return;}
189 for (i=0, lump_p = lumps ; i<numlumps ; i++,lump_p++)
191 W_CleanupName (lump_p->name, lump_p->name);
192 for (j = 0;j < TEXWAD_MAXIMAGES;j++)
194 if (texwadlump[j].name[0]) // occupied slot, check the name
196 if (!strcmp(lump_p->name, texwadlump[j].name)) // name match, replace old one
202 if (j >= TEXWAD_MAXIMAGES)
203 break; // abort loading
204 W_CleanupName (lump_p->name, texwadlump[j].name);
205 texwadlump[j].file = file;
206 texwadlump[j].position = LittleLong(lump_p->filepos);
207 texwadlump[j].size = LittleLong(lump_p->disksize);
210 // leaves the file open
214 qbyte *W_ConvertWAD3Texture(miptex_t *tex)
216 qbyte *in, *data, *out, *pal;
219 in = (qbyte *)((int) tex + tex->offsets[0]);
220 data = out = Mem_Alloc(tempmempool, tex->width * tex->height * 4);
223 image_width = tex->width;
224 image_height = tex->height;
225 pal = in + (((image_width * image_height) * 85) >> 6);
227 for (d = 0;d < image_width * image_height;d++)
230 if (tex->name[0] == '{' && p == 255)
231 out[0] = out[1] = out[2] = out[3] = 0;
245 qbyte *W_GetTexture(char *name)
254 W_CleanupName (name, texname);
255 for (i = 0;i < TEXWAD_MAXIMAGES;i++)
257 if (texwadlump[i].name[0])
259 if (!strcmp(texname, texwadlump[i].name)) // found it
261 file = texwadlump[i].file;
262 if (Qseek(file, texwadlump[i].position, SEEK_SET))
263 {Con_Printf("W_GetTexture: corrupt WAD3 file");return NULL;}
265 tex = Mem_Alloc(tempmempool, texwadlump[i].size);
268 if (Qread(file, tex, texwadlump[i].size) < texwadlump[i].size)
269 {Con_Printf("W_GetTexture: corrupt WAD3 file");return NULL;}
271 tex->width = LittleLong(tex->width);
272 tex->height = LittleLong(tex->height);
273 for (j = 0;j < MIPLEVELS;j++)
274 tex->offsets[j] = LittleLong(tex->offsets[j]);
275 data = W_ConvertWAD3Texture(tex);
283 image_width = image_height = 0;