Handling "Are you sure you want to quit"

I guess it’s a noob question, but what’s the proper way of handling such type of interaction if player presses “close window” button or Alt+F4?

The way I understand it - by the time I get to react to “(event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED)” SDL already posts a whole bunch of other messages to the queue and begins to destroy window. Am I supposed to just flush “window destroy(ed)” events and SDL_EVENT_QUIT? Because that sounds hacky and broken.
Or should I set event filter in this case?

If you want to prevent getting a SDL_EVENT_QUIT event when the user tries to close last window you can set the hint SDL_HINT_QUIT_ON_LAST_WINDOW_CLOSE to zero.

SDL_SetHint(SDL_HINT_QUIT_ON_LAST_WINDOW_CLOSE, "0");

On the window close button, Alt+F4 (Windows) or CMD+Q (macOS), you just get an SDL_EVENT_QUIT event and can decide what to do with that. For example, in my game I automatically pause the game and open the quit menu (inside my main menu) when receiving this event. SDL has not yet destroyed the window at this point.

3 Likes

Hm… I actually get SDL_EVENT_WINDOW_CLOSE_REQUESTED first and SDL_EVENT_QUIT comes next. But it seems like if I just ignore it and keep running the loop - nothing happens. The “animation” continues and the window responds.

Thanks!