Create a surface with same properties as window's surface

Hi. I’d like to know how you can create a SDL_Surface with SDL_CreateRGBSurfaceWithFormat that is exactly the same as the one used by the window.
Any help appreciated.

Maybe something like this?

Uint32 flags = SDL_GetWindowFlags(window);

int width;
int height;
SDL_GetWindowSize(window, &width, &height);

SDL_PixelFormat* fmt = SDL_AllocFormat(SDL_GetWindowPixelFormat(window)); // TODO: check for NULL

SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormat(flags, width, height, fmt->BitsPerPixel, fmt->format);

SDL_FreeFormat(fmt);

It compiles but I haven’t actually tested if it works correctly.


It’s tempting to call SDL_GetWindowSurface and simply get the format directly from that surface …

SDL_Surface* window_surface = SDL_GetWindowSurface(window); // TODO: check for NULL
SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormat(window_surface->flags, window_surface->w, window_surface->w, window_surface->format->BitsPerPixel, window_surface->format->format);

… but the docs says you should “not combine this with 3D or the rendering API” so that might not be an option.

Ok i’ll try this! Thanks!!