glutCloseFunc without terminating application [closed]

帅比萌擦擦* 提交于 2020-08-05 02:02:06

问题


I have created a window with glutCreateWindow and started a loop using glutMainLoop. I want to end that loop and close the window so I use glutLeaveMainLoop and glutCloseFunc to destroy it. Automatically, my application terminates.

I would like the application to persist after the window is destroyed. Is it possible?

According to this link I can do it, however I don't know how. I'm using freeglut.


回答1:


In 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.

which leads to 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.

And from 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.

Putting the pieces together:

#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;
}


来源:https://stackoverflow.com/questions/36619023/glutclosefunc-without-terminating-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!