/ source / tools / src / givedepth.cpp
givedepth.cpp
  1  #include "compat.h"
  2  
  3  #define MAXNUMTILES 256
  4  
  5  int artversion, numtiles;
  6  int localtilestart, localtileend;
  7  short tilesizx[MAXNUMTILES], tilesizy[MAXNUMTILES];
  8  int picanm[MAXNUMTILES];
  9  
 10  FILE * openartfile(char *fn)
 11  {
 12      FILE *fh;
 13  
 14      fh = fopen(fn,"rb");
 15      if (!fh) return NULL;
 16  
 17      fread(&artversion,4,1,fh); if (artversion != 1) { puts("Bad art version"); goto fail; }
 18      fread(&numtiles,4,1,fh);
 19      fread(&localtilestart,4,1,fh);
 20      fread(&localtileend,4,1,fh);
 21      numtiles = localtileend-localtilestart+1;
 22      if (numtiles > MAXNUMTILES) { puts("Too many tiles"); goto fail; }
 23      fread(tilesizx,2,numtiles,fh);
 24      fread(tilesizy,2,numtiles,fh);
 25      fread(picanm,4,numtiles,fh);
 26  
 27      return fh;
 28  fail:
 29      fclose(fh);
 30      return NULL;
 31  }
 32  
 33  int main(int argc, char **argv)
 34  {
 35      char const * palfile = "palette.dat", * voxfile = "output.vox";
 36      int tilenum;
 37      int depth;
 38      FILE *artfh, *voxfh, *palfh;
 39      int tilesz;
 40      unsigned char palette[768];
 41      unsigned char *tiledata;
 42      int i;
 43  
 44      if (argc < 4) {
 45          puts("givedepth <artfile.art> <tilenum> <depth> [palette.dat] [output.vox]");
 46          return 0;
 47      }
 48  
 49      tilenum = atoi(argv[2]);
 50      depth = atoi(argv[3]);
 51      if (argc >= 4) palfile = argv[4];
 52      if (argc >= 5) voxfile = argv[5];
 53  
 54      palfh = fopen(palfile,"rb");
 55      if (!palfh) {
 56          puts("Failure opening palette file");
 57          return 1;
 58      }
 59      fread(palette,768,1,palfh);
 60      fclose(palfh);
 61  
 62      artfh = openartfile(argv[1]);
 63      if (!artfh) {
 64          puts("Failure opening art file");
 65          return 1;
 66      }
 67  
 68      if (tilenum < 0 || tilenum > numtiles) {
 69          puts("Tilenum out of range in art file");
 70          fclose(artfh);
 71          return 1;
 72      }
 73      for (i=0; i<tilenum; i++) fseek(artfh, tilesizx[i] * tilesizy[i], SEEK_CUR);
 74  
 75      tilesz = tilesizx[tilenum]*tilesizy[tilenum];
 76      tiledata = (unsigned char *)malloc(tilesz);
 77      if (!tiledata) {
 78          puts("Could not allocate memory for tile");
 79          fclose(artfh);
 80          return 1;
 81      }
 82  
 83      fread(tiledata, tilesz, 1, artfh);
 84      fclose(artfh);
 85  
 86      voxfh = fopen(voxfile,"wb");
 87      if (!voxfh) {
 88          puts("Could not create output file");
 89          free(tiledata);
 90          return 1;
 91      }
 92      fwrite(&depth,4,1,voxfh);
 93      fwrite(&tilesizx[tilenum],4,1,voxfh);
 94      fwrite(&tilesizy[tilenum],4,1,voxfh);
 95      for (i=0; i<depth; i++) {
 96          fwrite(tiledata,tilesz,1,voxfh);
 97      }
 98      fwrite(palette,768,1,voxfh);
 99  
100      free(tiledata);
101  
102      return 0;
103  }