going to sleep
[ld42_outofspace] / src / main.cc
index 1ee34ad..ca23fbc 100644 (file)
@@ -1,11 +1,14 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
+#include <stdarg.h>
 #include <GL/freeglut.h>
 #include "game.h"
 
 #define KEYST_SZ       65536 / 32
 
+void draw_text(float x, float y, float r, float g, float b, const char *fmt, ...);
+
 static void display();
 static void idle();
 static void reshape(int x, int y);
@@ -74,12 +77,56 @@ unsigned int game_modkeys()
        return modkeys;
 }
 
+void draw_text(float x, float y, float r, float g, float b, const char *fmt, ...)
+{
+       char buf[256], *text = buf;
+       va_list ap;
+
+       va_start(ap, fmt);
+       vsprintf(buf, fmt, ap);
+       va_end(ap);
+
+       glMatrixMode(GL_MODELVIEW);
+       glPushMatrix();
+       glLoadIdentity();
+       glMatrixMode(GL_PROJECTION);
+       glPushMatrix();
+       glLoadIdentity();
+       glOrtho(0, win_width, 0, win_height, -1, 1);
+
+       glDisable(GL_LIGHTING);
+
+       glRasterPos2f(1, 1);
+       glColor3f(r, g, b);
+       while(*text) {
+               glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *text++);
+       }
+
+       glEnable(GL_LIGHTING);
+
+       glPopMatrix();
+       glMatrixMode(GL_MODELVIEW);
+       glPopMatrix();
+}
+
 static void display()
 {
        frame_time = glutGet(GLUT_ELAPSED_TIME);
        frame_dt = (frame_time - prev_time) / 1000.0f;
 
        game_draw();
+
+
+       static long frames, fps, prev_fps_upd;
+       draw_text(1, 1, 1, 1, 0, "fps: %ld", fps);
+
+       if(frame_time - prev_fps_upd >= 1000) {
+               fps = frames;
+               frames = 0;
+               prev_fps_upd = frame_time;
+       }
+       frames++;
+
        glutSwapBuffers();
 }