Browse Source

Calculate frame-rate for the user, as a purely luxurious feature

..and to reduce sample code size a little.
ocornut 10 years ago
parent
commit
cb0a4db048

+ 1 - 11
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

+ 1 - 11
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

+ 1 - 11
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

+ 1 - 11
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

+ 11 - 0
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)

+ 1 - 0
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