summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaweł Redman <pawel.redman@gmail.com>2018-04-14 15:40:24 +0200
committerPaweł Redman <pawel.redman@gmail.com>2018-04-14 15:41:05 +0200
commite132c6a9f75918978051e8eb23092f5d05c7c3fc (patch)
treed20a022b4991749d2a2d3bad4ebb752fbd059861 /src
parentbae14803b28c4e9641d0e2e7462fdc1e7405c0aa (diff)
Redo frequency counters.
Diffstat (limited to 'src')
-rw-r--r--src/common.hpp16
-rw-r--r--src/game/game.cpp12
-rw-r--r--src/game/interface.cpp2
-rw-r--r--src/main.cpp50
4 files changed, 29 insertions, 51 deletions
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)