Microsoft KB Archive/75431

{|
 * width="100%"|

INFO: An Efficient Animation Algorithm

 * }

Q75431

-

The information in this article applies to:


 * Microsoft Windows Software Development Kit (SDK) versions 3.1, 3.0
 * Microsoft Win32 Application Programming Interface (API), used with:
 * Microsoft Windows NT Server versions 3.5, 3.51
 * Microsoft Windows NT Workstation versions 3.5, 3.51
 * Microsoft Windows 95

-

SUMMARY
An application that shows an animated image cannot rely solely on Windows graphical device interface (GDI) functions because they will be too slow. Instead, the application must create its own set of bitmaps. This article discusses the process required and provides tips to improve performance and memory use.

This information applies to any type of animation or fast drawing, from painting the game pieces in Reversi to updating the time each second in Clock.

MORE INFORMATION
There are three major steps to this process:


 * 1) Allocate the bitmap.
 * 2) Prepare the bitmaps.
 * 3) Animate.

A temporary, off-screen DC is required to allow the application to select the bitmap. Note that selecting the object may fail if the bitmap has been discarded. If this has occurred, another bitmap must be allocated (if memory allows) and the bitmap must be initialized (as outlined in step 2, above).

  if ((hTmpDC = CreateCompatibleDC(hDC)) != NULL) {      HBITMAP hOldBm;

if (!(hOldBm = SelectObject(hTmpDC, hbm))          // must re-allocate bitmap. Note that this MAY FAIL!!! At this point, call the BitBlt function to copy the various stages of the animation sequence to the screen. If the cels in the bitmap contain sequential images, a simple loop will do the job nicely, as the following code demonstrates:

  for (i = 0; i < NUM_CELS; i++) {      BitBlt (hDC, x_pos, y_pos, X_SIZE, Y_SIZE, hTmpDC, 0,                    i * Y_SIZE, SRCCOPY);

// Some form of delay goes here. A real-time wait, based on        // clock ticks, is recommended. } When the drawing is done, delete the temporary DC:

  SelectObject(hTmpDC, hOldBm); DeleteDC(hTmpDC); It is important to cancel the selection of the bitmap between passes through the for loop. This allows the bitmap to be discarded if the system runs low on memory.

Additional query words: 3.00 3.10 3.50 4.00 animation win16sdk

Keywords : kbSDKWin32 kbOSWin310 kbOSWin95 kbDSupport kbOSWin300

Issue type : kbinfo

Technology : kbAudDeveloper kbSDKSearch kbWin32sSearch kbWin32API kbWinSDKSearch