1
\$\begingroup\$

I started learning OpenGL and GLUT by making a snake game. The problem I encountered is that when I press the "new game" in the menu, the window has to be resized so that the content of the window to be updated.

From what I have read, it's because of the MainLoop that's waiting for an event, but I don't know how to fix it. I've tried with glutPostRedisplay, but it doesn't change anything; maybe I'm placing it wrong. Here is the code associated with the restart of the game:

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 );
  init();
  glutReshapeFunc( reshape );
  glutDisplayFunc( dreptunghi );
  glutSpecialFunc( player );
  glutMainLoop();
  return 0;
}

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;
      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 );

      init();
      glutDisplayFunc( dreptunghi );
      glutReshapeFunc( reshape );
      glutSpecialFunc( player );
    }
  }
  glutSwapBuffers();
  //glutPostRedisplay();
}

void createMenu( void ) {
  glutCreateMenu( menu );
  glutAddMenuEntry( "New game!", 1 );
  glutAddMenuEntry( "Exit", 0 );
  glutAttachMenu( GLUT_RIGHT_BUTTON );
}

I have also tried to write a function for the display of the menu, that's called using glutIdleFunc, but it didn't solve anything either.

I have ran out of ideas.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.