##### Creating an Off-Screen Surface

An off-screen surface is often used to cache bitmaps that will later be blitted to the primary surface or a back buffer. You must declare the dimensions of an off-screen surface by including the DDSC_WIDTH and DDSD_HEIGHT flags and the corresponding values in the dwWidth and dwHeight members. Additionally, you must include the DDSCAPS_OFFSCREENPLAIN flag in the accompanying DDSCAPS structure.

By default, DirectDraw creates a surface in display memory unless it will not fit, in which case it creates the surface in system memory. You can explicitly choose display or system memory by including the DDSCAPS_SYSTEMMEMORY or DDSCAPS_VIDEOMEMORY flags in the dwCaps member of the DDSCAPS structure. The method fails, returning an error, if it can’t create the surface in the specified location.

The following example shows how to prepare for creating a simple off-screen surface.

DDSURFACEDESC ddsd;

ddsd.dwSize = sizeof(ddsd);

// Tell DirectDraw which members are valid.

ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;

// Request a simple off-screen surface, sized

// 100 by 100 pixels.

ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;

dwHeight = 100;

dwWidth = 100;

In previous versions of DirectX, the maximum width of off-screen surfaces was limited to the width of the primary surface. With DirectX 5, you can create surfaces as wide as you need, permitting that the display hardware can support them. Be careful when declaring wide off-screen surfaces; if the video card memory cannot hold a surface as wide as you request, the surface is created in system memory. If you explicitly choose video memory and the hardware can’t support it, the call fails.