Platform SDK: DirectX |
The information in this section pertains only to applications written in C and C++. See DirectDraw Visual Basic Tutorials.
After the primary surface and a back buffer have been created, the DDEx1 example writes some text on the back buffer surface by using standard Windows GDI functions, as shown in the following example.
if (g_pDDSBack->GetDC(&hdc) == DD_OK) { SetBkColor(hdc, RGB(0, 0, 255)); SetTextColor(hdc, RGB(255, 255, 0)); if (phase) { GetClientRect(hWnd, &rc); GetTextExtentPoint(hdc, szMsg, lstrlen(szMsg), &size); TextOut(hdc, (rc.right - size.cx) / 2, (rc.bottom - size.cy) / 2, szMsg, sizeof(szMsg) - 1); TextOut(hdc, 0, 0, szFrontMsg, lstrlen(szFrontMsg)); phase = 0; } else { TextOut(hdc, 0, 0, szBackMsg, lstrlen(szBackMsg)); phase = 1; } g_pDDSBack->ReleaseDC(hdc); }
The example uses the IDirectDrawSurface7::GetDC method to retrieve the handle of the device context, and it internally locks the back buffer in preparation for writing. If you are not going to use Windows functions that require a handle of a device context, you could use the IDirectDrawSurface7::Lock and IDirectDrawSurface7::Unlock methods to lock and unlock the back buffer.
Next, the phase variable determines whether the primary buffer message or the back buffer message should be written. If phase equals 1, the primary buffer 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.
After the message is written to the back buffer, the back buffer is unlocked by using the IDirectDrawSurface7::ReleaseDC method.
Locking the surface memory (whether the whole surface or part of a surface) ensures that your application and the system blitter cannot obtain access to the surface memory at the same time. This prevents errors from occurring while your application is writing to surface memory. In addition, your application cannot page flip until the surface memory is unlocked.
After the surface is locked, the example uses standard Windows GDI functions: SetBkColor to set the background color, SetTextColor to select the color of the text to be placed on the background, and TextOut to print the text and background color on the surfaces.
After the text has been written to the buffer, the example uses the IDirectDrawSurface7::ReleaseDC method to unlock the surface and release the handle. Whenever your application finishes writing to the back buffer, you must call either IDirectDrawSurface7::ReleaseDC or IDirectDrawSurface7::Unlock, depending on your application. Your application cannot flip the surface until the surface is unlocked.
Note After the surface is unlocked by using IDirectDrawSurface7::Unlock, the pointer to the surface memory is invalid. You must use IDirectDrawSurface7::Lock again to obtain a valid pointer to the surface memory.