Jump to content
  • Advertisement
Sign in to follow this  

modifying texture and render 2D graphics by sprite

This topic is 4110 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, I am trying to render some 2D graphics by using ID3DXSprite. But the only graphic format available for me is HBITMAP, so i try to load the graphics by directly copying into locked texture. However after loaded the graphics my window shows only white( I was setting background to gray) , and I try to dump the loaded texture by D3DXSaveTextureToFile, the result is the same graphics except upside-down. So I wondering if I missed some procedure or if there is better way to load HBITMAP into texture. This is my loading code
void * buff = malloc( size.x * size.y * 4);
ZeroMemory(&BitInfo, sizeof(BITMAPINFO));
BitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
HDC screenDC = CreateDC ( TEXT("DISPLAY"), NULL, NULL, NULL );

GetDIBits( screenDC, hBitmap, 0, 0, NULL, &BitInfo, DIB_RGB_COLORS);
BitInfo.bmiHeader.biBitCount = 32;
BitInfo.bmiHeader.biCompression = 0;
BitInfo.bmiHeader.biSizeImage = size.x * size.y * 4;

GetDIBits( screenDC, hBitmap, 0, size.y, buff, &BitInfo, DIB_RGB_COLORS);
D3DXCreateTexture( D3DDevice, size.x, size.y, 0, D3DX_DEFAULT, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &texture);	
texture->LockRect( 0, &locked, NULL, 0);
Decoded32bitARGB *pDst = reinterpret_cast< Decoded32bitARGB* >( locked.pBits );
BYTE * pSrc = reinterpret_cast< BYTE *>( buff);
DPitch = locked.Pitch/4;
int k=0;
for (int i=0; i<size.y; ++i)
	for (int j=0; j<size.x; ++j)
		pDst[i*DPitch+j].A = *(pSrc+k); k++;
		pDst[i*DPitch+j].R = *(pSrc+k); k++;
		pDst[i*DPitch+j].G = *(pSrc+k); k++;
		pDst[i*DPitch+j].B = *(pSrc+k); k++;
texture->UnlockRect( 0);
free( buff);

and this is my code for rendering by sprite
D3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(128, 128, 128), 1.0f, 0);
if( NULL != sprite)
	if( SUCCEEDED( sprite->Begin( D3DXSPRITE_ALPHABLEND ) ) )
		if( NULL != texture)
			sprite->Draw( texture, NULL, NULL, NULL, 0xFFFFFFFF);
D3DDevice->Present( NULL, NULL, NULL, NULL);

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!