Jump to content
  • Advertisement
Sign in to follow this  
tadobie

looking for a color in a RECT

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

The following code has an infinite loop in it. Since this doesn't work, can someone explain why? All I want to do is to detect if a certain color is inside a given RECT. Anyone know how?
			
	//thid function declares and Zeros the struct
	DDSURFACEDESC2 sd = Attacker->GetSurfaceDesc();
	
	RECT rct = GetOverlapRect(GetSizeRect(Attacker), 
                                  GetSizeRect(Defender));
	
        //this function calls lock for the surface
	Surface->LockSurface(rct, sd);

	LPDWORD pixels = (LPDWORD)(sd.lpSurface);
	
	DWORD colour = pixels[0];

	for(int i=0;i<sizeof(pixels);i++)
	{	
		colour = pixels;
		if(colour == COLLISION_COLOR)
		{
                   //this function calls unlock for the surface
			Attacker->UnlockSurface(rct);
			return true;
		}
	}
	return false;
}

Share this post


Link to post
Share on other sites
Advertisement
A few comments on that code:
  • sizeof(pixels) is always 4 (on 32-bit CPUs) because pixels is a pointer.
  • This line is redundant: DWORD colour = pixels[0]; since colour is overwritten in the loop.
  • UnlockSurface() isn't called if theres no collision.
  • I don't see any infinite loop in that code.

    You want to change your loop so that its:
    for(int i=0;i<(rct.right-rct.left)*(rct.bottom-rct.top);i++)
    So you loop for each pixel in the RECT.

    Share this post


    Link to post
    Share on other sites
    Sign in to follow this  

    • Advertisement
    ×

    Important Information

    By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

    We are the game development community.

    Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

    Sign me up!