summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/engine/engine.c47
-rw-r--r--src/main/engine/engine.h1
-rw-r--r--src/main/main.c3
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 <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__ */
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;
}