WIN 32 Alpha Blending

This topic is 4261 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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;
srcBitmapDC.CreateCompatibleDC(pScreenDC);
pOldSrcBitmap=srcBitmapDC.SelectObject(this);

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

for(i=0;i<=Width();i++)
for(j=0;j<=Height();j++)
{
if(srcBitmapDC.GetPixel(i,j)==color)
else
}

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



Share on other sites
Hi,

check out the TransparentBlt() and AlphaBlend() functions of the Win32 API.

Cheers,
Simon

Share on other sites
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.