diff options
| author | Paweł Redman <pawel.redman@gmail.com> | 2018-04-12 22:51:20 +0200 | 
|---|---|---|
| committer | Paweł Redman <pawel.redman@gmail.com> | 2018-04-12 22:51:20 +0200 | 
| commit | 445505ada572fbf7be3ce474a1af31419ff68c2b (patch) | |
| tree | 110a123eb999a03a6da01c7933a24a96686b2219 /src/main.cpp | |
| parent | 35ee11e5c1a468530bce671c797eccffc2a1c330 (diff) | |
Better game timing and better FPS/TPS display.
Diffstat (limited to 'src/main.cpp')
| -rw-r--r-- | src/main.cpp | 52 | 
1 files changed, 52 insertions, 0 deletions
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;  | 
