Your code fails because of these two lines of code:
D3DLOCKED_RECT *m_lockedRect;
D3DSURFACE_DESC *m_desc;
You declare pointers, but remember in C++, when you delare pointers, they point to invalid junk, not valid objects. So when you call m_texture->GetLevelDesc(0,m_desc), m_desc is invalid junk and the call fails. Your code should be:
D3DSURFACE_DESC m_desc;
m_texture->GetLevelDesc(0, &m_desc);
D3DLOCKED_RECT m_lockedRect;
m_texture->LockRect(0, &m_lockedRect, 0, 0);
Remember, GetLevelDesc and LockRect are basically fill functions. They just fill in pre-existing structures. The structures have to be valid.
And another thing, declare your variables as you need them. This is not C programming where you must declare all your variables at the top of the function :p. Some of these structures are pretty big and have to be pushed onto the stack even if say there’s a return call in your function somewhere and the structure never gets used. For something like a game these optimizations become very important.
This following code worked great for me.
HRESULT result;
// load texture from file
LPDIRECT3DTEXTURE9 m_texture;
result = D3DXCreateTextureFromFileEx(g_pd3dDevice, “texture.bmp”, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, 0, 0, &m_texture);
if(result != D3D_OK) {
MessageBox(hWnd, “D3DXCreateTextureFromFileEx failed.”, “”, MB_OK);
return E_FAIL;
}
D3DSURFACE_DESC m_desc;
m_texture->GetLevelDesc(0, &m_desc);
D3DLOCKED_RECT m_lockedRect;
if(m_texture->LockRect(0, &m_lockedRect, 0, 0) != D3D_OK) {
MessageBox(hWnd, “LockRect failed.”, “”, MB_OK);
return E_FAIL;
}
m_texture->UnlockRect(0);
// release texture
m_texture->Release();
m_texture = 0;