From 23e4dc7c9227e520623d492af7a6a874b29f64b7 Mon Sep 17 00:00:00 2001 From: Diederick Niehorster Date: Fri, 17 Jan 2014 16:18:46 +0000 Subject: [PATCH] timer demo now has a menu to: - set flicker tempo - test whether menus can be modified in the callback (there was a regression with this in freeglut 2.8.1 that was perchance already fixed in r1583, but make sure we're testing this from now on) git-svn-id: svn+ssh://svn.code.sf.net/p/freeglut/code/trunk/freeglut/freeglut@1637 7f0cb862-5218-0410-a997-914c9d46530a --- progs/demos/timer/timer.c | 72 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/progs/demos/timer/timer.c b/progs/demos/timer/timer.c index 44fa76c..11557f7 100644 --- a/progs/demos/timer/timer.c +++ b/progs/demos/timer/timer.c @@ -22,6 +22,62 @@ float color[][3] = { {0, 1, 1}, {1, 0, 1} }; +int timerInts[] = { + 250, + 500, + 1000 +}; +int timerSurroundInt = 1000, timerCenterInt = 1000; + +/* menu IDs, creation/update funcs and callback */ +int menuID, subMenuSurround, subMenuCenter; + +void createMenuEntries(int which) +{ + for (int i = 0; i < sizeof(timerInts) / sizeof(*timerInts); i++) + { + char temp[10] = {'\0'}; + /* flag current value */ + if ((which == 1 ? timerSurroundInt : timerCenterInt) == timerInts[i]) + temp[0] = '+'; + else + temp[0] = '-'; + + sprintf(temp + 1, " %4d ms", timerInts[i]); + + glutAddMenuEntry(temp, timerInts[i]); + } +} + +void updateMenuEntries(int which) +{ + for (int i = 0; i < sizeof(timerInts) / sizeof(*timerInts); i++) + { + char temp[10] = { '\0' }; + /* flag current value */ + if ((which == 1 ? timerSurroundInt : timerCenterInt) == timerInts[i]) + temp[0] = '+'; + else + temp[0] = '-'; + + sprintf(temp + 1, " %4d ms", timerInts[i]); + + glutChangeToMenuEntry(i+1, temp, timerInts[i]); + } +} + +void MenuSurround(int timerInt) +{ + timerSurroundInt = timerInt; + glutSetMenu(subMenuSurround); + updateMenuEntries(1); +} +void MenuCenter(int timerInt) +{ + timerCenterInt = timerInt; + glutSetMenu(subMenuCenter); + updateMenuEntries(2); +} int main(int argc, char **argv) { @@ -36,6 +92,18 @@ int main(int argc, char **argv) glutTimerFunc(1000, timer_func, 1); glutTimerFunc(500, timer_func, 2); + /* menus for setting timing */ + subMenuSurround = glutCreateMenu(MenuSurround); + createMenuEntries(1); + + subMenuCenter = glutCreateMenu(MenuCenter); + createMenuEntries(2); + + menuID = glutCreateMenu(MenuSurround); /* doesn't matter, no clickable entries in this menu */ + glutAddSubMenu("Center", subMenuCenter); + glutAddSubMenu("Surround", subMenuSurround); + glutAttachMenu(GLUT_RIGHT_BUTTON); + glutMainLoop(); return 0; } @@ -69,6 +137,6 @@ void timer_func(int which) glutPostRedisplay(); - /* (re)set the timer callback and ask glut to call it in 1 second */ - glutTimerFunc(1000, timer_func, which); + /* (re)set the timer callback and ask glut to call it in x ms */ + glutTimerFunc(which == 1 ? timerSurroundInt:timerCenterInt, timer_func, which); } -- 1.7.10.4