SDEngine
Game Engine
Loading...
Searching...
No Matches
FrameTimer.hpp
Go to the documentation of this file.
1// TODO(docs): Add file-level Doxygen header
2// - @file FrameTimer.hpp
3// - @brief Frame timing and fixed timestep management
4// - Explain the fixed timestep pattern
5// - GPU/CPU time tracking
6#pragma once
7
8#include <GLFW/glfw3.h>
9
10#include "SD/export.hpp"
11
12namespace sd {
13
14// TODO(docs): Document FrameTimer class
15// - Purpose: Manages frame timing, fixed timestep accumulation
16// - Usage pattern: Begin -> BeginWork -> EndWork -> ConsumeFixedStep (loop)
17// - Frame time clamping (max 0.25s)
18// - GPU wait time tracking
19// - Example game loop using FrameTimer
22public:
23 void begin() {
24 double now = glfwGetTime();
25 m_frame_time = now - m_last_time;
26 m_last_time = now;
27
28 if (m_frame_time > 0.25)
29 m_frame_time = 0.25;
30 m_accumulator += m_frame_time;
31 }
32
33 void begin_work() { m_work_start = glfwGetTime(); }
34
35 void end_work() {
36 m_frame_work_time = static_cast<float>(glfwGetTime() - m_work_start) - m_gpu_wait_time;
37 m_gpu_wait_time = 0.0f;
38 }
39
42 if (m_accumulator >= m_fixed_time_step) {
43 m_accumulator -= m_fixed_time_step;
44 return true;
45 }
46 return false;
47 }
48
49 void add_gpu_wait_time(float t) { m_gpu_wait_time += t; }
50
51 // Accessors
52 [[nodiscard]] float get_frame_time() const { return static_cast<float>(m_frame_time); }
53 [[nodiscard]] float get_frame_work_time() const { return m_frame_work_time; }
54 [[nodiscard]] double get_fixed_time_step() const { return m_fixed_time_step; }
55 void set_fixed_time_step(double step) { m_fixed_time_step = step; }
56
57private:
58 double m_last_time = 0.0;
59 double m_accumulator = 0.0;
60 double m_fixed_time_step = 1.0 / 60.0;
61 double m_work_start = 0.0;
62 float m_frame_work_time = 0.0f;
63 float m_gpu_wait_time = 0.0f;
64 double m_frame_time = 0.0;
65};
66
67} // namespace sd
Tracks frame timing, fixed timestep accumulation, and CPU work time.
Definition FrameTimer.hpp:21
void add_gpu_wait_time(float t)
Definition FrameTimer.hpp:49
void begin()
Definition FrameTimer.hpp:23
void end_work()
Definition FrameTimer.hpp:35
float get_frame_time() const
Definition FrameTimer.hpp:52
bool consume_fixed_step()
Returns true if a fixed step should run. Call in a loop.
Definition FrameTimer.hpp:41
void set_fixed_time_step(double step)
Definition FrameTimer.hpp:55
float get_frame_work_time() const
Definition FrameTimer.hpp:53
void begin_work()
Definition FrameTimer.hpp:33
double get_fixed_time_step() const
Definition FrameTimer.hpp:54
Definition Application.hpp:28
constexpr T g_type_max
Definition types.hpp:21