Archived

This topic is now archived and is closed to further replies.

Josh

Color key problems with 32 bit Color

Recommended Posts

I''m having a problem with using a color key on a DirectDraw surface under 32 bit color. To set a color key, I have a color match function that gets the closest color to the one requested then I use that color to set the color key. Here is the function:
static color_t win32_ColorMatch( LPDIRECTDRAWSURFACE a_Surface, color_t a_Color )
{
	HDC           hdc;
	color_t       rgb;
	color_t       Color = CLR_INVALID;
	DDSURFACEDESC ddsd;

	if( FAILED( a_Surface->GetDC( &hdc ) ) )
		return 0;

	rgb = GetPixel( hdc, 0, 0 );
	SetPixel(hdc, 0, 0, a_Color );
	a_Surface->ReleaseDC( hdc );

	ZeroMemory( &ddsd, sizeof( ddsd ) );
	ddsd.dwSize = sizeof( ddsd );

	if( FAILED( a_Surface->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL ) ) )
		return 0;

	Color = *( uint32* )ddsd.lpSurface;
	Color &= ( 1 << ddsd.ddpfPixelFormat.dwRGBBitCount ) - 1;

	if( FAILED( a_Surface->Unlock( &ddsd ) ) )
		return 0;

	if( FAILED( a_Surface->GetDC( &hdc ) ) )
		return 0;

	SetPixel( hdc, 0, 0, rgb );
	a_Surface->ReleaseDC( hdc );

	return Color;
}
 
color_t is a typedef and is a DWORD. This function works for 8, 16, and 24 bit color. However, it does not work for 32 bit color. Oh, and this is how I set the color key:
DDCOLORKEY ddck;

ddck.dwColorSpaceLowValue	= Color;
ddck.dwColorSpaceHighValue	= Color;

if( FAILED( m_Surface->SetColorKey( DDCKEY_SRCBLT, &ddck ) ) ) {
	throw sys_Error( "gfx", GFX_ERR_SETCOLORKEY );
}
 
Any ideas? Thanks! Josh http://www.jh-software.com

Share this post


Link to post
Share on other sites
I''m not entirly sure but yout line that says this:

Color &= ( 1 << ddsd.ddpfPixelFormat.dwRGBBitCount ) - 1;

Might be overflowing. You could use a special case for 32 bit that skips the Color &= bit.

e.g.

if(ddsd.ddpfPixelFormat.dwRGBBitCount != 32)
{
Color &= .......
}


Hope this helps.

Mark Allen
Lead Programmer
Tashco Studios
http://www.tashco.com

Share this post


Link to post
Share on other sites