Step 7: Flipping the Surfaces

After the surface memory is unlocked, you can use the IDirectDrawSurface3::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 parameter designates the primary surface and its associated back buffer. When IDirectDrawSurface3::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 IDirectDrawSurface3::Restore method. If the restore is successful, the application loops back to the IDirectDrawSurface3::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 IDirectDrawSurface3::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, IDirectDrawSurface3::Flip returns DDERR_WASSTILLDRAWING. In the example, the IDirectDrawSurface3::Flip call continues to loop until it returns DD_OK.