3
\$\begingroup\$

I've been looking to use SDL's own event messaging system to pass events around to different parts of my game, since it supports user events.

However, the documentation isn't very clear. Here is the code snippet it gives:

Uint32 myEventType = SDL_RegisterEvents(1);
if (myEventType != ((Uint32)-1)) {
    SDL_Event event;
    SDL_memset(&event, 0, sizeof(event)); /* or SDL_zero(event) */
    event.type = myEventType;
    event.user.code = my_event_code;
    event.user.data1 = significant_data;
    event.user.data2 = 0;
    SDL_PushEvent(&event);
}

My questions:

  • Does every type returned by SDL_RegisterEvents get read as SDL_UserEvent? If so, why even bother registering more than one event when you can use the event.user.code field to differentiate between types?

  • Do I need to call SDL_RegisterEvents each time I want to push my own event onto the event queue? Does this mean once the event I queue up is read, it trashes the type given to me by SDL_RegisterEvent and recycles it for another call?

The wiki is fairly vague, and there is next to nothing out there about using the SDL_UserEvent event type, so I am not entirely sure how to take advantage of SDLs built-in event handling system.

\$\endgroup\$
2
\$\begingroup\$

Does every type returned by SDL_RegisterEvents get read as SDL_UserEvent? If so, why even bother registering more than one event when you can use the event.user.code field to differentiate between types?

All the event types returned by SDL_RegisterEvents are considered "user events", yes.

The code field can be used for whatever you want. Two examples:

  • Register only one custom event type for your game, and use the code field to dispatch to code that handles the specific event. This way you consume less of the (technically) limited SDL user event ID pool (in practice this pool is in excess of 32k entries, however).

  • You can't really extend the user event structure, so you have a relatively limited number of places to shove specific data related to your event. You have the 32-bit code field and two pointers. The pointers mean you potentially have to manage the lifetime of the data they point to somehow, which can be a chore. So you can instead use the code field as an index into some polymorphic array containing additional data and whatnot for your event beyond what you can fit into the storage of SDL_UserEvent. Again, you could use the pointers for this, as well, so this is just SDL giving you some options for flexibility of implementation.

Do I need to call SDL_RegisterEvents each time I want to push my own event onto the event queue? Does this mean once the event I queue up is read, it trashes the type given to me by SDL_RegisterEvent and recycles it for another call?

You only need to call SDL_RegisterEvents once for any given event type. Once you do so, cache the returned ID (or ID range) somewhere and you can re-use it to create as many SDL_Events with that type as you want.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.