c++ - 如何在 GLFW 窗口中限制我的每秒帧数?(使用亲爱的 ImGui)
问题描述
目前,如果我的窗口足够小,我亲爱的 ImGui 应用程序(主要是带有一些自定义 OpenGL 渲染的演示窗口)运行速度约为 2000 fps。如何将其限制为显示器刷新率(甚至仅 60fps)?
我当前的代码如下所示:
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;
}
如您所见,它与 ImGui 示例中给出的 GLFW 和 OpenGL 3 的原始示例代码没有(几乎完全不同),除了我尝试使用glfwWindowHint(GLFW_REFRESH_RATE, 60)
.全屏模式下的窗口。另外,我认为这glfwSwapInterval(1)
也可能将刷新率限制为显示器的刷新率,但它似乎也没有这样做。任何帮助将非常感激。
编辑:GLFW 错误函数和加载。
解决方案
设置 glfwSwapInterval(0); 移除框架帽。
推荐阅读
- android - 生根过程后未找到 Moto G6 设备
- android - Flutter.dev 和 macOS Catalina (Gatekeeper) 的问题
- vb.net - 为什么 cmd 只是打印命令并且不起作用?
- c++ - 如何在同一个类的函数中获取类成员的函数指针?
- vba - 有没有一种方法可以在不使用 SendObject 命令的情况下从 Access 2002 创建新的 Outlook 电子邮件?
- python - 从列表格式的嵌套字典中获取键和值到数据框
- swift - 如何在swift中正确调用函数中的另一个函数?
- java - 如何循环异常直到用户成功或错误
- linux - 一个进程在多长时间之前会自动死亡
- java - 在字符串数学中的数字中加一个逗号