即使在关闭openGL窗口后仍继续编程

时间:2017-01-02 00:43:57

标签: c opengl glut freeglut

我一直在调用glutMainLoopEvent来处理图形。但是,在有人关闭窗口后,我想退出循环并显示Code reached here.。当窗口关闭时,会调用exit函数并停止整个应用程序。虽然我需要继续申请。我该如何修复代码?

#include <stdio.h>
#include <GL/freeglut.h>

//display function - draws a triangle rotating about the origin
void cback_render()
{
    //keeps track of rotations
    static float rotations = 0;

    //OpenGL stuff for triangle
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();
    glRotatef(rotations, 0, 0, 1);

    glBegin(GL_TRIANGLES);
        glVertex3f(0,0,0);
        glVertex3f(1,0,0);
        glVertex3f(0,1,0);
    glEnd();

    //display on screen
    glutSwapBuffers();

    //rotate triangle a little bit, wrapping around at 360°
    if (++rotations > 360) rotations -= 360;
}

void timer(int value )
{
    glutPostRedisplay();
    glutMainLoopEvent();
    glutTimerFunc(30, timer, 1);
}

int main(int argc, char **argv)
{
    //initialisations
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(512, 512);

    //create window and register display callback
    glutCreateWindow("freegluttest");
    glutDisplayFunc (cback_render);
    glutTimerFunc(30, timer, 1);

    //loop forever
    long i=0;
    while(1)
    {
        printf("[%ld]\n",i);
        i++;
        glutMainLoopEvent();
    }
    printf("Code reached here.");

    return 0;
}

1 个答案:

答案 0 :(得分:2)

使用GLUT_ACTION_ON_WINDOW_CLOSE允许程序在窗口关闭时继续。

glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,
              GLUT_ACTION_GLUTMAINLOOP_RETURNS);

来源:

http://www.lighthouse3d.com/cg-topics/glut-and-freeglut/ http://freeglut.sourceforge.net/docs/api.php