我目前正在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);
根据您的需求。