Color key problems with 32 bit Color

Started by
1 comment, last by Josh 24 years, 1 month ago
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
Joshhttp://www.jh-software.com
Advertisement
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
You are absolutely right! Thank you! I figured it was something simple that I was overlooking.


Josh
http://www.jh-software.com
Joshhttp://www.jh-software.com

This topic is closed to new replies.

Advertisement