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