DirectX SDK

Step 7: Flip the Surfaces

[Visual Basic]

The information in this section pertains only to applications written in C and C++. See DirectDraw Visual Basic Tutorials.

[C++]

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

Next: Step 8: Deallocate the DirectDraw Objects