+ * Linked list of windows to destroy ... this is so we don't destroy a window from the middle of
+ * its callback. Some C compilers take an extremely dim view of this.
+ */
+
+static SFG_WindowList* WindowsToDestroy = (SFG_WindowList*)NULL ;
+
+/*
+ * Function to add a window to the linked list of windows to destroy. Subwindows are automatically
+ * added because they hang from the window structure.
+ */
+void fgAddToWindowDestroyList ( SFG_Window* window, GLboolean needToClose )
+{
+ SFG_WindowList *new_list_entry = (SFG_WindowList*)malloc ( sizeof(SFG_WindowList) ) ;
+ new_list_entry->window = window ;
+ new_list_entry->needToClose = needToClose ;
+ new_list_entry->next = WindowsToDestroy ;
+ WindowsToDestroy = new_list_entry ;
+
+ /*
+ * Check the execution state. If this has been called from "glutDestroyWindow",
+ * a statement in that function will reset the "ExecState" after this function returns.
+ */
+ if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
+ {
+ /*
+ * Set the execution state flag to drop out of the main loop.
+ */
+ if ( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
+ fgState.ExecState = GLUT_EXEC_STATE_STOP ;
+ }
+}
+
+/*
+ * Function to close down all the windows in the "WindowsToDestroy" list
+ */
+void fgCloseWindows ()
+{
+ fgExecutionState ExecState = fgState.ExecState ;
+
+ SFG_WindowList *window_ptr = WindowsToDestroy ;
+ WindowsToDestroy = (SFG_WindowList*)NULL ; // In case the destroy callbacks cause more windows to be closed
+
+ while ( window_ptr )
+ {
+ SFG_WindowList *next = window_ptr->next ;
+ fgDestroyWindow ( window_ptr->window, window_ptr->needToClose ) ;
+ free ( window_ptr ) ;
+ window_ptr = next ;
+
+ if ( !window_ptr ) window_ptr = WindowsToDestroy ;
+ }
+
+ /*
+ * Since the "fgDestroyWindow" function could easily have set the "ExecState" to stop,
+ * let's set it back to what it was.
+ */
+ fgState.ExecState = ExecState ;
+}
+
+/*
+ * This function destroys a window and all of its subwindows. Actually,