Platform SDK: DirectX

Step 6: 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++]

The WM_TIMER message in DDEx1 is devoted to flipping to the back buffer. 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.

    case WM_TIMER:
        // Update and flip surfaces
        if (g_bActive && TIMER_ID == wParam)
        {
            UpdateFrame(hWnd);
            while (TRUE)
            {
                hRet = g_pDDSPrimary->Flip(NULL, 0);
                if (hRet == DD_OK)
                    break;
                if (hRet == DDERR_SURFACELOST)
                {
                    hRet = g_pDDSPrimary->Restore();
                    if (hRet != DD_OK)
                        break;
                }
                if (hRet != DDERR_WASSTILLDRAWING)
                    break;
            }
        }
        break;

In the example, the g_pDDSPrimary 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 7: Deallocate the DirectDraw Objects