SDL_PollEvent(3)
β¨ SDL_PollEvent β¨
ok bestie, welcome to SDL_PollEvent, one of the SDL3 FUNCTIONS π this one's literally the main character of your game loop, no cap.
πΈ NAME πΈ
SDL_PollEvent β poll for currently pending events. that's it, that's the vibe. π
π¦ SYNOPSIS π¦
#include <SDL3/SDL_events.h>
bool SDL_PollEvent(SDL_Event *event);
β¨ DESCRIPTION β¨
okay so. if event is not NULL, the next event gets removed from the queue and stored in the SDL_Event structure that event points to. gone. popped. slayed. π
if event is NULL, it just returns true if there's an event chillin' in the queue, but it will not remove it. like, it'll peek but it won't touch. respectful king π«Ά
β οΈ important caveat bestie: since this function may implicitly call SDL_PumpEvents(), you can only call this function in the thread that initialized the video subsystem. do not try it anywhere else, it's giving chaos π
SDL_PollEvent() is the favored way of receiving system events, because you can do it right from your main loop and it does not suspend the main loop while waiting on an event to be posted. no blocking, no waiting around, we move. π₯
the common practice is to fully process the event queue once every frame, usually as a first step before updating the game's state. iconic behavior, truly:
while (game_is_still_running) {
SDL_Event event;
while (SDL_PollEvent(&event)) { // poll until all events are handled!
// decide what to do with this event.
}
// update game state, draw the current frame
}
ngl there's a little quirk you should know about π Windows (and possibly other platforms) handles events weird while you're dragging/resizing a window, which can cause this function to block for significant amounts of time. not the vibe. the technical explanations and solutions are discussed on the wiki:
https://wiki.libsdl.org/SDL3/AppFreezeDuringDrag
go read it, it's worth it fr fr π
π FUNCTION PARAMETERS π
eventβ theSDL_Eventstructure to be filled with the next event from the queue, orNULL.
π RETURN VALUE π
returns true if this got an event, or false if there are none available. simple as that, bestie β¨
π§ THREAD SAFETY π§
this function should only be called on the main thread. main character energy only, we don't do side threads here π
β AVAILABILITY β
this function is available since SDL 3.2.0. so if you're on something older... bestie. upgrade. π
π SEE ALSO π
and that's the whole page!! π this was brought to you by Simple Directmedia Layer, version SDL 3.4.18 π go poll some events, bestie, you got this π«Άβ¨πΈ
π¦ where this page comes from
- fedora 43: SDL3-devel 3.4.18-1.fc43 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 43: SDL3-devel 3.4.18-1.fc43 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 44: SDL3-devel 3.4.18-1.fc44 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 44: SDL3-devel 3.4.18-1.fc44 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms