Are pixels mutated in SDL_CreateSurfaceFrom?

I updated some code and noticed SDL_CreateSurfaceFrom takes in void*pixels. I have a const unsigned int* that I tried to pass in. I used a cast to fix this. I assuming SDL_CreateSurfaceFrom wont actually mutate any pixels?

This reminds me, in SDL 1 (20+yrs ago) I used SDL_BlitSurface, the signature is (SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect). I only had a few years of experience back then. I couldn’t tell why my sprites were being truncated and I didn’t immediately noticed it was when I touched the edge. Once I reproduced it, it still took me a while to figure out what was happening. I had already read the docs but I guess I forgot dstrect is mutated when clipped

All in all it took me about 45mins to figure out that stupid bug. It traumatized and pissed me off so much that when I wrote my language and compiler, I had mut be required at the call site.

The docs for SDL_CreateSurfaceFrom says:

No copy is made of the pixel data. Pixel data is not managed automatically; you must free the surface before you free the pixel data.

So while SDL_CreateSurfaceFrom does not mutate the pixels the surface still stores the pixels as non-const to allow you to mutate them (either manually or by using an SDL function such as SDL_FillSurfaceRect) like with any other surface.

1 Like