diff options
-rw-r--r-- | src/common.hpp | 24 | ||||
-rw-r--r-- | src/game/game.cpp | 14 | ||||
-rw-r--r-- | src/game/interface.cpp | 11 | ||||
-rw-r--r-- | src/main.cpp | 52 | ||||
-rw-r--r-- | src/render.cpp | 2 |
5 files changed, 83 insertions, 20 deletions
diff --git a/src/common.hpp b/src/common.hpp index 1cd1921..bb95f0e 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -40,6 +40,27 @@ extern bool debug_AI; typedef uint64_t ntime_t; ntime_t nclock(void); +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); + +public: + freq_counter_t(size_t num_samples_); + ~freq_counter_t(void); + + void tick(void); + float freq_ma(void); +}; + +extern freq_counter_t fc_render; +extern freq_counter_t fc_game; + namespace procgen { class prng_t { uint32_t state = 0; @@ -351,9 +372,6 @@ namespace interface { std::list<log_entry_t> log; - double perf_hist[10] = {0}; - size_t perf_hist_index = 0; - void start_following(void); void stop_following(void); diff --git a/src/game/game.cpp b/src/game/game.cpp index 2a944af..4cac4eb 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -288,11 +288,12 @@ void state_t::resume(void) paused = false; } -#define TIME_DELTA ((ntime_t)1000000000 / 60) +#define TIME_DELTA ((ntime_t)1000000000 / 100) +#define TIME_LIMIT ((ntime_t)1000000000 / 10) void state_t::tick(ntime_t time_) { - size_t target, frames_this_tick = 0; + size_t target; if (paused) return; @@ -315,13 +316,12 @@ void state_t::tick(ntime_t time_) frames++; frames_since_t0++; - frames_this_tick++; time += TIME_DELTA; - if (frames_this_tick == 3) { - t0 = time_; - frames_since_t0 = 0; - frames_behind++; + fc_game.tick(); + + if (nclock() - time_ > TIME_LIMIT) { + frames_behind += target - frames_since_t0; break; } } diff --git a/src/game/interface.cpp b/src/game/interface.cpp index b187d42..c6d5e19 100644 --- a/src/game/interface.cpp +++ b/src/game/interface.cpp @@ -332,7 +332,6 @@ void state_t::render_to(render::state_t *render) size_t w = window->getSize().x, h = window->getSize().y; v2f_t x; std::stringstream ss; - double fps; if (select.selecting) { sf::Color color; @@ -391,18 +390,10 @@ void state_t::render_to(render::state_t *render) ss << render->stats.entities; render->render_text(x, em, ss.str(), render::ALIGN_LEFT_TOP, sf::Color::White); - perf_hist_index = (perf_hist_index + 1) % COUNT(perf_hist); - perf_hist[perf_hist_index] = 1.0 / render->dt; - - fps = 0.0; - for (size_t i = 0; i < COUNT(perf_hist); i++) - fps += perf_hist[i]; - fps /= COUNT(perf_hist); - x[1] += em; ss.str(std::string()); ss << std::fixed << std::setprecision(1); - ss << "FPS: " << fps; + ss << "FPS: " << fc_render.freq_ma() << ", " << fc_game.freq_ma() << " 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 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; diff --git a/src/render.cpp b/src/render.cpp index 9056d9c..88ad25f 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -317,6 +317,8 @@ void state_t::render(game::state_t *game) if (debug_draw_cmodels) drender_entity(ent); } + + fc_render.tick(); } void state_t::render(double phase, animated_texture_t *anim, rectf_t bounds, sf::Color color, bool mirror){ |