每次我右键单击鼠标时都会调用 opengl 菜单

opengl menu called every time I right click the mouse

这是我的简单菜单程序。出于测试目的,我每次 select 菜单时都会打印 "hello" 这个词。但是我不明白为什么我在菜单里选了select一个选项后,每次右击鼠标,"hello"字样又出现了。 我只希望菜单功能执行一次。我该怎么做?

#include <windows.h>
#include <GL/glut.h>
static int window;
static int menu_id;
static int submenu1, submenu2, submenu3, submenu4, submenu5, submenu6, submenu7, submenu8;
static int value = 0;
void menu(int num){
  if(num == 0){
    glutDestroyWindow(window);
    exit(0);
  }else{
    value = num;
  }
  glutPostRedisplay();
}
void createMenu(void){
    submenu1 = glutCreateMenu(menu);
    glutAddMenuEntry("red", 1);
    glutAddMenuEntry("green", 2);
    glutAddMenuEntry("blue", 3);
    glutAddMenuEntry("yellow", 4);
    glutAddMenuEntry("purple", 5);
    glutAddMenuEntry("orange", 6);
    glutAddMenuEntry("white", 7);
    glutAddMenuEntry("black", 8);


    submenu2 = glutCreateMenu(menu);
    glutAddMenuEntry("red", 9);
    glutAddMenuEntry("green", 10);
    glutAddMenuEntry("blue", 11);
    glutAddMenuEntry("yellow", 12);
    glutAddMenuEntry("purple", 13);
    glutAddMenuEntry("orange", 14);
    glutAddMenuEntry("white", 15);
    glutAddMenuEntry("black", 16);


    submenu3 = glutCreateMenu(menu);
    glutAddMenuEntry("red", 17);
    glutAddMenuEntry("green", 18);
    glutAddMenuEntry("blue", 19);
    glutAddMenuEntry("yellow", 20);
    glutAddMenuEntry("purple", 21);
    glutAddMenuEntry("orange", 22);
    glutAddMenuEntry("white", 23);
    glutAddMenuEntry("black", 24);


    submenu4 = glutCreateMenu(menu);
    glutAddMenuEntry("red", 25);
    glutAddMenuEntry("green", 26);
    glutAddMenuEntry("blue", 27);
    glutAddMenuEntry("yellow", 28);
    glutAddMenuEntry("purple", 29);
    glutAddMenuEntry("orange", 30);
    glutAddMenuEntry("white", 31);
    glutAddMenuEntry("black", 32);

    submenu5 = glutCreateMenu(menu);
    glutAddMenuEntry("red", 33);
    glutAddMenuEntry("green", 34);
    glutAddMenuEntry("blue", 35);
    glutAddMenuEntry("yellow", 36);
    glutAddMenuEntry("purple", 37);
    glutAddMenuEntry("orange", 38);
    glutAddMenuEntry("white", 39);
    glutAddMenuEntry("black", 40);


    submenu6 = glutCreateMenu(menu);
    glutAddMenuEntry("red", 41);
    glutAddMenuEntry("green", 42);
    glutAddMenuEntry("blue", 43);
    glutAddMenuEntry("yellow", 44);
    glutAddMenuEntry("purple", 45);
    glutAddMenuEntry("orange", 46);
    glutAddMenuEntry("white", 47);
    glutAddMenuEntry("black", 48);

    submenu7 = glutCreateMenu(menu);
    glutAddSubMenu("filled", submenu1);
    glutAddSubMenu("outline", submenu2);

    submenu8 = glutCreateMenu(menu);
    glutAddSubMenu("filled", submenu3);
    glutAddSubMenu("outline", submenu4);
    menu_id = glutCreateMenu(menu);

    glutAddSubMenu("add rectangle", submenu7);
    glutAddSubMenu("add ellipse", submenu8);
    glutAddSubMenu("add line", submenu5);
    glutAddSubMenu("add bezier curve", submenu6);
    glutAttachMenu(GLUT_LEFT_BUTTON);
}
void display(void){
  glClear(GL_COLOR_BUFFER_BIT);
  if(value == 1){
       printf("hello\n");

  }else if(value == 2){
      printf("hello\n");

  }else if(value == 3){
      printf("hello\n");

  }else if(value == 4){
      printf("hello\n");

  }else if(value == 5){
      printf("hello\n");

  }else if(value == 6){
      printf("hello\n");

  }else if(value == 7){
      printf("hello\n");

  }else if(value == 8){
      printf("hello\n");

  }else if(value == 9){
      printf("hello\n");

  }else if(value == 10){
      printf("hello\n");

  }else if(value == 11){
      printf("hello\n");

  }else if(value == 12){
      printf("hello\n");

  }else if(value == 13){
      printf("hello\n");

  }else if(value == 14){
      printf("hello\n");

  }else if(value == 15){
      printf("hello\n");

  }else if(value == 16){
      printf("hello\n");

  }else if(value == 17){
      printf("hello\n");

  }else if(value == 18){
      printf("hello\n");

  }else if(value == 19){
      printf("hello\n");

  }else if(value == 20){
      printf("hello\n");

  }else if(value == 21){
      printf("hello\n");

  }else if(value == 22){
      printf("hello\n");

  }else if(value == 23){
      printf("hello\n");

  }else if(value == 24){
      printf("hello\n");

  }else if(value == 25){
      printf("hello\n");

  }else if(value == 26){
      printf("hello\n");

  }else if(value == 27){
      printf("hello\n");

  }else if(value == 28){
      printf("hello\n");

  }else if(value == 29){
      printf("hello\n");

  }else if(value == 30){
      printf("hello\n");

  }else if(value == 31){
      printf("hello\n");

  }else if(value == 32){
      printf("hello\n");

  }else if(value == 33){
      printf("hello\n");

  }else if(value == 34){
      printf("hello\n");

  }else if(value == 35){
      printf("hello\n");

  }else if(value == 36){
      printf("hello\n");

  }else if(value == 37){
      printf("hello\n");

  }else if(value == 38){
      printf("hello\n");

  }else if(value == 39){
      printf("hello\n");

  }else if(value == 40){
      printf("hello\n");

  }else if(value == 41){
      printf("hello\n");

  }else if(value == 42){
      printf("hello\n");

  }else if(value == 43){
      printf("hello\n");

  }else if(value == 44){
      printf("hello\n");

  }else if(value == 45){
      printf("hello\n");

  }else if(value == 46){
      printf("hello\n");

  }else if(value == 47){
      printf("hello\n");

  }else if(value == 48){
      printf("hello\n");

  }
  glFlush();
}
int main(int argc, char *argv[]){
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition((glutGet(GLUT_SCREEN_WIDTH)-640)/2,
                       (glutGet(GLUT_SCREEN_HEIGHT)-480)/2);
    window = glutCreateWindow("Project 1");
    createMenu();
    glClearColor(255.0f, 255.0f, 255.0f, 0.0f);
    glutDisplayFunc(display);
    glutMainLoop();
    return EXIT_SUCCESS;
}

value 被设置为一个数字,然后再也不会设置回 0,因此 "Hello" 将继续打印。如果在 printf("hello\n"); 之后放置 value = 0;,您的程序可能会有更好的机会。