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 {
8
18 public:
19 static TimingManager& Get() {
20 static TimingManager instance;
21 return instance;
22 }
23
28 float Update() {
29 uint64_t current_time = SDL_GetPerformanceCounter();
30 float delta_time = 0.0f;
31
32 if (last_time_ > 0) {
33 delta_time = (current_time - last_time_) / static_cast<float>(frequency_);
34
35 // Clamp delta time to prevent huge jumps (e.g., when debugging)
36 if (delta_time > 0.1f) {
37 delta_time = 0.1f;
38 }
39
40 accumulated_time_ += delta_time;
42
43 // Update FPS counter once per second
44 if (accumulated_time_ >= 1.0f) {
45 fps_ = static_cast<float>(frame_count_) / accumulated_time_;
46 frame_count_ = 0;
47 accumulated_time_ = 0.0f;
48 }
49 }
50
51 last_time_ = current_time;
52 last_delta_time_ = delta_time;
53 return delta_time;
54 }
55
59 float GetDeltaTime() const {
60 return last_delta_time_;
61 }
62
66 float GetFPS() const {
67 return fps_;
68 }
69
73 float GetElapsedTime() const {
74 if (last_time_ == 0) return 0.0f;
75 uint64_t current_time = SDL_GetPerformanceCounter();
76 return (current_time - first_time_) / static_cast<float>(frequency_);
77 }
78
82 void Reset() {
83 last_time_ = 0;
84 first_time_ = SDL_GetPerformanceCounter();
85 accumulated_time_ = 0.0f;
86 frame_count_ = 0;
87 fps_ = 0.0f;
88 last_delta_time_ = 0.0f;
89 }
90
91 private:
93 frequency_ = SDL_GetPerformanceFrequency();
94 first_time_ = SDL_GetPerformanceCounter();
95 last_time_ = 0;
96 accumulated_time_ = 0.0f;
97 frame_count_ = 0;
98 fps_ = 0.0f;
99 last_delta_time_ = 0.0f;
100 }
101
102 uint64_t frequency_;
103 uint64_t first_time_;
104 uint64_t last_time_;
106 uint32_t frame_count_;
107 float fps_;
109
110 TimingManager(const TimingManager&) = delete;
112};
113
114} // namespace yaze
115
116#endif // YAZE_APP_CORE_TIMING_H
117
Provides accurate timing for animations and frame pacing.
Definition timing.h:17
void Reset()
Reset the timing state.
Definition timing.h:82
static TimingManager & Get()
Definition timing.h:19
float Update()
Update the timing manager (call once per frame)
Definition timing.h:28
float last_delta_time_
Definition timing.h:108
float accumulated_time_
Definition timing.h:105
uint64_t last_time_
Definition timing.h:104
float GetDeltaTime() const
Get the last frame's delta time in seconds.
Definition timing.h:59
TimingManager & operator=(const TimingManager &)=delete
float GetElapsedTime() const
Get total elapsed time since first update.
Definition timing.h:73
TimingManager(const TimingManager &)=delete
uint64_t frequency_
Definition timing.h:102
float GetFPS() const
Get current FPS.
Definition timing.h:66
uint32_t frame_count_
Definition timing.h:106
uint64_t first_time_
Definition timing.h:103
Main namespace for the application.
Definition controller.cc:20