Search code examples
c++sdlsdl-1.2

Is SDL_LockSurface needed to access the pixel format?


SDL_LockSurface's documentation suggests, not very clearly, that pixel format of some surfaces may change during their lifetime, and so the proper way to access a surface's pixel format would be to first check if the surface needs locking with SDL_MUSTLOCK, and if so, lock it using SDL_LockSurface.

However, a lot of online code samples access the format without performing either check.

Is locking the surface really necessary to access the pixel format? Can the format change during surface lifetime so as to require locking?

I'm using SDL 1.2 in case there's any difference between versions.


Solution

  • It is unlikely that the pixel format will be changed and if it does, then it wouldn't happen in the middle of your accessing it because the format resides in client memory (touched only by you and SDL). If you still feel concerned about the possibility, then always create surfaces with the SDL_SWSURFACE flag or lock the surface before accessing the format.