home *** CD-ROM | disk | FTP | other *** search
/ Superpower (Alt) / SUPERPOWER.iso / q / util / mbq319 / qlumpy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-01  |  5.0 KB  |  282 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. boolean            savesingle;
  19. boolean            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.     printf ("grabbing from %s...\n",name);
  65.     LoadLBM (name, &byteimage, &lbmpalette);
  66.  
  67.     byteimagewidth = bmhd.w;
  68.     byteimageheight = bmhd.h;
  69. }
  70.  
  71.  
  72. /*
  73. ================
  74. CreateOutput
  75. ================
  76. */
  77. void CreateOutput (void)
  78. {
  79.     outputcreated = true;
  80. //
  81. // create the output wadfile file
  82. //
  83.     NewWad (destfile, false);    // create a new wadfile
  84. }
  85.  
  86. /*
  87. ===============
  88. WriteLump
  89. ===============
  90. */
  91. void WriteLump (int type, int compression)
  92. {
  93.     int        size;
  94.     
  95.     if (!outputcreated)
  96.         CreateOutput ();
  97.  
  98. //
  99. // dword align the size
  100. //
  101.     while ((int)lump_p&3)
  102.         *lump_p++ = 0;
  103.  
  104.     size = lump_p - lumpbuffer;
  105.     if (size > MAXLUMP)
  106.         Error ("Lump size exceeded %d, memory corrupted!",MAXLUMP);
  107.  
  108. //
  109. // write the grabbed lump to the wadfile
  110. //
  111.     AddLump (lumpname,lumpbuffer,size,type, compression);
  112. }
  113.  
  114. /*
  115. ===========
  116. WriteFile
  117.  
  118. Save as a seperate file instead of as a wadfile lump
  119. ===========
  120. */
  121. void WriteFile (void)
  122. {
  123.     char    filename[1024];
  124.     
  125.     sprintf (filename,"%s/%s.lmp", destfile, lumpname);
  126.     printf ("saved %s\n", filename);
  127.     SaveFile (filename, lumpbuffer, lump_p-lumpbuffer);        
  128. }
  129.  
  130. /*
  131. ================
  132. ParseScript
  133. ================
  134. */
  135. void ParseScript (void)
  136. {
  137.     int            cmd;
  138.     int            size;
  139.     
  140.     do
  141.     {
  142.         //
  143.         // get a command / lump name
  144.         //
  145.         GetToken (true);
  146.         if (endofscript)
  147.             break;
  148.         if (!strcmpi (token,"$LOAD"))
  149.         {
  150.             GetToken (false);
  151.             LoadScreen (token);
  152.             continue;
  153.         }
  154.  
  155.         if (!strcmpi (token,"$DEST"))
  156.         {
  157.             GetToken (false);
  158.             strcpy (destfile, token);
  159.             continue;
  160.         }
  161.  
  162.         if (!strcmpi (token,"$SINGLEDEST"))
  163.         {
  164.             GetToken (false);
  165.             strcpy (destfile, token);
  166.             savesingle = true;
  167.             continue;
  168.         }
  169.  
  170.  
  171.         //
  172.         // new lump
  173.         //
  174.         if (strlen(token) >= sizeof(lumpname) )
  175.             Error ("\"%s\" is too long to be a lump name",token);
  176.         memset (lumpname,0,sizeof(lumpname));            
  177.         strcpy (lumpname, token);
  178.         for (size=0 ; size<sizeof(lumpname) ; size++)
  179.             lumpname[size] = tolower(lumpname[size]);
  180.  
  181.         //
  182.         // get the grab command
  183.         //
  184.         lump_p = lumpbuffer;
  185.  
  186.         GetToken (false);
  187.  
  188.         //
  189.         // call a routine to grab some data and put it in lumpbuffer
  190.         // with lump_p pointing after the last byte to be saved
  191.         //
  192.         for (cmd=0 ; commands[cmd].name ; cmd++)
  193.             if ( !strcmpi(token,commands[cmd].name) )
  194.             {
  195.                 commands[cmd].function ();
  196.                 break;
  197.             }
  198.  
  199.         if ( !commands[cmd].name )
  200.             Error ("Unrecognized token '%s' at line %i",token,scriptline);
  201.     
  202.         grabbed++;
  203.         
  204.         if (savesingle)
  205.             WriteFile ();
  206.         else    
  207.             WriteLump (TYP_LUMPY+cmd, 0);
  208.         
  209.     } while (script_p < scriptend_p);
  210. }
  211.  
  212. /*
  213. =================
  214. ProcessLumpyScript
  215.  
  216. Loads a script file, then grabs everything from it
  217. =================
  218. */
  219. void ProcessLumpyScript (char const *basename)
  220. {
  221.     char            script[256];
  222.  
  223.     printf ("qlumpy script: %s\n",basename);
  224.     
  225. //
  226. // create default destination directory
  227. //
  228.     strcpy (destfile,basename);
  229.     StripExtension (destfile);
  230.     strcat (destfile,".wad");        // unless the script overrides, save in cwd
  231.  
  232. //
  233. // save in a wadfile by default
  234. //
  235.     savesingle = false;
  236.     grabbed = 0;
  237.     outputcreated = false;
  238.     
  239.     
  240. //
  241. // read in the script file
  242. //
  243.     strcpy (script, basename);
  244.     DefaultExtension (script, ".ls");
  245.     LoadScriptFile (script);
  246.     
  247.     strcpy (basepath, basename);
  248.     
  249.     ParseScript ();                // execute load / grab commands
  250.     
  251.     if (!savesingle)
  252.     {
  253.         WriteWad ();                // write out the wad directory
  254.         printf ("%i lumps grabbed in a wad file\n",grabbed);
  255.     }
  256.     else
  257.         printf ("%i lumps written seperately\n",grabbed);
  258. }
  259.  
  260.  
  261. /*
  262. ==============================
  263. main
  264. ==============================
  265. */
  266. int main (int argc, char **argv)
  267. {
  268.     int        i;
  269.     
  270.     printf ("\nqlumpy "VERSION" by John Carmack, copyright (c) 1994 Id Software\n");
  271.  
  272.     if (argc == 1)
  273.         Error ("qlumpy scriptfile [scriptfile ...]");
  274.  
  275.     lumpbuffer = malloc (MAXLUMP);
  276.  
  277.     for (i=1 ; i<argc ; i++)
  278.         ProcessLumpyScript (argv[i]);
  279.         
  280.     return 0;
  281. }
  282.