yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
timing.h
Go to the documentation of this file.
1#ifndef YAZE_APP_CORE_TIMING_H
2#define YAZE_APP_CORE_TIMING_H
3
5
6#include <cstdint>
7
8namespace yaze {
9
19 public:
20 static TimingManager& Get() {
21 static TimingManager instance;
22 return instance;
23 }
24
29 float Update() {
30 uint64_t current_time = SDL_GetPerformanceCounter();
31 float delta_time = 0.0f;
32
33 if (last_time_ > 0) {
34 delta_time = (current_time - last_time_) / static_cast<float>(frequency_);
35
36 // Clamp delta time to prevent huge jumps (e.g., when debugging)
37 if (delta_time > 0.1f) {
38 delta_time = 0.1f;
39 }
40
41 accumulated_time_ += delta_time;
43
44 // Update FPS counter once per second
45 if (accumulated_time_ >= 1.0f) {
46 fps_ = static_cast<float>(frame_count_) / accumulated_time_;
47 frame_count_ = 0;
48 accumulated_time_ = 0.0f;
49 }
50 }
51
52 last_time_ = current_time;
53 last_delta_time_ = delta_time;
54 return delta_time;
55 }
56
60 float GetDeltaTime() const { return last_delta_time_; }
61
65 float GetFPS() const { return fps_; }
66
70 float GetElapsedTime() const {
71 if (last_time_ == 0)
72 return 0.0f;
73 uint64_t current_time = SDL_GetPerformanceCounter();
74 return (current_time - first_time_) / static_cast<float>(frequency_);
75 }
76
80 void Reset() {
81 last_time_ = 0;
82 first_time_ = SDL_GetPerformanceCounter();
83 accumulated_time_ = 0.0f;
84 frame_count_ = 0;
85 fps_ = 0.0f;
86 last_delta_time_ = 0.0f;
88 }
89
93 void BeginFrame() {
94 frame_start_time_ = SDL_GetPerformanceCounter();
95 }
96
101 float GetFrameElapsedMs() const {
102 if (frame_start_time_ == 0) return 0.0f;
103 uint64_t current = SDL_GetPerformanceCounter();
104 return ((current - frame_start_time_) * 1000.0f) / static_cast<float>(frequency_);
105 }
106
112 constexpr float kTargetFrameTimeMs = 16.67f; // 60fps target
113 return kTargetFrameTimeMs - GetFrameElapsedMs();
114 }
115
116 private:
118 frequency_ = SDL_GetPerformanceFrequency();
119 first_time_ = SDL_GetPerformanceCounter();
120 last_time_ = 0;
121 accumulated_time_ = 0.0f;
122 frame_count_ = 0;
123 fps_ = 0.0f;
124 last_delta_time_ = 0.0f;
125 }
126
127 uint64_t frequency_;
128 uint64_t first_time_;
129 uint64_t last_time_;
130 uint64_t frame_start_time_ = 0;
132 uint32_t frame_count_;
133 float fps_;
135
136 TimingManager(const TimingManager&) = delete;
138};
139
140} // namespace yaze
141
142#endif // YAZE_APP_CORE_TIMING_H
Provides accurate timing for animations and frame pacing.
Definition timing.h:18
void Reset()
Reset the timing state.
Definition timing.h:80
static TimingManager & Get()
Definition timing.h:20
float GetFrameBudgetRemainingMs() const
Get remaining frame budget in milliseconds (targeting 60fps)
Definition timing.h:111
float Update()
Update the timing manager (call once per frame)
Definition timing.h:29
float last_delta_time_
Definition timing.h:134
float accumulated_time_
Definition timing.h:131
float GetFrameElapsedMs() const
Get elapsed time within the current frame in milliseconds.
Definition timing.h:101
uint64_t last_time_
Definition timing.h:129
float GetDeltaTime() const
Get the last frame's delta time in seconds.
Definition timing.h:60
TimingManager & operator=(const TimingManager &)=delete
uint64_t frame_start_time_
Definition timing.h:130
float GetElapsedTime() const
Get total elapsed time since first update.
Definition timing.h:70
TimingManager(const TimingManager &)=delete
uint64_t frequency_
Definition timing.h:127
void BeginFrame()
Mark the start of a new frame for budget tracking.
Definition timing.h:93
float GetFPS() const
Get current FPS.
Definition timing.h:65
uint32_t frame_count_
Definition timing.h:132
uint64_t first_time_
Definition timing.h:128
SDL2/SDL3 compatibility layer.