Loading a Texture with D3DX
Microsoft DirectX 9.0 SDK Update (October 2004)

Loading a Texture with D3DX


With D3DX, it is relatively simple to load a texture. Here's an example that does just that. In order to access D3DX, you will need to create a Microsoft Direct3D object and a Direct3D device.

// Create Direct3D object
IDirect3D9 *pd3d = Direct3DCreate9(D3D_SDK_VERSION);

// Create a device
IDirect3DDevice9 *pdevice;
D3DPRESENT_PARAMETERS present;

memset(&present, 0, sizeof(present));
present.BackBufferFormat = D3DFMT_UNKNOWN;
present.Windowed = TRUE;

pd3d->CreateDevice(0, D3DDEVTYPE_REF, hwnd /* from your app */, 
    0, &present, &pdevice);


// Load the texture in as uncompressed
IDirect3DTexture9 *ptexture;

D3DXCreateTextureFromFileEx(pdevice, "myfile.tga", 
    D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0,
    D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH,
    D3DX_DEFAULT, D3DX_DEFAULT, 0, 0, 0,
    &ptexture);

// Get a pointer to the raw data 
D3DLOCKED_RECT rect;
ptexture->LockRect(0, &rect, D3DLOCK_READONLY);


// Access the raw texture data with the rect.pBits pointer
...


// Clean up
ptexture->UnlockRect(0);
ptexture->Release();
pdevice->Release();


© 2004 Microsoft Corporation. All rights reserved.
Feedback? Please provide us with your comments on this topic.
For more help, visit the DirectX Developer Center.