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 // wad.c
21 
22 #include "quakedef.h"
23 
24 int			wad_numlumps;
25 lumpinfo_t	*wad_lumps;
26 byte		*wad_base;
27 
28 void SwapPic (qpic_t *pic);
29 
30 /*
31 ==================
32 W_CleanupName
33 
34 Lowercases name and pads with spaces and a terminating 0 to the length of
35 lumpinfo_t->name.
36 Used so lumpname lookups can proceed rapidly by comparing 4 chars at a time
37 Space padding is so names can be printed nicely in tables.
38 Can safely be performed in place.
39 ==================
40 */
W_CleanupName(char * in,char * out)41 void W_CleanupName (char *in, char *out)
42 {
43 	int		i;
44 	int		c;
45 
46 	for (i=0 ; i<16 ; i++ )
47 	{
48 		c = in[i];
49 		if (!c)
50 			break;
51 
52 		if (c >= 'A' && c <= 'Z')
53 			c += ('a' - 'A');
54 		out[i] = c;
55 	}
56 
57 	for ( ; i< 16 ; i++ )
58 		out[i] = 0;
59 }
60 
61 
62 
63 /*
64 ====================
65 W_LoadWadFile
66 ====================
67 */
W_LoadWadFile(char * filename)68 void W_LoadWadFile (char *filename)
69 {
70 	lumpinfo_t		*lump_p;
71 	wadinfo_t		*header;
72 	unsigned		i;
73 	int				infotableofs;
74 
75 	wad_base = COM_LoadHunkFile (filename);
76 	if (!wad_base)
77 		Sys_Error ("W_LoadWadFile: couldn't load %s", filename);
78 
79 	header = (wadinfo_t *)wad_base;
80 
81 	if (header->identification[0] != 'W'
82 	|| header->identification[1] != 'A'
83 	|| header->identification[2] != 'D'
84 	|| header->identification[3] != '2')
85 		Sys_Error ("Wad file %s doesn't have WAD2 id\n",filename);
86 
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_p = wad_lumps ; i<wad_numlumps ; i++,lump_p++)
92 	{
93 		lump_p->filepos = LittleLong(lump_p->filepos);
94 		lump_p->size = LittleLong(lump_p->size);
95 		W_CleanupName (lump_p->name, lump_p->name);
96 		if (lump_p->type == TYP_QPIC)
97 			SwapPic ( (qpic_t *)(wad_base + lump_p->filepos));
98 	}
99 }
100 
101 
102 /*
103 =============
104 W_GetLumpinfo
105 =============
106 */
W_GetLumpinfo(char * name)107 lumpinfo_t	*W_GetLumpinfo (char *name)
108 {
109 	int		i;
110 	lumpinfo_t	*lump_p;
111 	char	clean[16];
112 
113 	W_CleanupName (name, clean);
114 
115 	for (lump_p=wad_lumps, i=0 ; i<wad_numlumps ; i++,lump_p++)
116 	{
117 		if (!strcmp(clean, lump_p->name))
118 			return lump_p;
119 	}
120 
121 	Sys_Error ("W_GetLumpinfo: %s not found", name);
122 	return NULL;
123 }
124 
W_GetLumpName(char * name)125 void *W_GetLumpName (char *name)
126 {
127 	lumpinfo_t	*lump;
128 
129 	lump = W_GetLumpinfo (name);
130 
131 	return (void *)(wad_base + lump->filepos);
132 }
133 
W_GetLumpNum(int num)134 void *W_GetLumpNum (int num)
135 {
136 	lumpinfo_t	*lump;
137 
138 	if (num < 0 || num > wad_numlumps)
139 		Sys_Error ("W_GetLumpNum: bad number: %i", num);
140 
141 	lump = wad_lumps + num;
142 
143 	return (void *)(wad_base + lump->filepos);
144 }
145 
146 /*
147 =============================================================================
148 
149 automatic byte swapping
150 
151 =============================================================================
152 */
153 
SwapPic(qpic_t * pic)154 void SwapPic (qpic_t *pic)
155 {
156 	pic->width = LittleLong(pic->width);
157 	pic->height = LittleLong(pic->height);
158 }
159