Sign in to follow this  
tadobie

looking for a color in a RECT

Recommended Posts

tadobie    122
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[i];
		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
Evil Steve    2017
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

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

    Sign in to follow this