Jump to content
  • Advertisement
Sign in to follow this  
NukeCorr

C++ bitmap collision?

This topic is 3660 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

I was wondering how to make in C++ and SDL to get a pixel in (x,y)? I have few functions but I don't know how to use them:
Uint32 GetPixel(int x, int y, SDL_Surface *Surf)
{
	Uint32 *bufp;

	if ((x < 0)||(x > Surf->w - 1)||(y < 0)||(y > Surf->h - 1))
        return 0;

    if ( SDL_MUSTLOCK(Surf) )
    {
        if ( SDL_LockSurface(Surf) < 0 )
        {
            return 0;
        }
    }
    switch (Surf->format->BytesPerPixel)
    {
    case 2:
        { // 16
            bufp = (Uint32 *) Surf->pixels + y*Surf->pitch/2 + x;
        }
        break;

    case 4:
        { //32
            bufp = (Uint32 *) Surf->pixels + y*Surf->pitch/4 + x;
        }
        break;
    }

    if ( SDL_MUSTLOCK(Surf) )
    {
        SDL_UnlockSurface(Surf);
    }

	return *bufp;  
}

int GetColor(Uint32 pixel, int rgb)
{
	Uint8 r;
	Uint8 g;
	Uint8 b;

	SDL_GetRGB(pixel, Screen->format, &r, &g, &b);

	switch (rgb)
	{
	case 0:
		return (int)r;
		break;
	case 1:
		return (int)g;
		break;
	case 2:
		return (int)b;
		break;
	}

	return (int)r;
}
Here's where I tried to do this:
bool Collision()
{
	int rgb = GetPixel(Player.x,Player.y,Screen);

	if(rgb == RGB(0,255,0))
		return true;

	return false;
}
But I can't get it work correctly

Share this post


Link to post
Share on other sites
Advertisement
Hi, I did this in one of my previous games.

GetPixel takes in the bitmap-pSurface, and the x and y position of the pixel.

SDL_Color GetPixel ( SDL_Surface* pSurface , int x , int y )
{
SDL_Color color ;
Uint32 col = 0 ;

//determine position
char* pPosition = ( char* ) pSurface->pixels ;

//offset by y
pPosition += ( pSurface->pitch * y ) ;

//offset by x
pPosition += ( pSurface->format->BytesPerPixel * x ) ;

//copy pixel data
memcpy ( &col , pPosition , pSurface->format->BytesPerPixel ) ;

//convert color
SDL_GetRGB ( col , pSurface->format , &color.r , &color.g , &color.b ) ;


return ( color ) ;
}





To call getPixel you just use something like this

SDL_Color moveX;
SDL_LockSurface(Surf);
moveX = GetPixel(backGroundbmp, 122, 450);
SDL_UnlockSurface(Surf);

int red,green, blue;
red = int(moveX.r);
blue = int(moveX.b);
green = int(moveX.g);





int red, green, and blue will then hold the rgb (color) value of the pixel.


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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!