fixed timer.c: wouldn't compile in C89 mode due to recent change which added two
[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 int timerInts[] = {
26     250,
27     500,
28     1000
29 };
30 int timerSurroundInt = 1000, timerCenterInt = 1000;
31
32 /* menu IDs, creation/update funcs and callback */
33 int menuID, subMenuSurround, subMenuCenter;
34
35 void createMenuEntries(int which)
36 {
37         int i;
38     for (i = 0; i < sizeof(timerInts) / sizeof(*timerInts); i++)
39     {
40         char temp[10] = {'\0'};
41         /* flag current value */
42         if ((which == 1 ? timerSurroundInt : timerCenterInt) == timerInts[i])
43             temp[0] = '+';
44         else
45             temp[0] = '-';
46
47         sprintf(temp + 1, " %4d ms", timerInts[i]);
48
49         glutAddMenuEntry(temp, timerInts[i]);
50     }
51 }
52
53 void updateMenuEntries(int which)
54 {
55         int i;
56     for (i = 0; i < sizeof(timerInts) / sizeof(*timerInts); i++)
57     {
58         char temp[10] = { '\0' };
59         /* flag current value */
60         if ((which == 1 ? timerSurroundInt : timerCenterInt) == timerInts[i])
61             temp[0] = '+';
62         else
63             temp[0] = '-';
64
65         sprintf(temp + 1, " %4d ms", timerInts[i]);
66
67         glutChangeToMenuEntry(i+1, temp, timerInts[i]);
68     }
69 }
70 \r
71 void MenuSurround(int timerInt)\r
72 {\r
73     timerSurroundInt = timerInt;\r
74     glutSetMenu(subMenuSurround);\r
75     updateMenuEntries(1);\r
76 }
77 void MenuCenter(int timerInt)\r
78 {\r
79     timerCenterInt = timerInt;\r
80     glutSetMenu(subMenuCenter);\r
81     updateMenuEntries(2);\r
82 }
83
84 int main(int argc, char **argv)
85 {
86         glutInit(&argc, argv);
87         glutInitWindowSize(128, 128);
88         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
89         glutCreateWindow("timer test");
90
91         glutDisplayFunc(disp);
92
93     /* get timer started, its reset in the timer function itself */
94     glutTimerFunc(1000, timer_func, 1);
95     glutTimerFunc(500, timer_func, 2);
96
97     /* menus for setting timing */
98     subMenuSurround = glutCreateMenu(MenuSurround);\r
99     createMenuEntries(1);\r
100 \r
101     subMenuCenter = glutCreateMenu(MenuCenter);\r
102     createMenuEntries(2);\r
103 \r
104     menuID = glutCreateMenu(MenuSurround);  /* doesn't matter, no clickable entries in this menu */\r
105     glutAddSubMenu("Center", subMenuCenter);\r
106     glutAddSubMenu("Surround", subMenuSurround);\r
107     glutAttachMenu(GLUT_RIGHT_BUTTON);
108
109         glutMainLoop();
110         return 0;
111 }
112
113 void disp(void)
114 {
115         glClearColor(color[cidx][0], color[cidx][1], color[cidx][2], 1);
116         glClear(GL_COLOR_BUFFER_BIT);
117
118     glPointSize(10.f);
119     glColor3f(color[pcidx][0], color[pcidx][1], color[pcidx][2]);
120     glBegin(GL_POINTS);
121         glVertex2i(0,0);
122     glEnd();
123
124         glutSwapBuffers();
125 }
126
127 void timer_func(int which)
128 {
129         /* advance the color index and trigger a redisplay */
130     switch (which)
131     {
132     case 1:
133         cidx = (cidx + 1) % (sizeof color / sizeof *color);
134         break;
135     case 2:
136         pcidx = (pcidx + 1) % (sizeof color / sizeof *color);
137         break;
138     }
139     
140         glutPostRedisplay();
141
142         /* (re)set the timer callback and ask glut to call it in x ms */
143     glutTimerFunc(which == 1 ? timerSurroundInt:timerCenterInt, timer_func, which);
144 }