ImGui 降低CPU占用率

ImGui命名空间下添加两个函数

 

static ULONGLONG g_last_draw_tick = 0;

IMGUI_API bool ImGui::Is_Redraw()
{
    ImGuiContext& g = *GImGui;
    bool result = g_last_draw_tick == 0 || !g.InputEventsQueue.empty();

    if (!result && ::GetTickCount64() - g_last_draw_tick > 100)
    {
        result = true;
    }

    if (result)
    {
        g_last_draw_tick = ::GetTickCount64();
    }

    return result;
}

IMGUI_API void ImGui::Set_Redraw()
{
    g_last_draw_tick = 0;
}

 

绘制代码修改如下.

 

do
{
    MSG msg = { 0 };
    while (::PeekMessageA(&msg, NULL, 0U, 0U, PM_REMOVE))
    {
        ::TranslateMessage(&msg);
        ::DispatchMessage(&msg);
    }
    if (msg.message == WM_QUIT) break;

    if (ImGui::Is_Redraw())
    {
        auto begin = ::GetTickCount64();
        draw_imgui(_draw_cb);
        console.log("绘制耗时:{}", ::GetTickCount64() - begin);
    }
    else {
        ::SleepEx(1, TRUE);
    }
} while (true);

 

发表评论

您的电子邮箱地址不会被公开。