home *** CD-ROM | disk | FTP | other *** search
/ Doom 2 Explosion / Doom2Explosion.bin / doom2exp / programs / doomtx / retex.c < prev    next >
Text File  |  1994-08-26  |  5KB  |  175 lines

  1. /* RETEX v1.0. Part of DoomTex, by Steve McCrea 26/8/94 */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <dos.h>
  5. #include <io.h>
  6. #include <sys\types.h>
  7. #include <sys\stat.h>
  8. #include <malloc.h>
  9. #include <fcntl.h>
  10.  
  11. int    fh;
  12. FILE    *fp;
  13.  
  14. void error(char *errstr)
  15. {    printf("\nRetex: ");
  16.     printf(errstr);
  17.     exit(1);    }
  18.  
  19. main(argc,argv)
  20. int argc;
  21. char *argv[];
  22. {
  23.     char texname[10], patname[10], nearnam[10];
  24.     char *pnames, *pnam;
  25.     char *texgarb, *texg;
  26.     long *offsets, *offs;
  27.     long numtexs, numpats, onumpats, lnumpats, i;
  28.     int patnum, x, y, j, k;
  29.     
  30.     if (argc != 5)
  31.         error("Usage: retex <textfile> <old pnames> <new pnames> <textures>\n");
  32.  
  33.     texname[8]='\0';
  34.     patname[8]='\0';
  35.     nearnam[8]='\0';
  36.     
  37.     if (!(fp=fopen(argv[1], "rt")))
  38.         error("Can't open <textfile>.\n");
  39.     if ((pnames=(char *)malloc(8192))==NULL)
  40.         error("Can't allocate 8K for pnames.\n");
  41.     if ((texgarb=(char *)malloc(32768))==NULL)
  42.         error("Can't allocate 32K for textures.\n");
  43.     if ((offsets=(long *)malloc(8192))==NULL)
  44.         error("Can't allocate 8K for offsets.\n");
  45.     if (!(fh=open(argv[2], O_BINARY|O_RDONLY)))
  46.         error("Can't open the <old pnames> resource.\n");
  47.     read(fh, &numpats, 4);
  48.     onumpats=numpats;
  49.     if (read(fh, pnames, 8*numpats)!=8*numpats)
  50.         error("The <old pnames> resource is short.\n");
  51.     for (i=0; i<8*numpats; i++)
  52.         if (*(pnames+i)>0x40 && *(pnames+i)<0x5b) *(pnames+i) |= 0x20;
  53.     pnam=pnames;
  54.     texg=texgarb;
  55.     offs=offsets;
  56.     numtexs=0;
  57.     lnumpats=0;
  58.  
  59. // Skip to the textures
  60.  
  61.     if (fscanf(fp, "%s", texname)==EOF)
  62.         error("End of <textfile> before TEXTURES found.\n");
  63.     while (strcmp(texname, "TEXTURES"))
  64.         if (fscanf(fp, "%s", texname)==EOF)
  65.             error("End of <textfile> before TEXTURES found.\n");
  66.     for (k=0; k<8; k++) nearnam[k]=texname[k];
  67.  
  68. // Loop thru textures
  69.  
  70.     numtexs=0;
  71.     if (fscanf(fp, "%s", texname)==EOF)
  72.         error("End of <textfile> before EOF found.\n");
  73.     while (strcmp(texname, "EOF")) {
  74.         if (texname[0]<0x41 || texname[0]>0x5a) {
  75.             printf("\nRetex: After %8s: %8s not a valid texture or patch name.\n", nearnam, texname);
  76.             exit(1);
  77.             }
  78.         for (k=0; k<8; k++) nearnam[k]=texname[k];
  79.         numtexs++;
  80.         *offs++=texg-texgarb;
  81.         for (k=0;k<8;k++) *(texg++)=texname[k];
  82.         *((long *)texg)=0; texg+=4;
  83.         fscanf(fp, "%d %d", &x, &y);
  84.         if (x<1 || x>256) {
  85.             printf("\nRetex: Texture %8s x value %d out of range.\n", texname, x);
  86.             exit(1);
  87.             }
  88.         i=1; j=0; for (k=0; k<9; k++) { i *= 2; if (i==x) j=1; }
  89.         if (j==0) printf("\nWarning: Texture %8s x value %d is not a power of 2.\n", texname, x);
  90.         *((int *)texg)=x; texg+=2;
  91.         if (y<1 || y>128) {
  92.             printf("\nRetex: Texture %8s y value %d out of range.\n", texname, y);
  93.             exit(1);
  94.             }
  95.         *((int *)texg)=y; texg+=2;
  96.         *((long *)texg)=0; texg+=6;
  97.         if (fscanf(fp, "%s", patname)==EOF)
  98.             error("File truncated.\n");
  99.         lnumpats=0;
  100.         while (patname[0]>0x60 && patname[0]<0x7b) {
  101.             lnumpats++;
  102.             i=0; j=0;
  103.             while (j<100 && i<numpats) {
  104.                 k=0; while (k<9) if (patname[k++]==0) break;
  105.                 k--;
  106.                 if (strncmp(patname, pnames+8*i, 8) == 0)
  107.                     j=100;
  108.                 i++;
  109.                 }
  110.             if (j==100)
  111.                 patnum=i-1;
  112.             else {
  113.                 patnum=numpats++;
  114.                 for (k=0; k<8; k++)
  115.                     *(pnames+8*patnum+k)=patname[k];
  116.                 }
  117.             fscanf(fp, "%d %d", &x, &y);
  118.             if (x<-512 || y<-512 || x>512 || y>512) {
  119.                 printf("\nRetex: Patch %8s out of bounds.\n", patname);
  120.                 exit(1);
  121.                 }
  122.             *((int *)texg)=x; texg+=2;
  123.             *((int *)texg)=y; texg+=2;
  124.             *((int *)texg)=patnum; texg+=2;
  125.             *((long *)texg)=0x00000001; texg+=4;
  126.             if (fscanf(fp, "%s", patname)==EOF)
  127.                 error("End of <textfile> before EOF found.\n");
  128.             }
  129.         if (lnumpats==0) {
  130.             printf("\nRetex: Texture %8s without a patch.\n", texname);
  131.             exit(1);
  132.             }
  133.         *((int *)(texg-10*lnumpats-2))=lnumpats;
  134.         for (k=0; k<8; k++) texname[k]=patname[k];
  135.         printf("#");
  136.         }
  137.     switch (numpats-onumpats) {
  138.         case 0:    {
  139.             printf("\nNo new patches.\n");
  140.             break;
  141.             }
  142.         default: {
  143.             printf("\n%d new patches:\n", numpats-onumpats);
  144.             j=0;
  145.             for (i=onumpats; i<numpats; i++) {
  146.                 for (k=0; k<8; k++) patname[k]=*(pnames+8*i+k);
  147.                 printf("%8s", patname);
  148.                 if (++j==7) {j=0; printf("\n"); }
  149.                     else printf("  ");
  150.                 }
  151.             }
  152.         }
  153.     offs=offsets;
  154.     for (i=0; i<numtexs; i++)
  155.         *offs++ += 4*numtexs+4;
  156.  
  157.     if (!(fh=open(argv[3], O_CREAT|O_BINARY|O_WRONLY, S_IREAD|S_IWRITE)))
  158.         error("Can't create <pnames> resource.\n");
  159.     write(fh, &numpats, 4);
  160.     if (write(fh, pnames, 8*numpats) != 8*numpats)
  161.         error("No space to write <pnames>.\n");
  162.     close(fh);
  163.     if (!(fh=open(argv[4], O_CREAT|O_BINARY|O_WRONLY, S_IREAD|S_IWRITE)))
  164.         error("Can't create <texture> resource.\n");
  165.     write(fh, &numtexs, 4);
  166.     write(fh, offsets, 4*numtexs);
  167.     if (write(fh, texgarb, texg-texgarb) != texg-texgarb)
  168.         error("No space to write <textures>.\n");
  169.     close(fh);
  170.  
  171.     free(pnames);
  172.     free(texgarb);
  173.     
  174.     exit(0);
  175. }