timer.c 3.3 KB

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