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.
31 Lowercases name and pads with spaces and a terminating 0 to the length of
33 Used so lumpname lookups can proceed rapidly by comparing 4 chars at a time
34 Space padding is so names can be printed nicely in tables.
35 Can safely be performed in place.
38 static void W_CleanupName (const char *in, char *out)
43 for (i=0 ; i<16 ; i++ )
49 if (c >= 'A' && c <= 'Z')
58 static int wad_numlumps = 0;
59 static lumpinfo_t *wad_lumps = NULL;
60 static unsigned char *wad_base = NULL;
62 unsigned char *W_GetLumpName(const char *name)
71 W_CleanupName (name, clean);
75 if ((wad_base = FS_LoadFile ("gfx.wad", cls.permanentmempool, false, &filesize)))
77 if (memcmp(wad_base, "WAD2", 4))
79 Con_Print("gfx.wad doesn't have WAD2 id\n");
85 header = (wadinfo_t *)wad_base;
86 wad_numlumps = LittleLong(header->numlumps);
87 infotableofs = LittleLong(header->infotableofs);
88 wad_lumps = (lumpinfo_t *)(wad_base + infotableofs);
90 for (i=0, lump = wad_lumps ; i<wad_numlumps ; i++,lump++)
92 lump->filepos = LittleLong(lump->filepos);
93 lump->size = LittleLong(lump->size);
94 W_CleanupName (lump->name, lump->name);
100 for (lump = wad_lumps, i = 0;i < wad_numlumps;i++, lump++)
101 if (!strcmp(clean, lump->name))
102 return (wad_base + lump->filepos);
105 Con_DPrintf("W_GetLumpByName(\"%s\"): couldn't find file in gfx.wad\n", name);
107 Con_DPrintf("W_GetLumpByName(\"%s\"): couldn't load gfx.wad\n", name);
112 =============================================================================
114 automatic byte swapping
116 =============================================================================
119 // LordHavoc: added alternate WAD2/WAD3 system for HalfLife texture wads
120 #define TEXWAD_MAXIMAGES 16384
121 typedef struct texwadlump_s
129 static texwadlump_t texwadlump[TEXWAD_MAXIMAGES];
136 void W_LoadTextureWadFile (char *filename, int complain)
138 lumpinfo_t *lumps, *lump_p;
145 file = FS_Open (filename, "rb", false, false);
149 Con_Printf("W_LoadTextureWadFile: couldn't find %s\n", filename);
153 if (FS_Read(file, &header, sizeof(wadinfo_t)) != sizeof(wadinfo_t))
154 {Con_Print("W_LoadTextureWadFile: unable to read wad header\n");return;}
156 if(memcmp(header.identification, "WAD3", 4))
157 {Con_Printf("W_LoadTextureWadFile: Wad file %s doesn't have WAD3 id\n",filename);return;}
159 numlumps = LittleLong(header.numlumps);
160 if (numlumps < 1 || numlumps > TEXWAD_MAXIMAGES)
161 {Con_Printf("W_LoadTextureWadFile: invalid number of lumps (%i)\n", numlumps);return;}
162 infotableofs = LittleLong(header.infotableofs);
163 if (FS_Seek (file, infotableofs, SEEK_SET))
164 {Con_Print("W_LoadTextureWadFile: unable to seek to lump table\n");return;}
165 if (!(lumps = (lumpinfo_t *)Mem_Alloc(tempmempool, sizeof(lumpinfo_t)*numlumps)))
166 {Con_Print("W_LoadTextureWadFile: unable to allocate temporary memory for lump table\n");return;}
168 if (FS_Read(file, lumps, sizeof(lumpinfo_t) * numlumps) != (fs_offset_t)sizeof(lumpinfo_t) * numlumps)
169 {Con_Print("W_LoadTextureWadFile: unable to read lump table\n");return;}
171 for (i=0, lump_p = lumps ; i<numlumps ; i++,lump_p++)
173 W_CleanupName (lump_p->name, lump_p->name);
174 for (j = 0;j < TEXWAD_MAXIMAGES;j++)
176 if (texwadlump[j].name[0]) // occupied slot, check the name
178 if (!strcmp(lump_p->name, texwadlump[j].name)) // name match, replace old one
184 if (j >= TEXWAD_MAXIMAGES)
185 break; // abort loading
186 W_CleanupName (lump_p->name, texwadlump[j].name);
187 texwadlump[j].file = file;
188 texwadlump[j].position = LittleLong(lump_p->filepos);
189 texwadlump[j].size = LittleLong(lump_p->disksize);
192 // leaves the file open
196 unsigned char *W_ConvertWAD3Texture(miptex_t *tex)
198 unsigned char *in, *data, *out, *pal;
201 in = (unsigned char *)tex + tex->offsets[0];
202 data = out = (unsigned char *)Mem_Alloc(tempmempool, tex->width * tex->height * 4);
205 image_width = tex->width;
206 image_height = tex->height;
207 pal = in + (((image_width * image_height) * 85) >> 6);
209 for (d = 0;d < image_width * image_height;d++)
212 if (tex->name[0] == '{' && p == 255)
213 out[0] = out[1] = out[2] = out[3] = 0;
227 unsigned char *W_GetTexture(char *name)
236 W_CleanupName (name, texname);
237 for (i = 0;i < TEXWAD_MAXIMAGES;i++)
239 if (texwadlump[i].name[0])
241 if (!strcmp(texname, texwadlump[i].name)) // found it
243 file = texwadlump[i].file;
244 if (FS_Seek(file, texwadlump[i].position, SEEK_SET))
245 {Con_Print("W_GetTexture: corrupt WAD3 file\n");return NULL;}
247 tex = (miptex_t *)Mem_Alloc(tempmempool, texwadlump[i].size);
250 if (FS_Read(file, tex, texwadlump[i].size) < texwadlump[i].size)
251 {Con_Print("W_GetTexture: corrupt WAD3 file\n");return NULL;}
253 tex->width = LittleLong(tex->width);
254 tex->height = LittleLong(tex->height);
255 for (j = 0;j < MIPLEVELS;j++)
256 tex->offsets[j] = LittleLong(tex->offsets[j]);
257 data = W_ConvertWAD3Texture(tex);
265 image_width = image_height = 0;