Think of SDL's event system as having an internal list, called the event queue. As things happen in your application window – the user moves the mouse, presses a key, clicks a button, tries to close the window – SDL detects these occurrences and adds corresponding SDL_Event
structures to the back of this queue.
Your main application loop typically uses functions like SDL_PollEvent()
to check this queue.
// Typical event loop in main()
SDL_Event E;
while (true) {
// Check if events are waiting in the queue
while (SDL_PollEvent(&E)) {
// SDL_PollEvent takes an event from the FRONT
// of the queue and puts it into 'E'.
// If the queue is empty, it returns 0.
// Handle the event 'E'
if (E.type == SDL_QUIT) { /* ... */ }
if (E.type == SDL_MOUSEBUTTONDOWN) { /* ... */ }
// ... pass E to UIManager.HandleEvent(E) ...
}
// ... Rendering ...
}
SDL_PollEvent()
looks at the front of the queue. If an event is waiting, it removes that event from the queue, copies its data into the SDL_Event
variable you provide (like E
), and returns 1 (true). If the queue is empty, it returns 0 (false) and doesn't modify E
.
SDL_PushEvent()
: Adding to the QueueThe SDL_PushEvent()
function allows your code to manually add an SDL_Event
structure to this same event queue.
int SDL_PushEvent(SDL_Event* event);
You create an SDL_Event
structure, fill it with the appropriate data for the event type you want to simulate or signal, and then pass a pointer to it to SDL_PushEvent()
.
// Button.cpp (Example from lesson)
#include "Button.h"
void Button::OnLeftClick() {
// Create an event structure on the stack
SDL_Event QuitEvent{};
// Set its type field
QuitEvent.type = SDL_QUIT;
// Push a *copy* of QuitEvent onto the queue
SDL_PushEvent(&QuitEvent);
}
When you call SDL_PushEvent()
, SDL takes a copy of the event data you provided and adds that copy to the back of the internal event queue.
The event pushed by SDL_PushEvent()
simply joins the end of the line in the main event queue. It sits there waiting its turn alongside events generated directly by user input or window system actions.
When your main loop eventually calls SDL_PollEvent()
(or SDL_WaitEvent()
), it will process events from the front of the queue in the order they were added. If your pushed event reaches the front of the queue, SDL_PollEvent()
will retrieve it just like any other event, and your event handling logic (e.g., the if (E.type == SDL_QUIT)
check in main
) will react to it.
SDL_PushEvent()
?Button
) can trigger a high-level application action (like quitting) without needing direct pointers or references all the way back up to main
. It just pushes the relevant event, and the main loop handles it naturally.Note: SDL also allows you to define and register your own custom event types, providing a more structured way to send application-specific messages via the event queue. We will cover custom events and more advanced event management techniques in detail in a later lesson.
Answers to questions are automatically generated and may not have been reviewed.
Learn to create interactive buttons in SDL2 and manage communication between different UI components.