3 * Written by John Tsiombikas <nuclear@member.fsf.org>
5 * Demonstrate the use of glutTimerFunc, by changing the color of the
6 * framebuffer every (approximately) 1 sec.
12 void timer_func(int unused);
14 /* color index will be advanced every time the timer expires */
30 int timerSurroundInt = 1000, timerCenterInt = 1000;
32 /* menu IDs, creation/update funcs and callback */
33 int menuID, subMenuSurround, subMenuCenter;
35 void createMenuEntries(int which)
38 for (i = 0; i < sizeof(timerInts) / sizeof(*timerInts); i++)
40 char temp[10] = {'\0'};
41 /* flag current value */
42 if ((which == 1 ? timerSurroundInt : timerCenterInt) == timerInts[i])
47 sprintf(temp + 1, " %4d ms", timerInts[i]);
49 glutAddMenuEntry(temp, timerInts[i]);
53 void updateMenuEntries(int which)
56 for (i = 0; i < sizeof(timerInts) / sizeof(*timerInts); i++)
58 char temp[10] = { '\0' };
59 /* flag current value */
60 if ((which == 1 ? timerSurroundInt : timerCenterInt) == timerInts[i])
65 sprintf(temp + 1, " %4d ms", timerInts[i]);
67 glutChangeToMenuEntry(i+1, temp, timerInts[i]);
71 void MenuSurround(int timerInt)
\r
73 timerSurroundInt = timerInt;
\r
74 glutSetMenu(subMenuSurround);
\r
75 updateMenuEntries(1);
\r
77 void MenuCenter(int timerInt)
\r
79 timerCenterInt = timerInt;
\r
80 glutSetMenu(subMenuCenter);
\r
81 updateMenuEntries(2);
\r
84 int main(int argc, char **argv)
86 glutInit(&argc, argv);
87 glutInitWindowSize(128, 128);
88 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
89 glutCreateWindow("timer test");
91 glutDisplayFunc(disp);
93 /* get timer started, its reset in the timer function itself */
94 glutTimerFunc(1000, timer_func, 1);
95 glutTimerFunc(500, timer_func, 2);
97 /* menus for setting timing */
98 subMenuSurround = glutCreateMenu(MenuSurround);
\r
99 createMenuEntries(1);
\r
101 subMenuCenter = glutCreateMenu(MenuCenter);
\r
102 createMenuEntries(2);
\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);
115 glClearColor(color[cidx][0], color[cidx][1], color[cidx][2], 1);
116 glClear(GL_COLOR_BUFFER_BIT);
119 glColor3f(color[pcidx][0], color[pcidx][1], color[pcidx][2]);
127 void timer_func(int which)
129 /* advance the color index and trigger a redisplay */
133 cidx = (cidx + 1) % (sizeof color / sizeof *color);
136 pcidx = (pcidx + 1) % (sizeof color / sizeof *color);
142 /* (re)set the timer callback and ask glut to call it in x ms */
143 glutTimerFunc(which == 1 ? timerSurroundInt:timerCenterInt, timer_func, which);