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
22 void begin() {
23 double now = glfwGetTime();
24 m_frame_time = now - m_last_time;
25 m_last_time = now;
26
27 if (m_frame_time > 0.25)
28 m_frame_time = 0.25;
29 m_accumulator += m_frame_time;
30 }
31
32 void begin_work() { m_work_start = glfwGetTime(); }
33
34 void end_work() {
35 m_frame_work_time = static_cast<float>(glfwGetTime() - m_work_start) - m_gpu_wait_time;
36 m_gpu_wait_time = 0.0f;
37 }
38
41 if (m_accumulator >= m_fixed_time_step) {
42 m_accumulator -= m_fixed_time_step;
43 return true;
44 }
45 return false;
46 }
47
48 void add_gpu_wait_time(float t) { m_gpu_wait_time += t; }
49
50 // Accessors
51 [[nodiscard]] float get_frame_time() const { return static_cast<float>(m_frame_time); }
52 [[nodiscard]] float get_frame_work_time() const { return m_frame_work_time; }
53 [[nodiscard]] double get_fixed_time_step() const { return m_fixed_time_step; }
54 void set_fixed_time_step(double step) { m_fixed_time_step = step; }
55
56
57 double m_last_time = 0.0;
58 double m_accumulator = 0.0;
59 double m_fixed_time_step = 1.0 / 60.0;
60 double m_work_start = 0.0;
61 float m_frame_work_time = 0.0f;
62 float m_gpu_wait_time = 0.0f;
63 double m_frame_time = 0.0;
64};
65
66} // namespace sd
Definition Application.hpp:22
Tracks frame timing, fixed timestep accumulation, and CPU work time.
Definition FrameTimer.hpp:21
void add_gpu_wait_time(float t)
Definition FrameTimer.hpp:48
void begin()
Definition FrameTimer.hpp:22
void end_work()
Definition FrameTimer.hpp:34
float get_frame_time() const
Definition FrameTimer.hpp:51
bool consume_fixed_step()
Returns true if a fixed step should run. Call in a loop.
Definition FrameTimer.hpp:40
void set_fixed_time_step(double step)
Definition FrameTimer.hpp:54
float get_frame_work_time() const
Definition FrameTimer.hpp:52
void begin_work()
Definition FrameTimer.hpp:32
double get_fixed_time_step() const
Definition FrameTimer.hpp:53
consteval U64 type_id_of()
Definition type_id.hpp:6