I noticed that SDL3/SDL_CreateSurface() has a “SDL_PixelFormat format” argument, but SDL_GetWindowSurface() lacks this argument, so when it needs to create a new surface for the window, I have no way to set the pixel format of the newly created surface. Is there any ways I’m missing?
Thank you slouken, now I understand. I have an array of rgb color values and would like to make it look the same between different platforms, say, Linux and Windows on x64. I found the default pixel format of window surface on my Linux is XBGR8888, but on Windows it is XRGB8888. This results in different showing on these platforms. What can I do to make them showing the same colors?
Query the window surface’s pixel format and make your array of RGB values match its byte order. Then blitting it to the window surface will be very fast.
Put your array in an SDL_Surface and use SDL’s surface blitting functions, which will handle the format conversion for you, at the expense of some performance since you’re redoing the conversion every frame.
Put your array in an SDL_Surface and use SDL’s pixel format conversion routines to convert it to a new surface that matches the window surface’s format. Then use SDL’s surface blitting functions to draw it.
Use the hardware-accelerated 2D renderer. The GPU can do the pixel format conversion essentially for free.