RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
FrameTimer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <cstdint>
5
6namespace RST::Time {
7
16 class FrameTimer {
17 public:
18 using Clock = std::chrono::steady_clock;
19
21 explicit FrameTimer(std::uint32_t targetFps = 0) noexcept;
22
24 void tick() noexcept;
25
27 [[nodiscard]] double getDeltaTime() const noexcept { return _deltaTime; }
28
29 [[nodiscard]] double getCurrentFps() const noexcept { return (_deltaTime > 0.0) ? (1.0 / _deltaTime) : 0.0; }
30
32 [[nodiscard]] double getAverageFps() const noexcept { return (_averageFrameTime > 0.0) ? (1.0 / _averageFrameTime) : 0.0; }
33
34 [[nodiscard]] std::uint64_t getFrameCount() const noexcept { return _frameCount; }
35
37 [[nodiscard]] double getTotalTime() const noexcept { return std::chrono::duration<double>(_previousTime - _startTime).count(); }
38
39 void setTargetFps(std::uint32_t targetFps) noexcept;
40 [[nodiscard]] std::uint32_t getTargetFps() const noexcept { return _targetFps; }
41
42 private:
43 Clock::time_point _startTime;
44 Clock::time_point _previousTime;
45 Clock::duration _targetFrameTime{};
46 double _deltaTime = 0.0;
47 double _averageFrameTime = 0.0;
48 std::uint64_t _frameCount = 0;
49 std::uint32_t _targetFps = 0;
50 };
51
52}
Measures the frames of a game or render loop, and can cap the frame rate.
Definition FrameTimer.hpp:16
void setTargetFps(std::uint32_t targetFps) noexcept
std::chrono::steady_clock Clock
Definition FrameTimer.hpp:18
double getCurrentFps() const noexcept
Definition FrameTimer.hpp:29
double getDeltaTime() const noexcept
Seconds between the last two ticks.
Definition FrameTimer.hpp:27
void tick() noexcept
Ends a frame, after sleeping until the target frame time if there is one.
double getAverageFps() const noexcept
Frame rate smoothed over the recent frames.
Definition FrameTimer.hpp:32
std::uint32_t getTargetFps() const noexcept
Definition FrameTimer.hpp:40
FrameTimer(std::uint32_t targetFps=0) noexcept
Starts timing; targetFps caps the frame rate, 0 means no cap.
std::uint64_t getFrameCount() const noexcept
Definition FrameTimer.hpp:34
double getTotalTime() const noexcept
Seconds from construction to the last tick.
Definition FrameTimer.hpp:37
Stopwatch, frame timing and date formatting.
Definition DateTime.hpp:8