1

Can someone tell me what the format field of the SDL_PixelFormat is for? What does it contain? The documentation of the SDL2 library does not give enough detail.

Here are the bytes of two instances of such an object. The format field covers the first 4 bytes.

04 18 16 16 00 00 00 00 
00 00 00 00 00 00 00 00 
20 04 00 00 00 00 FF 00 
00 FF 00 00 FF 00 00 00 
00 00 00 00 00 00 00 08 
10 08 00 00 01 00 00 00 
50 47 D6 B4 88 59 00 00 
04 20 36 16 00 00 00 00 
00 00 00 00 00 00 00 00 
20 04 00 00 00 00 FF 00 
00 FF 00 00 FF 00 00 00 
00 00 00 FF 00 00 00 00 
10 08 00 18 01 00 00 00 
00 00 00 00 00 00 00 00 

The first string of bytes comes from a window surface retrieved with SDL_GetWindowSurface, and the other one comes from an image surface created with SDL_LoadBMP.

3
  • 2
    Wild guess: Perhaps it's constructed with SDL_DEFINE_PIXELFORMAT macro from SDL_pixels.h. Commented Dec 19, 2024 at 14:59
  • 2
    I think I was correct: Latter example value 0x16362004 seems to match SDL_PIXELFORMAT_ARGB8888, which would make sense for 32-bit color mode (with alpha channel) created from BMP file . Commented Dec 19, 2024 at 15:16
  • @user694733 Alright then since these constants are part of an enum, I suppose I can think of the format field as a unique identifier. Commented Dec 19, 2024 at 16:32

1 Answer 1

3

The format field stores the pixel format type, see: SDL_PixelFormatEnum.

For example, the SDL_AllocFormat function creates a SDL_PixelFormat corresponding to a pixel format and stores the given parameter in the specific field. Besides that, sets also all the other fields to the corresponding values.

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer helped me find the relevant code github.com/libsdl-org/SDL/blob/SDL2/src/video/SDL_pixels.c#L509. Indeed, the parameter is copied to the format field in the SDL_InitFormat function. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.