Search code examples
c++openglevent-handlingglutkeypress

Pausing a c++ GLUT program with keyboard entry


I am trying to pause my GLUT program during its execution by pressing a key on the keyboard. It seems to not recognize my entry. Here are the relevant sections of my code:

static bool paused = false;
void handleKeypress(unsigned char key, //The key that was pressed                                                                                                           
                                        int x, int y) {    //The current mouse coordinates                                                                                  
        switch (key) {
                case 27: //Escape key                                                                                                                                       
                  exit(0); //Exit the program                                                                                                                               
                case 'p':
                  paused = !paused;
                  break;
        }
}

int main(int argc, char** argv) {
        //Initialize GLUT                                                                                                                                                   
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
        glutInitWindowSize(600, 400); //Set the window size                                                                                                                 

        //Create the window                                                                                                                                                 
        glutCreateWindow("Fractals in Motion");
        initRendering(); //Initialize rendering                                                                                                                             

        //Set handler functions for drawing, keypresses, and window resizes                                                                                                 
        if(!paused)
          {
            glutDisplayFunc(drawScene);       //drawScene draws everything does the real work
            glutTimerFunc(10, update, 0); //Add a timer                                                                                                                     
          }
        //glutKeyboardFunc(handleKeypress);                                                                                                                                 
        glutReshapeFunc(handleResize);

        glutMainLoop(); //Start the main loop.  glutMainLoop doesn't return.                                                                                                
        return 0; //This line is never reached                                                                                                                              
}

I actually got the skeleton of this code from this very will written tutorial:

http://www.videotutorialsrock.com/opengl_tutorial/basic_shapes/home.php

However, I cannot seem to get the program to pause when I press the 'p' key. If you have better methods please let me know!


Solution

  • its not working because glutKeyboardFunc(handleKeypress) is commented out for some reason. uncomment and it should work.