c++ - glfw如何破坏主循环中的窗口

标签 c++ opengl window glfw

我正在尝试使用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;
}

有什么方法可以从主循环内部破坏窗口吗?

最佳答案

是的,您可以在主循环中调用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而不打印任何错误消息。我不明白为什么它不适合您。

关于c++ - glfw如何破坏主循环中的窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61719790/

相关文章:

c++ - 如何在 std::multimap 中返回严格小于给定键的最大键?

c++ - 列出 Windows 中已安装的程序

c++ - 为什么我的三角形对象没有被绘制?

c++ - 如何使用 std::ref?

c++ - OpenGL(核心配置文件)纹理缺失或黑色

opengl - 为什么 OpenGL 给对象提供句柄而不是指针?

c++ - LNK2028 和 LNK2019 错误

javascript - 使用 jquery html() 将脚本插入到新窗口

c# - 在 WPF C# 中发送值时无法从一个页面转到新页面

c++ - MFC 中的窗口角坐标