0

I am currently working on an OpenGL 2.1/Freeglut project in C++ and I need to get a return value from a glut loop using the glutMainLoop() function without leaving immediately the current program. The solution I've found by now is to call the glutLeaveMainLoop() but it's leaving the program entirely.

So here's my question, is there any way to set directly a return value to any exiting Freeglut function (since glut doesn't allow actions like that), or any other technics to close the glut window without leaving the current program ?

Sorry if my question can seems idiot and thanks for reading.

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

Sorry about the missing code, hope it will be more understandable.

vallentin
  • 23,478
  • 6
  • 59
  • 81
  • Hi Lezard, Welcome to stackoverflow, Please share your code snippet what you have tried so far and what error you are facing. – 2787184 Mar 27 '17 at 05:51
  • @ravi Thank you it's done, hope it's more clear now. –  Mar 27 '17 at 06:09
  • Is there a reason you need to leave the main loop? What are you overall trying to accomplish? – vallentin Mar 27 '17 at 06:15
  • Actually yes, I need an int as a return value since this OpenGL part is from a bigger program that needs to handle multiple graphic library. –  Mar 27 '17 at 06:18

1 Answers1

2

Add this line of code before you call glutMainLoop();:

glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);

or

glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);

depending on your needs.

vallentin
  • 23,478
  • 6
  • 59
  • 81
Jim Buck
  • 20,482
  • 11
  • 57
  • 74