Acces pixels efficiently

Hello,

I have got a quite noobish question:
How do I acces pixels of a surface efficientl? I tried to use SDL_FillRect, it did not work,
but anyway I do not think that this the solution.
Can I acces the pixels via surface->pixels?
And which format do I have to use?
At last something about my project:
I am about to code an engine, just for my personal confidence, so I have to acces the pixels very often and very fast.

Thanking you in anticipation

Faerbit

the solution. Can I acces the pixels via surface->pixels?

Yes. However, you have to lock the surface first, to guarantee that
while you are accessing the memory block pointed by surface->pixels
nothing else modifies it and also that it is actually in memory
and at a fixed location. Then don’t forget to unlock it, of course…

And which format do I have to use?

That depends on the format of the surface. You have several options:

  • You get the format information from the surface and write code to
    handle all possible formats. There are, unfortunately, a lot of
    possible formats. So it’s possible, but tedious and chances are, your
    code will be inferior to the optimised format conversion code already
    in SDL.

  • If you know that most of your time is spent manipulating the surface,
    the simplest solution might be to create a software surface in the
    format that best suits the needs of your software and blit it to the
    surface representing your screen when you’re done. In that case you
    can control the format of the software surface regardless of the HW
    limitations enforced on the surface that’s your screen. Blitting is
    fast and the format conversion is done by rather efficient routines
    in SDL. You can extend that paradigm with intermediate HW and/or SW
    surfaces if you use compex composition for the final image.

It’s all a question of what you want to do and what are the limitations
of the environment in which SDL operates.

The above works with 1.2, I’ve no idea if the surface structure is the
same in 1.3 or not.

Zoltan

At first I want to thank you, you helped me a lot…
But I still have a question:
In which format does SDL store the pixel from a software surface?
Are they stored in this UintXX format, and how do I get which pixel I am writing to?
I am just confused, because in the documentary only a pointer on pixel is specified, but not a pixel.

So far Faerbit

The format is described in the SDL_PixelFormat structure. For
efficiency, you usually want a fixed structure so you will have to
convert your surfaces when you load them.

The Video examples on the SDL doc wiki provide examples of how to use
the pixel format: http://www.libsdl.org/cgi/docwiki.cgi/Pixel_Access

It depends heavily on what you want to achieve. For some effects,
using OpenGL and shaders will provide orders of magnitude better
performance than bit twiddling on individual pixels in system memory.
For graphics, batching is the key to efficiency.

– Brian.On 27 March 2010 19:01, Faerbit wrote:

At first I want to thank you, you helped me a lot…
But I still have a question:
In which format does SDL store the pixel from a software surface?
Are they stored in this UintXX format, and how do I get which pixel I am
writing to?
I am just confused, because in the documentary only a pointer on pixel is
specified, but not a pixel.

So far Faerbit