diff options
| author | Hannes Schulze <haschu0103@outlook.de> | 2025-02-06 18:24:59 +0100 |
|---|---|---|
| committer | Hannes Schulze <haschu0103@outlook.de> | 2025-02-06 18:24:59 +0100 |
| commit | d060e3a50c987d10799ccc739bdbf9d7d7a13c10 (patch) | |
| tree | 4b5585383ada85608b57bcf76e5828817962fddf /lib/nes/common/fps-counter.cc | |
| parent | dbed1c0fa5599b1b96c221b89b10f1e350351645 (diff) | |
implement settings
Diffstat (limited to 'lib/nes/common/fps-counter.cc')
| -rw-r--r-- | lib/nes/common/fps-counter.cc | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/nes/common/fps-counter.cc b/lib/nes/common/fps-counter.cc new file mode 100644 index 0000000..ef6169b --- /dev/null +++ b/lib/nes/common/fps-counter.cc @@ -0,0 +1,29 @@ +#include "nes/common/fps-counter.hh" + +namespace nes +{ + auto fps_counter::frame(std::chrono::microseconds const elapsed_time) -> void + { + if (elapsed_time.count() <= 0) { return; } + + if (buffer_position_ == 0) + { + // Update fps count. + auto sum = u32{ 0 }; + for (auto i = u32{ 0 }; i < buffer_size; ++i) { sum += frame_times_[i]; } + if (sum > 0) + { + buffered_fps_ = (buffer_size * 1000000) / sum; + } + else + { + buffered_fps_ = 0; + } + } + + // Add the frame time to the buffer, replacing any existing content in the ring buffer. + auto const frame_time = static_cast<u32>(elapsed_time.count()); + frame_times_[buffer_position_] = frame_time; + buffer_position_ = (buffer_position_ + 1) % buffer_size; + } +} // namespace nes
\ No newline at end of file |
