home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / FGDEMO40.ZIP / SOURCE.COM / TGSDEMO.CPP < prev    next >
Text File  |  1995-02-12  |  7KB  |  261 lines

  1. /* **********************************************************
  2.    **                                                      **
  3.    **     MOVING SKY WITH SCALING LOGO DEMO                **
  4.    **                                                      **
  5.    **     programmer: Michael Miller  (516) 825-6735       **
  6.    **     last updated : 2/10/95                           **
  7.    **                                                      **
  8.    **********************************************************
  9.    **                                                      **
  10.    **  tools: Fastgraph 4.0 and Fastgraph/Image 2.0        **
  11.    **         Compiles with either Borland C++, Turbo C++, **
  12.    **         Microsoft C/C++, Visual C++, or Watcom C/C++ **
  13.    **                                                      **
  14.    **  specs: Uses mode 20 and resizes video memory to a   **
  15.    **         single 640x400 page                          **
  16.    **                                                      **
  17.    **         The sky is displayed directly to the screen  **
  18.    **                                                      **
  19.    **         An IMAGE class object loads the logo an is   **
  20.    **         responsible for scaling and displaying it    **
  21.    **                                                      **
  22.    **********************************************************
  23. */
  24.  
  25. #include <fastgraf.h>
  26. #include <fgimage.h>
  27. #include <iostream.h>
  28.  
  29. // constants
  30. #define VIDEO_MODE 20
  31. #define SCREEN_WIDTH  640
  32. #define SCREEN_HEIGHT 200
  33. #define VISUAL_WIDTH  320
  34. #define VISUAL_HEIGHT 200
  35.  
  36. //-------------------------- CLASS DEFINITIONS
  37.  
  38. int handle;
  39.  
  40. /* /\/\/\/\/\/\/\/\/\/\/\/\/
  41.    \/\/\/ CLASS IMAGE /\/\/\
  42.    /\/\/\/\/\/\/\/\/\/\/\/\/ */
  43.  
  44. class IMAGE
  45. {
  46. protected :
  47.  
  48.    char *imagedata, *scaledata;
  49.    int image_width, image_height, center_x, center_y;
  50.  
  51. public :
  52.    // By passing the width and height I can place a full screen
  53.    // PCX file into the image file and only use the part I want
  54.  
  55.    IMAGE(char *pcxfile,int width,int height)
  56.    {
  57.       // assign image_width & image_height
  58.       image_width  = width;
  59.       image_height = height;
  60.  
  61.       // find page center coordinates
  62.       center_x = (VISUAL_WIDTH - image_width)/2;
  63.       center_y = ((VISUAL_HEIGHT - image_height)/2)+image_height;
  64.  
  65.       // dynamically allocate memory for imagedata and backround
  66.       imagedata = new char[(image_width+1) * (image_height+1)];
  67.       scaledata = new char[(image_width+1) * (image_height+1)];
  68.  
  69.       // load the actual image from the pcxfile, populate imagedata
  70.       fg_move(0,0);
  71.       fgi_showpcx(pcxfile,0,handle);
  72.       fg_move(0,image_height-1);
  73.       fg_getimage(imagedata,image_width,image_height);
  74.    }
  75.  
  76.    ~IMAGE() { delete imagedata; }
  77.  
  78.    int GET_WIDTH()  { return image_width; }
  79.    int GET_HEIGHT() { return image_height; }
  80.    int CENTER_X()   { return center_x; }
  81.    int CENTER_Y()   { return center_y; }
  82.  
  83.    // draws the object at the current position
  84.    void show()
  85.    {
  86.       fg_drwimage(imagedata,image_width,image_height);
  87.    }
  88.  
  89.    // draws a scaled object,
  90.    //   x,y are the current offscreen coordinates they allow the
  91.    //   image to be centered as the sky pans
  92.    void show(double scale,int x,int y)
  93.    {
  94.       if (scale < 0.0) return;
  95.  
  96.       // Image is scaled down , calculate the center based on its size
  97.       if (scale < 1.0)
  98.       {
  99.          int dwidth  = int (image_width  * scale );
  100.          int dheight = int (image_height * scale );
  101.          int xloc = x+(320-dwidth)/2;
  102.          int yloc = y+((200-dheight)/2)+dheight;
  103.          fg_move(xloc,yloc);
  104.  
  105.          fg_scale(imagedata,scaledata,image_width,image_height,dwidth,dheight);
  106.          fg_drwimage(scaledata,dwidth,dheight);
  107.          return;
  108.       }
  109.       else
  110.       {
  111.          fg_move(center_x+x,center_y+y);
  112.          fg_drwimage(imagedata,image_width,image_height);
  113.          return;
  114.       }
  115.     }
  116. };
  117.  
  118. //-------------------------- GLOBAL VARIABLE DEFINITIONS
  119.  
  120. IMAGE *logo;
  121.  
  122. //-------------------------- FUNCTION DEFINITIONS
  123.  
  124. void move_backround()
  125. {
  126.    // initialization
  127.    int visual_offset = 200;
  128.    int work_offset = 0;
  129.    int work_page   = 0;
  130.  
  131.    int image_x = logo->CENTER_X();
  132.    int image_y = logo->CENTER_Y();
  133.    int image_width  = logo->GET_WIDTH();
  134.    int image_height = logo->GET_HEIGHT();
  135.  
  136.    char *buffer[2];
  137.    buffer[0] = new char[image_width*image_height];
  138.    buffer[1] = new char[image_width*image_height];
  139.  
  140.    fg_pan(0,visual_offset);
  141.  
  142.    // get buffer[0] from page 0
  143.    fg_move(image_x+2,image_y+work_offset);
  144.    fg_getimage(buffer[0],image_width,image_height);
  145.  
  146.    visual_offset = 0;
  147.    work_offset   = 200;
  148.    work_page     = 1;
  149.  
  150.    // get buffer[1] from page 1
  151.    fg_move(image_x+1,image_y+work_offset);
  152.    fg_getimage(buffer[1],image_width,image_height);
  153.  
  154.    // setup keyboard handler
  155.    fg_kbinit(1);
  156.  
  157.    int counter = 0;
  158.  
  159.    for (int t = 1; t < 320; t++)
  160.    {
  161.       if (work_offset == 0)
  162.          work_page = 0;
  163.       else
  164.          work_page = 1;
  165.  
  166.       // repaint backround on work_page
  167.       fg_move(image_x+t,image_y+work_offset);
  168.       fg_drwimage(buffer[work_page],image_width,image_height);
  169.  
  170.       // get backround on work_page
  171.       fg_move(image_x+t+2,image_y+work_offset);
  172.       fg_getimage(buffer[work_page],image_width,image_height);
  173.  
  174.       // check for pan < 309 and no key pressed
  175.       if (t < 309 && counter == 0)
  176.          logo->show(t*0.06,t+2,work_offset);
  177.       else if (counter > 0)
  178.       {
  179.          counter++;
  180.          logo->show((11-counter)*0.1,t+2,work_offset);
  181.          if (counter == 10)
  182.          {
  183.             fg_pan(0,work_offset);
  184.             fg_move(image_x+t,image_y+visual_offset);
  185.             fg_drwimage(buffer[1-work_page],image_width,image_height);
  186.             fg_pan(0,visual_offset);
  187.             return;
  188.          }
  189.       }
  190.       else
  191.          logo->show((320-t)*0.1,t+2,work_offset);
  192.  
  193.       // test for key pressed
  194.       if (fg_kblast() != 0 && counter == 0)
  195.       {
  196.          fg_kbinit(0);
  197.          counter = 1;
  198.       }
  199.  
  200.       visual_offset = 200 - visual_offset;
  201.       work_offset = 200 - work_offset;
  202.  
  203.       // pan screen to t and change display memory to visual_offset
  204.       fg_pan(t,visual_offset);
  205.    }
  206. }
  207.  
  208. //-------------------------- MAIN FUNCTION DEFINITION
  209.  
  210. void main()
  211. {
  212.    // make sure the image library file is present
  213.    handle = fgi_open("TGSDEMO.FGI");
  214.    if (handle == 0)
  215.    {
  216.       cout << "Unable to open TGSDEMO.FGI image library." << endl;
  217.       return;
  218.    }
  219.  
  220.    // initialize variables
  221.    int old_mode;
  222.    unsigned char key, aux;
  223.  
  224.    // establish the video mode
  225.    old_mode = fg_getmode();
  226.    if (fg_testmode(VIDEO_MODE,4) == 0)
  227.    {
  228.       fgi_close(handle);
  229.       return;
  230.    }
  231.    fg_setmode(VIDEO_MODE);
  232.  
  233.    //resize the video screen to one large page
  234.    fg_resize(640,400);
  235.  
  236.    //display the offpage prt of screen memory
  237.    fg_pan(0,200);
  238.  
  239.    // initialize the graphic objects
  240.    logo = new IMAGE("LOGO.PCX",263,39);
  241.  
  242.    // copy 0-199 to 200-399
  243.    fgi_showpcx("SKY.PCX",0,handle);
  244.  
  245.    // move to screen displaying the sky
  246.    fg_pan(0,0);
  247.  
  248.    // copy the sky to the offscreen
  249.    fg_transfer(0,639,0,199,0,399,0,0);
  250.  
  251.    // action!
  252.    move_backround();
  253.  
  254.    // clean up and exit to DOS
  255.    fg_kbinit(0);
  256.    delete logo;
  257.    fg_setmode(old_mode);
  258.    fg_reset();
  259.    fgi_close(handle);
  260. }
  261.