Adding a Menu Instead
It is easy to convert the above program into a menu driven one instead of
using keyboard and mouse routines. Just replace the references to the
mouse function with the following segments of code:
glutCreateMenu(menu); glutAddMenuEntry("Spin the Picture", SPIN); glutAddMenuEntry("Stop Spinning", STOP); glutAddMenuEntry("Quit the Program", QUIT); glutAttachMenu(GLUT_RIGHT_BUTTON);
#define SPIN 1 #define STOP 2 #define QUIT 3 void menu (int item) { switch(item) { case SPIN: glutIdleFunc(spinDisplay); break; case STOP: glutIdleFunc(NULL); break; case QUIT: exit(0); break; } }
void createMenu(void) { int speedMenu = glutCreateMenu(changespeed); glutAddMenuEntry("Faster", 0); glutAddMenuEntry("Slower", 1); glutAddMenuEntry("Reset", 2); glutCreateMenu(menu); glutAddMenuEntry("Spin the Teapot", SPIN); glutAddSubMenu("Change the Speed", speedMenu); glutAddMenuEntry("Stop Spinning", STOP); glutAddMenuEntry("Quit the Program", QUIT); glutAttachMenu(GLUT_RIGHT_BUTTON); }
void changespeed(int item) { switch (item) { case 0: spinspeed += 2.0; break; case 1: spinspeed -= 2.0; //if (spinspeed < 0) spinspeed = 0; break; case 2: spinspeed = 5.0; } }
void menu (int item) { switch(item) { case SPIN: glutIdleFunc(spinDisplay); break; case STOP: glutIdleFunc(NULL); break; case QUIT: exit(0); break; } }