[SDL] truble to get the color of a surface pixel

antonmasteR antonmaster at mail.ru
Tue Jul 4 09:41:46 PDT 2006


You can check the pixel color with this function:

Uint32 GetPixel(SDL_Surface *surf, int x, int y)
{
	//This function returns pixels color
	int bpp = surf->format->BytesPerPixel;
	Uint8 *p = (Uint8 *)surf->pixels + y * surf->pitch + x * bpp;
	
	switch (bpp)
	{
		case 1:
			return *p;

		case 2:
			return *(Uint16 *)p;

		case 3:
			if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
				return p[0] << 16 | p[1] << 8 | p[2];
			else
				return p[0] | p[1] << 8 | p[2] << 16;
		
		case 4:
			return *(Uint32 *)p;
		
		default:
			return 0;
	}
}

then use SDL_GetRGB to convert the Uint32 color value into pixel rgb format.


---
http://gamescreators.sf.net      Linux game developing community

-----Original Message-----
From: f <ferranf at gmail.com>
To: sdl at libsdl.org
Date: Mon, 3 Jul 2006 21:06:34 +0200
Subject: [SDL] truble to get the color of a surface pixel

> 
> i need to get the color of a pixel of specific position of a surface,
> i'm trying something like this:
> 
> 
> int frogoutofbounds(SDL_Surface *maskbackground)
> {
>         SDL_Color col;
>         Uint32 pixupleft;
>         SDL_PixelFormat *fmt;
>         Uint8 r,g,b;
>         pixupleft=frog.pos.x+(frog.pos.y*800);
>         fmt=maskbackground->format;
>         SDL_GetRGB(pixupleft,fmt,&r,&g,&b);
>         col.g=g;
>         printf("green color:%d\n",col.g);
> ......
> 
> 
> but this don't return me the rgb code, it returns me an non-sense
> number that isn't correct.
> 
> frog is a global struct that have the position of a sprite (frog.pos)
> , i want to know if the upper left corner of the sprite  (800x600
> screen size) is over the "black zone" of the mask of the background.
> 
> thanks in advance
> 
> 
> -- 
> f || www.ct.upc.edu/%7eferran
> 
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl
> 




More information about the SDL mailing list