c++ - 从 Freeglut/Opengl 2.1 项目获取返回值

标签 c++ opengl glut freeglut

我目前正在使用 C++ 开发 OpenGL 2.1/Freeglut 项目,我需要使用 glutMainLoop() 函数从 glut 循环中获取返回值,而不立即离开当前程序。我现在找到的解决方案是调用 glutLeaveMainLoop() 但它会完全离开程序。

所以这是我的问题,有没有办法直接为任何退出的 Freeglut 函数设置返回值(因为 glut 不允许这样的操作),或者任何其他技术来关闭 glut 窗口而不离开当前程序?

抱歉,如果我的问题看起来很白痴,谢谢阅读。

int MenuStatus; //will be edit during the process

void runMainLoop( int val) //called by glutTimerFunc below
{
    update();
    render();
    glutTimerFunc(1000 / SCREEN_FPS, runMainLoop, val);
    //In this statement, I need the program to leave the glutMainLoop()
    if (MenuStatus > 0)
    glutLeaveMainLoop();
}

int HandleMenu(int ac, char **av)
{
    glutInit(&ac, av);
    glutInitContextVersion(2, 1);
    glutInitDisplayMode(GLUT_DOUBLE);
    glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
    glutCreateWindow("Arcade - OpenGL");
    if (!initGL())
    {
        std::cerr << "Unable to initialize graphics library!" << std::endl;
        return 1;
    }
    glutKeyboardFunc(handleKeys);
    glutDisplayFunc(render);
    //runMainLoop callback
    glutTimerFunc( 1000 / SCREEN_FPS, runMainLoop, 0 );
    glutMainLoop();
    //return statement never reached.
    return MenuStatus;
}

抱歉遗漏了代码,希望它能更容易理解。

最佳答案

在调用 glutMainLoop(); 之前添加这行代码:

glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);

glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);

根据您的需要。

关于c++ - 从 Freeglut/Opengl 2.1 项目获取返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43038647/

相关文章:

C++ 对数字字符串数组进行排序 - 比较函数不起作用

java - 如果不是 GL_QUADS,那又如何呢?

c++ - 片段着色器中的“错误 : sampler arrays indexed with non-constant expressions are forbidden in GLSL 1. 30 及更高版本”

C++:从 openGL 中去除莫尔效应

c++ - 如何将这个多边形旋转到与旋转立方体相同的位置?

c++ - 调整图像大小 OpenCV

c++ - 找不到 Visual Studio 平台 2015 工具集 ='v141'

c - 如何将使用顶点数组的 OpenGL 代码转换为使用顶点缓冲对象的代码?

c++ - 模板参数推导/替换失败 - std::find()

c++ - 如何在透视 View 中绘制文本