[SDL] Pixel access segfault

Started by
2 comments, last by TomVD 11 years, 7 months ago
I try to access a pixel on a surface with the getpixel function from SDL's help:
http://sdl.beuc.net/...ki/Pixel_Access

It works for 32bit surfaces, but gives segfault for 8bit surfaces at line return *p; after case 1:.
If I change that line to return *(Uint32*)p; I don't get segfault, but when I try to use the returned value by either SDL_GetRGB or SDL_GetRGBA, I get segfault, so the returned value is obviously incorrect. (However with the 32bit surface these functions work, so it's not me using them wrong way.)
I haven't tested for 16 or 24 bit surfaces.
Aslo the 32bit surface was loaded from a png with per pixel alpha, but the 8bit surface was loaded from a bmp with colorkey alpha, but I don't think this matters.
Any idea how to get it working?
Advertisement
I have used that exact code myself before using memory fences with no problems.

Are you sure you're accessing at valid pixels? E.g., the top left pixel is (0,0), NOT (1,1). Accessing a 512x512 image at location (512,512) could cause a segfault.

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

Yes, I'm sure because exactly the same code works for 32 bit surfaces. If would try to access with invalid parameters, it would cause segfault for 32 bit surafces as well.
Hmm, an 8bit surface is not stored as RGB values, but as indexes to a palette. The getRGB is using that index to look up the RGB value in your palette. Maybe you didn't create that palette data which might explain the segfault?

We used 8 bit surfaces and palettes in our retro game remake project, but it didn't give use anything but head-aches. Unless you are coding for a very old device with limited memory, or want to give yourself a challenge, I personally would suggest NOT using 8 bit surfaces.

This topic is closed to new replies.

Advertisement