窗口未关闭GLFW [英] Window not closing GLFW
问题描述
我正在用OpenGL和GLFW编写游戏引擎.但是,以某种方式无法关闭我的窗口.我尝试了很多事情,但是没有效果.我的代码有什么问题?
I am writing a game engine in OpenGL and GLFW. However, somehow my window can't be closed. I tried many things but it doesn't have effect. What's wrong with my code?
我在其中找不到错误的内容-一切对我来说似乎很好.
I can't find the wrong thing in it - everything seems fine to me.
代码:
int running;
GLFWwindow* window;
Window::~Window()
{
glfwTerminate();
}
Window::Window(int width, int height, const std::string& title)
: m_width(width),
m_height(height),
m_title(title){
glfwInit();
if (!glfwInit())
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
window = glfwCreateWindow(height, width, __FILE__, NULL, NULL);
if (!window) {
glfwTerminate();
}
running = true;
}
void Window::MainLoop()
{
do
{
glfwMakeContextCurrent(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
glfwPollEvents();
Draw();
glfwSwapBuffers(window);
}
while(running);
}
void Window::Draw()
{
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
}
谢谢!
推荐答案
有几件事,但问题似乎是您从未设置running = false
.
There are several things but the issue seems to be that you never set running = false
.
尝试使您的while状况看起来像这样while(!glfwWindowShouldClose(window));
Try making your while condition looking like this while(!glfwWindowShouldClose(window));
此外,如果您希望能够通过按Escape键来关闭窗口,则应该可以:while(!glfwWindowShouldClose(window) && glfwGetKey(window_, GLFW_KEY_ESCAPE) != GLFW_PRESS);
Also if you would like to be able to close the window by pressing escape this should work: while(!glfwWindowShouldClose(window) && glfwGetKey(window_, GLFW_KEY_ESCAPE) != GLFW_PRESS);
还可以考虑将您的int running
设置为bool
.
Also consider making your int running
a bool
.
如果您不想更改它们,例如glfwMakeContextCurrent(window);
和glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
之类的内容就不必放在循环中.
And things such as glfwMakeContextCurrent(window);
and glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
do not need to be inside your loop if you do not intend to change them.
有关openGL的更多信息并获得一些基本的理解和工作示例,请考虑阅读 https://learnopengl.com/.
For more information about openGL and to gain some basic understanding and working examples consider reading https://learnopengl.com/.
这篇关于窗口未关闭GLFW的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!