GLFW: window & contextGLFW 1.0

glfwSwapBuffers

Shows the frame you just drew: swaps the hidden back buffer with the visible front buffer.

Signature

void glfwSwapBuffers(GLFWwindow* window);

How it works

The window has two color buffers. You draw into the back buffer, which nobody sees, while the front buffer is on the screen. glfwSwapBuffers exchanges them, so the finished frame appears all at once, without the viewer watching it being drawn (flicker).

With a swap interval of 1 (glfwSwapInterval), the swap waits for the monitor's next refresh, which also limits the loop to the refresh rate (vsync).

Parameters

windowGLFWwindow*

The window whose buffers to swap.

Tips & pitfalls

  • Call it once per frame, after the last draw call and before glfwPollEvents.

  • After the swap the back buffer's content is undefined: clear it with glClear at the start of every frame.

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_WINDOW_CONTEXT
The window was created with GLFW_CLIENT_API set to GLFW_NO_API, so it has no context.
GLFW_PLATFORM_ERROR
The operating system or window system reported a failure; the description says which.

Example

example.cpp
1while (!glfwWindowShouldClose(window)) {
2    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
3    drawScene();
4    glfwSwapBuffers(window);   // show the frame
5    glfwPollEvents();          // then read input
6}
Official GLFW documentation