【问题标题】:glfw how to destroy window in mainloopglfw如何在mainloop中销毁窗口
【发布时间】:2020-05-10 22:56:31
【问题描述】:

我正在尝试使用 glfw 开发一个程序,其中窗口在主循环中关闭。但是,我收到了这个奇怪的运行时错误:

X Error of failed request:  GLXBadDrawable
  Major opcode of failed request:  152 (GLX)
  Minor opcode of failed request:  11 (X_GLXSwapBuffers)
  Serial number of failed request:  158
  Current serial number in output stream:  158

这是我要运行的代码

#include <GL/glfw3.h>

int main()
{
    GLFWwindow* window;

    if(!glfwInit())
        return -1;

    window = glfwCreateWindow(400, 400, "window", nullptr, nullptr);

    if(!window)
    {
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    while(!glfwWindowShouldClose(window))
    {
        glfwDestroyWindow(window);

        glfwSwapBuffers(window);

        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

有没有办法从主循环内部破坏窗口?

【问题讨论】:

  • 当之前的测试glfwWindowShouldClose 告诉你不要关闭它时,你为什么要打电话给glfwDestroyWindow
  • @Ripi2 好点,这是否意味着我必须手动使用while(true) 循环并在窗口关闭时中断?如果是这样,我如何创建一个事件侦听器以在窗口关闭时提醒我?
  • 只需将glfwDestroyWindow移动到glfwTerminate之前
  • 为什么要在主循环中销毁窗口?为什么要在创建窗口后立即销毁窗口?
  • @MaximV 为什么要在主循环中调用glfwDestroyWindow。我不明白你想要达到什么目的。这根本没有意义。你为什么不直接打破循环?拨打glfwDestroyWindow 后,您无法拨打glfwSwapBuffersglfwPollEvents。因此答案是否定的。你不能打电话给glfwDestroyWindow,然后glfwSwapBuffers

标签: c++ opengl window glfw


【解决方案1】:

是的,您可以在主循环中调用glfwDestroyWindow。以下是我的做法。

#include <GLFW/glfw3.h>

int main()
{
    GLFWwindow* window;
    
    if(!glfwInit())
        return -1;

    window = glfwCreateWindow(400, 400, "My Window", NULL, NULL);

    if(!window)
    {
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    while(!glfwWindowShouldClose(window))
    {
        if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
        {
            glfwDestroyWindow(window);
            break;
        }

        glfwSwapBuffers(window);

        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

按 Q 键销毁窗口。尝试运行此代码以查看它是否有效。我运行了这段代码,这个过程只是简单地返回了 0 而没有打印任何错误消息。我不明白为什么它不适合你。

【讨论】:

  • 所以我只需要确保在窗口被销毁时我不会重复吗?
  • 是的。这就是break 声明的目的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-22
  • 2011-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多