1 // "Build Engine & Tools" Copyright (c) 1993-1997 Ken Silverman
2 // Ken Silverman's official web site: "http://www.advsys.net/ken"
3 // See the included license file "BUILDLIC.TXT" for license info.
4 //
5 // This file has been modified from Ken Silverman's original release
6 // by Jonathon Fowler (jf@jonof.id.au)
7 
8 #include "compat.h"
9 #include "pragmas.h"
10 
11 #define MAXWADS 4096
12 
13 static int artversion, localtilestart, localtileend;
14 static int fil1, fil2;
15 static char wadata[MAXWADS][8];
16 static int wadplc[MAXWADS], wadlen[MAXWADS], numwads;
17 static int xoffses[1024], ylookup[256], picanm[MAXWADS];
18 static short tilesizx[MAXWADS], tilesizy[MAXWADS];
19 static unsigned char pal[768], palookup[8192];
20 static unsigned char screen[65536], tempbuf[131072];
21 
22 
loadwadheader(void)23 void loadwadheader(void)
24 {
25 	int i, j;
26 
27 	Bread(fil1,&tempbuf[0],12);
28 	numwads = ((int)tempbuf[4])+(((int)tempbuf[5])<<8)+(((int)tempbuf[6])<<16)+(((int)tempbuf[7])<<24);
29 	i = ((int)tempbuf[8])+(((int)tempbuf[9])<<8)+(((int)tempbuf[10])<<16)+(((int)tempbuf[11])<<24);
30 	Blseek(fil1,i,BSEEK_SET);
31 	Bread(fil1,&tempbuf[0],numwads*16);
32 	j = 0;
33 	for(i=0;i<numwads;i++)
34 	{
35 		wadplc[i] = ((int)tempbuf[j])+(((int)tempbuf[j+1])<<8)+(((int)tempbuf[j+2])<<16)+(((int)tempbuf[j+3])<<24);
36 		j += 4;
37 		wadlen[i] = ((int)tempbuf[j])+(((int)tempbuf[j+1])<<8)+(((int)tempbuf[j+2])<<16)+(((int)tempbuf[j+3])<<24);
38 		j += 4;
39 		wadata[i][0] = tempbuf[j+0]; wadata[i][1] = tempbuf[j+1];
40 		wadata[i][2] = tempbuf[j+2]; wadata[i][3] = tempbuf[j+3];
41 		wadata[i][4] = tempbuf[j+4]; wadata[i][5] = tempbuf[j+5];
42 		wadata[i][6] = tempbuf[j+6]; wadata[i][7] = tempbuf[j+7];
43 		j += 8;
44 	}
45 }
46 
convpalette(void)47 void convpalette(void)
48 {
49 	int i, fil3;
50 	short danumshades;
51 
52 	i = 0;
53 	while (Bstrncasecmp(wadata[i],"PLAYPAL",7) != 0) i++;
54 	Blseek(fil1,wadplc[i],BSEEK_SET);
55 	Bread(fil1,pal,768);
56 	for(i=0;i<768;i++) pal[i] >>= 2;
57 
58 	i = 0;
59 	while (Bstrncasecmp(wadata[i],"COLORMAP",8) != 0) i++;
60 	Blseek(fil1,wadplc[i],BSEEK_SET);
61 	Bread(fil1,palookup,8192);
62 
63 	if ((fil3 = Bopen("palette.dat",BO_BINARY|BO_TRUNC|BO_CREAT|BO_WRONLY,BS_IREAD|BS_IWRITE)) == -1)
64 		{ printf("Cannot save palette.dat\n"); exit(0); }
65 	Bwrite(fil3,pal,768);
66 	danumshades = 32;
67 	Bwrite(fil3,&danumshades,2);
68 	Bwrite(fil3,palookup,8192);
69 	Bclose(fil3);
70 }
71 
saveart(short tilenum,short xlen,short ylen)72 void saveart (short tilenum, short xlen, short ylen)
73 {
74 	int i, x, p, pend;
75 
76 	pend = ylookup[ylen];
77 
78 	tilesizx[tilenum] = xlen;
79 	tilesizy[tilenum] = ylen;
80 	i = 0;
81 	for(x=0;x<xlen;x++)
82 		for(p=x;p<pend;p+=320)
83 			tempbuf[i++] = screen[p];
84 	if (Bwrite(fil2,&tempbuf[0],i) < 0)
85 	{
86 		printf("NOT ENOUGH DISK SPACE!\n");
87 		exit(0);
88 	}
89 }
90 
savenames(void)91 void savenames(void)
92 {
93 	char buffer[160];
94 	int fil3, i, j;
95 
96 	if ((fil3 = Bopen("names.h",BO_BINARY|BO_TRUNC|BO_CREAT|BO_WRONLY,BS_IREAD|BS_IWRITE)) == -1)
97 		return;
98 
99 	strcpy(buffer,"//Be careful when changing this file - it is parsed by Editart and Build.");
100 	buffer[73] = 13, buffer[74] = 10, buffer[75] = 0;
101 	Bwrite(fil3,&buffer[0],75);
102 
103 	strcpy(buffer,"#define ");
104 	for(i=0;i<numwads;i++)
105 		if (wadata[i][0] != 0)
106 		{
107 			j = 8;
108 			while ((j < 16) && (wadata[i][j-8] != 0))
109 				{ buffer[j] = wadata[i][j-8]; j++; }
110 			buffer[j++] = 32;
111 
112 			if (i >= 10000) buffer[j++] = ((i/10000)%10)+48;
113 			if (i >= 1000) buffer[j++] = ((i/1000)%10)+48;
114 			if (i >= 100) buffer[j++] = ((i/100)%10)+48;
115 			if (i >= 10) buffer[j++] = ((i/10)%10)+48;
116 			buffer[j++] = (i%10)+48;
117 
118 			buffer[j++] = 13;
119 			buffer[j++] = 10;
120 			Bwrite(fil3,&buffer[0],j);
121 		}
122 
123 	Bclose(fil3);
124 }
125 
showart(char * part)126 void showart (char *part)
127 {
128 	unsigned char yoff, ylen;
129 	short xsiz, ysiz;
130 	int i, j, z, zx, zzx, x, y, p, pend, junk, curplc;
131 
132 	curplc = -1;
133 	if ((Bstrncasecmp(part,"L_START",7) == 0) || (Bstrncasecmp(part,"S_START",7) == 0) || (Bstrncasecmp(part,"P_START",7) == 0))
134 	{
135 		if (Bstrncasecmp(part,"L_START",7) == 0)
136 			z = 462;
137 		else
138 		{
139 			z = 0;
140 			while (Bstrncasecmp(wadata[z],part,7) != 0) z++;
141 			z++;
142 		}
143 
144 		do
145 		{
146 			if (Bstrncasecmp(wadata[z],"P1_START",8) == 0) z++;
147 			if (Bstrncasecmp(wadata[z],"P1_END",6) == 0) z++;
148 			if (Bstrncasecmp(wadata[z],"P2_START",8) == 0) z++;
149 			if (Bstrncasecmp(wadata[z],"S_START",7) == 0) break;
150 			if (Bstrncasecmp(wadata[z],"S_END",5) == 0) break;
151 			if (Bstrncasecmp(wadata[z],"P_END",5) == 0) break;
152 
153 			if (curplc != wadplc[z]) Blseek(fil1,wadplc[z],BSEEK_SET);
154 			read(fil1,&tempbuf[0],wadlen[z]);
155 			curplc = wadplc[z]+wadlen[z];
156 
157 			xsiz = (int)tempbuf[0]+(((int)tempbuf[1])<<8);
158 			ysiz = (int)tempbuf[2]+(((int)tempbuf[3])<<8);
159 			if ((xsiz <= 0) || (ysiz <= 0) || (xsiz > 320) || (ysiz > 200)) goto skipit;
160 			i = 8;
161 			for(zx=0;zx<xsiz;zx++)
162 			{
163 				xoffses[zx] = ((int)tempbuf[i])+(((int)tempbuf[i+1])<<8)+(((int)tempbuf[i+2])<<16)+(((int)tempbuf[i+3])<<24);
164 				i += 4;
165 			}
166 
167 			clearbuf(screen,ylookup[ysiz]>>2,0xffffffff);
168 
169 			for(x=0;x<xsiz;x++)
170 			{
171 				i = xoffses[x];
172 				yoff = tempbuf[i++];
173 				while (yoff != 255)
174 				{
175 					p = ylookup[yoff]+x;
176 					pend = p+ylookup[tempbuf[i]];
177 					i += 2;
178 					for(;p<pend;p+=320)
179 						screen[p] = tempbuf[i++];
180 					i++;
181 					yoff = tempbuf[i++];
182 				}
183 			}
184 
185 			saveart(z,xsiz,ysiz);
186 skipit:
187 			z++;
188 		} while (z < numwads);
189 	}
190 	else
191 	{
192 		z = 0;
193 		while (Bstrncasecmp(wadata[z],part,7) != 0) z++;
194 		z++;
195 
196 		while (1)
197 		{
198 			if (Bstrncasecmp(wadata[z],"F1_START",8) == 0) z++;
199 			if (Bstrncasecmp(wadata[z],"F1_END",6) == 0) z++;
200 			if (Bstrncasecmp(wadata[z],"F_END",5) == 0) break;
201 
202 			if (wadlen[z] == 4096)
203 			{
204 				if (curplc != wadplc[z]) Blseek(fil1,wadplc[z],BSEEK_SET);
205 				Bread(fil1,&tempbuf[0],4096);
206 				curplc = wadplc[z]+4096;
207 				i = 0;
208 				for(x=0;x<64;x++)
209 					for(p=x;p<320*64;p+=320)
210 						screen[p] = tempbuf[i++];
211 
212 				saveart(z,64,64);
213 			}
214 
215 			z++;
216 		}
217 	}
218 }
219 
main(int argc,char ** argv)220 int main(int argc, char **argv)
221 {
222 	int i, j, endoffile;
223 	char wadfile[80];
224 
225 	printf("Wad2Art!                                       Copyright 1995 by Ken Silverman\n");
226 
227 	if (argc != 2)
228 	{
229 		printf("Command line parameters: Wad2Art [Doom IWAD file]\n");
230 		printf("   Creates TILES000.ART, PALETTE.DAT, and NAMES.H in current directory.\n");
231 		printf("   Ex: wad2art c:\\doom\\doom.wad\n");
232 		exit(0);
233 	}
234 
235 	strcpy(wadfile,argv[1]);
236 	if (strchr(wadfile,'.') == 0) strcat(wadfile,".wad");
237 	if ((fil1 = Bopen(wadfile,BO_BINARY|BO_RDONLY,BS_IREAD)) == -1)
238 		{ printf("Wad not found\n"); exit(0); }
239 	if ((fil2 = Bopen("tiles000.art",BO_BINARY|BO_TRUNC|BO_CREAT|BO_WRONLY,BS_IREAD|BS_IWRITE)) == -1)
240 		{ printf("Can't open art file\n"); exit(0); }
241 
242 	j = 0;
243 	for(i=0;i<256;i++) { ylookup[i] = j; j += 320; }
244 
245 	printf("Loading wad header...\n");
246 	loadwadheader();
247 	Blseek(fil2,16+(numwads<<3),SEEK_SET);
248 	for(i=0;i<numwads;i++)
249 		{ tilesizx[i] = 0; tilesizy[i] = 0; picanm[i] = 0L; }
250 
251 	printf("Saving names.h\n");
252 	savenames();
253 	printf("Converting palette\n");
254 	convpalette();
255 
256 	printf("Saving tiles000.art\n");
257 	showart("L_START");
258 	showart("S_START");
259 	showart("P_START");
260 	showart("F_START");
261 
262 	printf("Saving tiles000.art header\n");
263 	artversion = 1; localtilestart = 0; localtileend = numwads-1;
264 
265 	endoffile = Btell(fil2);
266 	Blseek(fil2,0,BSEEK_SET);
267 	Bwrite(fil2,&artversion,4);
268 	Bwrite(fil2,&numwads,4);
269 	Bwrite(fil2,&localtilestart,4);
270 	Bwrite(fil2,&localtileend,4);
271 	Bwrite(fil2,&tilesizx[0],numwads<<1);
272 	Bwrite(fil2,&tilesizy[0],numwads<<1);
273 	Bwrite(fil2,&picanm[0],numwads<<2);
274 	Blseek(fil2,endoffile,BSEEK_SET);
275 
276 	Bclose(fil2);
277 	Bclose(fil1);
278 
279 	printf("Congratulations!  Your disk actually had enough space this time!\n");
280 
281 	return 0;
282 }
283 
284