Example: Scaling and Scrolling

TGSdemo is a C++ program that displays a scaled logo against a background of continuously scrolling clouds, as shown here:

The logo and background images are stored in three 256-color PCX files:


Fastgraph logo (320x200)


TGS logo (320x200)


sky and cloud background (640x200)

All three PCX files were preprocessed by the WinPal and WinMatch utilities. WinPal reduced Sky.pcx from 256 to the 236 non-system colors. WinMatch then remapped the Logo1.pcx and Logo2.pcx palettes so they have the same palette as the Sky.pcx file. This lets fg_showpcx() load all three images with color reduction disabled, so we don't have to worry about potential palette conflicts.

The WinMain() function loads the Sky.pcx image into the first 640 columns of a 1600x200 virtual buffer, called the background buffer. It then creates a mirror image of the first 640 columns in the next 640 columns. That is, column 640 matches column 639, 641 matches 638, and so forth. This gives the appearance of a continuous stream of clouds. Finally, it copies the first 320 columns to the final 320 columns to facilitate wrapping. The result looks like this:

TGSdemo uses a class called IMAGE to manage the logo bitmaps. The IMAGE constructor function builds the full path name for the PCX file, allocates memory with the new[] operator for the original and scaled bitmaps, displays the PCX file containing the logo in the 320x200 workspace buffer, and finally calls fg_getimage() to retrieve the logo as a 256-color bitmap. The constructor expects four parameters: the PCX file name, the image width and height, and the vertical position where we'll display the image in the workspace buffer. The destructor function frees the bitmap memory using the delete[] operator.

The IMAGE class has a Show() member function that displays a logo bitmap horizontally centered in the workspace buffer. If we want to display the logo at full size, Show() displays the original bitmap using fg_drwimage(). If we're displaying the logo at a reduced size, Show() creates a scaled bitmap with fg_scale(), then calls fg_drwimage() to display the scaled bitmap.

Two static objects, Logo1 and Logo2, of type IMAGE are created in the MoveBackground() declarations. The size of each logo is 264x40 pixels; the first logo will be displayed at y=160 in the workspace buffer, and the second at y=80. MoveBackground() performs one frame of scrolling and is called from the message loop when no messages are waiting. It uses an index variable x to point to the starting column in the 1600x200 background buffer for the current frame. We copy the 320x200 region whose left edge is x from the background buffer to the 320x200 workspace buffer. In each frame, we increment x by eight pixels. When x reaches 1280, we are displaying the rightmost 320 columns of the background buffer (which are the same as its leftmost 320 columns), so we wrap back to the beginning of the background by resetting x to zero.

C++ version

<< Prev

Next >>

Fastgraph Home Page


copyright 2001 Ted Gruber Software, Inc.