• Advertisement
Sign in to follow this  

[SDL] Pixel access segfault

This topic is 1965 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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? Edited by Vaclav

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement