You can examine the “pixels” element of the surface. e.g.: my_surface->pixels
It’s a pointer to a chunk of memory containing the color values, which you
can then break down into R, G and B values.
Here’s a simple example:
Uint32 getpixel(SDL_Surface * surf, int x, int y)
Uint8 * p;
bpp = surf->format->BytesPerPixel;
p = (Uint8 *) (surf->pixels + (y * surf->pitch) + (x * bpp));
case 1: return *p; break;
case 2: return *(Uint16 *)p; break;
if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p << 16 | p << 8 | p;
return p | p << 8 | p << 16;
case 4: return *(Uint32 *)p;
You send it your surface, and the X and Y coordinates, and it sends back
a Uint32 (32-bit unsigned integer) which contains the color.
At that point, you can use SDL_GetRGB() to figure out what the actual
R, G and B values are inside that Uint32. (It depends on what format
the surface is.)
SDL_Surface * my_surface;
Uint8 r, g, b;
int x, y;
the_color = getpixel(my_surface, x, y);
SDL_GetRGB(the_color, my_surface->format, &r, &g, &b);
At this point, the Uint8’s (unsigned 8-bit integers) “r”, “g” and “b"
will have the value of the pixel at position “x”,“y” inside the surface
Whew!On Fri, Mar 14, 2003 at 05:46:46PM +0100, Dirk Beijaard wrote:
I this approach usable, or am I doing something impossible? If possible,
how do I retrieve the color?
bill at newbreedsoftware.com Hire me!