[SDL] Pixel access in SDL_Surface

Jacob Welsh welshjf at gmail.com
Fri Apr 4 05:51:43 PDT 2008


Mason Wheeler wrote:
> How do you access pixels directly?  The SDL_Surface struct has a
> "pixels" member, but it's defined as a void*, which really doesn't
> provide much useful data, and I can't seem to find any tutorials on
> how it works.  I'm trying to write an import routine for a graphics
> format that SDL_Image doesn't support.  It's 8-bit and palletized,
> and I've got the palette stuff all figured out, but I can't figure
> out how to set the pixels themselves.  Any help?

http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fSurface

The members of SDL_Surface w, h, pitch, and format give all you need for 
addressing pixels. But if you're using 8 bit, a pixel (x,y) is just 
pixels[x+y*pitch]. Don't forget to lock/unlock the surface for direct 
access. It's a void pointer because the element size depends on the 
color format used.

--Jacob


More information about the SDL mailing list