不终止应用程序的 glutCloseFunc

glutCloseFunc without terminating application

我用 glutCreateWindow 创建了一个 window 并使用 glutMainLoop 开始了一个循环。我想结束那个循环并关闭 window 所以我使用 glutLeaveMainLoopglutCloseFunc 来销毁它。我的应用程序自动终止。

我希望应用程序在 window 被销毁后继续存在。可能吗?

根据这个link我可以做到,但我不知道怎么做。我正在使用 freeglut.

the doc for glutCloseFunc()中:

Users looking to prevent FreeGLUT from exiting when a window is closed, should look into using glutSetOption to set GLUT_ACTION_ON_WINDOW_CLOSE.

这导致 the glutSetOption() docs:

GLUT_ACTION_ON_WINDOW_CLOSE - Controls what happens when a window is closed by the user or system:

  • GLUT_ACTION_EXIT will immediately exit the application (default, GLUT's behavior).
  • GLUT_ACTION_GLUTMAINLOOP_RETURNS will immediately return from the main loop.
  • GLUT_ACTION_CONTINUE_EXECUTION will continue execution of remaining windows.

并且来自 glutLeaveMainLoop()

The glutLeaveMainLoop function causes freeglut to stop the event loop. If the GLUT_ACTION_ON_WINDOW_CLOSE option has been set to GLUT_ACTION_GLUTMAINLOOP_RETURNS or GLUT_ACTION_CONTINUE_EXECUTION, control will return to the function which called glutMainLoop; otherwise the application will exit.

拼凑:

#include <GL/freeglut.h>
#include <iostream>

void display()
{
    glClear( GL_COLOR_BUFFER_BIT );
    glutSwapBuffers();
}

int main( int argc, char** argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
    glutCreateWindow( "GLUT" );
    glutDisplayFunc( display );
    glutSetOption( GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS ); 
    std::cout << "Before glutMainLoop()!" << std::endl;
    glutMainLoop();
    std::cout << "Back in main()!" << std::endl;
    return 0;
}