home *** CD-ROM | disk | FTP | other *** search
/ Quake++ for Quake / Quake++.iso / quake / edquake / code / wad.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-12  |  4.4 KB  |  175 lines

  1. /*EdQuake v0.50 source code
  2.   (c) Copyright 1996 Scott Mitting
  3.   email:smitting@netusa1.net
  4.   ----------------------------------
  5.   WAD.C  -  WAD2 file routines
  6.  
  7.   __variables:
  8.   wad_t      wad             -- pointer to wad directory
  9.   wadentry_t wadentry           -- pointer to wad entry
  10.   __functions:
  11.   void getwadentry(int e)               -- gets an entry
  12.   void openwad(int e)            -- opens wad from pak file
  13.   int selectwadentry()            -- selects a wad entry
  14.   void showwadmenu(int showE, int curE) -- low level function
  15.   void wadexport(int e, char*filename)  -- save wad entry to disk
  16.   ----------------------------------
  17.   the entire source code is under renovation to make it easier
  18.   to understand.  happy coding.
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include "wad.h"
  24. #include "pak.h"
  25. #include "svgacc.h"
  26. #include "grfx.h"
  27. #include "disk.h"
  28. #include "input.h"
  29.  
  30. wad_t      wad;
  31. wadentry_t wadentry;
  32.  
  33. void openwad(int e)
  34. {
  35.    char buf;
  36.    int t = -1;
  37.    getentry(e);
  38.    fseek(pak.p,pakentry.loc,0);
  39.    wad.startloc = pakentry.loc;
  40.    if (fgetc(pak.p) != 'W') t = 0;
  41.    if (fgetc(pak.p) != 'A') t = 0;
  42.    if (fgetc(pak.p) != 'D') t = 0;
  43.    if (fgetc(pak.p) != '2') t = 0;
  44.    if (!t)
  45.    {
  46.       wad.startloc = 0;
  47.       return;
  48.    }
  49.    wad.tablelen = fgetl(pak.p);
  50.    wad.tableloc = wad.startloc + fgetl(pak.p);
  51. }
  52.  
  53. //currently no support for compression
  54. void getwadentry(int e)
  55. {
  56.    int t;
  57.    fseek(pak.p, wad.tableloc + (e * 32), 0);
  58.    wadentry.loc = wad.startloc + fgetl(pak.p);
  59.    wadentry.len = fgetl(pak.p);
  60.    wadentry.dsize = fgetl(pak.p);
  61.    wadentry.type = fgetc(pak.p);
  62.    wadentry.compression = fgetc(pak.p);
  63.    fgeti(pak.p);  //not used
  64.    for (t = 0; t < 16; t++) wadentry.name[t] = fgetc(pak.p);
  65. }
  66.  
  67. void showwadmenu(int showE, int curE)
  68. {
  69.    int t,qt;
  70.    char buf[80];
  71.    qt = 40;
  72.    mousehide();
  73.    for (t = 0; t < 19; t++)
  74.    {
  75.      getwadentry(t + showE);
  76.      if (t != curE)
  77.      {
  78.     drwfillbox(SET, BLACK,6,qt,300,qt+14);
  79.     sprintf(buf, "%s",       wadentry.name);
  80.     drwstring(SET, WHITE,BLACK,buf,11,qt);
  81.      }
  82.      else
  83.      {
  84.     drwfillbox(SET,RED,6,qt,300,qt+14);
  85.     sprintf(buf, "%s",       wadentry.name);
  86.     drwstring(SET, YELLOW,RED,buf,11,qt);
  87.      }
  88.      qt+=14;
  89.    }
  90.    mouseshow();
  91. }
  92.  
  93. void wadexport(int e, char*filename)
  94. {
  95.    FILE *out;
  96.    long byte;
  97.    int  t;
  98.  
  99.    getwadentry(e);
  100.    out = fopen(filename, "wb");
  101.    if (!out) return;
  102.    fseek(pak.p, wadentry.loc, 0);
  103.    for (byte = 1; byte <= wadentry.len; byte++)
  104.        fputc(fgetc(pak.p),out);
  105.    fclose(out);
  106. }
  107.  
  108. int selectwadentry(int def)
  109. {
  110.    int k;
  111.    int curE = 0, showE = def;
  112.    int mx, my, mbuts;
  113.    button_t up, down, xbutton;
  114.    mousehide();
  115.    xbutton = inputbox(0,20,320,312,"Select WAD Entry");
  116.    up = scrollbutton(302,280,"");
  117.    down = scrollbutton(302,294,"");
  118.    mouseshow();
  119.    if (showE + 18 > wad.tablelen) {showE = wad.tablelen - 18; curE=18;}
  120.    while (k != 27)
  121.    {
  122.      showwadmenu(showE, curE);
  123.      scrollbar(302,40,278,showE+curE,wad.tablelen);
  124.         while (!kbhit() && !mbuts)
  125.         {
  126.            mousestatus(&mx,&my,&mbuts);
  127.            if (mbuts & 1)//scroll
  128.            {
  129.           if (hitbutton(mx,my,up)) showE-=1;
  130.           if (hitbutton(mx,my,down)) showE+=1;
  131.           if (hitbutton(mx,my,xbutton)) return -1;
  132.           //select
  133.           if (mx < 300 && my < 300 && my > 40)
  134.              curE =  ((my - 40) / 14)+1;
  135.            }
  136.            if (mbuts & 2)//scroll fast
  137.            {
  138.           if (hitbutton(mx,my,up)) showE-=3;
  139.           if (hitbutton(mx,my,down)) showE+=3;
  140.           if (mx < 300 && my < 300 && my > 40) return showE+curE;
  141.            }
  142.  
  143.         }
  144.         if (!mbuts) k = getkey();
  145.         mbuts = 0;
  146.  
  147.      if (k == a_DOWN) curE++;
  148.      if (k == a_UP)   curE--;
  149.      if (k == a_PGDN) showE+=15;
  150.      if (k == a_PGUP) showE-=15;
  151.      if (k == a_HOME) curE=1;
  152.      if (k == a_END)  curE=19;
  153.      if (k == a_ENTER) return showE+curE;
  154.      if (k == a_ESC)   return -1;
  155.      if (curE > 18) {showE++;curE=18;}
  156.      if (curE < 0)  {showE--;curE=0;}
  157.      if (showE < 0) {showE = 0; curE = 0;}
  158.      if (showE + 18 > wad.tablelen) {showE = wad.tablelen - 18; curE=18;}
  159.    }
  160.    return -1;
  161. }
  162.  
  163. int findwad(char *filename)
  164. {
  165.    int t;
  166.    if (wad.tablelen == 0) return -1;
  167.    for (t = 0; t < wad.tablelen; t++)
  168.    {
  169.       getwadentry(t);
  170.       if (strcmpi(filename, wadentry.name) == 0) t += 1000;
  171.    }
  172.    if (t < 1000) return -1;
  173.    t-=1001;
  174.    return t;
  175. }