Jump to content
  • Advertisement
Sign in to follow this  
shadowcomplex

WIN 32 Alpha Blending

This topic is 4447 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

I was wondering if there is a fast way to use Alpha Blending with the WIN 32 GDI. I have implemented a version of it (code below) that works well, except that it is a performance killer. Right now I am only using it for static scenarios like displaying menu information but I would like to be able to use it in game. Normally rendering my main character and his surroundings I have FPS near 75, but if I alpha blend the main character each frame instead of just transparent drawing, the frames drop to around 20 per second. I am sure that my code below is hideous and ugly, but I was just happy that it worked. Any help on optimizing it/or alternative solutions would be great. Thanks a lot.
void CEBitmap::PasteTransparentAlpha(CEBitmap* pDestBitmap, int x, int y, COLORREF color, int alpha, CDC* pScreenDC)
{
int i,j;

BLENDFUNCTION blender;
blender.BlendOp=AC_SRC_OVER;
blender.BlendFlags=0;
blender.SourceConstantAlpha=alpha;
blender.AlphaFormat=0;

COLORREF destColor,srcColor;
CDC srcBitmapDC,destBitmapDC;
CBitmap *pOldSrcBitmap,*pOldDestBitmap;
CDC maskBitmapDC; CBitmap maskBitmap;
srcBitmapDC.CreateCompatibleDC(pScreenDC);
pOldSrcBitmap=srcBitmapDC.SelectObject(this);

destBitmapDC.CreateCompatibleDC(pScreenDC);
pOldDestBitmap=destBitmapDC.SelectObject(pDestBitmap);

maskBitmapDC.CreateCompatibleDC(pScreenDC);
maskBitmap.CreateCompatibleBitmap(pScreenDC,Width(),Height());
maskBitmapDC.SelectObject(maskBitmap);

for(i=0;i<=Width();i++)
	for(j=0;j<=Height();j++)
	{
	if(srcBitmapDC.GetPixel(i,j)==color)
		maskBitmapDC.SetPixel(i,j,(destBitmapDC.GetPixel(x+i-1,y+j-1)));
	else
		maskBitmapDC.SetPixel(i,j,color);
	}

::AlphaBlend((HDC) destBitmapDC,x,y,Width(),Height(),(HDC) srcBitmapDC,0,0,Width(),Height(),blender);
::TransparentBlt((HDC) destBitmapDC,x,y,Width(),Height(),
(HDC) maskBitmapDC,0,0,Width(),Height(),color);
destBitmapDC.SelectObject(pOldDestBitmap);
srcBitmapDC.SelectObject(pOldSrcBitmap);
}

Share this post


Link to post
Share on other sites
Advertisement
I already use both of these functions (they are in the code), its just that ::Alphablend() is killing my performance for some reason.
Thanks though.

Share this post


Link to post
Share on other sites
First, don't recreate the mask every time, do it once (or whenever your mask color changes), store it, and re-use it. That should give you a decent boost.

If it's still too slow, consider drawing from DDB to DDB, that way AlphaBlend and TransparentBlt, and some other blits call will have a good chance of being hardware accelerated. I don't know what CEBitmap represents, but if it's a DIB or DIBSection chances are you're not getting much hardware acceleration... except maybe color-conversion.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!