title: How to close an openGL window
body: <p>Here, I have two loops. The first loop processes the graphics for a few seconds and then the code goes to the second loop. I process the graphical events via <code>glutMainLoopEvent</code> in the first loop. Before the second loop starts, I would like to close the graphic window. It seems the command <code>glutLeaveMainLoop</code> cannot close the window. What other function should I use to force the window closed right after the first loop?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;GL/freeglut.h&gt;
  #include &lt;boost/thread/thread.hpp&gt; // for sleep
  
  void cback_render()
  {
      if(!glutGetWindow())
          return ;
      static float rotations = 0;
  
      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();
  
      glutSwapBuffers();
  
      if (++rotations &gt; 360) rotations -= 360;
  }
  
  void timer(int )
  {
      if(!glutGetWindow())
          return ;
      glutPostRedisplay();
      glutMainLoopEvent();
      glutTimerFunc(30, timer, 1);
  }
  
  int main(int argc, char **argv)
  {
      glutInit(&amp;argc, argv);
      glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
      glutInitWindowPosition(100, 100);
      glutInitWindowSize(512, 512);
      glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
  
      glutCreateWindow("freegluttest");
      glutDisplayFunc (cback_render);
      glutTimerFunc(30, timer, 1);
  
      long i=0;
      while(glutGetWindow() &amp;&amp; i&lt; 30)
      {
          printf("[%ld]\n",i);
          i++;
          glutMainLoopEvent();
          boost::this_thread::sleep( boost::posix_time::milliseconds(100) );
      }
      glutMainLoopEvent();
  
      glutLeaveMainLoop(); // does not work
      glutMainLoopEvent();
  
      // do something else ....
      while(1)
      {
          // other calculations
          boost::this_thread::sleep( boost::posix_time::milliseconds(100) );
      }
  
      return 0;
  }
  </code></pre>