7

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?

Sethbeastalan
  • 71
  • 1
  • 3

2 Answers2

10

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

h4tch
  • 264
  • 1
  • 8
5

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.

yothsoggoth
  • 393
  • 3
  • 10