summaryrefslogtreecommitdiff
path: root/src/main/engine
diff options
context:
space:
mode:
authorAlessandro Iezzi <aiezzi@alessandroiezzi.it>2025-05-17 01:16:55 +0200
committerAlessandro Iezzi <aiezzi@alessandroiezzi.it>2025-05-17 01:16:55 +0200
commite7f81ff8f43b49331c368e3be09326551e0e6213 (patch)
tree60c5c2f7c8ab60fa88a934a4008dafe701c6218d /src/main/engine
parentd9ceba3831a7dd09b0a177dd366c87420a014eee (diff)
downloadtris-e7f81ff8f43b49331c368e3be09326551e0e6213.tar.gz
tris-e7f81ff8f43b49331c368e3be09326551e0e6213.zip
Add engine_loop and FPS calculation
Diffstat (limited to 'src/main/engine')
-rw-r--r--src/main/engine/engine.c47
-rw-r--r--src/main/engine/engine.h1
2 files changed, 48 insertions, 0 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 <http://www.gnu.org/licenses/>.
*/
+#include <stdio.h>
+#include <time.h>
#include <GL/gl.h>
#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__ */