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)
37 for (int i = 0; i < sizeof(timerInts) / sizeof(*timerInts); i++)
39 char temp[10] = {'\0'};
40 /* flag current value */
41 if ((which == 1 ? timerSurroundInt : timerCenterInt) == timerInts[i])
46 sprintf(temp + 1, " %4d ms", timerInts[i]);
48 glutAddMenuEntry(temp, timerInts[i]);
52 void updateMenuEntries(int which)
54 for (int i = 0; i < sizeof(timerInts) / sizeof(*timerInts); i++)
56 char temp[10] = { '\0' };
57 /* flag current value */
58 if ((which == 1 ? timerSurroundInt : timerCenterInt) == timerInts[i])
63 sprintf(temp + 1, " %4d ms", timerInts[i]);
65 glutChangeToMenuEntry(i+1, temp, timerInts[i]);
69 void MenuSurround(int timerInt)
\r
71 timerSurroundInt = timerInt;
\r
72 glutSetMenu(subMenuSurround);
\r
73 updateMenuEntries(1);
\r
75 void MenuCenter(int timerInt)
\r
77 timerCenterInt = timerInt;
\r
78 glutSetMenu(subMenuCenter);
\r
79 updateMenuEntries(2);
\r
82 int main(int argc, char **argv)
84 glutInit(&argc, argv);
85 glutInitWindowSize(128, 128);
86 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
87 glutCreateWindow("timer test");
89 glutDisplayFunc(disp);
91 /* get timer started, its reset in the timer function itself */
92 glutTimerFunc(1000, timer_func, 1);
93 glutTimerFunc(500, timer_func, 2);
95 /* menus for setting timing */
96 subMenuSurround = glutCreateMenu(MenuSurround);
\r
97 createMenuEntries(1);
\r
99 subMenuCenter = glutCreateMenu(MenuCenter);
\r
100 createMenuEntries(2);
\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);
113 glClearColor(color[cidx][0], color[cidx][1], color[cidx][2], 1);
114 glClear(GL_COLOR_BUFFER_BIT);
117 glColor3f(color[pcidx][0], color[pcidx][1], color[pcidx][2]);
125 void timer_func(int which)
127 /* advance the color index and trigger a redisplay */
131 cidx = (cidx + 1) % (sizeof color / sizeof *color);
134 pcidx = (pcidx + 1) % (sizeof color / sizeof *color);
140 /* (re)set the timer callback and ask glut to call it in x ms */
141 glutTimerFunc(which == 1 ? timerSurroundInt:timerCenterInt, timer_func, which);