To create a surface representing a single level of a mipmap, specify the DDSCAPS_MIPMAP and DDSCAPS_COMPLEX flags in the DDSURFACEDESC structure passed to the IDirectDraw4::CreateSurface method. Because all mipmaps are also textures, the DDSCAPS_TEXTURE flag must also be specified.
It is possible to create each level manually and build the chain by using the IDirectDrawSurface4::AddAttachedSurface method. However, this is not recommended. Many 3-D hardware vendors optimize their drivers for the IDirectDraw4::CreateSurface method. Applications that build mipmap chains with calls to IDirectDrawSurface4::AddAttachedSurface may find that mipmapping is not as fast.
The following example demonstrates how your application can use the IDirectDraw4::CreateSurface method to build a chain of five mipmap levels of sizes 256×256, 128×128, 64×64, 32×32, and 16×16:
// This code fragment assumes that the variable lpDD is a
// valid pointer to a DirectDraw interface.
DDSURFACEDESC ddsd;
LPDIRECTDRAWSURFACE4 lpDDMipMap;
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_MIPMAPCOUNT;
ddsd.dwMipMapCount = 5;
ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE |
DDSCAPS_MIPMAP | DDSCAPS_COMPLEX;
ddsd.dwWidth = 256UL;
ddsd.dwHeight = 256UL;
ddres = lpDD->CreateSurface(&ddsd, &lpDDMipMap);
if (FAILED(ddres))
.
.
.
You can omit the number of mipmap levels, in which case the IDirectDraw4::CreateSurface method will create a chain of surfaces, each a power of two smaller than the previous one, down to the smallest possible size. It is also possible to omit the width and height, in which case IDirectDraw4::CreateSurface will create the number of levels you specify, with a minimum level size of 1×1.
Note: Each surface in a mipmap chain has dimensions that are one-half that of the previous surface in the chain. If the top-level mipmap has dimensions of 256×128, the dimensions of the second-level mipmap are 128×64, the third-level is 64×32, and so on down to 2×1. If you explicitly specify dimensions in the dwWidth and dwHeight members, you should be aware of some restrictions. Namely, you cannot request a number of mipmap levels in dwMipMapCount that would cause either the width or height of any mipmap in the chain to be smaller than 1. Take the very simple case of a 4×2 top-level mipmap surface: the maximum value allowed for dwMipMapCount here is 2: the top-level dimensions are 4×2, and the dimensions for the second level 2×1. A value larger than 2 in dwMipMapCount would result in a fractional value in the height of the second-level mipmap, and is therefore disallowed.
After your application creates the mipmap surfaces, it needs to associate the surface with a texture. If you are using texture handles, use the procedures outlined in Creating a Texture Handle. If you are using texture interface pointers, see Obtaining a Texture Interface Pointer.