GLFW: window & contextGLFW 3.0

glfwWindowShouldClose

Asks whether the user wants the window closed. The condition of the main loop.

Signature

int glfwWindowShouldClose(GLFWwindow* window);

How it works

Clicking the window's close button (or Alt+F4) does NOT close it. GLFW only raises a flag, and your loop decides what to do. That lets you ask "save before quitting?" or finish the frame first.

The usual main loop runs while (!glfwWindowShouldClose(window)). You can raise the flag yourself with glfwSetWindowShouldClose, e.g. when Esc is pressed.

Parameters

windowGLFWwindow*

The window to ask about.

Return value

GLFW_TRUE if the close flag is set, otherwise GLFW_FALSE.

Tips & pitfalls

Errors

When the call is invalid it does nothing and sets an error, which glGetError returns:

GLFW_NOT_INITIALIZED
Called before glfwInit or after glfwTerminate.

Example

example.cpp
1while (!glfwWindowShouldClose(window)) {
2    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
3        glfwSetWindowShouldClose(window, GLFW_TRUE);
4    render();
5    glfwSwapBuffers(window);
6    glfwPollEvents();
7}
Official GLFW documentation