SDL2全屏切换:窗口位置重置

时间:2017-10-03 13:58:38

标签: c++ windows sdl-2

我试图在SDL2中切换全屏,到目前为止它的工作原理。但是当模式返回到窗口模式时,位置被重置为0,0。我尝试在窗口进入全屏之前存储窗口的最后位置但是在窗口返回窗口后,位置仍然被重置为0。 / p>

我输出最后一个位置似乎是正确的,但在返回窗口时仍然会重置为0,0。我需要听一个事件或什么吗?

void ToggleFullscreen(SDL_Window* window) {
    Uint32 flag = SDL_WINDOW_FULLSCREEN;
    bool isFullscreen = SDL_GetWindowFlags(window) & flag;
    if(!isFullscreen){
        SDL_GetWindowPosition(window, &lastWindowX, &lastWindowY);
    }

    SDL_SetWindowFullscreen(window, isFullscreen ? 0 : flag);

    if(isFullscreen){
        cout << "set window to: " << lastWindowX << " " << lastWindowY << endl;
        SDL_SetWindowPosition(window, lastWindowX, lastWindowY);
    }

}

1 个答案:

答案 0 :(得分:0)

查看我的一个项目,它具有设置窗口,全屏或全屏桌面的功能,我发现这两行:

// Fixes problem when Game::changeResolution is called in fullscreen or fullscreen desktop and then return to windowed.
SDL_SetWindowSize(m_pWindow, m_width, m_height);
SDL_SetWindowPosition(m_pWindow, SDL_WINDOWPOS_CENTERED, 
SDL_WINDOWPOS_CENTERED);

首先完成SDL_SetWindowSize,一切正常。然后我评论了SDL_SetWindowSize行并再次运行。从SDL_WINDOW_FULLSCREEN_DESKTOP返回时,会将其位置重置为(0,0)

虽然当从SDL_WINDOW_FULLSCREEN返回窗口时,它被放置好了。尝试添加该行,看看它是否为您解决了问题。另请尝试在通话之间添加SDL_Delay(2000);