From e132c6a9f75918978051e8eb23092f5d05c7c3fc Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Sat, 14 Apr 2018 15:40:24 +0200 Subject: Redo frequency counters. --- src/common.hpp | 16 ++++------------ src/game/game.cpp | 12 ++++++++++-- src/game/interface.cpp | 2 +- src/main.cpp | 50 ++++++++++++++------------------------------------ 4 files changed, 29 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/common.hpp b/src/common.hpp index f4ce9ff..ae4deed 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -48,21 +48,13 @@ ntime_t nclock(void); #define MSEC(x) ((ntime_t)x * 1000000) class freq_counter_t { - ntime_t *samples; - size_t num_samples, collected = 0; - ntime_t last_tick = 0; - - float last_ma; - ntime_t last_ma_time = 0; - - void push(ntime_t sample); + size_t ticks; + ntime_t t0 = 0; + float last_reading; public: - freq_counter_t(size_t num_samples_); - ~freq_counter_t(void); - void tick(void); - float freq_ma(void); + float read(ntime_t ival); }; extern freq_counter_t fc_render; diff --git a/src/game/game.cpp b/src/game/game.cpp index bc8d3c7..185e2d1 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -307,7 +307,7 @@ void state_t::resume(void) } #define TIME_DELTA ((ntime_t)1000000000 / 100) -#define TIME_LIMIT ((ntime_t)1000000000 / 10) +#define TIME_LIMIT ((ntime_t)1000000000 / 20) void state_t::tick(ntime_t time_) { @@ -344,7 +344,15 @@ void state_t::tick(ntime_t time_) fc_game.tick(); if (nclock() - time_ > TIME_LIMIT) { - frames_behind += target - frames_since_t0; + size_t left; + + left = target - frames_since_t0; + t0 = nclock(); + frames_since_t0 = 0; + frames_behind++; + + interface->print("(Lag: " + std::to_string(left) + ")"); + break; } } diff --git a/src/game/interface.cpp b/src/game/interface.cpp index 5b2d7b8..5872d17 100644 --- a/src/game/interface.cpp +++ b/src/game/interface.cpp @@ -381,7 +381,7 @@ void state_t::render_to(render::state_t *render) x[1] -= em; ss.str(std::string()); ss << std::fixed << std::setprecision(1); - ss << "FPS: " << fc_render.freq_ma() << ", " << fc_game.freq_ma() << " Hz"; + ss << "FPS: " << fc_render.read(SEC(1)) << ", " << fc_game.read(SEC(1)) << " Hz"; render->render_text(x, em, ss.str(), render::ALIGN_LEFT_TOP, sf::Color::White); x[1] -= em; diff --git a/src/main.cpp b/src/main.cpp index cb6d753..f0b6906 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,57 +38,35 @@ 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); + if (!t0) + t0 = nclock(); - last_tick = now; + ticks++; } -float freq_counter_t::freq_ma(void) +float freq_counter_t::read(ntime_t ival) { 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; + if (!t0) + return 0.0f; - to_count = std::min(collected, num_samples); + if (now - t0 < ival) + return last_reading; - for (size_t i = 0; i < to_count; i++) - res += samples[i] * 1.0e-9f; - res = 1.0f / (res / to_count); + last_reading = ticks / (float)(now - t0) * 1.0e+9f; + t0 = now; + ticks = 0; - last_ma_time = now; - last_ma = res; - return res; + return last_reading; } -freq_counter_t fc_render = freq_counter_t(20); -freq_counter_t fc_game = freq_counter_t(10); +freq_counter_t fc_render = freq_counter_t(); +freq_counter_t fc_game = freq_counter_t(); #ifdef CONFIG_SHOW_RSS size_t sys_get_rss(void) -- cgit