Blitting and Flipping

DirectDraw provides two ways to update the screen for full screen programs: blitting and flipping. From the perspective of updating the screen, blitting applications are very similar to windowed applications. That is, fg_vbinit() creates a primary surface, and we use fg_vballoc() to create one or more virtual buffers for the additional DirectDraw surfaces. The difference is that we can create virtual buffers the same size as the screen resolution and then use fg_vbpaste() instead of fg_vbscale() when updating the entire screen.

Applications that use flipping are a bit more complex. As mentioned earlier, we set up a flipping environment by setting the last fg_ddsetup() parameter to FG_DX_FLIP. This causes fg_vbinit() to attach an additional surface to the primary DirectDraw surface. The primary surface thus consists of two surfaces, called the front buffer and the back buffer. As you might expect, the front buffer is the visible surface, while the back buffer resides in video memory but is not visible. This creates a traditional page flipping environment. That is, we construct an animation frame on the back buffer, flip it to the front (making it visible), and then begin drawing the next frame on the other buffer, which is now the back buffer. We repeat this process for each frame.

When we establish a flipping environment, fg_vbinit() assigns virtual buffer handle 0 to the back buffer. Its size will be the same as the resolution defined by fg_ddsetup(). We don't use fg_vballoc() or fg_vbfree() on this virtual buffer, though we open and close it just as we would any other virtual buffer. We can of course use fg_vballoc() to create additional virtual buffers in flipping applications, but only the back buffer (virtual buffer 0) can be flipped. In addition, the virtual buffer handle 0 always references the back buffer portion of the primary DirectDraw surface, even after a page flip.

The fg_ddflip() function flips the back buffer to the front. In other words, it makes the contents of the back buffer visible. If successful, fg_ddflip() returns zero; otherwise it returns one of the error codes listed in the documentation supplied with the DirectX SDK. Another function, fg_ddflipnw(), also flips the back buffer to the front but does not wait if the drawing surface is still busy. Generally you'll want to use fg_ddflip() for surface flipping.

The fg_gdiflip() function makes the Windows GDI drawing surface the visible surface in DirectDraw applications that use flipping. This function must be used immediately before displaying a dialog box, message box, or other GDI object to guarantee that object will be visible. The fg_gdiflip() function has no parameters and no return value; it is present only in Fastgraph's DirectX libraries.

A small number of DirectDraw implementations have problems displaying the mouse cursor correctly in page flipping programs (sometimes only when the application's color depth does not match the system's display color depth). While such implementations are the exception, and not as common in more recent versions of DirectX, it is still worth noting here. You may wish to consider implementing a sprite-based mouse cursor using Fastgraph's bitmap functions in DirectDraw page flipping programs, especially those intended for retail distribution.

<< Prev

Next >>

Fastgraph Home Page


copyright 2001 Ted Gruber Software, Inc.