我目前正在使用 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/