Create surface from C/C++ data?


#1

Basically, I want to hard-code an error-handling sprite that is guaranteed to be present. What I’d like to do is to make a simple square surface and assign its pixels value to an array of whatever type SDL_Surface stores its pixels as. That way, even missing files can’t break the error sprite, and I can use it to indicate a draw error without having to print a ton of messages or stop the game entirely, sort of like how Source Engine does. How would I go about doing this?


#2

You can access the pixel data directly from any surface you create. SDL_Surface is pretty straightforward so you can create the surface and directly edit the pixel data yourself if you want to since SDL_Surface.pixeldata is read/writable. Just keep the remarks in mind when editing RLE optimized surfaces.

You can also create a surface with either: SDL_CreateRGBSurfaceWithFormatFrom or SDL_CreateRGBSurfaceFrom to directly load pixel data from an array. You’ll probably have to create and edit the surface normally and then save off the pixel data.


#3

https://blog.gibson.sh/2015/04/13/how-to-integrate-your-sdl2-window-icon-or-any-image-into-your-executable/ might be of interest :slight_smile:


#4

I am not sure this is a good idea, if there is files missing or something broke then I think you should fix it.
Unless your tracking which files are missing / did not load correctly.

This will create an empty surface which is 1 pixel in size.
SDL_Surface* surface = SDL_CreateRGBSurface(0, 1, 1, 32, 0, 0, 0, 0);

When you load your surface and it fails u can return the empty surface and it will render as black I believe.