GLFW: window & contextGLFW 1.0

glfwSwapInterval

Turns vsync on or off: how many monitor refreshes glfwSwapBuffers waits for.

Signature

void glfwSwapInterval(int interval);

How it works

The monitor redraws itself at a fixed rate, such as 60 times per second. With interval 1 each swap waits for the next refresh: no tearing (half of one frame over half of another), and the loop runs at the monitor's rate instead of using 100% of the GPU.

With interval 0 the swap happens at once. Use it to measure how fast your renderer really is.

Parameters

intervalint

Refreshes to wait per swap.

Common values

0
No wait (vsync off): maximum frame rate, tearing possible.
1
Wait for one refresh (vsync on). The usual choice.
-1
Adaptive: like 1, but a late frame is shown at once. Only if the driver supports swap_control_tear.

Tips & pitfalls

  • It applies to the CURRENT context, so call it after glfwMakeContextCurrent.

  • The driver control panel can override it, so 0 does not always turn vsync off.

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_NO_CURRENT_CONTEXT
No OpenGL context is current on this thread: call glfwMakeContextCurrent first.
GLFW_PLATFORM_ERROR
The operating system or window system reported a failure; the description says which.

Example

example.cpp
1glfwMakeContextCurrent(window);
2glfwSwapInterval(1);   // vsync on
Official GLFW documentation