From d4611757ca2f710b3295847834a47217d19746ef Mon Sep 17 00:00:00 2001 From: lvntky Date: Mon, 25 Nov 2024 11:50:18 +0300 Subject: [feature] fps counter added --- fbgl.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'fbgl.h') diff --git a/fbgl.h b/fbgl.h index 62febfd..03dd073 100644 --- a/fbgl.h +++ b/fbgl.h @@ -42,6 +42,7 @@ #include #include #include +#include #ifdef FBGL_USE_FREETYPE #include @@ -104,6 +105,7 @@ typedef struct fbgl_keyboard { */ static struct termios orig_termios; static fbgl_keyboard_t keyboard = { 0 }; +static struct timespec previous_frame_time = { 0 }; #ifdef FBGL_HIDE_CURSOR #include @@ -149,6 +151,7 @@ void fbgl_disable_raw_mode(void); void fbgl_cleanup(int sig); int fbgl_check_esc_key(void); void fbgl_set_signal_handlers(void); +float fbgl_get_fps(void); /*Create and destroy methods*/ int fbgl_init(const char *device, fbgl_t *fb); @@ -655,6 +658,30 @@ uint32_t *fb_get_data(fbgl_t const *fb) return fb->pixels; } +float fbgl_get_fps(void) +{ + struct timespec current_time; + clock_gettime(CLOCK_MONOTONIC, ¤t_time); + + if (previous_frame_time.tv_sec == 0 && + previous_frame_time.tv_nsec == 0) { + previous_frame_time = current_time; + return 0.0f; + } + + double time_diff = + (current_time.tv_sec - previous_frame_time.tv_sec) + + (current_time.tv_nsec - previous_frame_time.tv_nsec) / 1e9; + + previous_frame_time = current_time; + + if (time_diff > 0.0) { + return 1.0 / time_diff; + } else { + return 0.0f; // Avoid division by zero + } +} + #endif // FBGL_IMPLEMENTATION #ifdef __cplusplus -- cgit v1.2.3