Calculate frame-rate for the user, as a purely luxurious feature
..and to reduce sample code size a little.
diff --git a/examples/directx11_example/main.cpp b/examples/directx11_example/main.cpp
index c07aa9a..6224c02 100644
--- a/examples/directx11_example/main.cpp
+++ b/examples/directx11_example/main.cpp
@@ -568,17 +568,7 @@
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 752e257..9311d9a 100644
--- a/examples/directx9_example/main.cpp
+++ b/examples/directx9_example/main.cpp
@@ -334,17 +334,7 @@
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 a9a2716..cd42635 100644
--- a/examples/opengl3_example/main.cpp
+++ b/examples/opengl3_example/main.cpp
@@ -346,17 +346,7 @@
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 b69507e..554eeaf 100644
--- a/examples/opengl_example/main.cpp
+++ b/examples/opengl_example/main.cpp
@@ -255,17 +255,7 @@
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 e12d261..b628277 100644
--- a/imgui.cpp
+++ b/imgui.cpp
@@ -1022,6 +1022,11 @@
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 @@
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 2658be4..31e527f 100644
--- a/imgui.h
+++ b/imgui.h
@@ -589,6 +589,7 @@
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