From cb0a4db0481cccf7185f12bf1bfccfdacbdfcd0e Mon Sep 17 00:00:00 2001 From: ocornut Date: Wed, 11 Feb 2015 18:28:17 +0000 Subject: [PATCH] Calculate frame-rate for the user, as a purely luxurious feature ..and to reduce sample code size a little. --- examples/directx11_example/main.cpp | 12 +----------- examples/directx9_example/main.cpp | 12 +----------- examples/opengl3_example/main.cpp | 12 +----------- examples/opengl_example/main.cpp | 12 +----------- imgui.cpp | 11 +++++++++++ imgui.h | 1 + 6 files changed, 16 insertions(+), 44 deletions(-) diff --git a/examples/directx11_example/main.cpp b/examples/directx11_example/main.cpp index c07aa9ab..6224c020 100644 --- a/examples/directx11_example/main.cpp +++ b/examples/directx11_example/main.cpp @@ -568,17 +568,7 @@ int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, int) ImGui::ColorEdit3("clear color", (float*)&clear_col); if (ImGui::Button("Test Window")) show_test_window ^= 1; if (ImGui::Button("Another Window")) show_another_window ^= 1; - - // Calculate and show frame rate - static int ms_per_frame_idx = 0; - static float ms_per_frame[60] = { 0 }; - static float ms_per_frame_accum = 0.0f; - ms_per_frame_accum -= ms_per_frame[ms_per_frame_idx]; - ms_per_frame[ms_per_frame_idx] = ImGui::GetIO().DeltaTime * 1000.0f; - ms_per_frame_accum += ms_per_frame[ms_per_frame_idx]; - ms_per_frame_idx = (ms_per_frame_idx + 1) % 60; - const float ms_per_frame_avg = ms_per_frame_accum / 60; - ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", ms_per_frame_avg, 1000.0f / ms_per_frame_avg); + ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); } // 2. Show another simple window, this time using an explicit Begin/End pair diff --git a/examples/directx9_example/main.cpp b/examples/directx9_example/main.cpp index 752e2570..9311d9a3 100644 --- a/examples/directx9_example/main.cpp +++ b/examples/directx9_example/main.cpp @@ -334,17 +334,7 @@ int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, int) ImGui::ColorEdit3("clear color", (float*)&clear_col); if (ImGui::Button("Test Window")) show_test_window ^= 1; if (ImGui::Button("Another Window")) show_another_window ^= 1; - - // Calculate and show frame rate - static int ms_per_frame_idx = 0; - static float ms_per_frame[60] = { 0 }; - static float ms_per_frame_accum = 0.0f; - ms_per_frame_accum -= ms_per_frame[ms_per_frame_idx]; - ms_per_frame[ms_per_frame_idx] = ImGui::GetIO().DeltaTime * 1000.0f; - ms_per_frame_accum += ms_per_frame[ms_per_frame_idx]; - ms_per_frame_idx = (ms_per_frame_idx + 1) % 60; - const float ms_per_frame_avg = ms_per_frame_accum / 60; - ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", ms_per_frame_avg, 1000.0f / ms_per_frame_avg); + ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); } // 2. Show another simple window, this time using an explicit Begin/End pair diff --git a/examples/opengl3_example/main.cpp b/examples/opengl3_example/main.cpp index a9a2716e..cd42635e 100644 --- a/examples/opengl3_example/main.cpp +++ b/examples/opengl3_example/main.cpp @@ -346,17 +346,7 @@ int main(int argc, char** argv) ImGui::ColorEdit3("clear color", (float*)&clear_col); if (ImGui::Button("Test Window")) show_test_window ^= 1; if (ImGui::Button("Another Window")) show_another_window ^= 1; - - // Calculate and show frame rate - static int ms_per_frame_idx = 0; - static float ms_per_frame[60] = { 0 }; - static float ms_per_frame_accum = 0.0f; - ms_per_frame_accum -= ms_per_frame[ms_per_frame_idx]; - ms_per_frame[ms_per_frame_idx] = ImGui::GetIO().DeltaTime * 1000.0f; - ms_per_frame_accum += ms_per_frame[ms_per_frame_idx]; - ms_per_frame_idx = (ms_per_frame_idx + 1) % 60; - const float ms_per_frame_avg = ms_per_frame_accum / 60; - ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", ms_per_frame_avg, 1000.0f / ms_per_frame_avg); + ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); } // 2. Show another simple window, this time using an explicit Begin/End pair diff --git a/examples/opengl_example/main.cpp b/examples/opengl_example/main.cpp index b69507ec..554eeaf4 100644 --- a/examples/opengl_example/main.cpp +++ b/examples/opengl_example/main.cpp @@ -255,17 +255,7 @@ int main(int argc, char** argv) ImGui::ColorEdit3("clear color", (float*)&clear_col); if (ImGui::Button("Test Window")) show_test_window ^= 1; if (ImGui::Button("Another Window")) show_another_window ^= 1; - - // Calculate and show frame rate - static int ms_per_frame_idx = 0; - static float ms_per_frame[60] = { 0 }; - static float ms_per_frame_accum = 0.0f; - ms_per_frame_accum -= ms_per_frame[ms_per_frame_idx]; - ms_per_frame[ms_per_frame_idx] = ImGui::GetIO().DeltaTime * 1000.0f; - ms_per_frame_accum += ms_per_frame[ms_per_frame_idx]; - ms_per_frame_idx = (ms_per_frame_idx + 1) % 60; - const float ms_per_frame_avg = ms_per_frame_accum / 60; - ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", ms_per_frame_avg, 1000.0f / ms_per_frame_avg); + ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); } // 2. Show another simple window, this time using an explicit Begin/End pair diff --git a/imgui.cpp b/imgui.cpp index e12d2611..b628277c 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -1022,6 +1022,11 @@ struct ImGuiState int LogStartDepth; int LogAutoExpandMaxDepth; + // Misc + float FramerateSecPerFrame[120]; // calculate estimate of framerate for user + int FramerateSecPerFrameIdx; + float FramerateSecPerFrameAccum; + ImGuiState() { Initialized = false; @@ -1719,6 +1724,12 @@ void ImGui::NewFrame() for (size_t i = 0; i < IM_ARRAYSIZE(g.IO.KeysDown); i++) g.IO.KeysDownTime[i] = g.IO.KeysDown[i] ? (g.IO.KeysDownTime[i] < 0.0f ? 0.0f : g.IO.KeysDownTime[i] + g.IO.DeltaTime) : -1.0f; + // Calculate frame-rate for the user, as a purely luxurious feature + g.FramerateSecPerFrameAccum += g.IO.DeltaTime - g.FramerateSecPerFrame[g.FramerateSecPerFrameIdx]; + g.FramerateSecPerFrame[g.FramerateSecPerFrameIdx] = g.IO.DeltaTime; + g.FramerateSecPerFrameIdx = (g.FramerateSecPerFrameIdx + 1) % IM_ARRAYSIZE(g.FramerateSecPerFrame); + g.IO.Framerate = 1.0f / (g.FramerateSecPerFrameAccum / (float)IM_ARRAYSIZE(g.FramerateSecPerFrame)); + // Clear reference to active widget if the widget isn't alive anymore g.HoveredId = 0; if (!g.ActiveIdIsAlive && g.ActiveIdPreviousFrame == g.ActiveId && g.ActiveId != 0) diff --git a/imgui.h b/imgui.h index 2658be47..31e527fe 100644 --- a/imgui.h +++ b/imgui.h @@ -589,6 +589,7 @@ struct ImGuiIO bool WantCaptureMouse; // Mouse is hovering a window or widget is active (= ImGui will use your mouse input) bool WantCaptureKeyboard; // Widget is active (= ImGui will use your keyboard input) + float Framerate; // Framerate estimation, in frame per second. Rolling average estimation based on IO.DeltaTime over 120 frames //------------------------------------------------------------------ // [Internal] ImGui will maintain those fields for you