home *** CD-ROM | disk | FTP | other *** search
/ C++ Games Programming / CPPGAMES.ISO / thx / source / utils / gfxshow.cpp < prev    next >
C/C++ Source or Header  |  1995-05-12  |  4KB  |  164 lines

  1. #include <fastgraf.h>
  2. #include <fstream.h>
  3. #include <stdlib.h>
  4. #include <values.h>
  5. #include <io.h>
  6.  
  7. const int MAX_BITMAPS = 100;
  8. const int STRLEN = 80;
  9. const int VMODE = 22;
  10. const int PANINC = 4;
  11. const int PANLIM = 3;
  12.  
  13. struct bitmap_info
  14.   {
  15.   int w,h;
  16.   long size;
  17.   };
  18.  
  19. bitmap_info bitmap[MAX_BITMAPS];
  20.  
  21. void install_palette(char* fname);
  22. void show_images(char* gfxlib);
  23. void pan_screen();
  24. int count;
  25.  
  26. int main(int argc,char** argv)
  27.   {
  28.   if (argc<2)
  29.     {
  30.     cout << "\nUSAGE : GFXSHOW <gfxlib> [palette]\n";
  31.     cout << "  GFXSHOW displays the entries of a GFX graphics library.\n";
  32.     cout << "  The library to be displayed is 'gfxlib', and a palette\n";
  33.     cout << "  file can be supplied, which GFXSHOW will use to display\n";
  34.     cout << "  the library.\n\n";
  35.     cout << "  Also, the UP and DOWN arrow keys can be used to pan the\n";
  36.     cout << "  images if there is more than will fit on the screen.\n";
  37.     return -1;
  38.     }
  39.  
  40.   if (access(argv[1],0)!=0)
  41.     {
  42.     cout << "can't open GFX library '" << argv[1] << "'\n";
  43.     return -1;
  44.     }
  45.  
  46.   int oldmode=fg_getmode();
  47.   fg_waitfor(18);
  48.   fg_setmode(VMODE);
  49.   fg_resize(320,240*PANLIM);
  50.  
  51.   if (argc==3)
  52.     install_palette(argv[2]);
  53.  
  54.   show_images(argv[1]);
  55.   pan_screen();
  56.  
  57.   fg_setmode(oldmode);
  58.   for (int i=0;i<count;i++)
  59.     {
  60.     cout << (i+1) << ": " << bitmap[i].w << "x" 
  61.                           << bitmap[i].h << " " 
  62.                           << bitmap[i].size << endl;
  63.     }
  64.   
  65.   cout << "ok\n";
  66.   return 0;
  67.   }
  68.  
  69. void pan_screen()
  70.   {
  71.   int y=0;
  72.   unsigned char ascii,aux;
  73.   do{
  74.     fg_getkey(&ascii,&aux);
  75.     if (aux==0x48)     // UP ARROW
  76.       {
  77.       if (y>0)
  78.         fg_pan(0,y-=PANINC);
  79.       }
  80.     else if (aux==0x50)  // DOWN ARROW
  81.       {
  82.       if (y<PANLIM*240-240)
  83.         fg_pan(0,y+=PANINC);
  84.       }
  85.     } while (ascii!=27);
  86.   }
  87.  
  88. void show_images(char* gfxlib)
  89.   {
  90.   int x=0,y=0;
  91.   int w,h;
  92.   long size;
  93.   char* buf;
  94.  
  95.   ifstream lib(gfxlib,ios::binary);
  96.   lib.read((char*)&count,sizeof(count));
  97.  
  98.   int maxh=0;
  99.   for (int i=0;i<count;i++)
  100.     {
  101.     lib.read((char*)&w,sizeof(w));
  102.     lib.read((char*)&h,sizeof(h));
  103.     lib.read((char*)&size,sizeof(size));
  104.     buf=new char[(size_t)size];
  105.  
  106.     if (size<=(long)MAXINT)
  107.       lib.read(buf,(int)size);
  108.     else
  109.       {
  110.       lib.read(buf,(int)MAXINT);
  111.       lib.read(buf+MAXINT,(int)(size-(long)MAXINT));
  112.       }
  113.  
  114.     if (x+w > 319)
  115.       {
  116.       x=0;
  117.       y+=maxh+2;
  118.       maxh=0;
  119.       }
  120.     fg_move(x,y+h);
  121.     fg_drwimage(buf,w,h);
  122.     bitmap[i].w=w;
  123.     bitmap[i].h=h;
  124.     bitmap[i].size=size;
  125.     if (maxh < h)  maxh=h;
  126.     x+=w+2;
  127.     delete buf;
  128.     }
  129.   }
  130.  
  131. void install_palette(char* fname)
  132.   {
  133.   struct pal_entry       // struct compadible with the
  134.     {                    // fg_setdacs routine
  135.     unsigned char r;
  136.     unsigned char g;
  137.     unsigned char b;
  138.     };
  139.   char str[STRLEN];
  140.   int i,max;
  141.   pal_entry* pal_array;
  142.   int r,g,b;
  143.   ifstream palfile(fname);
  144.   if (palfile.bad())  return;
  145.   palfile.getline(str,STRLEN);
  146.   palfile.getline(str,STRLEN);
  147.   palfile.getline(str,STRLEN);
  148.   max=atoi(str);
  149.   pal_array=new pal_entry[max];
  150.   for (i=0;i<max;i++)
  151.     {
  152.     palfile >> r;
  153.     palfile >> g;
  154.     palfile >> b;
  155.     pal_array[i].r=(unsigned char)r;
  156.     pal_array[i].g=(unsigned char)g;
  157.     pal_array[i].b=(unsigned char)b;
  158.     }
  159.   fg_setdacs(0,max,(char*)pal_array);
  160.   delete pal_array;
  161.   }
  162.  
  163.  
  164.