home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 December / PCWKCD1296.iso / sharewar / quake106 / utils / qlumpy / qlumpy.c next >
C/C++ Source or Header  |  1996-09-12  |  5KB  |  302 lines

  1. #define VERSION "2.0"
  2. #include "qlumpy.h"
  3.  
  4.  
  5. #define MAXLUMP        0x50000         // biggest possible lump
  6.  
  7.  
  8. byte            *byteimage, *lbmpalette;
  9. int              byteimagewidth, byteimageheight;
  10.  
  11. char            basepath[1024];
  12. char            lumpname[16];
  13.  
  14. char            destfile[1024];
  15.  
  16. byte            *lumpbuffer, *lump_p;
  17.  
  18. qboolean        savesingle;
  19. qboolean        outputcreated;
  20.  
  21. /*
  22. =============================================================================
  23.  
  24.                             MAIN
  25.  
  26. =============================================================================
  27. */
  28.  
  29. void GrabRaw (void);
  30. void GrabPalette (void);
  31. void GrabPic (void);
  32. void GrabMip (void);
  33. void GrabColormap (void);
  34. void GrabColormap2 (void);
  35.  
  36. typedef struct
  37. {
  38.     char    *name;
  39.     void    (*function) (void);
  40. } command_t;
  41.  
  42. command_t       commands[] =
  43. {
  44.     {"palette",GrabPalette},
  45.     {"colormap",GrabColormap},
  46.     {"qpic",GrabPic},
  47.     {"miptex",GrabMip},
  48.     {"raw",GrabRaw},
  49.  
  50.     {"colormap2",GrabColormap2},
  51.  
  52.     {NULL,NULL}                     // list terminator
  53. };
  54.  
  55.  
  56.  
  57. /*
  58. ==============
  59. LoadScreen
  60. ==============
  61. */
  62. void LoadScreen (char *name)
  63. {
  64.     char    *expanded;
  65.  
  66.     expanded = ExpandPathAndArchive (name);
  67.  
  68.     printf ("grabbing from %s...\n",expanded);
  69.     LoadLBM (expanded, &byteimage, &lbmpalette);
  70.  
  71.     byteimagewidth = bmhd.w;
  72.     byteimageheight = bmhd.h;
  73. }
  74.  
  75.  
  76. /*
  77. ================
  78. CreateOutput
  79. ================
  80. */
  81. void CreateOutput (void)
  82. {
  83.     outputcreated = true;
  84. //
  85. // create the output wadfile file
  86. //
  87.     NewWad (destfile, false);    // create a new wadfile
  88. }
  89.  
  90. /*
  91. ===============
  92. WriteLump
  93. ===============
  94. */
  95. void WriteLump (int type, int compression)
  96. {
  97.     int        size;
  98.     
  99.     if (!outputcreated)
  100.         CreateOutput ();
  101.  
  102. //
  103. // dword align the size
  104. //
  105.     while ((int)lump_p&3)
  106.         *lump_p++ = 0;
  107.  
  108.     size = lump_p - lumpbuffer;
  109.     if (size > MAXLUMP)
  110.         Error ("Lump size exceeded %d, memory corrupted!",MAXLUMP);
  111.  
  112. //
  113. // write the grabbed lump to the wadfile
  114. //
  115.     AddLump (lumpname,lumpbuffer,size,type, compression);
  116. }
  117.  
  118. /*
  119. ===========
  120. WriteFile
  121.  
  122. Save as a seperate file instead of as a wadfile lump
  123. ===========
  124. */
  125. void WriteFile (void)
  126. {
  127.     char    filename[1024];
  128.     char    *exp;
  129.  
  130.     sprintf (filename,"%s/%s.lmp", destfile, lumpname);
  131.     exp = ExpandPath(filename);
  132.     printf ("saved %s\n", exp);
  133.     SaveFile (exp, lumpbuffer, lump_p-lumpbuffer);        
  134. }
  135.  
  136. /*
  137. ================
  138. ParseScript
  139. ================
  140. */
  141. void ParseScript (void)
  142. {
  143.     int            cmd;
  144.     int            size;
  145.     
  146.     do
  147.     {
  148.         //
  149.         // get a command / lump name
  150.         //
  151.         GetToken (true);
  152.         if (endofscript)
  153.             break;
  154.         if (!Q_strcasecmp (token,"$LOAD"))
  155.         {
  156.             GetToken (false);
  157.             LoadScreen (token);
  158.             continue;
  159.         }
  160.  
  161.         if (!Q_strcasecmp (token,"$DEST"))
  162.         {
  163.             GetToken (false);
  164.             strcpy (destfile, ExpandPath(token));
  165.             continue;
  166.         }
  167.  
  168.         if (!Q_strcasecmp (token,"$SINGLEDEST"))
  169.         {
  170.             GetToken (false);
  171.             strcpy (destfile, token);
  172.             savesingle = true;
  173.             continue;
  174.         }
  175.  
  176.  
  177.         //
  178.         // new lump
  179.         //
  180.         if (strlen(token) >= sizeof(lumpname) )
  181.             Error ("\"%s\" is too long to be a lump name",token);
  182.         memset (lumpname,0,sizeof(lumpname));            
  183.         strcpy (lumpname, token);
  184.         for (size=0 ; size<sizeof(lumpname) ; size++)
  185.             lumpname[size] = tolower(lumpname[size]);
  186.  
  187.         //
  188.         // get the grab command
  189.         //
  190.         lump_p = lumpbuffer;
  191.  
  192.         GetToken (false);
  193.  
  194.         //
  195.         // call a routine to grab some data and put it in lumpbuffer
  196.         // with lump_p pointing after the last byte to be saved
  197.         //
  198.         for (cmd=0 ; commands[cmd].name ; cmd++)
  199.             if ( !Q_strcasecmp(token,commands[cmd].name) )
  200.             {
  201.                 commands[cmd].function ();
  202.                 break;
  203.             }
  204.  
  205.         if ( !commands[cmd].name )
  206.             Error ("Unrecognized token '%s' at line %i",token,scriptline);
  207.     
  208.         grabbed++;
  209.         
  210.         if (savesingle)
  211.             WriteFile ();
  212.         else    
  213.             WriteLump (TYP_LUMPY+cmd, 0);
  214.         
  215.     } while (script_p < scriptend_p);
  216. }
  217.  
  218. /*
  219. =================
  220. ProcessLumpyScript
  221.  
  222. Loads a script file, then grabs everything from it
  223. =================
  224. */
  225. void ProcessLumpyScript (char *basename)
  226. {
  227.     char            script[256];
  228.  
  229.     printf ("qlumpy script: %s\n",basename);
  230.     
  231. //
  232. // create default destination directory
  233. //
  234.     strcpy (destfile, ExpandPath(basename));
  235.     StripExtension (destfile);
  236.     strcat (destfile,".wad");        // unless the script overrides, save in cwd
  237.  
  238. //
  239. // save in a wadfile by default
  240. //
  241.     savesingle = false;
  242.     grabbed = 0;
  243.     outputcreated = false;
  244.     
  245.     
  246. //
  247. // read in the script file
  248. //
  249.     strcpy (script, basename);
  250.     DefaultExtension (script, ".ls");
  251.     LoadScriptFile (script);
  252.     
  253.     strcpy (basepath, basename);
  254.     
  255.     ParseScript ();                // execute load / grab commands
  256.     
  257.     if (!savesingle)
  258.     {
  259.         WriteWad ();                // write out the wad directory
  260.         printf ("%i lumps grabbed in a wad file\n",grabbed);
  261.     }
  262.     else
  263.         printf ("%i lumps written seperately\n",grabbed);
  264. }
  265.  
  266.  
  267. /*
  268. ==============================
  269. main
  270. ==============================
  271. */
  272. int main (int argc, char **argv)
  273. {
  274.     int        i;
  275.     
  276.     printf ("\nqlumpy "VERSION" by John Carmack, copyright (c) 1994 Id Software\n");
  277.  
  278.     if (argc == 1)
  279.         Error ("qlumpy [-archive directory] scriptfile [scriptfile ...]");
  280.  
  281.     lumpbuffer = malloc (MAXLUMP);
  282.  
  283.     if (!strcmp(argv[1], "-archive"))
  284.     {
  285.         archive = true;
  286.         strcpy (archivedir, argv[2]);
  287.         printf ("Archiving source to: %s\n", archivedir);
  288.         i = 3;
  289.     }
  290.     else
  291.         i = 1;
  292.  
  293.  
  294.     for ( ; i<argc ; i++)
  295.     {
  296.         SetQdirFromPath (argv[i]);
  297.         ProcessLumpyScript (argv[i]);
  298.     }
  299.         
  300.     return 0;
  301. }
  302.