44fa76c0a6434163a4dcea3f5682faa9d0a37f22
[freeglut] / progs / demos / timer / timer.c
1 /* Timer demo
2  *
3  * Written by John Tsiombikas <nuclear@member.fsf.org>
4  *
5  * Demonstrate the use of glutTimerFunc, by changing the color of the
6  * framebuffer every (approximately) 1 sec.
7  */
8 #include <stdio.h>
9 #include <GL/glut.h>
10
11 void disp(void);
12 void timer_func(int unused);
13
14 /* color index will be advanced every time the timer expires */
15 int cidx = 0;
16 int pcidx = 2;
17 float color[][3] = {
18         {1, 0, 0},
19         {0, 1, 0},
20         {0, 0, 1},
21         {1, 1, 0},
22         {0, 1, 1},
23         {1, 0, 1}
24 };
25
26 int main(int argc, char **argv)
27 {
28         glutInit(&argc, argv);
29         glutInitWindowSize(128, 128);
30         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
31         glutCreateWindow("timer test");
32
33         glutDisplayFunc(disp);
34
35     /* get timer started, its reset in the timer function itself */
36     glutTimerFunc(1000, timer_func, 1);
37     glutTimerFunc(500, timer_func, 2);
38
39         glutMainLoop();
40         return 0;
41 }
42
43 void disp(void)
44 {
45         glClearColor(color[cidx][0], color[cidx][1], color[cidx][2], 1);
46         glClear(GL_COLOR_BUFFER_BIT);
47
48     glPointSize(10.f);
49     glColor3f(color[pcidx][0], color[pcidx][1], color[pcidx][2]);
50     glBegin(GL_POINTS);
51         glVertex2i(0,0);
52     glEnd();
53
54         glutSwapBuffers();
55 }
56
57 void timer_func(int which)
58 {
59         /* advance the color index and trigger a redisplay */
60     switch (which)
61     {
62     case 1:
63         cidx = (cidx + 1) % (sizeof color / sizeof *color);
64         break;
65     case 2:
66         pcidx = (pcidx + 1) % (sizeof color / sizeof *color);
67         break;
68     }
69     
70         glutPostRedisplay();
71
72         /* (re)set the timer callback and ask glut to call it in 1 second */
73         glutTimerFunc(1000, timer_func, which);
74 }