Example: Fish Tank Animation Demo

The Fishtank program is a good example of multi-object, non-destructive animation. In this program, several types of tropical fish swim back and forth against a coral reef background, as shown here:

Let's begin with the Fishtank program's WM_CREATE handler. After we set up the device context and default logical palette in the usual manner, WM_CREATE creates two 320x200 virtual buffers. It loads the coral background from the PCX file CORAL.PCX into the second virtual buffer (this virtual buffer will always contain a clean copy of the background).

The last thing the WM_CREATE handler does is load the fish bitmaps, using the function GetFish(). This function loads the six fish sprites from the file FISH.PCX into the first virtual buffer. After the call to fg_showpcx(), the first virtual buffer will look like this:

The GetFish() function uses fg_getimage() to retrieve each of the six fish into a 256-color bitmap. The FishX and FishY arrays contain the (x,y) coordinates for the lower left corner of each bitmap, while the FishWidth and FishHeight arrays define the size of each bitmap. We store the fish bitmaps in the arrays Fish1 through Fish6, and notice especially how we set up the Fishes array with pointers to the six fish bitmap arrays. This lets us access the individual fish bitmaps through a simple index. The fact that the background is color 0 is of particular importance, for we want the pixels around the fish (but within the bitmap rectangle) to be transparent when we display the bitmaps. Before returning, GetFish() calls fg_erase() to clear the first virtual buffer. If we didn't do this, the above screen would appear briefly in the client area if a WM_PAINT message occurs before we construct the first animation frame.

The Fishtank message loop controls the fish movement. We use the animation-based form of the message loop that lets us do other activity when the message queue is empty. That is, if messages are available, we process them in the usual manner, but if no messages are waiting, we call GoFish() to perform one animation frame.

The GoFish() function builds each animation frame in the first virtual buffer, which we'll call the workspace buffer. It starts constructing the new frame by copying the background to the workspace buffer, which effectively erases the previous frame. In each frame, we update the positions of 11 fish, though we'll usually not see them all in a given frame. The arrays defined at the top of GoFish() each contain 11 elements (one for each fish) that specify the type of fish, and its current position, range, speed, and direction. After we determine the new position of a fish, we call PutFish() to display it at its new location in the workspace buffer. The PutFish() function just uses fg_move() and either fg_clpimage() or fg_flpimage() to display the bitmap, depending on whether we want a left-facing or right-facing fish. After GoFish() places all 11 fish, the animation frame is ready, so we call fg_vbscale() to display it in the client area.

C/C++ version

C++Builder version

Delphi version

Visual Basic version

<< Prev

Next >>

Contents
Fastgraph Home Page

 

copyright 2001 Ted Gruber Software, Inc.