Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close GLUT window without terminating of application?

Tags:

c++

qt

glut

I've created an application with Qt Creator (OS Ubuntu 13.04). One function creates a window and draws a graphic using GLUT library, picture is right. But when I try to close window and continue working with my program, it terminates. How can I avoid this?

There is the code of my function:

void plot(int argc, char**argv,.../*other arguments*/)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_ALPHA);
    glutCreateWindow("Green Window");
    //some code
    //...
    glutDisplayFunc( draw );
    glutMainLoop();
}        

Application output prints "... exited with code 0"

like image 699
Alex Koksin Avatar asked Jul 31 '13 13:07

Alex Koksin


3 Answers

If you read e.g. this reference of glutMainLoop you will see that glutMainLoop never returns. That means it will call exit directly instead of returning.

If you're using Qt, then it's able to open windows containing OpenGL contexts, windows compatible with the rest of Qt and which you can close at will.

like image 78
Some programmer dude Avatar answered Oct 18 '22 09:10

Some programmer dude


You might want to switch to freeglut which has implemented the function glutLeaveMainLoop(). As the documentation says:

The glutLeaveMainLoop function causes freeglut to stop its event loop.

Usage

void glutLeaveMainLoop ( void );

Description

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

If the application has two nested calls to glutMainLoop and calls glutLeaveMainLoop, the behaviour of freeglut is undefined. It may leave only the inner nested loop or it may leave both loops. If the reader has a strong preference for one behaviour over the other he should contact the freeglut Programming Consortium and ask for the code to be fixed.

Changes From GLUT

GLUT does not include this function.

Source: http://freeglut.sourceforge.net/docs/api.php

like image 25
Rohit Pruthi Avatar answered Oct 18 '22 07:10

Rohit Pruthi


For FreeGlut. If You want to close only window which has created with GLUT. Look:

int handle = glutCreateWindow("Red square example");
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);

No thanks.

like image 31
OnuchinVA Avatar answered Oct 18 '22 09:10

OnuchinVA