summaryrefslogtreecommitdiff
path: root/src/main/engine/engine.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/engine/engine.c')
-rw-r--r--src/main/engine/engine.c47
1 files changed, 47 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();
+}