简体   繁体   English

使用键盘输入暂停c ++ GLUT程序

[英]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. 我试图通过按键盘上的键来暂停我的GLUT程序。 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 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. 但是,当我按“ p”键时,似乎无法使程序暂停。 If you have better methods please let me know! 如果您有更好的方法,请告诉我!

its not working because glutKeyboardFunc(handleKeypress) is commented out for some reason. 它不起作用,因为glutKeyboardFunc(handleKeypress)由于某种原因被注释掉了。 uncomment and it should work. 取消注释,它应该可以工作。

Your program runs in two stages: 您的程序分两个阶段运行:

  • Initialization 初始化
  • Main loop 主循环

Everything before glutMainLoop is initialization, telling GLUT all the different settings and callbacks you want to use. glutMainLoop之前的所有事情都是初始化,告诉GLUT您要使用的所有不同设置和回调。 During the main loop, GLUT will call all your callbacks and you can draw. 在主循环中,GLUT将调用您的所有回调,您可以进行绘制。

The problem is that you're setting paused during the main loop and checking it during the initialization. 问题在于您在主循环中设置了paused状态,并在初始化期间对其进行了检查。 Since initialization always happens before the main loop, setting paused won't actually do anything. 由于初始化总是在主循环之前进行,因此设置paused实际上不会做任何事情。

The solution is to not rely on checking paused during initialization, but instead modify your callbacks to immediately return if paused is true . 解决方案是不依赖初始化期间检查paused ,而是修改您的回调以在pausedtrue立即返回。

# 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 在您的程序中为键盘功能添加此功能,并且无论您在程序中使用glutPostRedisplay()的哪个位置,在其上方添加if(paused == 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM