How to use DirectDraw
A version of this page is also available for
4/8/2010
This topic includes code snippets that initialize DirectDraw and demonstrate simple drawing.
Initializing DirectDraw
This code example initializes DirectDraw.
#include <ddraw.h>
HRESULT hr;
IDirectDraw * pDD = NULL;
IDirectDrawSurface * pDDSPrimary = NULL;
DDSURFACEDESC ddsd;
hr = DirectDrawCreate(NULL, &pDD, NULL);
hr = pDD->SetCooperativeLevel(hwnd, DDSCL_FULLSCREEN);
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
hr = pDD->CreateSurface(&ddsd, &pDDSPrimary, NULL);
Initializing DirectDraw
This code example sets a pixel at the address x=57, y=97 to the color red.
#include <ddraw.h>
HRESULT hr;
hr = pDDSPrimary->Lock(NULL, &ddsd, DDLOCK_WAITNOTBUSY, NULL);
// Set pixel 57, 97 to Red (assuming RGB565 pixel fmt)
int x = 57;
int y = 97;
BYTE * pPixelOffset = (BYTE*)ddsd.lpSurface
+ x * ddsd.lXPitch
+ y * ddsd.lPitch;
*(WORD*)pPixelOffset = 0xf800;
hr = pDDSPrimary->Unlock();