2018-08-15 23:01:54 +08:00
|
|
|
#include "fps_counter.h"
|
2018-02-15 18:10:58 +08:00
|
|
|
|
|
|
|
#include <SDL2/SDL_timer.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
fps_counter_init(struct fps_counter *counter) {
|
2019-03-03 06:52:22 +08:00
|
|
|
counter->started = false;
|
2018-02-15 18:10:58 +08:00
|
|
|
// no need to initialize the other fields, they are meaningful only when
|
|
|
|
// started is true
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
fps_counter_start(struct fps_counter *counter) {
|
2019-03-03 06:52:22 +08:00
|
|
|
counter->started = true;
|
2018-02-15 18:10:58 +08:00
|
|
|
counter->slice_start = SDL_GetTicks();
|
|
|
|
counter->nr_rendered = 0;
|
|
|
|
#ifdef SKIP_FRAMES
|
|
|
|
counter->nr_skipped = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
fps_counter_stop(struct fps_counter *counter) {
|
2019-03-03 06:52:22 +08:00
|
|
|
counter->started = false;
|
2018-02-15 18:10:58 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static void
|
|
|
|
display_fps(struct fps_counter *counter) {
|
2018-02-15 18:10:58 +08:00
|
|
|
#ifdef SKIP_FRAMES
|
|
|
|
if (counter->nr_skipped) {
|
2019-03-03 03:09:56 +08:00
|
|
|
LOGI("%d fps (+%d frames skipped)", counter->nr_rendered,
|
|
|
|
counter->nr_skipped);
|
2018-02-15 18:10:58 +08:00
|
|
|
} else {
|
|
|
|
#endif
|
|
|
|
LOGI("%d fps", counter->nr_rendered);
|
|
|
|
#ifdef SKIP_FRAMES
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static void
|
|
|
|
check_expired(struct fps_counter *counter) {
|
2019-03-03 06:52:22 +08:00
|
|
|
uint32_t now = SDL_GetTicks();
|
2018-02-15 18:10:58 +08:00
|
|
|
if (now - counter->slice_start >= 1000) {
|
|
|
|
display_fps(counter);
|
|
|
|
// add a multiple of one second
|
2019-03-03 06:52:22 +08:00
|
|
|
uint32_t elapsed_slices = (now - counter->slice_start) / 1000;
|
2018-02-15 18:10:58 +08:00
|
|
|
counter->slice_start += 1000 * elapsed_slices;
|
|
|
|
counter->nr_rendered = 0;
|
|
|
|
#ifdef SKIP_FRAMES
|
|
|
|
counter->nr_skipped = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
fps_counter_add_rendered_frame(struct fps_counter *counter) {
|
2018-02-15 18:10:58 +08:00
|
|
|
check_expired(counter);
|
|
|
|
++counter->nr_rendered;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SKIP_FRAMES
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
fps_counter_add_skipped_frame(struct fps_counter *counter) {
|
2018-02-15 18:10:58 +08:00
|
|
|
check_expired(counter);
|
|
|
|
++counter->nr_skipped;
|
|
|
|
}
|
|
|
|
#endif
|