SDL2: How to properly toggle fullscreen?

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

https://github.com/libsdl-org/SDL/issues/1428


Even now there still appears to be a problem with SDL_SetWindowFullscreen. I tried to add fullscreen functionality to my video player with this function. However, it would randomly crash when transitioning between fullscreen and windowed mode.

I found a temporary work around that appears to be working correctly for now.

SDL_DisplayMode dm;

if (SDL_GetDesktopDisplayMode(0, &dm))
{
    printf("Error getting desktop display mode\n");
    return -1;
}

if (SDL_PollEvent(&event))
{
    switch (event.type)
    {
        case SDL_KEYUP:
            switch (event.key.keysym.sym)
            {
                case SDLK_f:
                    SDL_RestoreWindow(screen); //Incase it's maximized...
                    SDL_SetWindowSize(screen, dm.w, dm.h + 10);
                    SDL_SetWindowPosition(screen, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
            }
            break;
    }
}

It's basically "fake" fullscreen. It resizes the window so the client area covers the whole screen and the minimize, maximize, exit buttons are off screen.

Hope this helps.