Step 7: Flipping the Surfaces

After the surface memory is unlocked, you can use the IDirectDrawSurface::Flip method to flip the back buffer to the primary surface, as shown in the following example:

while(1)

{

HRESULT ddrval;

ddrval = lpDDSPrimary->Flip(NULL, 0);

if(ddrval == DD_OK)

{

break;

}

if(ddrval == DDERR_SURFACELOST)

{

ddrval = lpDDSPrimary->Restore();

if(ddrval != DD_OK)

{

break;

}

}

if(ddrval != DDERR_WASSTILLDRAWING)

{

break;

}

}

In the example, lpDDSPrimary designates the primary surface and its associated back buffer. When IDirectDrawSurface::Flip is called, the front and back surfaces are exchanged (only the pointers to the surfaces are changed; no data is actually moved). If the flip is successful and returns DD_OK, the application breaks from the while loop.

If the flip returns with a DDERR_SURFACELOST value, an attempt is made to restore the surface by using the IDirectDrawSurface::Restore method. If the restore is successful, the application loops back to the IDirectDrawSurface::Flip call and tries again. If the restore is unsuccessful, the application breaks from the while loop, and returns with an error.

Note When you call IDirectDrawSurface::Flip, the flip does not complete immediately. Rather, a flip is scheduled for the next time a vertical blank occurs on the system. If, for example, the previous flip has not occurred, IDirectDrawSurface::Flip returns DDERR_WASSTILLDRAWING. In the example, the IDirectDrawSurface::Flip call continues to loop until it returns DD_OK.