home *** CD-ROM | disk | FTP | other *** search
/ Doom 2 Explosion / Doom2Explosion.bin / doom2exp / programs / ibsp101s / wad_dwd.c < prev    next >
C/C++ Source or Header  |  1994-08-05  |  5KB  |  190 lines

  1. /*
  2.    WAD_DWD.C
  3.    (c) 1994 Ron Rossbach (ej070@cleveland.freenet.edu)
  4.  
  5.    This program converts an ordinary WAD file into a slightly extended version
  6.    of a DWD file.  The DWD file can then be used as input to IDBSP
  7.  
  8.    Use and distribution of this code permitted according to the terms of the GNU
  9.    General Public License.
  10.  
  11.    version 1.0.1 by Antony Suter <antony@werple.apana.org.au>
  12.       these fixes and modification (c) 1994 by Antony Suter
  13.  */
  14.  
  15. #include "wad_dwd.h"
  16.  
  17. /*
  18.    ===============
  19.    =
  20.    = main
  21.    =
  22.    ===============
  23.  */
  24. int                 main(int argc, char *argv[])
  25. {
  26.     char                dwd_name[81];
  27.     char                wad_name[81];
  28.     char                tmp[9];
  29.     FILE               *dwd,
  30.                        *wad;
  31.     int                 i;
  32.     long                numthings,
  33.                         numvertexes,
  34.                         numlines,
  35.                         numsides,
  36.                         numsectors;
  37.     int                 episode,
  38.                         level;
  39.     mapvertex_t        *vertexes = NULL;
  40.     mapsidedef_t       *sidedefs = NULL;
  41.     maplinedef_t       *linedefs = NULL;
  42.     mapsector_t        *sectors = NULL;
  43.     mapthing_t         *things = NULL;
  44.     lumpinfo_t         *lumps = NULL;
  45.     wadinfo_t           wadheader;
  46.  
  47.     printf("** WAD_DWD - a WAD->DWD converter for DOOM - version 1.0.1 **"
  48.         "\n(c) 1994 Ron Rossbach (ej070@cleveland.freenet.edu)"
  49.         "\nThis compilation by Antony Suter <antony@werple.apana.org.au>"
  50.         "\n\nDOOM is a registered trademark of id Software, Inc."
  51.         "\n\nSee the accompanying README for terms of use, distribution, etc.");
  52.  
  53.     if (argc < 2 || argc > 3)
  54.         {
  55.         printf("\n\nUsage: \"wad_dwd wadfile [dwdfile]\""
  56.             "\nTMP.DWD is the default if dwdfile is not specified.");
  57.         Error("\nExiting....");
  58.         }
  59.  
  60.     strcpy(wad_name, argv[1]);
  61.  
  62.     if (argc == 3)
  63.         strcpy(dwd_name, argv[2]);
  64.     else
  65.         strcpy(dwd_name, "tmp.dwd");
  66.  
  67.     if ((wad = fopen(wad_name, "rb")) == NULL)
  68.         Error("Cannot open WAD file");
  69.  
  70.     if ((dwd = fopen(dwd_name, "w")) == NULL)
  71.         {
  72.         fclose(wad);
  73.         Error("Cannot open DWD file");
  74.         }
  75.  
  76.     /*
  77.        Read the WAD header
  78.      */
  79.     fread(&wadheader, sizeof(wadinfo_t), 1, wad);
  80.     fseek(wad, wadheader.infotableofs, SEEK_SET);
  81.  
  82.     printf("\n\nWAD file: %s\nDWD file: %s\n", wad_name, dwd_name);
  83.  
  84.     /*
  85.        Read the WAD directory
  86.      */
  87.     lumps = ReadWAD(wad, wadheader.numlumps, sizeof(lumpinfo_t));
  88.  
  89.     /*
  90.        Process directory entries
  91.      */
  92.     WriteHeader(dwd);
  93.     for (i = 0; i < wadheader.numlumps; i++)
  94.         {
  95.         strncpy(tmp, lumps[i].name, 8);
  96.         tmp[8] = '\0';
  97.  
  98.         if (strlen(tmp) <= 0)
  99.             continue;
  100.  
  101.         /* Handle map levels */
  102.         if (sscanf(tmp, "E%dM%d", &episode, &level) == 2)
  103.             {
  104.             printf("\nWriting Level: %s", tmp);
  105.             things = (mapthing_t *) ReadStorage(wad, &lumps[++i], &numthings, sizeof(mapthing_t));
  106.             linedefs = (maplinedef_t *) ReadStorage(wad, &lumps[++i], &numlines, sizeof(maplinedef_t));
  107.             sidedefs = (mapsidedef_t *) ReadStorage(wad, &lumps[++i], &numsides, sizeof(mapsidedef_t));
  108.             vertexes = (mapvertex_t *) ReadStorage(wad, &lumps[++i], &numvertexes, sizeof(mapvertex_t));
  109.             i += 4;
  110.             sectors = (mapsector_t *) ReadStorage(wad, &lumps[i], &numsectors, sizeof(mapsector_t));
  111.             i += 2;
  112.             fprintf(dwd, "\nlevel:E%dM%d\n", episode, level);
  113.             WriteLines(dwd, numlines, linedefs, vertexes, sidedefs, sectors);
  114.             WriteThings(dwd, numthings, things);
  115.             free(vertexes);
  116.             free(sidedefs);
  117.             free(linedefs);
  118.             free(sectors);
  119.             }
  120.         /* Other Resources */
  121.         else
  122.             {
  123.             printf("\nWriting Resource: %s", tmp);
  124.             fprintf(dwd, "\n%s :%d\n", tmp, lumps[i].size);
  125.             ExtractResource(wad, &lumps[i]);
  126.             }
  127.         }
  128.  
  129.     free(lumps);
  130.     fclose(wad);
  131.     fclose(dwd);
  132.  
  133.     printf("\n");
  134.  
  135.     return 0;
  136. }
  137.  
  138. /*
  139.    ===============
  140.    =
  141.    = progress
  142.    =
  143.    = provides some visual feedback for the user....
  144.    =
  145.    ===============
  146.  */
  147. void                progress(void)
  148. {
  149.     char               *s = "/-\\|/-\\|";
  150.     static unsigned char pcnt = 0;
  151.  
  152.     if ((pcnt & 15) == 0)
  153.         {
  154.         printf("%c\b", s[((pcnt) / 16) & 7]);
  155.         fflush(stdout);
  156.         }
  157.     pcnt++;
  158. }
  159.  
  160. /*
  161.    ====================
  162.    =
  163.    = Error
  164.    =
  165.    = Displays an error message and exits program
  166.    =
  167.    ====================
  168.  */
  169. void                Error(char *error,...)
  170. {
  171.     va_list             argptr;
  172.  
  173.     va_start(argptr, error);
  174.     vprintf(error, argptr);
  175.     va_end(argptr);
  176.     printf("\n");
  177.     exit(1);
  178. }
  179.  
  180.  
  181. void               *SafeCalloc(unsigned num, size_t size)
  182. {
  183.     void               *ret = (void *)calloc(num, size);
  184.  
  185.     if (!ret)
  186.         Error("\nSafeCalloc: Failed to allocate %u of %u bytes", num, size);
  187.  
  188.     return ret;
  189. }
  190.