关于opengl:将GLFW窗口设置为不可调整大小

Setting a GLFW window as not resizable

我有一个GLFW3窗口,试图将其从可调整大小更改为不可调整大小。

在创建窗口之后,我尝试更改"窗口提示",但这没有任何作用,因为提示仅影响要创建的窗口。

我试过的

1
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE)

这可能吗? 我想到的一种实现方法是具有onResize函数,该函数在设置为不可调整大小后将窗口大小更改回当前大小。 这似乎很hacky。


从Ubuntu 18.10中的GLFW 3.2.1-1开始,您的方法适用:

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <GLFW/glfw3.h>

int main(void) {
    GLFWwindow* window;
    if (!glfwInit())
        return -1;
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    window = glfwCreateWindow(640, 480, __FILE__, NULL, NULL);
    if (!window) {
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    while (!glfwWindowShouldClose(window)) {
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}

编译并运行:

1
2
g++ -std=c++11 -Wall -Wextra -pedantic-errors -o main.out main.cpp -lglfw
./main.out

当我将鼠标悬停在创建的窗口的边框上时,光标永远不会更改为调整大小模式。


GLFW当前没有创建窗口后更改该状态的API。

当您想使用GLFW时,我看到两个选择:

  • 您已经拥有的解决方法。
  • 切换该状态时,请创建一个新窗口。
  • 使用GLFW本机访问来获取真实的窗口句柄并为每个平台实现该功能(您在乎)。
  • 所有变种对我似乎都不太吸引。选项2特别糟糕,因为GL上下文与GLFW中的窗口相关联,应该通过使用额外的(不可见的)窗口和共享的GL上下文来做到这一点,但这将是丑陋的。

    选项3的优点是,一旦为所有相关平台实施后,它应该可以正常工作。由于GLFW是开源的,因此还启用了选项3b):直接在GLFW中实现此功能并扩展API。您甚至可以将其集成到官方GLFW版本中。


    我的解决方案:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    // before create:
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

    // create window
    // ...

    // after create
    void setResizable(arg) {
        if(arg)
            glfwSetWindowSizeLimits(window, 0, 0, 0xffff, 0xffff);
        else {
            int w, h;
            glfwGetWindowSize(window, &w, &h);
            glfwSetWindowSizeLimits(window, w, h, w, h);
        }
    }


    这可行,但我强烈建议其他解决方案,因为这仅在您严格需要能够切换它的情况下。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    IntBuffer wid = BufferUtils.createIntBuffer(1);
    IntBuffer hei = BufferUtils.createIntBuffer(1);

    glfwGetWindowSize(window, wid, hei);

    int windowWidth = wid.get();
    int windowHeight = hei.get(); // I recommend making this public

    while(!glfwWindowShouldClose(window)) {
        glfwSetWindowSize(window, windowWidth, windowHeight);
        // People can still maximize the window ... Comment if you have a solution :)
    }