From f63605d6052e6cda688fd0c2711ae8401b8214e0 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Tue, 26 Mar 2013 17:04:42 +0000
Subject: [PATCH] [Change] Some cleanup with loading.

---
 src/lephisto.c | 47 ++++++++++++++++++++++++++++++++---------------
 src/player.c   |  7 ++++++-
 src/space.c    |  6 ++++++
 3 files changed, 44 insertions(+), 16 deletions(-)

diff --git a/src/lephisto.c b/src/lephisto.c
index 4a9ffc3..49d0729 100644
--- a/src/lephisto.c
+++ b/src/lephisto.c
@@ -54,6 +54,8 @@ int indjoystick = -1;
 char* namjoystick = NULL;
 
 // Prototypes.
+static void load_all(void);
+static void unload_all(void);
 void main_loop(void);
 static void display_fps(const double dt);
 static void window_caption(void);
@@ -153,13 +155,7 @@ int main(int argc, char** argv) {
   toolkit_init(); // Init the toolkit.
 
   // Data loading.
-  commodity_load();
-  factions_load();
-  spfx_load();
-  outfit_load();
-  ships_load();
-  fleet_load();
-  space_load();
+  load_all();
 
   menu_main();
 
@@ -180,18 +176,18 @@ int main(int argc, char** argv) {
     main_loop();
   }
 
-  // Unload data.
+  // Clean up some stuff.
   player_cleanup(); // Cleans up the player stuff.
+  gui_free();       // Free up the gui.
   weapon_exit();    // Destroy all active weapons.
   space_exit();     // Clean up the universe!!!
   pilots_free();    // Free the pilots, they where locked up D:
-  gui_free();       // Free up the gui.
-  fleet_free();
-  ships_free();
-  outfit_free();
-  spfx_free();      // Remove the special effects.
-  factions_free();
-  commodity_free();
+  space_exit();     // Cleans up the universe itself.
+
+  // Unload data.
+  unload_all();
+
+  // Cleanup opengl fonts.
   gl_freeFont(NULL);
   gl_freeFont(&gl_smallFont);
 
@@ -208,6 +204,27 @@ int main(int argc, char** argv) {
   exit(EXIT_SUCCESS);
 }
 
+void load_all(void) {
+  // Ordering of these is very important as they are interdependent.
+  commodity_load();
+  factions_load();
+  spfx_load();
+  outfit_load();
+  ships_load();
+  fleet_load();
+  space_load();
+}
+
+void unload_all(void) {
+  // Data unloading - order should not matter, but inverse load_all is good.
+  fleet_free();
+  ships_free();
+  outfit_free();
+  spfx_free();      // Remove the special effects.
+  factions_free();
+  commodity_free();
+}
+
 // Slip main loop from main() for secondary loop hack in toolkit.c.
 void main_loop(void) {
   sound_update(); // Do sound stuff.
diff --git a/src/player.c b/src/player.c
index 10d54bb..bc5f5ae 100644
--- a/src/player.c
+++ b/src/player.c
@@ -138,11 +138,15 @@ void player_destroyed(void);
 
 // Prompt player name.
 void player_new(void) {
+  int i;
   // Let's not seg fault due to a lack of environment.
   player_setFlag(PLAYER_DESTROYED);
   vectnull(&player_cam);
   gl_bindCamera(&player_cam);
 
+  for(i = 0; i < msg_max; i++)
+    memset(msg_stack[i].str, '\0', MSG_SIZE_MAX);
+
   // Cleanup player stuff if we'll be re-creating.
   player_cleanup();
 
@@ -318,11 +322,12 @@ void player_message(const char* fmt, ...) {
   if(fmt == NULL) return; // Message not valid.
 
   // Copy old messages back.
-  for(i = 1; i < msg_max; i++)
+  for(i = 1; i < msg_max; i++) {
     if(msg_stack[msg_max-i-1].str[0] != '\0') {
       strcpy(msg_stack[msg_max-i].str, msg_stack[msg_max-i-1].str);
       msg_stack[msg_max-i].t = msg_stack[msg_max-i-1].t;
     }
+  }
   // Add the new one.
   va_start(ap, fmt);
   vsprintf(msg_stack[0].str, fmt, ap);
diff --git a/src/space.c b/src/space.c
index 3ac1730..3420ab0 100644
--- a/src/space.c
+++ b/src/space.c
@@ -739,6 +739,12 @@ void space_exit(void) {
     free(systems_stack[i].planets);
   }
   free(systems_stack);
+  systems_stack   = NULL;
+  systems_nstack  = 0;
+
   if(stars) free(stars);
+
+  stars   = NULL;
+  nstars  = 0;
 }