Sign in to follow this  
shadowcomplex

WIN 32 Alpha Blending

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this