Archived

This topic is now archived and is closed to further replies.

PHRICTION

DirectGrahics: Creating texture from HBITMAP

Recommended Posts

psykr    295
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 dc

DWORD* 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 texture

D3DLOCKED_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.

Share this post


Link to post
Share on other sites
PHRICTION    122
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.

Share this post


Link to post
Share on other sites