RE: [GD-Windows] In memory bitmap to HBITMAP
Brought to you by:
vexxed72
|
From: Jon W. <hp...@mi...> - 2001-12-07 02:23:02
|
Here's my personal utility offscreen bitmap generation function:
void
MyBitmap::setSize(
int width,
int height
)
{
assert( !bitmap ); // can't re-allocate bitmap once done
assert( !bits );
xSize = width;
ySize = height;
dc = gl->getUtilDC( );
BITMAPINFOHEADER bmi;
memset( &bmi, 0, sizeof( bmi ) );
bmi.biSize = sizeof( bmi );
bmi.biWidth = width;
bmi.biHeight = height;
bmi.biPlanes = 1;
bmi.biBitCount = 32;
bmi.biCompression = BI_RGB;
bitmap = CreateDIBSection( dc, (BITMAPINFO *)&bmi, DIB_RGB_COLORS, (void **)&bits, 0, 0 );
assert( bitmap != 0 );
assert( bits != 0 );
textColor = 0xffffff;
bgColor = 0;
fillRect( 0, 0, width, height, 0 );
}
getUtilDC() returns a DC previously returned from CreateCompatibleDC().
fillRect() draws into the bitmap as such:
void
MyBitmap::fillRect(
int left,
int top,
int width,
int height,
unsigned int pixel
)
{
assert( bitmap );
assert( dc );
HBITMAP oldBitmap = (HBITMAP)SelectObject( dc, bitmap );
HBRUSH b = CreateSolidBrush( MK_COLORREF(pixel) );
RECT r;
r.left = left;
r.top = top;
r.right = left+width;
r.bottom = top+height;
BOOL res = FillRect( dc, &r, b );
assert( res );
SelectObject( dc, oldBitmap );
DeleteObject( b );
dirty = true;
}
Hope this helps.
Cheers,
/ h+
> -----Original Message-----
> From: gam...@li...
> [mailto:gam...@li...]On Behalf Of
> Brian Hook
> Sent: Thursday, December 06, 2001 6:04 PM
> To: gam...@li...
> Subject: [GD-Windows] In memory bitmap to HBITMAP
>
>
> I'm struggling with something fairly basic -- I want to load a BMP out
> of a compressed file, convert to an HBITMAP, then blit it into a DDraw
> surface. I know how to do the first and last part, but the middle is
> causing problems. Even worse, I'm not getting any errors.
>
> Loading the bitmap doesn't seem to be a problem. I load it up and parse
> and find the BITMAPFILEHEADER and BITMAPINFOHEADER no probs. I also
> locate the raw bits no problem (and inspect with the debugger to make
> sure I'm getting what I think I'm getting -- in this case, solid
> magenta). Looks fine.
>
> The next step is to create a DC compatible with the desktop:
>
> HDC hDC = CreateCompatibleDC( NULL );
>
> I then create a HBITMAP compatible with DC, and using the bits I pulled:
>
> HBITMAP hbm;
>
> hbm = CreateDIBitmap( hDC, lpBMIH, CBM_INIT, lpBits, lpBMIH,
> DIB_RGB_COLORS );
>
> lpBMIH are simply pointers to the appropriate BITMAPINFOHEADER. They
> have correct values in them. The bitmap is returned successfully.
>
> To verify things, I grab the bits out of the Bitmap to make sure
> everything is cool:
>
> GetDIBits( hDC, hbm, 0, iHeight, tempbuffer, &bmi, DIB_RGB_COLORS );
>
> It returns the correct number of scan lines, but tempbuffer is filled
> with black. This means either the CreateDIBitmap() filled in the bitmap
> with black, or the GetDIBits() failed somehow. I can't figure out which
> is the real culprit, but the bitmap is pretty much a black square.
>
> Any suggestions on how to hunt this down?
>
>
>
>
> _______________________________________________
> Gamedevlists-windows mailing list
> Gam...@li...
> https://lists.sourceforge.net/lists/listinfo/gamedevlists-windows
>
|