SDL_Event Union
The SDL_Event is an union that stores any events popped from the event queue, now I thought it would be a good thing to go back to recollect what unions were, I barely ever used unions so I forgot what they were and after recollecting - Unions are special datatypes that can allocate memory to only of their members, unlike structs they don't have memory for each member.
union Student {
int rollNo;
int age;
}
There the size of the struct will be 4 bytes (ignoring padding if any).
Now this struck that if SDL_Event is an union so it will only allocate memory for one of its members and I am accessing event.type which is a member of the SDL_Event union. The type field is of type Uint32 which is a 4 byte (32 bits) positive integer.
So if only field type is getting memory how can we still access fields like event.key (which is of type SDL_KeyboardEvent) where key is also a member of the SDL_Event union ?
To clear this confusion I went to Gemini and after a few convos here is what I understood (Keep in mind I can be wrong) :
Gemini told that the field type (Uint32) is a shared field. And on checking a few event struct objects that are part of the SDL_Event union do have the same type field.
For example, SDL_KeyboardEvent struct is defined as:
typedef struct SDL_KeyboardEvent
{
Uint32 type; /**< SDL_KEYDOWN or SDL_KEYUP */
Uint32 timestamp; /**< In milliseconds, populated using SDL_GetTicks() */
Uint32 windowID; /**< The window with keyboard focus, if any */
Uint8 state; /**< SDL_PRESSED or SDL_RELEASED */
Uint8 repeat; /**< Non-zero if this is a key repeat */
Uint8 padding2;
Uint8 padding3;
SDL_Keysym keysym; /**< The key that was pressed or released */
} SDL_KeyboardEvent;
This piece of definition is written in a SDL header file and as we see it indeed has the same type field.
Now apparently whenever a event occurs a struct of that specific type is stored in the event queue and when the SDL_PollEvent() function is called its popped from the queue and stored in the SDL_Event union and as we know unions can only have any one of its members, it now has a SDL_KeyboardEvent struct object namely - key !
This arose a question - If key is the one inside the union how can I still access event.type ? Because event.key is the one in the union. Now this is where I had the "lightbulb moment" (stole this word from Gemini LoL).
What happens is that unions share the same memory layout for any member (the union's size is that of the biggest member) similarly here, regardless of whatever is stored in the SDL_Event union they will have the same memory layout and since the type field is shared across every event struct and the union itself, its guaranteed that the first 4 bytes of the union will have the type field.
Now, if my union has a keyboard event struct or a mouse event struct the first 4 bytes is my type field so I can just do event.type because even if don't have memory allocated for type field directly, the struct that is stored in the union stores the type field in the same memory location therefore I can just try to access event.type because at the end of the day event.type has the same memory location as event.key.type !
This is brilliant engineering ~!
Click here for Source Code.