Skip to content

Commit

Permalink
[Examples] Fixed time record output and improved Fonts example FPS ou…
Browse files Browse the repository at this point in the history
…tput.

- Added missing '%' character to time record output (when F1 is pressed in examples).
- Show average FPS in Fonts example after every 500 milliseconds.
  • Loading branch information
LukasBanana committed Jun 23, 2024
1 parent 1160610 commit cbb251f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
8 changes: 6 additions & 2 deletions examples/Cpp/ExampleBase/ExampleBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#endif
#include <inttypes.h>

#define IMMEDIATE_SUBMIT_CMDBUFFER 0


/*
* Global helper functions
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
40 changes: 33 additions & 7 deletions examples/Cpp/Fonts/Example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <ExampleBase.h>
#include <LLGL/Platform/Platform.h>
#include <chrono>

#define FONT_COURIER_NEW_16 ( 0 )
#define FONT_LUCIDA_CONSOLE_32 ( 1 )
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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<int>(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<std::chrono::milliseconds>(currentTimePoint - avgFPS.lastTimePoint).count();
if (timeSinceLastAvgFPSUpdate > 500 && avgFPS.samples > 0)
{
displayNumbers.averageFPS = static_cast<int>(avgFPS.sum / static_cast<double>(avgFPS.samples) + 0.5);
avgFPS.samples = 0;
avgFPS.sum = 0.0;
avgFPS.lastTimePoint = currentTimePoint;
}

// Reset batch counter
numBatches = 0;
Expand Down Expand Up @@ -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
);
Expand All @@ -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
);
Expand Down

0 comments on commit cbb251f

Please sign in to comment.