C++ glfw如何在主循环中销毁窗口
我正在尝试使用glfw开发一个程序,其中窗口在主循环中关闭。但是,我遇到了一个奇怪的运行时错误:C++ glfw如何在主循环中销毁窗口,c++,opengl,window,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
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;
}
#包括
int main()
{
GLFWwindow*窗口;
如果(!glfwInit())
返回-1;
window=glfwCreateWindow(400400,“window”,nullptr,nullptr);
如果(!窗口)
{
glfwTerminate();
返回-1;
}
glfwMakeContextCurrent(窗口);
而(!glfwWindowShouldClose(窗口))
{
GLFW窗口(窗口);
glfwSwapBuffers(窗口);
glfwPollEvents();
}
glfwTerminate();
返回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;
}
#包括
int main()
{
GLFWwindow*窗口;
如果(!glfwInit())
返回-1;
window=glfwCreateWindow(400400,“我的窗口”,NULL,NULL);
如果(!窗口)
{
glfwTerminate();
返回-1;
}
glfwMakeContextCurrent(窗口);
而(!glfwWindowShouldClose(窗口))
{
如果(glfwGetKey(窗口,GLFW_键=GLFW_按)
{
GLFW窗口(窗口);
打破
}
glfwSwapBuffers(窗口);
glfwPollEvents();
}
glfwTerminate();
返回0;
}
按Q键破坏窗口。试着运行这段代码,看看它是否能工作。我运行了这段代码,进程只返回了0,没有打印任何错误消息。我不明白为什么它不适合你。当测试
glfwWindowShouldClose
之前告诉你不要关闭它时,你为什么调用glfwDestroyWindow
?@Ripi2很好,这是否意味着我必须手动使用while(true)
循环,并在窗口关闭时中断?如果是这样,我如何创建事件侦听器以在窗口关闭时向我发出警报?只需在glfwderminate
之前移动glfwderminate
为什么要在主循环中销毁窗口?为什么要在创建窗口后立即销毁该窗口?@MaximV为什么要在主循环中调用glfwderystowwindow
。我不明白你想要实现什么。这毫无意义。你为什么不打破这个循环呢?调用glfwSwapBuffers
或glfwwPollEvents
后,不能调用glfwSwapBuffers
。因此答案是否定的。你不能调用glfwSwapBuffers
然后再调用glfwSwapBuffers
,所以我只需要确保在销毁窗口时不会重复?是的。这就是break
语句的目的。