From 445505ada572fbf7be3ce474a1af31419ff68c2b Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Thu, 12 Apr 2018 22:51:20 +0200 Subject: Better game timing and better FPS/TPS display. --- src/main.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 9af2736..1e43d76 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,6 +33,58 @@ ntime_t nclock(void) std::chrono::nanoseconds(1) - nclock_ref; } +freq_counter_t::freq_counter_t(size_t num_samples_) +{ + num_samples = num_samples_; + samples = (ntime_t*)malloc(sizeof(ntime_t) * num_samples); +} + +freq_counter_t::~freq_counter_t(void) +{ + free(samples); +} + +void freq_counter_t::push(ntime_t sample) +{ + collected++; + samples[collected % num_samples] = sample; +} + +void freq_counter_t::tick(void) +{ + ntime_t now = nclock(); + + if (last_tick) + push(now - last_tick); + + last_tick = now; +} + +float freq_counter_t::freq_ma(void) +{ + ntime_t now; + float res = 0.0f; + size_t to_count; + + now = nclock(); + + if (last_ma_time && now - last_ma_time < 500000000) + return last_ma; + + to_count = std::min(collected, num_samples); + + for (size_t i = 0; i < to_count; i++) + res += samples[i] * 1.0e-9f; + res = 1.0f / (res / to_count); + + last_ma_time = now; + last_ma = res; + return res; +} + +freq_counter_t fc_render = freq_counter_t(20); +freq_counter_t fc_game = freq_counter_t(10); + int main() { nclock_ref = nclock() - 1; -- cgit