changed Draw_CachePic to load both tga/png/jpg and lmp/wad images at once and take...
[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 /*
28 ==================
29 W_CleanupName
30
31 Lowercases name and pads with spaces and a terminating 0 to the length of
32 lumpinfo_t->name.
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.
36 ==================
37 */
38 static void W_CleanupName (const char *in, char *out)
39 {
40         int             i;
41         int             c;
42
43         for (i=0 ; i<16 ; i++ )
44         {
45                 c = in[i];
46                 if (!c)
47                         break;
48
49                 if (c >= 'A' && c <= 'Z')
50                         c += ('a' - 'A');
51                 out[i] = c;
52         }
53
54         for ( ; i< 16 ; i++ )
55                 out[i] = 0;
56 }
57
58 unsigned char *W_GetLumpName(const char *name)
59 {
60         int i;
61         fs_offset_t filesize;
62         lumpinfo_t *lump;
63         char clean[16];
64         wadinfo_t *header;
65         int infotableofs;
66         static int wad_loaded = false;
67         static int wad_numlumps = 0;
68         static lumpinfo_t *wad_lumps = NULL;
69         static unsigned char *wad_base = NULL;
70
71         W_CleanupName (name, clean);
72
73         if (!wad_loaded)
74         {
75                 wad_loaded = true;
76                 if ((wad_base = FS_LoadFile ("gfx.wad", cls.permanentmempool, false, &filesize)))
77                 {
78                         if (memcmp(wad_base, "WAD2", 4))
79                         {
80                                 Con_Print("gfx.wad doesn't have WAD2 id\n");
81                                 Mem_Free(wad_base);
82                                 wad_base = NULL;
83                         }
84                         else
85                         {
86                                 header = (wadinfo_t *)wad_base;
87                                 wad_numlumps = LittleLong(header->numlumps);
88                                 infotableofs = LittleLong(header->infotableofs);
89                                 wad_lumps = (lumpinfo_t *)(wad_base + infotableofs);
90
91                                 for (i=0, lump = wad_lumps ; i<wad_numlumps ; i++,lump++)
92                                 {
93                                         lump->filepos = LittleLong(lump->filepos);
94                                         lump->size = LittleLong(lump->size);
95                                         W_CleanupName (lump->name, lump->name);
96                                 }
97                         }
98                 }
99         }
100
101         for (lump = wad_lumps, i = 0;i < wad_numlumps;i++, lump++)
102                 if (!strcmp(clean, lump->name))
103                         return (wad_base + lump->filepos);
104
105         if (wad_base)
106                 Con_DPrintf("W_GetLumpByName(\"%s\"): couldn't find file in gfx.wad\n", name);
107         else
108                 Con_DPrintf("W_GetLumpByName(\"%s\"): couldn't load gfx.wad\n", name);
109         return NULL;
110 }
111
112 /*
113 =============================================================================
114
115 automatic byte swapping
116
117 =============================================================================
118 */
119
120 // LordHavoc: added alternate WAD2/WAD3 system for HalfLife texture wads
121 #define TEXWAD_MAXIMAGES 16384
122 typedef struct texwadlump_s
123 {
124         char name[16];
125         qfile_t *file;
126         int position;
127         int size;
128 } texwadlump_t;
129
130 static texwadlump_t texwadlump[TEXWAD_MAXIMAGES];
131
132 /*
133 ====================
134 W_LoadTextureWadFile
135 ====================
136 */
137 void W_LoadTextureWadFile (char *filename, int complain)
138 {
139         lumpinfo_t              *lumps, *lump_p;
140         wadinfo_t               header;
141         int                             i, j;
142         int                             infotableofs;
143         qfile_t                 *file;
144         int                             numlumps;
145
146         file = FS_Open (filename, "rb", false, false);
147         if (!file)
148         {
149                 if (complain)
150                         Con_Printf("W_LoadTextureWadFile: couldn't find %s\n", filename);
151                 return;
152         }
153
154         if (FS_Read(file, &header, sizeof(wadinfo_t)) != sizeof(wadinfo_t))
155         {Con_Print("W_LoadTextureWadFile: unable to read wad header\n");return;}
156
157         if(memcmp(header.identification, "WAD3", 4))
158         {Con_Printf("W_LoadTextureWadFile: Wad file %s doesn't have WAD3 id\n",filename);return;}
159
160         numlumps = LittleLong(header.numlumps);
161         if (numlumps < 1 || numlumps > TEXWAD_MAXIMAGES)
162         {Con_Printf("W_LoadTextureWadFile: invalid number of lumps (%i)\n", numlumps);return;}
163         infotableofs = LittleLong(header.infotableofs);
164         if (FS_Seek (file, infotableofs, SEEK_SET))
165         {Con_Print("W_LoadTextureWadFile: unable to seek to lump table\n");return;}
166         if (!(lumps = (lumpinfo_t *)Mem_Alloc(tempmempool, sizeof(lumpinfo_t)*numlumps)))
167         {Con_Print("W_LoadTextureWadFile: unable to allocate temporary memory for lump table\n");return;}
168
169         if (FS_Read(file, lumps, sizeof(lumpinfo_t) * numlumps) != (fs_offset_t)sizeof(lumpinfo_t) * numlumps)
170         {Con_Print("W_LoadTextureWadFile: unable to read lump table\n");return;}
171
172         for (i=0, lump_p = lumps ; i<numlumps ; i++,lump_p++)
173         {
174                 W_CleanupName (lump_p->name, lump_p->name);
175                 for (j = 0;j < TEXWAD_MAXIMAGES;j++)
176                 {
177                         if (texwadlump[j].name[0]) // occupied slot, check the name
178                         {
179                                 if (!strcmp(lump_p->name, texwadlump[j].name)) // name match, replace old one
180                                         break;
181                         }
182                         else // empty slot
183                                 break;
184                 }
185                 if (j >= TEXWAD_MAXIMAGES)
186                         break; // abort loading
187                 W_CleanupName (lump_p->name, texwadlump[j].name);
188                 texwadlump[j].file = file;
189                 texwadlump[j].position = LittleLong(lump_p->filepos);
190                 texwadlump[j].size = LittleLong(lump_p->disksize);
191         }
192         Mem_Free(lumps);
193         // leaves the file open
194 }
195
196
197 unsigned char *W_ConvertWAD3Texture(miptex_t *tex)
198 {
199         unsigned char *in, *data, *out, *pal;
200         int d, p;
201
202         in = (unsigned char *)tex + tex->offsets[0];
203         data = out = (unsigned char *)Mem_Alloc(tempmempool, tex->width * tex->height * 4);
204         if (!data)
205                 return NULL;
206         image_width = tex->width;
207         image_height = tex->height;
208         pal = in + (((image_width * image_height) * 85) >> 6);
209         pal += 2;
210         for (d = 0;d < image_width * image_height;d++)
211         {
212                 p = *in++;
213                 if (tex->name[0] == '{' && p == 255)
214                         out[0] = out[1] = out[2] = out[3] = 0;
215                 else
216                 {
217                         p *= 3;
218                         out[0] = pal[p];
219                         out[1] = pal[p+1];
220                         out[2] = pal[p+2];
221                         out[3] = 255;
222                 }
223                 out += 4;
224         }
225         return data;
226 }
227
228 unsigned char *W_GetTexture(char *name)
229 {
230         char texname[17];
231         int i, j;
232         qfile_t *file;
233         miptex_t *tex;
234         unsigned char *data;
235
236         texname[16] = 0;
237         W_CleanupName (name, texname);
238         for (i = 0;i < TEXWAD_MAXIMAGES;i++)
239         {
240                 if (texwadlump[i].name[0])
241                 {
242                         if (!strcmp(texname, texwadlump[i].name)) // found it
243                         {
244                                 file = texwadlump[i].file;
245                                 if (FS_Seek(file, texwadlump[i].position, SEEK_SET))
246                                 {Con_Print("W_GetTexture: corrupt WAD3 file\n");return NULL;}
247
248                                 tex = (miptex_t *)Mem_Alloc(tempmempool, texwadlump[i].size);
249                                 if (!tex)
250                                         return NULL;
251                                 if (FS_Read(file, tex, texwadlump[i].size) < texwadlump[i].size)
252                                 {Con_Print("W_GetTexture: corrupt WAD3 file\n");return NULL;}
253
254                                 tex->width = LittleLong(tex->width);
255                                 tex->height = LittleLong(tex->height);
256                                 for (j = 0;j < MIPLEVELS;j++)
257                                         tex->offsets[j] = LittleLong(tex->offsets[j]);
258                                 data = W_ConvertWAD3Texture(tex);
259                                 Mem_Free(tex);
260                                 return data;
261                         }
262                 }
263                 else
264                         break;
265         }
266         image_width = image_height = 0;
267         return NULL;
268 }
269