home *** CD-ROM | disk | FTP | other *** search
/ C++ Games Programming / CPPGAMES.ISO / fgl / fglight / expas.arj / TEMP / FISHTANK.PAS < prev   
Pascal/Delphi Source File  |  1995-01-20  |  10KB  |  338 lines

  1. {***********************************************************************
  2.  
  3. FISHTANK.PAS -- 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 or Fastgraph/Light
  15. version 3.xx:
  16.  
  17.    TPC FISHTANK
  18.  
  19. For more examples of animation using Fastgraph, or for an evaluation
  20. copy of Fastgraph/Light, call DDBBS at (702) 796-7134.  For Fastgraph
  21. voice support, call Ted Gruber Software at (702) 735-1980.
  22.  
  23. ***********************************************************************}
  24.  
  25. program fishtank;
  26. uses fgmain, fgbitmap, fgmisc, fgpr;
  27.  
  28. var
  29.  
  30.   old_mode : integer;
  31.  
  32.   { fish bitmaps }
  33.  
  34.   fish1 : array [0..699]  of byte;
  35.   fish2 : array [0..1025] of byte;
  36.   fish3 : array [0..883]  of byte;
  37.   fish4 : array [0..839]  of byte;
  38.   fish5 : array [0..681]  of byte;
  39.   fish6 : array [0..1223] of byte;
  40.  
  41. const
  42.  
  43.   { palette values }
  44.  
  45.   colors : array [1..16] of integer = (0,1,2,3,4,5,6,7,16,0,18,19,20,21,22,23);
  46.  
  47.   { number of fish }
  48.  
  49.   NFISH = 11;
  50.  
  51.   { location of fish x & y }
  52.  
  53.   fish_x1 : array [0..5] of integer = (0,   64,128,200,  0, 80);
  54.   fish_y1 : array [0..5] of integer = (199,199,199,199,150,150);
  55.  
  56.   { size of fish (width and height) }
  57.  
  58.   width   : array [0..5] of integer = ( 28, 27, 34, 28, 31, 34);
  59.   height  : array [0..5] of integer = ( 25, 38, 26, 30, 22, 36);
  60.  
  61.   { for convenience, an array of pointers to fish bitmaps }
  62.  
  63.   fishes : array [0..5] of ^byte = (@fish1,@fish2,@fish3,@fish4,@fish5,@fish6);
  64.  
  65. {
  66. ************************************************************************
  67. *                                                                      *
  68. *            min -- determine the smaller of two integer values        *
  69. *                                                                      *
  70. ************************************************************************
  71. }
  72.  
  73. function min (value1, value2 : integer) : integer;
  74.  
  75. begin
  76.  
  77.   if (value1 <= value2) then
  78.     min := value1
  79.   else
  80.     min := value2;
  81.  
  82. end;
  83.  
  84. {
  85. ************************************************************************
  86. *                                                                      *
  87. *            max -- determine the larger of two integer values         *
  88. *                                                                      *
  89. ************************************************************************
  90. }
  91.  
  92. function max (value1, value2 : integer) : integer;
  93.  
  94. begin
  95.  
  96.   if (value1 >= value2) then
  97.     max := value1
  98.   else
  99.     max := value2;
  100.  
  101. end;
  102.  
  103. {
  104. ************************************************************************
  105. *                                                                      *
  106. *            get_fish -- fill up the fish bitmap arrays                *
  107. *                                                                      *
  108. ************************************************************************
  109. }
  110.  
  111. procedure get_fish;
  112.  
  113. var
  114.  
  115.   fish_num : integer;
  116.  
  117. begin
  118.  
  119.   { read the fish from a packed pixel run file and display them on page 1 }
  120.  
  121.   fg_setpage(1);
  122.   fg_move(0,199);
  123.   fg_dispfile('fish.ppr'+chr(0),320,1);
  124.  
  125.   { build the fish bitmaps }
  126.  
  127.   for fish_num := 0 to 5 do
  128.   begin
  129.     fg_move(fish_x1[fish_num],fish_y1[fish_num]);
  130.     fg_getimage(fishes[fish_num]^,width[fish_num],height[fish_num]);
  131.   end;
  132.  
  133. end;
  134.  
  135. {
  136. ************************************************************************
  137. *                                                                      *
  138. *      put_fish -- draw one of the six fish anywhere you want          *
  139. *                                                                      *
  140. ************************************************************************
  141. }
  142.  
  143. procedure put_fish (fish_num, x, y, fish_dir : integer);
  144.  
  145. begin
  146.  
  147.   { move to position where the fish will appear }
  148.  
  149.   fg_move(x,y);
  150.  
  151.   { draw a left- or right-facing fish, depending on fish_dir }
  152.  
  153.   if (fish_dir = 0) then
  154.     fg_flpimage(fishes[fish_num]^,width[fish_num],height[fish_num])
  155.   else
  156.     fg_clpimage(fishes[fish_num]^,width[fish_num],height[fish_num]);
  157.  
  158. end;
  159.  
  160. {
  161. ************************************************************************
  162. *                                                                      *
  163. *             go_fish -- make the fish swim around                     *
  164. *                                                                      *
  165. ************************************************************************
  166. }
  167.  
  168. procedure go_fish;
  169.  
  170. type
  171.  
  172.   fish_array = array [0..NFISH-1] of integer;
  173.  
  174. const
  175. {
  176. *   There are 11 fish total, and 6 different kinds of fish.  These
  177. *   arrays keep track of what kind of fish each fish is, and how each
  178. *   fish moves:
  179. *
  180. *   fish()   -- which fish bitmap applies to this fish?
  181. *   x()      -- starting x coordinate
  182. *   y()      -- starting y coordinate
  183. *
  184. *   xmin()   -- how far left (off screen) the fish can go
  185. *   xmax()   -- how far right (off screen) the fish can go
  186. *   xinc()   -- how fast the fish goes left and right
  187. *   dir()    -- starting direction for each fish
  188. *
  189. *   ymin()   -- how far up this fish can go
  190. *   ymax()   -- how far down this fish can go
  191. *   yinc()   -- how fast the fish moves up or down
  192. *   yturn()  -- how long fish can go in the vertical direction
  193. *               before stopping or turning around
  194. *   ycount() -- counter to compare to yturn
  195. }
  196.  
  197.   fish   : fish_array =
  198.                 (   1,   1,   2,   3,   3,   0,   0,   5,   4,   2,   3);
  199.   x      : fish_array =
  200.                 (-100,-150,-450,-140,-200, 520, 620,-800, 800, 800,-300);
  201.   y      : fish_array =
  202.                 (  40,  60, 150,  80,  70, 190, 180, 100,  30, 130,  92);
  203.  
  204.   xmin   : fish_array =
  205.                 (-300,-300,-800,-200,-200,-200,-300,-900,-900,-900,-400);
  206.   xmax   : fish_array =
  207.                 ( 600, 600,1100,1000,1000, 750, 800,1200,1400,1200, 900);
  208.   xinc   : fish_array =
  209.                 (   2,   2,   8,   5,   5,  -3,  -3,   7,  -8,  -9,   6);
  210.   dir    : fish_array =
  211.                 (   0,   0,   0,   0,   0,   1,   1,   0,   1,   1,   0);
  212.  
  213.   ymin   : fish_array =
  214.                 (  40,  60, 120,  70,  60, 160, 160,  80,  30, 110,  72);
  215.   ymax   : fish_array =
  216.                 (  80, 100, 170, 110, 100, 199, 199, 120,  70, 150, 122);
  217.   yturn  : fish_array =
  218.                 (  50,  30,  10,  30,  20,  10,  10,  10,  30,   20, 10);
  219.   ycount : fish_array =
  220.                 (   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0);
  221.   yinc   : fish_array =
  222.                 (   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0);
  223.  
  224. var
  225.  
  226.   i : integer;
  227.   key, aux : byte;
  228.  
  229. begin
  230.  
  231.   { make the fish swim around }
  232.  
  233.   repeat
  234.   begin
  235.  
  236.     { copy the background from page 2 to page 1 }
  237.  
  238.     fg_copypage(2,1);
  239.  
  240.     { put all the fish on the background }
  241.  
  242.     for i := 0 to NFISH-1 do
  243.     begin
  244.       fg_setpage(1);
  245.       inc(ycount[i]);
  246.       if (ycount[i] > yturn[i]) then
  247.       begin
  248.          ycount[i] := 0;
  249.          yinc[i] := random(3) - 1;
  250.       end;
  251.       inc(y[i],yinc[i]);
  252.       y[i] := min(ymax[i],max(y[i],ymin[i]));
  253.  
  254.       if (x[i] >= 0) and (x[i] < 320) then
  255.         put_fish(fish[i],x[i],y[i],dir[i])
  256.       else if (x[i] < 0) and (x[i] > -72) then
  257.       begin
  258.         fg_transfer(0,71,0,199,104,199,1,3);
  259.         fg_setpage(3);
  260.         put_fish(fish[i],x[i]+104,y[i],dir[i]);
  261.         fg_transfer(104,175,0,199,0,199,3,1);
  262.       end;
  263.       inc(x[i],xinc[i]);
  264.       if (x[i] <= xmin[i]) or (x[i] >= xmax[i]) then
  265.       begin
  266.         xinc[i] := -xinc[i];
  267.         dir[i] := 1 - dir[i];
  268.       end;
  269.     end;
  270.  
  271.     { copy page 1 to page 0 }
  272.  
  273.     fg_setpage(0);
  274.     fg_copypage(1,0);
  275.  
  276.     { intercept a keystroke, if it is escape exit the program }
  277.  
  278.     fg_intkey(key,aux);
  279.  
  280.   end;
  281.   until (key = 27);
  282.  
  283. end;
  284.  
  285. {
  286. ************************************************************************
  287. *                                                                      *
  288. *                                 main                                 *
  289. *                                                                      *
  290. ************************************************************************
  291. }
  292.  
  293. begin
  294.  
  295.   { make sure the system supports video mode 13 with 4 pages }
  296.  
  297.   fg_initpm;
  298.   if (fg_testmode(13,4) = 0) then
  299.   begin
  300.     writeln;
  301.     writeln('This program requires an EGA or VGA card');
  302.     writeln('with at least 128k.  If an EGA card is');
  303.     writeln('present, it must be the active adapter.');
  304.     halt;
  305.   end;
  306.  
  307.   { initialize the video environment }
  308.  
  309.   old_mode := fg_getmode;
  310.   fg_setmode(13);
  311.   fg_palettes(colors);
  312.   randomize;
  313.  
  314.   { get the coral background from a file and put it on page 2 }
  315.  
  316.   fg_setpage(2);
  317.   fg_move(0,199);
  318.   fg_dispfile('coral.ppr'+chr(0),320,1);
  319.  
  320.   { copy the background from page 2 to page 0, the visual page }
  321.  
  322.   fg_copypage(2,0);
  323.  
  324.   { get the fish }
  325.  
  326.   get_fish;
  327.  
  328.   { make the fish go }
  329.  
  330.   go_fish;
  331.  
  332.   { restore the original video state }
  333.  
  334.   fg_setmode(old_mode);
  335.   fg_reset;
  336.  
  337. end.
  338.