sprites in windows

Started by
3 comments, last by stooge_dev 20 years, 8 months ago
This code draws a bitmap to the screen with the transparent pixels skipped. It works, except the background shows through the image that is drawn. How can I fix this?

int DrawSprite(HDC hdc, HBITMAP hbmColour,int x, int y, COLORREF crTransparent)
{
	HDC hdcMem, hdcMem2;
	HBITMAP hbmMask, hOldBitmap, hOldBitmap2;
	BITMAP bm;
	GetObject(hbmColour, sizeof(BITMAP), &bm);
	hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);

	hdcMem = CreateCompatibleDC(hdc);
	hdcMem2 = CreateCompatibleDC(hdc);

	hOldBitmap = SelectObject(hdcMem, hbmColour);
	hOldBitmap2 = SelectObject(hdcMem2, hbmMask);

	SetBkColor(hdcMem, crTransparent);

	BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

	BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);

   BitBlt(hdc, x, y, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCAND);

	BitBlt(hdc, x, y, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCPAINT);

   SelectObject(hdcMem, hOldBitmap);
   SelectObject(hdcMem2, hOldBitmap2);
	DeleteDC(hdcMem);
	DeleteDC(hdcMem2);
   DeleteObject(hbmMask);
	return 0;
}
Advertisement
you need an offscreen buffer, google for double buffering
That''s what is supposed to be accomplished by transparancy.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
You can get the program from here and click on space invader clone. As you'll see the star is showing through the red area of the ship. This gives the star a red tint instead of making it hidden under the ship.

[edited by - stooge_dev on August 11, 2003 6:07:30 PM]
no. all of what they said is incorrect. the colors look starced and its semi transparent? yeah thats a flaw with the bitblt function in windows. EVERY blitted sprite has to have a mask, and if you are using the masks correctly, then check your copy method. i dont think SRCINVERT is the right one, try SRCPAINT.
Because you touch yourself at night!

This topic is closed to new replies.

Advertisement