/ source / tools / src / wad2art.cpp
wad2art.cpp
  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 char pal[768], palookup[8192];
 20  static char screen[65536], tempbuf[131072];
 21  
 22  
 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  
 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          { Bprintf("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  
 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          Bprintf("NOT ENOUGH DISK SPACE!\n");
 87          exit(0);
 88      }
 89  }
 90  
 91  void savenames(void)
 92  {
 93      BFILE * fil3;
 94      int i;
 95  
 96      fil3 = Bfopen("names.h","w");
 97  
 98      if (fil3 != NULL)
 99      {
100          Bfprintf(fil3,"//Be careful when changing this file - it is parsed by Editart and Build.\n");
101  
102          for(i=0; i<numwads; i++)
103              if (wadata[i][0] != 0)
104                  Bfprintf(fil3,"#define %s %d\n", wadata[i], i);
105  
106          Bfclose(fil3);
107      }
108  }
109  
110  void showart (char const * part)
111  {
112      char yoff;
113      short xsiz, ysiz;
114      int i, z, zx, x, p, pend, curplc;
115  
116      curplc = -1;
117      if ((Bstrncasecmp(part,"L_START",7) == 0) || (Bstrncasecmp(part,"S_START",7) == 0) || (Bstrncasecmp(part,"P_START",7) == 0))
118      {
119          if (Bstrncasecmp(part,"L_START",7) == 0)
120              z = 462;
121          else
122          {
123              z = 0;
124              while (Bstrncasecmp(wadata[z],part,7) != 0) z++;
125              z++;
126          }
127  
128          do
129          {
130              if (Bstrncasecmp(wadata[z],"P1_START",8) == 0) z++;
131              if (Bstrncasecmp(wadata[z],"P1_END",6) == 0) z++;
132              if (Bstrncasecmp(wadata[z],"P2_START",8) == 0) z++;
133              if (Bstrncasecmp(wadata[z],"S_START",7) == 0) break;
134              if (Bstrncasecmp(wadata[z],"S_END",5) == 0) break;
135              if (Bstrncasecmp(wadata[z],"P_END",5) == 0) break;
136  
137              if (curplc != wadplc[z]) Blseek(fil1,wadplc[z],BSEEK_SET);
138              read(fil1,&tempbuf[0],wadlen[z]);
139              curplc = wadplc[z]+wadlen[z];
140  
141              xsiz = (int)tempbuf[0]+(((int)tempbuf[1])<<8);
142              ysiz = (int)tempbuf[2]+(((int)tempbuf[3])<<8);
143              if ((xsiz <= 0) || (ysiz <= 0) || (xsiz > 320) || (ysiz > 200)) goto skipit;
144              i = 8;
145              for(zx=0;zx<xsiz;zx++)
146              {
147                  xoffses[zx] = ((int)tempbuf[i])+(((int)tempbuf[i+1])<<8)+(((int)tempbuf[i+2])<<16)+(((int)tempbuf[i+3])<<24);
148                  i += 4;
149              }
150  
151              clearbuf(screen,ylookup[ysiz]>>2,0xffffffff);
152  
153              for(x=0;x<xsiz;x++)
154              {
155                  i = xoffses[x];
156                  yoff = tempbuf[i++];
157                  while (yoff != 255)
158                  {
159                      p = ylookup[yoff]+x;
160                      pend = p+ylookup[tempbuf[i]];
161                      i += 2;
162                      for(;p<pend;p+=320)
163                          screen[p] = tempbuf[i++];
164                      i++;
165                      yoff = tempbuf[i++];
166                  }
167              }
168  
169              saveart(z,xsiz,ysiz);
170  skipit:
171              z++;
172          } while (z < numwads);
173      }
174      else
175      {
176          z = 0;
177          while (Bstrncasecmp(wadata[z],part,7) != 0) z++;
178          z++;
179  
180          while (1)
181          {
182              if (Bstrncasecmp(wadata[z],"F1_START",8) == 0) z++;
183              if (Bstrncasecmp(wadata[z],"F1_END",6) == 0) z++;
184              if (Bstrncasecmp(wadata[z],"F_END",5) == 0) break;
185  
186              if (wadlen[z] == 4096)
187              {
188                  if (curplc != wadplc[z]) Blseek(fil1,wadplc[z],BSEEK_SET);
189                  Bread(fil1,&tempbuf[0],4096);
190                  curplc = wadplc[z]+4096;
191                  i = 0;
192                  for(x=0;x<64;x++)
193                      for(p=x;p<320*64;p+=320)
194                          screen[p] = tempbuf[i++];
195  
196                  saveart(z,64,64);
197              }
198  
199              z++;
200          }
201      }
202  }
203  
204  int main(int argc, char **argv)
205  {
206      int i, j, endoffile;
207      char wadfile[80];
208  
209      Bprintf("Wad2Art!                                       Copyright 1995 by Ken Silverman\n");
210  
211      if (argc != 2)
212      {
213          Bprintf("Command line parameters: Wad2Art <Doom IWAD file>\n");
214          Bprintf("   Creates TILES000.ART, PALETTE.DAT, and NAMES.H in current directory.\n");
215          Bprintf("   Ex: wad2art c:\\doom\\doom.wad\n");
216          exit(0);
217      }
218  
219      strcpy(wadfile,argv[1]);
220      if (strchr(wadfile,'.') == 0) strcat(wadfile,".wad");
221      if ((fil1 = Bopen(wadfile,BO_BINARY|BO_RDONLY,BS_IREAD)) == -1)
222          { Bprintf("Wad not found\n"); exit(0); }
223      if ((fil2 = Bopen("tiles000.art",BO_BINARY|BO_TRUNC|BO_CREAT|BO_WRONLY,BS_IREAD|BS_IWRITE)) == -1)
224          { Bprintf("Can't open art file\n"); exit(0); }
225  
226      j = 0;
227      for(i=0;i<256;i++) { ylookup[i] = j; j += 320; }
228  
229      Bprintf("Loading wad header...\n");
230      loadwadheader();
231      Blseek(fil2,16+(numwads<<3),SEEK_SET);
232      for(i=0;i<numwads;i++)
233          { tilesizx[i] = 0; tilesizy[i] = 0; picanm[i] = 0L; }
234  
235      Bprintf("Saving names.h\n");
236      savenames();
237      Bprintf("Converting palette\n");
238      convpalette();
239  
240      Bprintf("Saving tiles000.art\n");
241      showart("L_START");
242      showart("S_START");
243      showart("P_START");
244      showart("F_START");
245  
246      Bprintf("Saving tiles000.art header\n");
247      artversion = 1; localtilestart = 0; localtileend = numwads-1;
248  
249      endoffile = Btell(fil2);
250      Blseek(fil2,0,BSEEK_SET);
251      Bwrite(fil2,&artversion,4);
252      Bwrite(fil2,&numwads,4);
253      Bwrite(fil2,&localtilestart,4);
254      Bwrite(fil2,&localtileend,4);
255      Bwrite(fil2,&tilesizx[0],numwads<<1);
256      Bwrite(fil2,&tilesizy[0],numwads<<1);
257      Bwrite(fil2,&picanm[0],numwads<<2);
258      Blseek(fil2,endoffile,BSEEK_SET);
259  
260      Bclose(fil2);
261      Bclose(fil1);
262  
263      Bprintf("Congratulations!  Your disk actually had enough space this time!\n");
264  
265      return 0;
266  }
267