Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
SSW_8_11.doc
Скачиваний:
2
Добавлен:
01.05.2025
Размер:
5 Мб
Скачать
☆

Step 6: Writing to the Surface.The first half of the wm_timer message in ddex1 is devoted to writing to the back buffer, as shown in the following example:

case WM_TIMER:

// Flip surfaces.

if(bActive)

{if (lpDDSBack->GetDC(&hdc) == DD_OK)

{ SetBkColor(hdc, RGB(0, 0, 255));

SetTextColor(hdc, RGB(255, 255, 0));

if(phase)

{ TextOut(hdc, 0, 0, szFrontMsg, lstrlen(szFrontMsg));

phase = 0;

}

else

{ TextOut(hdc, 0, 0, szBackMsg, lstrlen(szBackMsg));

phase = 1;

}

lpDDSBack->ReleaseDC(hdc);

}

The line of code that calls the IDirectDrawSurface2::GetDC method locks the back buffer in preparation for writing. The SetBkColor and SetTextColor functions set the colors of the background and text.

Next, the "phase" variable determines whether the primary buffer message or the back buffer message should be written. If "phase" equals 1, the primary surface message is written, and "phase" is set to 0. If "phase" equals 0, the back buffer message is written, and "phase" is set to 1. Note, however, that in both cases the messages are written to the back buffer.

After the message is written to the back buffer, the back buffer is unlocked by using the IDirectDrawSurface::ReleaseDC method.

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.

Step 8: Deallocating the DirectDraw Objects. When you press the F12 key, the DDEX1 application processes the WM_DESTROY message before exiting the application. This message calls the finiObjects function, which contains all of the IUnknown::Release calls, as shown below:

static void finiObjects(void)

{

if(lpDD != NULL)

{

if(lpDDSPrimary != NULL)

{

lpDDSPrimary->Release();

lpDDSPrimary = NULL;

}

lpDD->Release();

lpDD = NULL;

}

} // finiObjects

The application checks if the pointers to the DirectDraw object (lpDD) and the DirectDrawSurface object (lpDDSPrimary) are not equal to NULL. Then DDEX1 calls the IDirectDrawSurface::Release method to decrease the reference count of the DirectDrawSurface object by 1. Because this brings the reference count to 0, the DirectDrawSurface object is deallocated. The DirectDrawSurface pointer is then destroyed by setting its value to NULL. Next, the application calls IDirectDraw::Release to decrease the reference count of the DirectDraw object to 0, deallocating the DirectDraw object. This pointer is then also destroyed by setting its value to NULL.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]