DirectGrahics: Creating texture from HBITMAP

Started by
1 comment, last by PHRICTION 20 years, 7 months ago
How would I go about creating a texture from a HBITMAP in Direct3D 9? I can''t seem to find anything examples.
Advertisement
There aren''t examples because it''s not too pretty.. well, at least not the way I do it. I''ve been trying to do a text renderer for a while, but I''ve been procrastinating. :-p Anyway, I needed to print GDI text to an HBITMAP, then copy that to a texture, and let me tell you.. it involved some damn ugly code.
// create bitmap for dcDWORD* pSrcData = 0;BITMAPINFO bmi = { { sizeof( BITMAPINFOHEADER ), nWidth, -nHeight, 1, 32, BI_RGB, 0, 0, 0, 0, }, NULL };HBITMAP hCurrentBmp = CreateDIBSection( hTextDC, &bmi, DIB_RGB_COLORS, (void**)&pSrcData, NULL, 0 );HBITMAP hPrevBmp = (HBITMAP)SelectObject( hTextDC, hCurrentBmp );// -- did gdi rendering here// lock textureD3DLOCKED_RECT LockedRect;if( SUCCEEDED( m_pTexture->LockRect( 0, &LockedRect, NULL, D3DLOCK_NOSYSLOCK ) ) ) {	// copy bitmap data to texture	DWORD* pDestData = (DWORD*)LockedRect.pBits;	for( int y = 0 ; y < nHeight ; y++ ) {		// copy this row of pixels		memcpy( pDestData, pSrcData, nWidth * sizeof( DWORD ) );		// skip past row of pixels		pDestData += ( LockedRect.Pitch / 4 );		pSrcData += nWidth;	}	m_pTexture->UnlockRect( 0 );}
Maybe not the correct way, but it works.
Thats a big help. Coincidently, I wanted to know this because I am in the process of writing a text renderer also. I was hoping there was a nice api call or something, but this works. I still haven''t got all the bugs worked out, but I have it outputing something that resembles text. Thanks.

This topic is closed to new replies.

Advertisement