SDL 2.0 Quit Event with Multiple Windows
Asked Answered
E

2

8

I'm using SDL 2.0, and decided to try out making multiple windows. Unfortunately, now I can't quit out of my program without going back to the IDE and force closing it.

The event handling is as simple as possible, I am only polling for the quit event, and it worked perfectly fine before I added the second window. Is the Quit Event ignored when using multiple windows? If so, how can I turn it back on?

Escribe answered 9/7, 2013 at 6:8 Comment(1)
can we see some code?Yarndyed
Y
10

The Quit Event is only sent when the last open window is trying to close, otherwise a window close event is sent.

Yearlong answered 3/8, 2013 at 18:27 Comment(0)
R
6

I also ran into this problem, and the documentation is a little sparse on the topic so I ended up here.

The summary of the problem is:

  • If you have only one Window, clicking the X button will trigger an SDL_QUIT event.
  • If you have two or more windows, clicking the X button will trigger an SDL_WINDOWEVENT event, with an internal type of SDL_WINDOWEVENT_CLOSE.

So if your typical code for single-window quit events might look something like this:

SDL_Event e;
while (SDL_PollEvent(&e))
{
    if (e.type == SDL_QUIT)
    {
        // ... Handle close ...
    }
}

The multi-window equivalent would be:

SDL_Event e;
while (SDL_PollEvent(&e))
{
    if (e.type == SDL_WINDOWEVENT
        && e.window.event == SDL_WINDOWEVENT_CLOSE)
    {
        // ... Handle window close for each window ...
        // Note, you can also check e.window.windowID to check which
        // of your windows the event came from.
        // e.g.:
        if (SDL_GetWindowID(myWindowA) == e.window.windowID)
        {
            // ... close window A ...
        }
    }
}

Note that on the last window, you will again receive SDL_QUIT because it's now the only active window - so best to structure your code in a way that correctly handles both depending on the circumstances.

See docs for more info.

Rayon answered 2/9, 2019 at 21:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.