2

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!

user6632515
  • 193
  • 1
  • 1
  • 10

1 Answers1

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/.

Aldarrion
  • 366
  • 3
  • 12
  • Great, thanks! It may be offtopic, but do you also know why I am getting an '" No matching function call to glfwCreateWindow" when replacing the __FILE__ with title? – user6632515 May 05 '17 at 12:11
  • Try `title.c_str()`. The function expects C type string = array of char (const char*). – Aldarrion May 05 '17 at 12:24