Damian damian_rajkowski at technologyonecorp.com
Wed Jan 19 22:18:57 PST 2005

Damian <damian_rajkowski <at> technologyonecorp.com> writes:

> I'm faced with what must surely be a common problem - reading the color of a 
> pixel on an SDL surface. I've struggled with three methods (shown below) 
> gleamed from the SDL documentation and other sources on the web, but in all 
> cases the program GPF's on the SDL_GetRGBA call. 

Oops. Its not SDL_GetRGBA thats failing, it seems to be the reference to the 
location in the SDL surface (*p) that kills it.

> Uint32 getpixel(SDL_Surface *surface, int x, int y) {
>         case 4: return *(Uint32 *)p;

In Method 1, it fails on this line, where it refers to *p.

> SDL_GetRGBA((Uint32)(*p),screen->format,&R,&G,&B,&A);

In Method 2, it fails on this line (again reference to *p).

I though it might be caused by using a hardware surface, but a software surface 
exhibits the same problem. 

Is anyone willing to post an example of a working getpixel() func?

More information about the SDL mailing list