首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在GLFW窗口中限制每秒帧数?(使用亲爱的ImGui)

如何在GLFW窗口中限制每秒帧数?(使用亲爱的ImGui)
EN

Stack Overflow用户
提问于 2019-01-23 00:49:14
回答 1查看 3.1K关注 0票数 3

目前,如果我的窗口足够小,我亲爱的ImGui应用程序(主要是带有一些自定义OpenGL渲染的演示窗口)运行在2000fps左右。如何将其限制为显示器刷新率(甚至仅为60fps)?

我当前的代码如下所示:

代码语言:javascript
运行
复制
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错误函数和加载。

EN

回答 1

Stack Overflow用户

发布于 2020-10-30 03:54:22

设置glfwSwapInterval(0);将删除framecap。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54312897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档