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.
34 typedef struct wadstate_s
36 unsigned char *gfx_base;
38 memexpandablearray_t hlwads;
42 static wadstate_t wad;
48 Lowercases name and pads with spaces and a terminating 0 to the length of
50 Used so lumpname lookups can proceed rapidly by comparing 4 chars at a time
51 Space padding is so names can be printed nicely in tables.
52 Can safely be performed in place.
55 static void W_CleanupName (const char *in, char *out)
60 for (i=0 ; i<16 ; i++ )
66 if (c >= 'A' && c <= 'Z')
75 static void W_SwapLumps(int numlumps, lumpinfo_t *lumps)
78 for (i = 0;i < numlumps;i++)
80 lumps[i].filepos = LittleLong(lumps[i].filepos);
81 lumps[i].disksize = LittleLong(lumps[i].disksize);
82 lumps[i].size = LittleLong(lumps[i].size);
83 W_CleanupName(lumps[i].name, lumps[i].name);
87 void W_UnloadAll(void)
91 // free gfx.wad if it is loaded
93 Mem_Free(wad.gfx_base);
95 // close all hlwad files and free their lumps data
96 for (i = 0;i < Mem_ExpandableArray_IndexRange(&wad.hlwads);i++)
98 w = (mwad_t *) Mem_ExpandableArray_RecordAtIndex(&wad.hlwads, i);
108 // free the hlwads array
109 Mem_ExpandableArray_FreeArray(&wad.hlwads);
111 memset(&wad, 0, sizeof(wad));
114 unsigned char *W_GetLumpName(const char *name, fs_offset_t *returnfilesize)
117 fs_offset_t filesize;
123 W_CleanupName (name, clean);
127 if ((wad.gfx_base = FS_LoadFile ("gfx.wad", cls.permanentmempool, false, &filesize)))
129 if (memcmp(wad.gfx_base, "WAD2", 4))
131 Con_Print("gfx.wad doesn't have WAD2 id\n");
132 Mem_Free(wad.gfx_base);
137 header = (wadinfo_t *)wad.gfx_base;
138 wad.gfx.numlumps = LittleLong(header->numlumps);
139 infotableofs = LittleLong(header->infotableofs);
140 wad.gfx.lumps = (lumpinfo_t *)(wad.gfx_base + infotableofs);
142 // byteswap the gfx.wad lumps in place
143 W_SwapLumps(wad.gfx.numlumps, wad.gfx.lumps);
148 for (lump = wad.gfx.lumps, i = 0;i < wad.gfx.numlumps;i++, lump++)
150 if (!strcmp(clean, lump->name))
153 *returnfilesize = lump->size;
154 return (wad.gfx_base + lump->filepos);
165 void W_LoadTextureWadFile (char *filename, int complain)
173 file = FS_OpenVirtualFile(filename, false);
177 Con_Printf(CON_ERROR "W_LoadTextureWadFile: couldn't find %s\n", filename);
181 if (FS_Read(file, &header, sizeof(wadinfo_t)) != sizeof(wadinfo_t))
182 {Con_Print(CON_ERROR "W_LoadTextureWadFile: unable to read wad header\n");FS_Close(file);file = NULL;return;}
184 if(memcmp(header.identification, "WAD3", 4))
185 {Con_Printf(CON_ERROR "W_LoadTextureWadFile: Wad file %s doesn't have WAD3 id\n",filename);FS_Close(file);file = NULL;return;}
187 numlumps = LittleLong(header.numlumps);
188 if (numlumps < 1 || numlumps > 65536)
189 {Con_Printf(CON_ERROR "W_LoadTextureWadFile: invalid number of lumps (%i)\n", numlumps);FS_Close(file);file = NULL;return;}
190 infotableofs = LittleLong(header.infotableofs);
191 if (FS_Seek (file, infotableofs, SEEK_SET))
192 {Con_Print(CON_ERROR "W_LoadTextureWadFile: unable to seek to lump table\n");FS_Close(file);file = NULL;return;}
194 if (!wad.hlwads.mempool)
195 Mem_ExpandableArray_NewArray(&wad.hlwads, cls.permanentmempool, sizeof(mwad_t), 16);
196 w = (mwad_t *) Mem_ExpandableArray_AllocRecord(&wad.hlwads);
198 w->numlumps = numlumps;
199 w->lumps = (lumpinfo_t *) Mem_Alloc(cls.permanentmempool, w->numlumps * sizeof(lumpinfo_t));
203 Con_Print(CON_ERROR "W_LoadTextureWadFile: unable to allocate temporary memory for lump table\n");
210 if (FS_Read(file, w->lumps, sizeof(lumpinfo_t) * w->numlumps) != (fs_offset_t)sizeof(lumpinfo_t) * numlumps)
212 Con_Print(CON_ERROR "W_LoadTextureWadFile: unable to read lump table\n");
221 W_SwapLumps(w->numlumps, w->lumps);
223 // leaves the file open
226 unsigned char *W_ConvertWAD3TextureBGRA(sizebuf_t *sb)
228 unsigned char *in, *data, *out, *pal;
230 unsigned char name[16];
231 unsigned int mipoffset[4];
233 MSG_BeginReading(sb);
234 MSG_ReadBytes(sb, 16, name);
235 image_width = MSG_ReadLittleLong(sb);
236 image_height = MSG_ReadLittleLong(sb);
237 mipoffset[0] = MSG_ReadLittleLong(sb);
238 mipoffset[1] = MSG_ReadLittleLong(sb); // should be mipoffset[0] + image_width*image_height
239 mipoffset[2] = MSG_ReadLittleLong(sb); // should be mipoffset[1] + image_width*image_height/4
240 mipoffset[3] = MSG_ReadLittleLong(sb); // should be mipoffset[2] + image_width*image_height/16
241 pal = sb->data + mipoffset[3] + (image_width / 8 * image_height / 8) + 2;
243 // bail if any data looks wrong
245 || image_width > 4096
247 || image_height > 4096
248 || mipoffset[0] != 40
249 || mipoffset[1] != mipoffset[0] + image_width * image_height
250 || mipoffset[2] != mipoffset[1] + image_width / 2 * image_height / 2
251 || mipoffset[3] != mipoffset[2] + image_width / 4 * image_height / 4
252 || (unsigned int)sb->cursize < (mipoffset[3] + image_width / 8 * image_height / 8 + 2 + 768))
255 in = (unsigned char *)sb->data + mipoffset[0];
256 data = out = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
259 for (d = 0;d < image_width * image_height;d++)
262 if (name[0] == '{' && p == 255)
263 out[0] = out[1] = out[2] = out[3] = 0;
277 unsigned char *W_GetTextureBGRA(char *name)
287 W_CleanupName(name, texname);
288 if (!wad.hlwads.mempool)
289 Mem_ExpandableArray_NewArray(&wad.hlwads, cls.permanentmempool, sizeof(mwad_t), 16);
290 range = Mem_ExpandableArray_IndexRange(&wad.hlwads);
291 for (k = 0;k < range;k++)
293 w = (mwad_t *)Mem_ExpandableArray_RecordAtIndex(&wad.hlwads, k);
296 for (i = 0;i < (unsigned int)w->numlumps;i++)
298 if (!strcmp(texname, w->lumps[i].name)) // found it
300 if (FS_Seek(w->file, w->lumps[i].filepos, SEEK_SET))
301 {Con_Print("W_GetTexture: corrupt WAD3 file\n");return NULL;}
303 MSG_InitReadBuffer(&sb, (unsigned char *)Mem_Alloc(tempmempool, w->lumps[i].disksize), w->lumps[i].disksize);
306 if (FS_Read(w->file, sb.data, w->lumps[i].size) < w->lumps[i].disksize)
307 {Con_Print("W_GetTexture: corrupt WAD3 file\n");return NULL;}
309 data = W_ConvertWAD3TextureBGRA(&sb);
315 image_width = image_height = 0;