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)
{
int bpp;
Uint8 * p;
bpp = surf->format->BytesPerPixel;
p = (Uint8 *) (surf->pixels + (y * surf->pitch) + (x * bpp));
switch (bpp)
{
case 1: return *p; break;
case 2: return *(Uint16 *)p; break;
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;
break;
case 4: return *(Uint32 *)p;
}
return 0;
}
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.)
An example:
Uint32 the_color;
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
“my_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!
Bill Kendrick Bill Kendrick | Résumé