Skip to main content

FramePacer.ixx File

Module defining the FramePacer class for frame rate control and timing synchronization. More...

Included Headers

#include <memory> #include <thread> #include <chrono> #include <cassert> #include <helios.engine.tooling.FrameStats> #include <helios.engine.util.time.Stopwatch>

Namespaces Index

namespacehelios
namespaceengine
namespacetooling

Classes Index

classFramePacer

Controls and maintains a target frame rate through precise timing and pacing. More...

Description

Module defining the FramePacer class for frame rate control and timing synchronization.

File Listing

The file content with the documentation metadata removed is:

1
5module;
6
7#include <memory>
8#include <thread>
9#include <chrono>
10#include <cassert>
11
12export module helios.engine.tooling.FramePacer;
13
14import helios.engine.util.time.Stopwatch;
15import helios.engine.tooling.FrameStats;
16
17export namespace helios::engine::tooling {
18
46 class FramePacer {
51
55 float targetFps_ = 0.0f;
56
57 public:
58
65 void setTargetFps(float fps) {
66 targetFps_ = fps;
67 }
68
81 return targetFps_;
82 }
83
91 void beginFrame() {
92 stopwatch_.start();
93 }
94
112 float workTime = stopwatch_.elapsedSeconds();
113
114 float waitTime = 0.0f;
115 float totalTime = workTime;
116
117 if (targetFps_ > 0.0f) {
118 float targetTime = 1.0f / targetFps_;
119 if (targetTime > workTime) {
120 auto requestedWaitTime = targetTime - workTime;
121 auto sleepDuration = std::chrono::duration<float>(requestedWaitTime);
122 std::this_thread::sleep_for(sleepDuration);
123 totalTime = stopwatch_.elapsedSeconds();
124 waitTime = totalTime - workTime;
125 }
126 }
127
128 return FrameStats{ totalTime, workTime, waitTime };
129 }
130 };
131}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.