0f1b49f135a28d5296e3c5284f20510e859e829c
[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;
16 float color[][3] = {
17         {1, 0, 0},
18         {0, 1, 0},
19         {0, 0, 1},
20         {1, 1, 0},
21         {0, 1, 1},
22         {1, 0, 1}
23 };
24
25 int main(int argc, char **argv)
26 {
27         glutInit(&argc, argv);
28         glutInitWindowSize(128, 128);
29         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
30         glutCreateWindow("timer test");
31
32         glutDisplayFunc(disp);
33
34     /* get timer started, its reset in the timer function itself */
35     glutTimerFunc(1000, timer_func, 0);
36
37         glutMainLoop();
38         return 0;
39 }
40
41 void disp(void)
42 {
43         glClearColor(color[cidx][0], color[cidx][1], color[cidx][2], 1);
44         glClear(GL_COLOR_BUFFER_BIT);
45
46         glutSwapBuffers();
47 }
48
49 void timer_func(int unused)
50 {
51         /* advance the color index and trigger a redisplay */
52         cidx = (cidx + 1) % (sizeof color / sizeof *color);
53         glutPostRedisplay();
54
55         /* (re)set the timer callback and ask glut to call it in 1 second */
56         glutTimerFunc(1000, timer_func, 0);
57 }