Window not closing GLFW

烈酒焚心 提交于 2020-01-05 07:33:51

问题


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.

Code:

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();
}

Thanks!


回答1:


There are several things but the issue seems to be that you never set running = false.

Try making your while condition looking like this while(!glfwWindowShouldClose(window));

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);

Also consider making your int running a bool.

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.

For more information about openGL and to gain some basic understanding and working examples consider reading https://learnopengl.com/.



来源:https://stackoverflow.com/questions/43804024/window-not-closing-glfw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!