Find color

Hey All,

I am a beginner at SDL, so my appologies if I am asking an obvious
question.

I am working on an interface for a game that shows an irregular shaped
2D map with
a LOT of area’s. To identify an area, I thought I would use a copy of
the map where
each area is filled in a unique color. I load that map on a seperate
surface and by
using the mouse pointer I find the pixel that was clicked. Then I want
to get the
color value of that pixel. My map has 24 pixel colors.

I this approach usable, or am I doing something impossible? If possible,
how
do I retrieve the color?

Thanks,
Dirk.

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!
http://newbreedsoftware.com/bill/ http://newbreedsoftware.com/bill/resume/

I this approach usable, or am I doing something impossible? If possible,
how do I retrieve the color?

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.

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.)

Of course, if you’re doing some sort of lookup table keyed by color,
it’s probably more efficient to use SDL_MapRGB() to map the colors
to their pixel values, and use the pixel values directly in the lookup.

Ron Steinke

“The sound of gunfire, off in the distance. I’m getting used to it now.”
– Talking Heads> From: Bill Kendrick

On Fri, Mar 14, 2003 at 05:46:46PM +0100, Dirk Beijaard wrote:

Bill Thanks for your info! I had almost had it figured out,
but the info you gave me did the trick.

Thanks,
Dirk

No problem. :^) Thanks to those who explained it to me a while back. :wink:

-bill!On Fri, Mar 14, 2003 at 07:30:14PM +0100, Dirk Beijaard wrote:

Bill Thanks for your info! I had almost had it figured out,
but the info you gave me did the trick.


bill at newbreedsoftware.com Hire me!
http://newbreedsoftware.com/bill/ http://newbreedsoftware.com/bill/resume/