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
4#include <SDL.h>
5#include <cstdint>
6
7namespace yaze {
8namespace core {
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 {
61 return last_delta_time_;
62 }
63
67 float GetFPS() const {
68 return fps_;
69 }
70
74 float GetElapsedTime() const {
75 if (last_time_ == 0) return 0.0f;
76 uint64_t current_time = SDL_GetPerformanceCounter();
77 return (current_time - first_time_) / static_cast<float>(frequency_);
78 }
79
83 void Reset() {
84 last_time_ = 0;
85 first_time_ = SDL_GetPerformanceCounter();
86 accumulated_time_ = 0.0f;
87 frame_count_ = 0;
88 fps_ = 0.0f;
89 last_delta_time_ = 0.0f;
90 }
91
92 private:
94 frequency_ = SDL_GetPerformanceFrequency();
95 first_time_ = SDL_GetPerformanceCounter();
96 last_time_ = 0;
97 accumulated_time_ = 0.0f;
98 frame_count_ = 0;
99 fps_ = 0.0f;
100 last_delta_time_ = 0.0f;
101 }
102
103 uint64_t frequency_;
104 uint64_t first_time_;
105 uint64_t last_time_;
107 uint32_t frame_count_;
108 float fps_;
110
111 TimingManager(const TimingManager&) = delete;
113};
114
115} // namespace core
116} // namespace yaze
117
118#endif // YAZE_APP_CORE_TIMING_H
119
Provides accurate timing for animations and frame pacing.
Definition timing.h:18
TimingManager(const TimingManager &)=delete
static TimingManager & Get()
Definition timing.h:20
TimingManager & operator=(const TimingManager &)=delete
float GetFPS() const
Get current FPS.
Definition timing.h:67
float GetElapsedTime() const
Get total elapsed time since first update.
Definition timing.h:74
void Reset()
Reset the timing state.
Definition timing.h:83
float Update()
Update the timing manager (call once per frame)
Definition timing.h:29
float GetDeltaTime() const
Get the last frame's delta time in seconds.
Definition timing.h:60
Main namespace for the application.