SDL2: How to properly toggle fullscreen?

emlai picture emlai · Jun 3, 2015 · Viewed 12.9k times · Source

I have problems deactivating fullscreen mode with my program. Entering fullscreen happens correctly, but trying to go back to windowed mode doesn't work, the only effect is that the cursor gets shown again.

Here's the MCVE/SSCCE that reproduces the issue for me:

void ToggleFullscreen(SDL_Window* Window) {
    Uint32 FullscreenFlag = SDL_WINDOW_FULLSCREEN;
    bool IsFullscreen = SDL_GetWindowFlags(Window) & FullscreenFlag;
    SDL_SetWindowFullscreen(Window, IsFullscreen ? 0 : FullscreenFlag);
    SDL_ShowCursor(IsFullscreen);
}

int main() {
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* Window = SDL_CreateWindow("",
        SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);

    bool Exit = false;
    for (SDL_Event Event; !Exit;) {
        SDL_WaitEvent(&Event);
        if (Event.type == SDL_KEYDOWN) {
            switch (Event.key.keysym.sym) {
                case SDLK_f: ToggleFullscreen(Window); break;
                case SDLK_q: Exit = true; break;
            }
        }
    }
    SDL_DestroyWindow(Window);
    SDL_Quit();
}

SDL_SetWindowFullscreen returns 0, as if the operation was successful. What am I doing wrong? (I'm using SDL 2.0.3 on OS X 10.10.3.)

Answer

Joe picture Joe · Jun 3, 2015

It looks like a known issue. Hopefully the SDL developers will fix it. I found the following bug report.

https://bugzilla.libsdl.org/show_bug.cgi?id=2479