From cbb251fed185da7ad227c2b5ecfe89b4b47d45d8 Mon Sep 17 00:00:00 2001 From: Laura Hermanns Date: Sun, 23 Jun 2024 19:05:15 -0400 Subject: [PATCH] [Examples] Fixed time record output and improved Fonts example FPS output. - Added missing '%' character to time record output (when F1 is pressed in examples). - Show average FPS in Fonts example after every 500 milliseconds. --- examples/Cpp/ExampleBase/ExampleBase.cpp | 8 +++-- examples/Cpp/Fonts/Example.cpp | 40 +++++++++++++++++++----- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/examples/Cpp/ExampleBase/ExampleBase.cpp b/examples/Cpp/ExampleBase/ExampleBase.cpp index 44cc4dc0aa..76c8f77692 100644 --- a/examples/Cpp/ExampleBase/ExampleBase.cpp +++ b/examples/Cpp/ExampleBase/ExampleBase.cpp @@ -26,6 +26,8 @@ #endif #include +#define IMMEDIATE_SUBMIT_CMDBUFFER 0 + /* * Global helper functions @@ -384,7 +386,7 @@ void ExampleBase::Run() "-------------------\n" ); for (const LLGL::ProfileTimeRecord& rec : frameProfile.timeRecords) - LLGL::Log::Printf("%s: " PRIu64 " ns\n", rec.annotation, rec.elapsedTime); + LLGL::Log::Printf("%s: %" PRIu64 " ns\n", rec.annotation, rec.elapsedTime); debuggerObj_->SetTimeRecording(false); showTimeRecords = false; @@ -502,7 +504,9 @@ ExampleBase::ExampleBase(const LLGL::UTF8String& title) LLGL::CommandBufferDescriptor cmdBufferDesc; { cmdBufferDesc.debugName = "Commands"; - //cmdBufferDesc.flags = LLGL::CommandBufferFlags::ImmediateSubmit; + #if IMMEDIATE_SUBMIT_CMDBUFFER + cmdBufferDesc.flags = LLGL::CommandBufferFlags::ImmediateSubmit; + #endif } commands = renderer->CreateCommandBuffer(cmdBufferDesc); diff --git a/examples/Cpp/Fonts/Example.cpp b/examples/Cpp/Fonts/Example.cpp index 66d74ea780..b46fe91c95 100644 --- a/examples/Cpp/Fonts/Example.cpp +++ b/examples/Cpp/Fonts/Example.cpp @@ -7,6 +7,7 @@ #include #include +#include #define FONT_COURIER_NEW_16 ( 0 ) #define FONT_LUCIDA_CONSOLE_32 ( 1 ) @@ -63,11 +64,19 @@ class Example_Fonts : public ExampleBase // Some numbers to display on screen. struct DisplayNumbers { - int counter = 0; - int fps = 0; + int frameCounter = 0; + int averageFPS = 0; } displayNumbers; + struct AverageFPS + { + int samples = 0; + double sum = 0.0; + std::chrono::system_clock::time_point lastTimePoint = std::chrono::system_clock::now(); + } + avgFPS; + // Pre-defined fonts struct FontMetaData { @@ -375,9 +384,26 @@ class Example_Fonts : public ExampleBase config.shadow = !config.shadow; // Update frame counters - displayNumbers.counter++; - if (displayNumbers.counter % 10 == 0) - displayNumbers.fps = static_cast(1.0 / timer.GetDeltaTime()); + displayNumbers.frameCounter++; + + // Update average FPS every 500 milliseconds + const double fps = 1.0 / timer.GetDeltaTime(); + + if (!std::isinf(fps)) + { + avgFPS.samples++; + avgFPS.sum += fps; + } + + auto currentTimePoint = std::chrono::system_clock::now(); + auto timeSinceLastAvgFPSUpdate = std::chrono::duration_cast(currentTimePoint - avgFPS.lastTimePoint).count(); + if (timeSinceLastAvgFPSUpdate > 500 && avgFPS.samples > 0) + { + displayNumbers.averageFPS = static_cast(avgFPS.sum / static_cast(avgFPS.samples) + 0.5); + avgFPS.samples = 0; + avgFPS.sum = 0.0; + avgFPS.lastTimePoint = currentTimePoint; + } // Reset batch counter numBatches = 0; @@ -419,7 +445,7 @@ class Example_Fonts : public ExampleBase // Draw frame counter DrawFont( - "Frame counter: " + std::to_string(displayNumbers.counter), + "Frame counter: " + std::to_string(displayNumbers.frameCounter), screenWidth - paragraphMargin, paragraphPosY, colorYellow, fontFlags | DrawRightAligned ); @@ -434,7 +460,7 @@ class Example_Fonts : public ExampleBase // Draw number of frames per second (FPS) DrawFont( - "FPS = " + std::to_string(displayNumbers.fps), + "FPS = " + std::to_string(displayNumbers.averageFPS), screenWidth - paragraphMargin, paragraphPosY, colorRed, fontFlags | DrawRightAligned );