Reputation: 16081
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!
Upvotes: 0
Views: 6896
Reputation: 1
# include<GL/glut.h>
void keys(unsigned char key, int x, int y)
{
if (key == 'a') paused = 1;
if (key == 'A') paused = 0;
glutPostRedisplay();
}
add this function in your program for keyboard function and wherever u are using glutPostRedisplay() in program add if(paused == 0) above it
Upvotes: 0
Reputation: 310
its not working because glutKeyboardFunc(handleKeypress)
is commented out for some reason. uncomment and it should work.
Upvotes: 3
Reputation: 129011
Your program runs in two stages:
Everything before glutMainLoop
is initialization, telling GLUT all the different settings and callbacks you want to use. During the main loop, GLUT will call all your callbacks and you can draw.
The problem is that you're setting paused
during the main loop and checking it during the initialization. Since initialization always happens before the main loop, setting paused
won't actually do anything.
The solution is to not rely on checking paused
during initialization, but instead modify your callbacks to immediately return if paused
is true
.
Upvotes: 2