Search code examples
c++sdlsdl-2

SDL2 - Window cannot be reopened


I am writing a simple SDL2 application with 2 windows.

The first window (window variable) is shown when the application starts, the second one (window2 variable) is hidden.

Expected behavior:

I click on the first window, the second window pops up, then I close the second window.

And I can close and reopen the window as much as I want.

Observed behavior:

Once I close the second window, if I reclick in the first window, the second window doesn't appear as expected.

As stated in my comment: the window doesn't appear in my window manager (i.e. Wayland).


The code:

#include <SDL2/SDL.h>

int main()
{
    SDL_Window* window, *window2 = NULL;

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        return 1;
    } else {
        window = SDL_CreateWindow("ONE", SDL_WINDOWPOS_UNDEFINED,
                     SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
        window2 = SDL_CreateWindow("TWO", SDL_WINDOWPOS_UNDEFINED, 
                      SDL_WINDOWPOS_UNDEFINED, 320, 240, SDL_WINDOW_HIDDEN);

        if (window == NULL || window2 == NULL) {
            SDL_DestroyWindow(window);
            SDL_DestroyWindow(window2);
            return 1;
        }

        bool running = true;

        while(running) {
            SDL_Event event;
            while (SDL_PollEvent(&event)) {
                if (event.type == SDL_WINDOWEVENT) {
                    if (event.window.event == SDL_WINDOWEVENT_CLOSE) {
                        if (SDL_GetWindowID(window) == event.window.windowID) {
                            running = false;
                        } else {
                            SDL_HideWindow(window2);
                        }
                    }
                } else if (event.type == SDL_MOUSEBUTTONDOWN) {
                    SDL_ShowWindow(window2);
                }
            }
        }
    }

    SDL_DestroyWindow(window);
    SDL_DestroyWindow(window2);

    SDL_Quit();

    return 0;
}

Solution

  • This is an SDL bug that may or may not have been fixed by this patch.