SDL2 切换假全屏SDL_WINDOW_RESIZABLE状态

SDL2 toggle SDL_WINDOW_RESIZABLE state for fake fullscreen

本文关键字:WINDOW RESIZABLE 状态 SDL SDL2      更新时间:2023-10-16

要在 SDL2 中执行没有模式集的"假"全屏窗口,您可以使用这样的东西创建一个无边框的最大化窗口。

int idx = SDL_GetWindowDisplayIndex(g_displayWindow);
SDL_Rect bounds;
SDL_GetDisplayBounds(idx, &bounds);
//SDL_SetWindowResizable(g_displayWindow, SDL_FALSE);
SDL_SetWindowBordered(g_displayWindow, SDL_FALSE);
SDL_SetWindowPosition(g_displayWindow, bounds.x, bounds.y);
SDL_SetWindowSize(g_displayWindow, bounds.w, bounds.h);

对于不可调整大小的窗口,这非常有效。在使用SDL_WINDOW_RESIZABLE创建的窗口中,屏幕的底部和右侧边缘(在窗口上)有一个恼人的灰色边框。不幸的是,没有SDL_SetWindowResizable函数(从 SDL 2.0.4 开始)。我们如何在不重新创建窗口的情况下摆脱调整大小的边框?

SDL_WINDOW_FULLSCREEN_DESKTOP和SDL_WINDOW_FULLSCREEN都做了一个我想避免的模式集 - 它需要更长的时间,更难alt-tab出来,如果游戏在调试器中遇到断点,它可以锁定整个系统。

这就是我想出的 - 经过测试并在 Windows 上运行。

void SDL_SetWindowResizable(SDL_Window *win, SDL_bool resizable)
{
    SDL_SysWMinfo info;
    SDL_VERSION(&info.version);
    SDL_GetWindowWMInfo(g_displayWindow, &info);
#if WIN32
    HWND hwnd = info.info.win.window;
    DWORD style = GetWindowLong(hwnd, GWL_STYLE);
    if (resizable)
        style |= WS_THICKFRAME;
    else
        style &= ~WS_THICKFRAME;
    SetWindowLong(hwnd, GWL_STYLE, style);
#endif
}

SDL_SetWindowResizable() 函数是在 2016 年 10 月发布的 SDL 2.0.5 中添加的。宣布推出 SDL 2.0.5

假设你的SDL_Window指针(你从SDL_CreateWindow()得到的)被命名为window,使用:

/* To disable resizing: */
SDL_SetWindowResizable(window, SDL_FALSE);
/* To enable resizing: */
SDL_SetWindowResizable(window, SDL_TRUE);