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 void SwapPic (qpic_t *pic);
30 Lowercases name and pads with spaces and a terminating 0 to the length of
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.
37 static void W_CleanupName (char *in, char *out)
42 for (i=0 ; i<16 ; i++ )
48 if (c >= 'A' && c <= 'Z')
57 void *W_GetLumpName(char *name)
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;
71 W_CleanupName (name, clean);
76 if ((temp = FS_LoadFile ("gfx.wad", tempmempool, false)))
78 if (memcmp(temp, "WAD2", 4))
79 Con_Print("gfx.wad doesn't have WAD2 id\n");
82 wad_mempool = Mem_AllocPool("gfx.wad", 0, NULL);
83 wad_base = Mem_Alloc(wad_mempool, fs_filesize);
85 memcpy(wad_base, temp, fs_filesize);
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);
93 for (i=0, lump = wad_lumps ; i<wad_numlumps ; i++,lump++)
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));
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);
110 Con_DPrintf("W_GetLumpByName(\"%s\"): couldn't find file in gfx.wad\n", name);
112 Con_DPrintf("W_GetLumpByName(\"%s\"): couldn't load gfx.wad\n", name);
117 =============================================================================
119 automatic byte swapping
121 =============================================================================
124 void SwapPic (qpic_t *pic)
126 pic->width = LittleLong(pic->width);
127 pic->height = LittleLong(pic->height);
130 // LordHavoc: added alternate WAD2/WAD3 system for HalfLife texture wads
131 #define TEXWAD_MAXIMAGES 16384
140 static texwadlump_t texwadlump[TEXWAD_MAXIMAGES];
147 void W_LoadTextureWadFile (char *filename, int complain)
149 lumpinfo_t *lumps, *lump_p;
156 file = FS_Open (filename, "rb", false);
160 Con_Printf("W_LoadTextureWadFile: couldn't find %s\n", filename);
164 if (FS_Read(file, &header, sizeof(wadinfo_t)) != sizeof(wadinfo_t))
165 {Con_Print("W_LoadTextureWadFile: unable to read wad header\n");return;}
167 if(memcmp(header.identification, "WAD3", 4))
168 {Con_Printf("W_LoadTextureWadFile: Wad file %s doesn't have WAD3 id\n",filename);return;}
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_Print("W_LoadTextureWadFile: unable to seek to lump table\n");return;}
176 if (!(lumps = Mem_Alloc(tempmempool, sizeof(lumpinfo_t)*numlumps)))
177 {Con_Print("W_LoadTextureWadFile: unable to allocate temporary memory for lump table\n");return;}
179 if (FS_Read(file, lumps, sizeof(lumpinfo_t) * numlumps) != sizeof(lumpinfo_t) * (size_t)numlumps)
180 {Con_Print("W_LoadTextureWadFile: unable to read lump table\n");return;}
182 for (i=0, lump_p = lumps ; i<numlumps ; i++,lump_p++)
184 W_CleanupName (lump_p->name, lump_p->name);
185 for (j = 0;j < TEXWAD_MAXIMAGES;j++)
187 if (texwadlump[j].name[0]) // occupied slot, check the name
189 if (!strcmp(lump_p->name, texwadlump[j].name)) // name match, replace old one
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);
203 // leaves the file open
207 qbyte *W_ConvertWAD3Texture(miptex_t *tex)
209 qbyte *in, *data, *out, *pal;
212 in = (qbyte *)((int) tex + tex->offsets[0]);
213 data = out = Mem_Alloc(tempmempool, tex->width * tex->height * 4);
216 image_width = tex->width;
217 image_height = tex->height;
218 pal = in + (((image_width * image_height) * 85) >> 6);
220 for (d = 0;d < image_width * image_height;d++)
223 if (tex->name[0] == '{' && p == 255)
224 out[0] = out[1] = out[2] = out[3] = 0;
238 qbyte *W_GetTexture(char *name)
247 W_CleanupName (name, texname);
248 for (i = 0;i < TEXWAD_MAXIMAGES;i++)
250 if (texwadlump[i].name[0])
252 if (!strcmp(texname, texwadlump[i].name)) // found it
254 file = texwadlump[i].file;
255 if (FS_Seek(file, texwadlump[i].position, SEEK_SET))
256 {Con_Print("W_GetTexture: corrupt WAD3 file\n");return NULL;}
258 tex = Mem_Alloc(tempmempool, texwadlump[i].size);
261 if (FS_Read(file, tex, texwadlump[i].size) < (size_t)texwadlump[i].size)
262 {Con_Print("W_GetTexture: corrupt WAD3 file\n");return NULL;}
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);
276 image_width = image_height = 0;