Currently, my Dear ImGui application (mainly the demo window with some custom OpenGL rendering) runs around 2000 fps if my window is small enough. How can I limit this to the monitor refresh rate (or even just 60fps)?
My current code looks like this:
static void glfw_error_callback(int error, const char * description)
{
fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}
int main(int, char **)
{
// Setup window
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit())
return 1;
const char * glsl_version = "#version 150";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
glfwWindowHint(GLFW_REFRESH_RATE, 60);
// Create window with graphics context
GLFWwindow * window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+OpenGL3 example", NULL, NULL);
if (window == NULL)
return 1;
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Enable vsync
bool err = gl3wInit() != 0;
if (err) {
fprintf(stderr, "Failed to initialize OpenGL loader!\n");
return 1;
}
// Setup Dear ImGui binding
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO & io = ImGui::GetIO();
(void)io;
// io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
// Other stylistic setup
...
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
// Create ImGui Windows
// Rendering
ImGui::Render();
int display_w, display_h;
glfwMakeContextCurrent(window);
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwMakeContextCurrent(window);
glfwSwapBuffers(window);
}
// Cleanup
return 0;
}
As you can see, it doesn't differ (nearly at all) from the original sample code for GLFW and OpenGL 3 given in the ImGui samples, apart from my unsuccessful attempt to limit the refresh rate using the glfwWindowHint(GLFW_REFRESH_RATE, 60)
, which I learned only affects the window in fullscreen mode. Also, I thought that the glfwSwapInterval(1)
might also limit the refresh rate to the monitor's refresh rate, but it seems to not be doing that either. Any help would be much appreciated.
Edit: GLFW error function and loading.