【问题标题】:Pausing a c++ GLUT program with keyboard entry使用键盘输入暂停 c++ GLUT 程序
【发布时间】:2013-08-05 01:23:10
【问题描述】:

我试图在执行期间通过按键盘上的一个键来暂停我的 GLUT 程序。它似乎无法识别我的条目。以下是我的代码的相关部分:

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                                                                                                                              
}

我实际上是从这个非常愿意编写的教程中得到了这段代码的骨架:

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

但是,当我按下“p”键时,我似乎无法让程序暂停。如果您有更好的方法,请告诉我!

【问题讨论】:

    标签: c++ opengl event-handling glut keypress


    【解决方案1】:

    它不起作用,因为 glutKeyboardFunc(handleKeypress) 由于某种原因被注释掉了。取消注释,它应该可以工作。

    【讨论】:

    • 大声笑我刚刚在下面的答案上写的评论现在可能很有趣:)
    【解决方案2】:

    您的程序分两个阶段运行:

    • 初始化
    • 主循环

    glutMainLoop 之前的一切都是初始化,告诉 GLUT 你想使用的所有不同的设置和回调。在主循环期间,GLUT 会调用你所有的回调,然后你就可以画图了。

    问题是您在主循环期间设置paused 并在初始化期间检查它。由于初始化总是发生在主循环之前,设置paused实际上不会做任何事情。

    解决方案是在初始化期间不要依赖检查paused,而是修改您的回调以在pausedtrue 时立即返回。

    【讨论】:

    • 我将以下内容放在我的 drawScene() 方法的顶部:if(paused) return; 但它仍然不起作用。我还在 handleKeypress() 方法的 case 'p' 中添加了一个打印语句。而且它从不打印我的声明。
    【解决方案3】:
    # include<GL/glut.h>
    void keys(unsigned char key, int x, int y)    
    {    
        if (key == 'a')     paused = 1;    
        if (key == 'A')     paused = 0;    
        glutPostRedisplay();    
    }
    

    在您的程序中添加此函数以实现键盘功能以及您在程序中使用 glutPostRedisplay() 的任何位置 添加 如果(暂停 == 0) 在它上面

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      • 1970-01-01
      相关资源
      最近更新 更多