Advertisement

Alpha Blending

Started by August 19, 2000 12:34 AM
2 comments, last by Qoy 24 years, 4 months ago
I've written a function to draw a project a shadow and draw it alpha blended, 50% black with everything under it. However, it's taken my frame rate down quite a bit. Not to unplayable levels, but it still bothers me. I can't think of anything else to do for it, so I thought I'd post it here to get opinions on optimization. Note:: I'm using 16 bit color, but I'm doing 2 pixels at a time, without MMX, just by using 32 bit variables. It does the shadows at a lower resolution, but I know that, and it's OK. It's not a problem with the design. Here's the code:
        
void DrawBlendShadowClip(Bitmap* bitmap,int x,int y,RECT* srcRect)
{
	// project the shadow

	x += -((x - LIGHT_SOURCE_X) >> SHADOW_PROJECT_X);
	y += -((y - LIGHT_SOURCE_Y) >> SHADOW_PROJECT_Y);

	// declare needed vars

	unsigned short* picBuffer = bitmap->bits;
	int srcWidth = srcRect->right - srcRect->left,
		srcHeight = srcRect->bottom - srcRect->top;
	int bitmapBottom = y + srcHeight;
	int xShift = 0, yShift = 0;

	// if it's out of range, don't bother
	if((y > SCREEN_HEIGHT) || (bitmapBottom < 0))
	{
		return;
	}
	// if y is negative, increment it and the buffer until it's not
	else if(y < 0)
	{
		yShift = -y;   // yShift = abs(y) because y < 0
		y = 0;            // y += yShift, because it starts at 0
		srcHeight -= yShift;
	}
	// now clip the bottom

	if(bitmapBottom > SCREEN_HEIGHT)
	{
		// just modify the height

		xShift = bitmapBottom - SCREEN_HEIGHT;
		srcHeight -= xShift;
	}

	// put the start of where we have to draw in picBuffer

	picBuffer += srcRect->left + ((srcRect->top + yShift) * bitmap->width);

	// declare some 32-bit conversion stuff to process 2 pixels at a time

	unsigned long* vBuffer = 
		(unsigned long*)((unsigned short*)desc.lpSurface + (x + (y * pixelLPitch)));
	unsigned long* pBuffer = (unsigned long*)picBuffer;
	unsigned long bWidth = bitmap->width >> 1;
	unsigned long sWidth = srcWidth >> 1;

	// blend, 2 pixels at a time
	for(int height = 0; height < srcHeight; height++)
	{
		for(unsigned int width = 0; width < sWidth; width++, pBuffer++, vBuffer++)
			if(*pBuffer != transparentColor)
				*vBuffer = (*vBuffer & lowBitMask) >> 1;

		vBuffer += lPitch32Bit - sWidth;
		pBuffer += bWidth - sWidth;
	}
} // end DrawBlendShadowClip

        
Thanks! Edited by - Qoy on 8/19/00 12:35:58 AM
Although I regret doing it, I''m gonna bump this up for one more day, because I can''t think of anything to do to make it faster
Advertisement
I suppose you read the programming section of this site first then you''ll know how to do correctly and fast alpha blending, it''s inside the articles: PlotPixel16bits, AlphaBlendind true color.
Yes I read the article, that''s where I got the method from. Unfortunately, the code from the article isn''t much faster than mind I dont think...

This topic is closed to new replies.

Advertisement