GLFW: window & contextGLFW 2.5

glfwWaitEvents

Like glfwPollEvents, but sleeps until at least one event arrives. Uses no CPU while nothing happens.

Signature

void glfwWaitEvents(void);

How it works

If nothing changes on screen unless the user does something (an editor, a viewer, a menu), redrawing 60 times per second wastes battery. glfwWaitEvents puts the thread to sleep until an event comes, handles it (callbacks run inside), and returns.

Two relatives: glfwWaitEventsTimeout(seconds) also wakes after a time limit, and glfwPostEmptyEvent() wakes a waiting main thread from another thread.

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.
GLFW_PLATFORM_ERROR
The operating system or window system reported a failure; the description says which.

Example

example.cpp
1// a viewer that redraws only when something happens
2while (!glfwWindowShouldClose(window)) {
3    render();
4    glfwSwapBuffers(window);
5    glfwWaitEvents();   // sleep until input
6}
Official GLFW documentation