home *** CD-ROM | disk | FTP | other *** search
/ C++ Games Programming / CPPGAMES.ISO / fgl / fglight / exc.arj / TEMP / FISHTANK.C < prev   
Text File  |  1995-01-20  |  10KB  |  304 lines

  1. /**********************************************************************\
  2.  
  3. FISHTANK.C -- This program demonstrates multi-object non-destructive
  4. animation.  The coral background is displayed on page 2 and copied to
  5. page 0, the visual page.  A packed pixel run file containing the 6 fish
  6. is displayed on page 1, and then fg_getimage is used to load the fish
  7. into the fish bitmaps.
  8.  
  9. To make the fish move, the background is copied to page 1 and the fish
  10. are put over the background using fg_clpimage and fg_flpimage.  The
  11. fish are clipped at the edge of the screen.  Page 1 is then copied to
  12. page 0 using fg_copypage.  This process is repeated in a loop.
  13.  
  14. To compile this program and link it with Fastgraph version 3.xx:
  15.  
  16.    BCC -ms FISHTANK.C FGS.LIB    (Borland C++)
  17.  
  18.    CL /AS FISHTANK.C /link FGS   (Microsoft C/C++ and Visual C++)
  19.  
  20.    QCL /AS FISHTANK.C /link FGS  (Microsoft QuickC)
  21.  
  22.    PC /ms FISHTANK               (Power C)
  23.    PCL FISHTANK ;FGS
  24.  
  25.    TCC -ms FISHTANK.C FGS.LIB    (Turbo C/C++)
  26.  
  27.    ZTC -ms FISHTANK.C FGS.LIB    (Zortech C++)
  28.  
  29. This program also can be linked with Fastgraph/Light (version 3.02 or
  30. later) if you replace the FGS library references with FGLS.
  31.  
  32. For more examples of animation using Fastgraph, or for an evaluation
  33. copy of Fastgraph/Light, call DDBBS at (702) 796-7134.  For Fastgraph
  34. voice support, call Ted Gruber Software at (702) 735-1980.
  35.  
  36. \**********************************************************************/
  37.  
  38. #include <fastgraf.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41.  
  42. void main(void);
  43. void get_fish(void);
  44. void go_fish(void);
  45. int  irandom(int min,int max);
  46. void put_fish(int fish_no,int x,int y,int fish_dir);
  47. void random_init(void);
  48.  
  49. #define MAX(x,y) ((x) > (y)) ? (x) : (y)
  50. #define MIN(x,y) ((x) < (y)) ? (x) : (y)
  51.  
  52. #define NFISH 11
  53.  
  54. int seed;
  55.  
  56. /*** fish bitmaps ***/
  57.  
  58. char fish1[700];
  59. char fish2[1026];
  60. char fish3[884];
  61. char fish4[840];
  62. char fish5[682];
  63. char fish6[1224];
  64.  
  65. /**********************************************************************\
  66. *                                                                      *
  67. *                                 main                                 *
  68. *                                                                      *
  69. \**********************************************************************/
  70.  
  71. int colors[] = {0,1,2,3,4,5,6,7,16,0,18,19,20,21,22,23};
  72.  
  73. void main()
  74. {
  75.    int old_mode;
  76.  
  77.    /* make sure the system supports video mode 13 with 4 pages */
  78.  
  79.    fg_initpm();
  80.    if (fg_testmode(13,4) == 0)
  81.    {
  82.       printf("\n");
  83.       printf("This program requires an EGA or VGA card\n");
  84.       printf("with at least 128k.  If an EGA card is\n");
  85.       printf("present, it must be the active adapter.\n");
  86.       exit(0);
  87.    }
  88.  
  89.    /* initialize the video environment */
  90.  
  91.    old_mode = fg_getmode();
  92.    fg_setmode(13);
  93.    fg_palettes(colors);
  94.    random_init();
  95.  
  96.    /* get the coral background from a file and put it on page 2 */
  97.  
  98.    fg_setpage(2);
  99.    fg_move(0,199);
  100.    fg_dispfile("coral.ppr",320,1);
  101.  
  102.    /* copy the background from page 2 to page 0, the visual page */
  103.  
  104.    fg_copypage(2,0);
  105.  
  106.    /* get the fish */
  107.  
  108.    get_fish();
  109.  
  110.    /* make the fish go */
  111.  
  112.    go_fish();
  113.  
  114.    /* restore the original video state */
  115.  
  116.    fg_setmode(old_mode);
  117.    fg_reset();
  118. }
  119.  
  120. /**********************************************************************\
  121. *                                                                      *
  122. *            get_fish -- fill up the fish bitmap arrays                *
  123. *                                                                      *
  124. \**********************************************************************/
  125.  
  126. int fish_x1[]  = {0,   64,128,200,  0, 80}; /* location of fish x & y */
  127. int fish_y1[]  = {199,199,199,199,150,150};
  128. int width[]    = { 28, 27, 34, 28, 31, 34}; /* size of fish: width */
  129. int height[]   = { 25, 38, 26, 30, 22, 36}; /* size of fish: height */
  130.  
  131. char *fishes[] = {fish1, /* for convenience, an array of pointers to fishes */
  132.                   fish2,
  133.                   fish3,
  134.                   fish4,
  135.                   fish5,
  136.                   fish6};
  137.  
  138. void get_fish()
  139. {
  140.    register int fish_num;
  141.  
  142.    /* get the fish from a file and put them on page 1 */
  143.  
  144.    fg_setpage(1);
  145.    fg_move(0,199);
  146.    fg_dispfile("fish.ppr",320,1);
  147.  
  148.    /* build the fish bitmaps */
  149.  
  150.    for (fish_num = 0; fish_num < 6; fish_num++)
  151.    {
  152.       fg_move(fish_x1[fish_num],fish_y1[fish_num]);
  153.       fg_getimage(fishes[fish_num],width[fish_num],height[fish_num]);
  154.    }
  155. }
  156.  
  157. /**********************************************************************\
  158. *                                                                      *
  159. *             go_fish -- make the fish swim around                     *
  160. *                                                                      *
  161. \**********************************************************************/
  162.  
  163. /*  There are 11 fish total, and 6 different kinds of fish.  These
  164. *   arrays keep track of what kind of fish each fish is, and how each
  165. *   fish moves:
  166. *
  167. *   fish[]   -- which fish bitmap applies to this fish?
  168. *   x[]      -- starting x coordinate
  169. *   y[]      -- starting y coordinate
  170. *
  171. *   xmin[]   -- how far left (off screen) the fish can go
  172. *   xmax[]   -- how far right (off screen) the fish can go
  173. *   xinc[]   -- how fast the fish goes left and right
  174. *   dir[]    -- starting direction for each fish
  175. *
  176. *   ymin[]   -- how far up this fish can go
  177. *   ymax[]   -- how far down this fish can go
  178. *   yinc[]   -- how fast the fish moves up or down
  179. *   yturn[]  -- how long fish can go in the vertical direction
  180. *               before stopping or turning around
  181. *   ycount[] -- counter to compare to yturn
  182. */
  183.  
  184. int fish[]   = {   1,   1,   2,   3,   3,   0,   0,   5,   4,   2,   3};
  185. int x[]      = {-100,-150,-450,-140,-200, 520, 620,-800, 800, 800,-300};
  186. int y[]      = {  40,  60, 150,  80,  70, 190, 180, 100,  30, 130,  92};
  187.  
  188. int xmin[]   = {-300,-300,-800,-200,-200,-200,-300,-900,-900,-900,-400};
  189. int xmax[]   = { 600, 600,1100,1000,1000, 750, 800,1200,1400,1200, 900};
  190. int xinc[]   = {   2,   2,   8,   5,   5,  -3,  -3,   7,  -8,  -9,   6};
  191. int dir[]    = {   0,   0,   0,   0,   0,   1,   1,   0,   1,   1,   0};
  192.  
  193. int ymin[]   = {  40,  60, 120,  70,  60, 160, 160,  80,  30, 110,  72};
  194. int ymax[]   = {  80, 100, 170, 110, 100, 199, 199, 120,  70, 150, 122};
  195. int yturn[]  = {  50,  30,  10,  30,  20,  10,  10,  10,  30,   20, 10};
  196. int ycount[] = {   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0};
  197. int yinc[]   = {   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0};
  198.  
  199. void go_fish()
  200. {
  201.    register int i;
  202.    unsigned char key, aux;
  203.  
  204.    /* make the fish swim around */
  205.  
  206.    do
  207.    {
  208.       /* copy the background from page 2 to page 1 */
  209.  
  210.       fg_copypage(2,1);
  211.  
  212.       /* put all the fish on the background */
  213.  
  214.       for (i = 0; i < NFISH; i++)
  215.       {
  216.          fg_setpage(1);
  217.          ycount[i]++;
  218.          if (ycount[i] > yturn[i])
  219.          {
  220.             ycount[i] = 0;
  221.             yinc[i] = irandom(-1,1);
  222.          }
  223.          y[i] += yinc[i];
  224.          y[i] = MIN(ymax[i],MAX(y[i],ymin[i]));
  225.  
  226.          if (x[i] >= 0 && x[i] < 320)
  227.          {
  228.             put_fish(fish[i],x[i],y[i],dir[i]);
  229.          }
  230.          else if (x[i] < 0 && x[i] > -72)
  231.          {
  232.             fg_transfer(0,71,0,199,104,199,1,3);
  233.             fg_setpage(3);
  234.             put_fish(fish[i],x[i]+104,y[i],dir[i]);
  235.             fg_transfer(104,175,0,199,0,199,3,1);
  236.          }
  237.          x[i] += xinc[i];
  238.          if (x[i] <= xmin[i] || x[i] >= xmax[i])
  239.          {
  240.             xinc[i] = -xinc[i];
  241.             dir[i] = 1 - dir[i];
  242.          }
  243.       }
  244.  
  245.       /* copy page 1 to page 0 */
  246.  
  247.       fg_setpage(0);
  248.       fg_copypage(1,0);
  249.  
  250.       /* intercept a keystroke, if it is escape exit the program */
  251.  
  252.       fg_intkey(&key,&aux);
  253.    }
  254.    while (key != 27);
  255. }
  256.  
  257. /**********************************************************************\
  258. *                                                                      *
  259. *                irandom -- random number generator                    *
  260. *                                                                      *
  261. \**********************************************************************/
  262.  
  263. irandom(min,max)
  264. int min,max;
  265. {
  266.    int temp;
  267.  
  268.    temp = seed ^ (seed >> 7);
  269.    seed = ((temp << 8) ^ temp) & 0x7FFF;
  270.    return((seed % (max-min+1)) + min);
  271. }
  272.  
  273. /**********************************************************************\
  274. *                                                                      *
  275. *      put_fish -- draw one of the six fish anywhere you want          *
  276. *                                                                      *
  277. \**********************************************************************/
  278.  
  279. void put_fish(fish_num,x,y,fish_dir)
  280. int fish_num,x,y,fish_dir;
  281. {
  282.    /* move to position where the fish will appear */
  283.  
  284.    fg_move(x,y);
  285.  
  286.    /* draw a left- or right-facing fish, depending on fish_dir */
  287.  
  288.    if (fish_dir == 0)
  289.       fg_flpimage(fishes[fish_num],width[fish_num],height[fish_num]);
  290.    else
  291.       fg_clpimage(fishes[fish_num],width[fish_num],height[fish_num]);
  292. }
  293.  
  294. /**********************************************************************\
  295. *                                                                      *
  296. *      random_init -- get a seed for the random number generator       *
  297. *                                                                      *
  298. \**********************************************************************/
  299.  
  300. void random_init()
  301. {
  302.    seed = (int)(fg_getclock() & 0x7FFF);
  303. }
  304.