Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDL2: How to properly toggle fullscreen?

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.)

like image 602
emlai Avatar asked Jun 03 '15 19:06

emlai


2 Answers

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

like image 143
Joe Avatar answered Nov 13 '22 02:11

Joe


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.

like image 36
Edward Severinsen Avatar answered Nov 13 '22 04:11

Edward Severinsen