Why does SDL_Event not separate out the type member?

So the SDL_Event union looks something like this (using C++ syntax):

union SDL_Event {
    uint8_t type;
    SDL_CommonEvent common;
    SDL_DisplayEvent display;
    // ...
};

most C unions I’ve seen instead would have used this approach:

struct SDL_Event {
    uint8_t type;
    SDL_EventData data;
};

union SDL_EventData {
    SDL_CommonEvent common;
    SDL_DisplayEvent display;
    // ...
};

The second approach saves retyping the type parameter in every union type.

Why was this done?

One idea coming to mind is the matter of alignment. Having a uint8_t could lead to padding which would interface poorly with languages such as C# where you have to hardcode the member offsets for interfacing with C unions. Although that padding would persist in the union types as well so I don’t think it’s this.

Thanks
Folling

1 Like

This change would not be ideal for me as a user of SDL. For example, instead of event.key.keysym.sym I would have to type event.data.key.keysym.sym. Maybe this could be fixed by using an anonymous union which I believe is now a standard feature since C11.

3 Likes

Given the SDL devs’ insistence on sticking to C89 (!), anonymous unions aren’t likely to happen.

1 Like