2017-01-02 157 views
1

我一直呼叫glutMainLoopEvent來處理圖形。但是,在某人關閉窗口後,我想退出該循環並顯示Code reached here.。看起來當一個窗口關閉時,一個exit函數被調用並且整個應用程序停止。雖然我需要應用程序繼續。我應該如何修復代碼?即使在openGL窗口關閉後繼續執行程序

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

回答

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

+0

如何檢測窗口被關閉,停止'while'循環? – ar2015

+0

也許你可以使用glutGetWindow()來查看窗口是否存在。如果該窗口不存在,它將返回0。 – Schomes

+0

請看:https://www.opengl.org/resources/libraries/glut/spec3/node18.html – Schomes