I wrote a c++ snake game using OpenGL and GLUT. The problem is that I implemented a small menu, with 2 buttons: new game, exit. I'm having a hard time with the "new game" part. I've been moving here and there lines of code and I don't know how it should be. Main looks like this:
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Aarghhh! O ramaaa !");
createMenu();
glClearColor(0.0, 0.0, 0.0, 0.0);
glutIdleFunc(display_menu);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(dreptunghi);
glutSpecialFunc(player);
glutMainLoop();
return 0;
}
where init, createMenu and display_menu look like this:
void init(void) {
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 800.0, 0.0, 600.0);
glShadeModel(GL_FLAT);
}
void menu(int num) {
if (num == 0)
exit(0);
else {
if (num == 1) {
menu_value = num;
}
}
//glutPostRedisplay();
}
void createMenu(void) {
glutCreateMenu(menu);
glutAddMenuEntry("New game!", 1);
glutAddMenuEntry("Exit", 0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void display_menu(void){
if (menu_value == 1) {
snake.clear();
i = 30.0;
j = 30.0;
alpha = 1.0;
value = -1;
speed = 3;
eaten = true;
collided_food = false;
collided_self = false;
createMenu();
glClearColor(0.0, 0.0, 0.0, 0.0);
glutIdleFunc(display_menu);
init();
glutDisplayFunc(dreptunghi);
glutReshapeFunc(reshape);
glutSpecialFunc(player);
//glutMainLoop();
}
glutSwapBuffers();
}
The thing is: if I place the content of display_menu inside the if statement from menu function, it works, but I have to resize the window for the redrawing to happen (I realised that this happens because MainLoop expects an event, but I have no idea how to beat this). If I keep it this way, it doesn't change anything to good, only to worse. I'm new to this and I'm having troubles finding out how this works.
If anybody has the same problem, this is how I solved it:
void menu(int num) {
if (num == 0) {
if (engine)
engine->drop();
exit(0);
}
else {
if (num == 1) {
menu_value = num;
snake.clear();
i = 30.0;
j = 30.0;
nou.set(i, j);
snake.push_back(nou);
alpha = 1.0;
value = -1;
speed = 3;
eaten = true;
collided_food = false;
collided_self = false;
createMenu();
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearColor(1.0, 1.0, 1.0, 0.0);
glutDisplayFunc(dreptunghi);
glutReshapeFunc(reshape);
glutSpecialFunc(player);
}
}
glutSwapBuffers();
glutPostRedisplay();
}
while the init() function remains intact.