changeset 8:640a1d1a730d

Timer/FPS counter
author jsipek@huey.fsl.cs.sunysb.edu
date Thu, 24 Nov 2005 02:18:06 -0400
parents e681fac7212c
children d325e84a13dd
files Makefile render.c run.sh
diffstat 3 files changed, 35 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Thu Nov 24 02:03:12 2005 -0400
+++ b/Makefile	Thu Nov 24 02:18:06 2005 -0400
@@ -1,7 +1,7 @@
 all: render transform
 
 render: render.c
-	gcc render.c -o render -I/usr/X11R6/include/ -L/usr/X11R6/lib -lX11 -lXi -lXmu -lglut -lGL -lGLU -g -Wall
+	gcc render.c -o render -I/usr/X11R6/include/ -L/usr/X11R6/lib -lX11 -lXi -lXmu -lglut -lGL -lGLU -g -Wall -lrt
 
 transform: transform.c
 	gcc -o transform transform.c -Wall -g
--- a/render.c	Thu Nov 24 02:03:12 2005 -0400
+++ b/render.c	Thu Nov 24 02:18:06 2005 -0400
@@ -8,17 +8,24 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <time.h>
 
 //  OpenGL
 #include "GL/glut.h"
 
-#define BUF_SIZE 1024
-#define TIMER_PERIOD 10
+#define BUF_SIZE	1024
+#define TIMER_PERIOD	10
+
+#define PIPE_IN		"/tmp/3de.in"
+#define PIPE_OUT	"/tmp/3de.out"
 
 int infd;
 int outfd;
 char *buf, *ptr;
 
+int frames;
+struct timespec start;
+
 void display(void);
 
 int valid_digit_char(char c)
@@ -115,6 +122,8 @@
 	}
 
 	ptr = buf;
+
+	frames++;
 }
 
 void init()
@@ -140,10 +149,25 @@
 	fprintf(stderr, "Error encountered in %s:%d: %s (%d)\n", f, l, strerror(errno), errno);
 }
 
+void goodbye()
+{
+	struct timespec end;
+	double delta;
+
+	clock_gettime(CLOCK_REALTIME, &end);
+
+	delta = ((double) end.tv_sec + 1000000000.0/(double)end.tv_nsec)
+	      - ((double) start.tv_sec + 1000000000.0/(double)start.tv_nsec);
+
+	printf("Displayed %d frames in %.4lf seconds = %.4lf fps\n", frames, delta, frames/delta);
+
+	close(infd);
+	close(outfd);
+}
+
 int main(int argc, char** argv)
 {
-#define PIPE_IN		"/tmp/3de.in"
-#define PIPE_OUT	"/tmp/3de.out"
+	frames = 0;
 
 	unlink(PIPE_IN);
 	unlink(PIPE_OUT);
@@ -164,6 +188,8 @@
 	if ((outfd = open(PIPE_OUT, O_RDWR | O_NONBLOCK | O_NDELAY)) == -1)
 		die();
 
+	atexit(goodbye);
+
 	glutInit(&argc,argv);
 	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
 	glutInitWindowSize(800,600);
@@ -172,11 +198,12 @@
 	glutDisplayFunc(display);
 	glutKeyboardFunc(key);
 	init();
+	
+	clock_gettime(CLOCK_REALTIME, &start);
+
 	glutTimerFunc(TIMER_PERIOD, timer2, 0);
 	glutMainLoop();
 
-	close(infd);
-	close(outfd);
 
 	return 0;
 }
--- a/run.sh	Thu Nov 24 02:03:12 2005 -0400
+++ b/run.sh	Thu Nov 24 02:18:06 2005 -0400
@@ -1,4 +1,4 @@
-#./render&
+./render&
 sleep 1
 
 PIPE="/tmp/3de.in"