Creating a Texture Handle

The following example demonstrates how to create an IDirect3DTexture2 interface. It also illustrates the process of obtaining a texture handle by calling the IDirect3DTexture2::GetHandle method. It then loads the texture using the IDirect3DTexture2::Load method. (Explicitly loading the texture in this way, however, is only required if you created the texture with the DDCAPS_ALLOCONLOAD surface capability.) Note that the DirectDraw surface you query must have the DDSCAPS_TEXTURE capability to support a Direct3D texture. See Creating Surfaces and DDSCAPS.

// This code fragment assumes that lpDDS is a valid pointer to
// a DirectDraw surface, and that lpD3DDevice is a valid pointer to
// an IDirect3DDevice3 interface.
 
LPDIRECT3DTEXTURE2 lpD3DTexture2;
D3DTEXTUREHANDLE d3dhTextureHandle;
 
// Get the texture interface pointer.
lpDDS->QueryInterface( IID_IDirect3DTexture2, 
                       &lpD3DTexture2);
 
// Associate the texture with a device.
lpD3DTexture2->GetHandle( lpD3DDevice,
                          d3dhTextureHandle);
 
// Load the texture.
lpD3DTexture2->Load(lpD3DTexture2);
 

Note  Loading a texture with the IDirect3DTexture2::Load method allocates the memory for the texture. Once this is done, your application may load a bitmap onto the surface associated with the texture from a resource or a file. Direct3D uses device-independent bitmaps (DIBs) as texture bitmaps. For details, see the documentation for DIB in the Platform SDK.