/* 
On Mac, compile with: 
gcc -framework OpenGL -framework GLUT -o tcb-c main.c

Run with:
./tcb-c <test> <timeout>
*/
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>

GLvoid InitGL(GLvoid) {}
GLvoid DrawGLScene(GLvoid) {}
GLvoid ReSizeGLScene(int Width, int Height) {}

int tickLength;

void putTicks (int ticks) {
	struct timeval tv;
	int tod = gettimeofday(&tv, NULL);

	if (tod == 0) {
		printf("tick: %2d, %6d%06d\n", ticks, tv.tv_sec, tv.tv_usec);
	} else {
		printf("tick: %2d, Error getting time\n", ticks);
	}
}

void test1 () {
	int ii;
	for (ii = 0; ii < 5; ii++) {
		glutTimerFunc(tickLength * ii, putTicks, ii); 
	}
}

void test2 (int ticks) {
	if (ticks > 5) return;
	if (ticks > 0) putTicks(ticks - 1);
	glutTimerFunc(tickLength, test2, ticks + 1); 
}

int main (int argc, char** argv) {
	if (argc != 3) {
		printf("Need two arguments.\n");
		exit(-1);
	}

	tickLength = atoi(argv[2]);

    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize (400, 400);
    glutInitWindowPosition (0, 0);
    glutCreateWindow ("TCB - Bug Test");

    InitGL();

	switch (argv[1][0]) {
		case '1':
			test1();
			break;
		case '2':
			test2(0);
			break;
	}

    glutDisplayFunc(DrawGLScene);
    glutReshapeFunc(ReSizeGLScene);

    glutMainLoop();

    return 0;
}