Reopening the FreeGLUT window

438 views Asked by At

I have tried to reopen the FreeGLUT window, after the user closes it.

I do it via two application. One works and the other fails. What is the problem with the one which fails with this error?

freeglut illegal glutInit() reinitialization attempt

main_ok.cpp:

#include "header.h"

int main()
{
    // 1
    init_gui();
    glutMainLoop();

    // 2
    init_gui();
    while(1)
    {
        glutMainLoopEvent();
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }

    return 0;
}

main_fail.cpp:

#include "header.h"

void refresh_gui()
{
    if(glutGetWindow())
        glutPostRedisplay();
    else
        init_gui();

    glutMainLoopEvent();
}

void calculations()
{
    while(1)
    {
        refresh_gui();
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
}


int main()
{
    init_gui();
    calculations();

    return 0;
}

header.h:

#include <chrono>
#include <thread>
#include <GL/freeglut.h>

void timer(int)
{
    if(!glutGetWindow())
        return ;
    glutPostRedisplay();
    glutMainLoopEvent();
    glutTimerFunc(30, timer, 1);
}

void display()
{
    if(!glutGetWindow())
        return ;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}


void init_gui()
{
    int argc=1;
    glutInit(&argc, nullptr);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(512, 512);
    glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);

    glutCreateWindow("freegluttest");
    glutDisplayFunc(display);
    glutTimerFunc(30, timer, 1);
}
0

There are 0 answers