That's a great design question! You absolutely can build your own event system entirely separate from SDL's. Choosing between SDL's user events and a custom system involves trade-offs between integration, simplicity, type safety, and flexibility.
SDL_UserEvent
SDL_PollEvent
or SDL_WaitEvent
. This simplifies the main application structure.SDL_PushEvent
is designed to be thread-safe. This provides a relatively easy way to send notifications from worker threads back to your main (event-processing) thread without manually implementing a thread-safe queue.SDL_UserEvent
Lack of Type Safety: The biggest drawback. data1
and data2
are void*
, requiring static_cast
and careful manual type management in the handler. Mistakes aren't caught at compile time. A custom system can use templates, std::variant
, std::any
, or specific event classes for strong type safety.
Limited Data Structure: You're constrained to the SDL_UserEvent
format (type
, timestamp
, windowID
, code
, data1
, data2
). A custom system can define event objects with any number and type of members needed.
Limited Features: SDL's queue is relatively simple. Custom systems can implement advanced features like:
SDL_SetEventFilter
).Dependency on SDL: Your core application logic communication becomes tied to the SDL event system. A custom system can be independent, potentially useful if you envision porting the core logic away from SDL later.
Potential Overhead: While usually negligible, creating and pushing SDL_Event
objects might have slightly more overhead than pushing a custom, potentially smaller event object onto a highly optimized custom queue, especially for very high-frequency events.
Choose SDL_UserEvent
if:
void*
is acceptable for your use case (or you implement careful wrappers).Consider a Custom Event System if:
code
, data1
, data2
.Designing robust, type-safe, and feature-rich event systems is a significant topic in itself, often explored in more detail in advanced C++ and game architecture discussions. We touch upon some of these design patterns and alternatives in later parts of the course.
In conclusion, SDL user events offer convenience and integration, while custom systems provide superior type safety, flexibility, and control at the cost of increased implementation effort.
Answers to questions are automatically generated and may not have been reviewed.
Learn how to create and manage your own game-specific events using SDL's event system.