taisei/src/framerate.c

58 lines
1.4 KiB
C
Raw Normal View History

2018-01-20 15:15:15 +01:00
/*
* This software is licensed under the terms of the MIT License.
2018-01-20 15:15:15 +01:00
* See COPYING for further information.
* ---
* Copyright (c) 2011-2019, Lukas Weber <laochailan@web.de>.
2019-07-03 20:00:56 +02:00
* Copyright (c) 2012-2019, Andrei Alexeyev <akari@taisei-project.org>.
2018-01-20 15:15:15 +01:00
*/
#include "taisei.h"
#include "framerate.h"
#include "global.h"
#include "video.h"
void fpscounter_reset(FPSCounter *fps) {
hrtime_t frametime = HRTIME_RESOLUTION / FPS;
const int log_size = ARRAY_SIZE(fps->frametimes);
2018-01-20 15:15:15 +01:00
for(int i = 0; i < log_size; ++i) {
fps->frametimes[i] = frametime;
}
fps->fps = HRTIME_RESOLUTION / (long double)frametime;
fps->frametime = frametime;
2018-01-20 15:15:15 +01:00
fps->last_update_time = time_get();
}
void fpscounter_update(FPSCounter *fps) {
const int log_size = ARRAY_SIZE(fps->frametimes);
hrtime_t update_time = time_get();
hrtime_t frametime = update_time - fps->last_update_time;
2018-01-20 15:15:15 +01:00
memmove(fps->frametimes, fps->frametimes + 1, (log_size - 1) * sizeof(hrtime_t));
fps->frametimes[log_size - 1] = frametime;
hrtime_t avg = 0;
2018-01-20 15:15:15 +01:00
for(int i = 0; i < log_size; ++i) {
avg += fps->frametimes[i];
}
fps->fps = HRTIME_RESOLUTION / (avg / (double)log_size);
fps->frametime = avg / log_size;
fps->last_update_time = update_time;
2018-01-20 15:15:15 +01:00
}
uint32_t get_effective_frameskip(void) {
uint32_t frameskip;
if(global.frameskip > 0) {
frameskip = global.frameskip;
} else {
frameskip = config_get_int(CONFIG_VID_FRAMESKIP);
}
return frameskip;
}