Does SDL_PixelFormatEnum represent all supported pixel formats?

SDL_PixelFormatEnum vs SDL_PixelFormat

Sometimes I don’t know what pixel format I’ll get (e.g. when using SDL_GetWindowSurface or SDL_CreateTextureFromSurface) and sometimes I might want to use whatever format requires the least conversion. I might therefore want to store and compare pixel formats but I’m not sure whether I should use SDL_PixelFormat or SDL_PixelFormatEnum.

SDL_PixelFormat seems to have the advantage that it can represent all formats. The disadvantage is that it’s more complicated to create, copy, compare, etc.

SDL_PixelFormatEnum is simpler but it cannot represent indexed formats completely because it doesn’t contain any information about the palette and it doesn’t seem to support arbitrary RGB masks.

Not all possible SDL_PixelFormat are supported by SDL?

In SDL 1.2 you could use SDL_CreateRGBSurface to create surfaces with arbitrary formats (e.g. with GRAB byte order where the B channel uses 3 bits while the rest use 8 bits) and SDL_BlitSurface seems to handle it fine.

SDL 2 and 3, however, rejects such weird formats with error “Unknown pixel format”. Does this mean it will reject all formats that are not one of the listed SDL_PixelFormatEnum values? In that case it seems like there is little reason to use SDL_PixelFormat if I don’t care about indexed formats and it should be safe to assume SDL will never give me a texture/surface with a format that would map to SDL_PIXELFORMAT_UNKNOWN? Can someone confirm this?

1 Like