locking LPDIRECT3DTEXTURE9


#1

LPDIRECT3DTEXTURE9 m_tex;
D3DLOCKED_RECT *m_lockedRect;
D3DSURFACE_DESC *m_desc;

D3DXCreateTextureFromFileEx(m_PartDevice, strfilename, 0,0,0,0,
D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, dwKeyColor, NULL, NULL, &m_texture);

m_texture->GetLevelDesc(0,m_desc);

m_texture->LockRect(0,m_lockedRect,NULL,0)))

//do somethng with m_lockedRect->pBits using m_desc to read pithc and stuff like that
m_texture->UnlockRect(0);

the lockrect call fails… does anyone know what i have to do to lock the texture so that i can manipulate it?

thanks…


#2

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;


#3

thanks dead eye :slight_smile: thats really helpful… i should have known that, but sometimes my brain doesnt work as it is supposed to :frowning: sorry to waste your time.


#4

No problem. No time wasted at all :).


#5

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.