Skip to main content
gamedev.net
🔒 Locked

create texture from memory

Started by Loncs 3 replies 4.1k views
Original Post
Loncs
Loncs
Hi! I'm having problems with creating a texture from memory. I want to load a simple 8x8 sized image, wich displays a cross, and draw it with d3dxsprite. But instead of a cross a black filled rectangle is displayed. Here is the code what I'm using :

D3DLOCKED_RECT r;
HRESULT hr;
DWORD c0=D3DCOLOR_ARGB(255,0,0,0), c1=D3DCOLOR_ARGB(255,255,255,255);
DWORD *pData, 
Image[64]={c0,c0,c0,c1,c0,c0,c0,c0,
 	   c0,c0,c0,c1,c0,c0,c0,c0,
	   c0,c0,c0,c1,c0,c0,c0,c0,
           c1,c1,c1,c1,c1,c1,c1,c0,
           c0,c0,c0,c1,c0,c0,c0,c0,
	   c0,c0,c0,c1,c0,c0,c0,c0,
	   c0,c0,c0,c1,c0,c0,c0,c0,
	   c0,c0,c0,c0,c0,c0,c0,c0};

m_pDevice->CreateTexture(8,8,1,D3DUSAGE_DYNAMIC,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&m_pPointTexture,NULL);
hr=m_pPointTexture->LockRect(0,&r,NULL,0);
pData=(DWORD*)r.pBits;
for (int i=0; i<8; i++)
  for (int j=0; j<8; j++) 
	pData[j*8+i]=Image[j*8+i];

m_pPointTexture->UnlockRect(0);	

What am I doing wrong?
Evil Steve
Evil Steve
Quote:
Original post by Loncs
Hi! I'm having problems with creating a texture from memory. I want to load a simple 8x8 sized image, wich displays a cross, and draw it with d3dxsprite. But instead of a cross a black filled rectangle is displayed. Here is the code what I'm using :

D3DLOCKED_RECT r;HRESULT hr;DWORD c0=D3DCOLOR_ARGB(255,0,0,0), c1=D3DCOLOR_ARGB(255,255,255,255);DWORD *pData, Image[64]={c0,c0,c0,c1,c0,c0,c0,c0, 	   c0,c0,c0,c1,c0,c0,c0,c0,	   c0,c0,c0,c1,c0,c0,c0,c0,           c1,c1,c1,c1,c1,c1,c1,c0,           c0,c0,c0,c1,c0,c0,c0,c0,	   c0,c0,c0,c1,c0,c0,c0,c0,	   c0,c0,c0,c1,c0,c0,c0,c0,	   c0,c0,c0,c0,c0,c0,c0,c0};m_pDevice->CreateTexture(8,8,1,D3DUSAGE_DYNAMIC,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&m_pPointTexture,NULL);hr=m_pPointTexture->LockRect(0,&r,NULL,0);pData=(DWORD*)r.pBits;for (int i=0; i<8; i++)  for (int j=0; j<8; j++) 	pData[j*8+i]=Image[j*8+i];m_pPointTexture->UnlockRect(0);	


What am I doing wrong?

1. Not using the Debug Runtimes
2. Not checking the return value from LockRect()
3. Ignoring the return value from CreateTexture()
4. Ignoring the Pitch member of the D3DLOCKED_RECT struct.
5. Locking a dynamic texture without the D3DLOCK_DISCARD or D3DLOCK_NOOVERWRITE flag.
6. Trying to create a dynamic texture in system memory instead of a dynamic texture in the default pool, or a non-dynamic texture in the managed pool (Preferable unless you're updating the texture very frequently (More than once a second))
Loncs
Loncs
Thanks for the quick answer. So I changed the createtexture and lockrect parts to this :

	if (m_pDevice->CreateTexture(8,8,1,D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pPointTexture,NULL)!=D3D_OK)		return false;	if (m_pPointTexture->LockRect(0,&r,NULL, D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE)!=D3D_OK)		return false;


But the results are still the same. So I guess the problem is because I'm ignoring the pitch. But I'm not sure how to use it in this case. Could you clarify that please. Thanks!
Evil Steve
Evil Steve
Quote:
Original post by Loncs
Thanks for the quick answer. So I changed the createtexture and lockrect parts to this :

	if (m_pDevice->CreateTexture(8,8,1,D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pPointTexture,NULL)!=D3D_OK)		return false;	if (m_pPointTexture->LockRect(0,&r,NULL, D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE)!=D3D_OK)		return false;


But the results are still the same. So I guess the problem is because I'm ignoring the pitch. But I'm not sure how to use it in this case. Could you clarify that please. Thanks!
As a nitpick - you should really use the SUCCEEDED or FAILED macros instead of checking directly against D3D_OK, that's only one of 2 billion possible success codes (Although it's pretty unlikely the functions would return another code).

As far as the pitch goes:
for (int y=0; y<8; y++){  pData=(DWORD*)((BYTE*)r.pBits + r.Pitch);  for (int x=0; x<8; x++)    pData[x]=Image[y*8+x];}

That should work I think (Not tested). If that still gives a black texture, try using D3DXSaveTextureToFile after calling UnlockRect() - that'll show you if the data being copied in is what you expect, or if it's the rendering that's causing issues.

EDIT: Brainfart when using pitch...
Loncs
Loncs
Works now, thanks a lot!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.