我试图在用户关闭后重新打开FreeGLUT窗口。
我是通过两个应用程序来完成的。一个工作,另一个工作失败。出现此错误的问题是什么问题?
freeglut非法glutInit()重新初始化尝试
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);
}