From e7f81ff8f43b49331c368e3be09326551e0e6213 Mon Sep 17 00:00:00 2001 From: Alessandro Iezzi Date: Sat, 17 May 2025 01:16:55 +0200 Subject: Add engine_loop and FPS calculation --- src/main/engine/engine.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/main/engine/engine.h | 1 + src/main/main.c | 3 +-- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/main/engine/engine.c b/src/main/engine/engine.c index b4b9f42..7752095 100644 --- a/src/main/engine/engine.c +++ b/src/main/engine/engine.c @@ -17,6 +17,8 @@ * along with Tris Game. If not, see . */ +#include +#include #include #include "../ui/ui.h" @@ -27,6 +29,17 @@ static float ystep; static int width; static int height; +typedef struct Engine { + void (*draw_frame)(); +} Engine; + +static Engine engine; + +/* FPS */ +static time_t start; +static int frames; +static time_t end; + void engine_init(int w, int h) { @@ -47,3 +60,37 @@ engine_draw_line(int x1, int y1, int x2, int y2) glVertex2f(_x1, _y1); glVertex2f(_x2, _y2); } + +static void +engine_calculate_fps() +{ + frames ++; + + end = time(NULL); + if (end - start >= 1.0) { + printf("FPS: %d\n", frames); + frames = 0; + start = time(NULL); + } +} + +static void +draw_frames() +{ + engine.draw_frame(); + engine_calculate_fps(); +} + +void +engine_loop(void (*draw_frame)()) +{ + engine.draw_frame = draw_frame; + + ui_set_loop_listener(draw_frames); + + /* FPS calculation */ + start = time(NULL); + frames = 0; + + ui_loop(); +} diff --git a/src/main/engine/engine.h b/src/main/engine/engine.h index cb9e562..691d8ec 100644 --- a/src/main/engine/engine.h +++ b/src/main/engine/engine.h @@ -22,5 +22,6 @@ void engine_init(int width, int height); void engine_draw_line(int x1, int y1, int x2, int y2); +void engine_loop(void (*draw_frame)()); #endif /* __ENGINE_H__ */ diff --git a/src/main/main.c b/src/main/main.c index 20a2a72..76876f0 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -31,8 +31,7 @@ void draw_frame() { int main(void) { engine_init(WIDTH, HEIGHT); ui_set_title("Tris Game"); - ui_set_loop_listener(draw_frame); - ui_loop(); + engine_loop(draw_frame); return EXIT_SUCCESS; } -- cgit v1.2.3