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

时间:2015-10-25 20:53:28

标签: c opengl glut freeglut

这是我的简单菜单程序。出于测试目的,我打印单词" 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;
}

1 个答案:

答案 0 :(得分:0)

value被设置为一个数字,然后永远不会被设置为0,因此将继续打印“Hello”。如果在printf("hello\n");之后,您放置了value = 0;,那么您的计划可能会有更好的机会。