Example: DirectDraw Frame-Based Animation

The FrameDD example is a DirectDraw program that uses frame-based animation and implements a WM_ACTIVATEAPP event handler. FrameDD fills a 640x480 virtual buffer with pixels of the same color, using a different color for each frame, and rapidly displays the frames through either DirectDraw blitting or flipping. Note especially how we use conditional compilation to control the choice of blitting or flipping. If blitting, we must create our drawing surface with fg_vballoc(), just as we've always done. If flipping, however, our drawing surface will be the back buffer of the DirectDraw primary surface. In this case, we set hVB to zero so we can open the drawing surface just like any other virtual buffer (the back buffer always has an implicit virtual buffer handle of zero).

The FrameDD animation-based message loop calls the program's Animate() function when no messages are waiting and FrameDD is the active application. Animate() fills the virtual buffer with pixels of the next color and then calls fg_vbpaste() if blitting or fg_ddflip() if page flipping. Note how FrameDD uses the AppIsActive global variable to keep track of the application's state. The AppIsActive global is initially FALSE and is updated in the WM_ACTIVATEAPP handler; its value will be TRUE when FrameDD is active, and FALSE when not. This allows the message loop to avoid calling Animate() when control switches from FrameDD to another application.

We must also remove the WM_PAINT event handler from FrameDD, as the BeginPaint() and EndPaint() functions cause problems when restoring the primary surface. We update the screen with fg_vbpaste() or fg_ddflip() outside of the WM_PAINT handler anyway, so removing it isn't an issue. Similarly, we also remove the InvalidateRect() call, Invalidate() call, or Refresh statement from the WM_SETFOCUS event handler.

C/C++ version

C++Builder version

Delphi version

Visual Basic version

<< Prev

Next >>

Fastgraph Home Page


copyright 2001 Ted Gruber Software, Inc.