Added FREEGLUT_REPLACE_GLUT configure option for MS Windows.
[freeglut] / progs / demos / timer / timer.c
index d9bd89b..5119870 100644 (file)
@@ -12,7 +12,8 @@ void disp(void);
 void timer_func(int unused);
 
 /* color index will be advanced every time the timer expires */
-int cidx;
+int cidx = 0;
+int pcidx = 2;
 float color[][3] = {
        {1, 0, 0},
        {0, 1, 0},
@@ -21,6 +22,64 @@ 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)
+{
+       int i;
+    for (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)
+{
+       int i;
+    for (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]);
+    }
+}
+\r
+void MenuSurround(int timerInt)\r
+{\r
+    timerSurroundInt = timerInt;\r
+    glutSetMenu(subMenuSurround);\r
+    updateMenuEntries(1);\r
+}
+void MenuCenter(int timerInt)\r
+{\r
+    timerCenterInt = timerInt;\r
+    glutSetMenu(subMenuCenter);\r
+    updateMenuEntries(2);\r
+}
 
 int main(int argc, char **argv)
 {
@@ -31,6 +90,22 @@ int main(int argc, char **argv)
 
        glutDisplayFunc(disp);
 
+    /* get timer started, its reset in the timer function itself */
+    glutTimerFunc(1000, timer_func, 1);
+    glutTimerFunc(500, timer_func, 2);
+
+    /* menus for setting timing */
+    subMenuSurround = glutCreateMenu(MenuSurround);\r
+    createMenuEntries(1);\r
+\r
+    subMenuCenter = glutCreateMenu(MenuCenter);\r
+    createMenuEntries(2);\r
+\r
+    menuID = glutCreateMenu(MenuSurround);  /* doesn't matter, no clickable entries in this menu */\r
+    glutAddSubMenu("Center", subMenuCenter);\r
+    glutAddSubMenu("Surround", subMenuSurround);\r
+    glutAttachMenu(GLUT_RIGHT_BUTTON);
+
        glutMainLoop();
        return 0;
 }
@@ -40,14 +115,30 @@ void disp(void)
        glClearColor(color[cidx][0], color[cidx][1], color[cidx][2], 1);
        glClear(GL_COLOR_BUFFER_BIT);
 
-       /* set the timer callback and ask glut to call it in 1 second */
-       glutTimerFunc(1000, timer_func, 0);
+    glPointSize(10.f);
+    glColor3f(color[pcidx][0], color[pcidx][1], color[pcidx][2]);
+    glBegin(GL_POINTS);
+        glVertex2i(0,0);
+    glEnd();
+
        glutSwapBuffers();
 }
 
-void timer_func(int unused)
+void timer_func(int which)
 {
        /* advance the color index and trigger a redisplay */
-       cidx = (cidx + 1) % (sizeof color / sizeof *color);
+    switch (which)
+    {
+    case 1:
+        cidx = (cidx + 1) % (sizeof color / sizeof *color);
+        break;
+    case 2:
+        pcidx = (pcidx + 1) % (sizeof color / sizeof *color);
+        break;
+    }
+    
        glutPostRedisplay();
+
+       /* (re)set the timer callback and ask glut to call it in x ms */
+    glutTimerFunc(which == 1 ? timerSurroundInt:timerCenterInt, timer_func, which);
 }