From 95ac1a61d78e27f456465233c69524a0124b753e Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Fri, 15 Feb 2013 23:51:20 +0000
Subject: [PATCH] [Change] Still cleaning up the place.

  -- Basically just moving things into conf.c to make things cleaner.
  -- Got to get the input out player.c That file is wayyyy too big.
---
 src/conf.c |  60 +++++++++++++++++++++++++++
 src/conf.h |   1 +
 src/main.c | 119 ++++++++++++++++-------------------------------------
 3 files changed, 97 insertions(+), 83 deletions(-)

diff --git a/src/conf.c b/src/conf.c
index 4214737..8949357 100644
--- a/src/conf.c
+++ b/src/conf.c
@@ -1,3 +1,7 @@
+#include <unistd.h>
+#include <string.h>
+#include <getopt.h>
+
 #include "lua.h"
 #include "lauxlib.h"
 #include "lualib.h"
@@ -38,6 +42,21 @@ extern char* namjoystick;
 // From player.c
 extern const char* keybindNames[]; // Keybindings.
 
+static void print_usae(char** argv);
+
+// Print usage.
+static void print_usage(char** argv) {
+  LOG("USAGE: %s [OPTION]", argv[0]);
+  LOG("Options are:");
+  LOG("\t-f,    --fullscreen  - Fullscreen");
+  LOG("\t-F,    --fps         - Limit frames per second");
+  LOG("\t-d s,  --data s      - Set the data file to be s");
+  LOG("\t-j n,  --joystick n  - Use joystick (n)");
+  LOG("\t-J s,  --joystick s  - Use joystick whose name contains (s)");
+  LOG("\t-h     --help        - Display this message and exit.");
+  LOG("\t-v                   - Print the version and exit");
+}
+
 // Set the default configuration.
 void conf_setDefaults(void) {
   // Global.
@@ -139,3 +158,44 @@ int conf_loadConfig(const char* file) {
   return 0;
 }
 
+// Parse some CLI options.
+void conf_parseCLI(int argc, char** argv) {
+  static struct option long_options[] = {
+    { "fullscreen",   no_argument,        0, 'f' },
+    { "fps",          required_argument,  0, 'F' },
+    { "data",         required_argument,  0, 'd' },
+    { "joystick",     required_argument,  0, 'j' },
+    { "Joystick",     required_argument,  0, 'J' },
+    { "help",         no_argument,        0, 'h' },
+    { "version",      no_argument,        0, 'v' },
+    { NULL, 0, 0, 0 }
+  };
+  int option_index = 0;
+  int c = 0;
+
+  while((c = getopt_long(argc, argv, "fF:d:J:j:hv", long_options, &option_index)) != -1) {
+    switch(c) {
+      case 'f':
+        gl_screen.fullscreen = 1;
+        break;
+      case 'F':
+        max_fps = atoi(optarg);
+        break;
+      case 'd':
+        data = strdup(optarg);
+        break;
+      case 'j':
+        indjoystick = atoi(optarg);
+        break;
+      case 'J':
+        namjoystick = strdup(optarg);
+        break;
+      case 'v':
+        LOG(APPNAME": version %d.%d.%d", VMAJOR, VMINOR, VREV);
+      case 'h':
+        print_usage(argv);
+        exit(EXIT_SUCCESS);
+    }
+  }
+}
+
diff --git a/src/conf.h b/src/conf.h
index 2c54f1e..cfd18bf 100644
--- a/src/conf.h
+++ b/src/conf.h
@@ -2,4 +2,5 @@
 
 void conf_setDefaults(void);
 int conf_loadConfig(const char* file);
+void conf_parseCLI(int argc, char** argv);
 
diff --git a/src/main.c b/src/main.c
index 701c68c..005e130 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,7 +1,5 @@
 #include <SDL.h>
-#include <unistd.h>
 #include <string.h>
-#include <getopt.h>
 
 #include "main.h"
 #include "conf.h"
@@ -31,6 +29,7 @@
 #define MINIMUM_FPS     0.5
 #define FONT_SIZE       12
 
+static int space = 1; // Global value, control whether or not player is flying.
 static int quit = 0; // Primary loop.
 static unsigned int time = 0; // Calculate FPS and movement.
 static char version[VERSION_LEN];
@@ -46,26 +45,12 @@ char* namjoystick = NULL;
 
 // Prototypes.
 
-static void print_usage(char** argv);
 static void display_fps(const double dt);
 static void window_caption(void);
 static void data_name(void);
 // Update.
-static void update_all(void);
-static void render_all(void);
-
-// Usage.
-static void print_usage(char** argv) {
-  LOG("USAGE: %s [OPTION]", argv[0]);
-  LOG("Options are:");
-  LOG("\t-f,    --fullscreen  - Fullscreen");
-  LOG("\t-F,    --fps         - Limit frames per second");
-  LOG("\t-d s,  --data s      - Set the data file to be s");
-  LOG("\t-j n,  --joystick n  - Use joystick (n)");
-  LOG("\t-J s,  --joystick s  - Use joystick whose name contains (s)");
-  LOG("\t-h     --help        - Display this message and exit.");
-  LOG("\t-v                   - Print the version and exit");
-}
+static void update_space(void);
+static void render_space(void);
 
 int main(int argc, char** argv) {
 
@@ -79,61 +64,13 @@ int main(int argc, char** argv) {
   // Input must be initialized for config to work.
   input_init();
 
-  // Set the default config values.
-  conf_setDefaults();
+  // Set the configuration.
+  conf_setDefaults(); // Default config values.
+  conf_loadConfig(CONF_FILE); // Have Lua parse config.
+  conf_parseCLI(argc, argv); // Parse CLI arguments.
 
-  // Have Lua parse the config file.
-  conf_loadConfig(CONF_FILE);
-
-  // Parse arguments.
-  static struct option long_options[] = {
-    { "fullscreen",   no_argument,        0, 'f' },
-    { "fps",          required_argument, 0, 'F' },
-    { "data",         required_argument,  0, 'd' },
-    { "joystick",     required_argument,  0, 'j' },
-    { "joystick",     required_argument,  0, 'J' },
-    { "help",         no_argument,        0, 'h' },
-    { "version",      no_argument,        0, 'v' },
-    { NULL, 0, 0, 0 }
-  };
-  int option_index = 0;
-  int c = 0;
-  while((c = getopt_long(argc, argv, "fF:d:J:j:hv", long_options, &option_index)) != -1) {
-    switch(c) {
-      case 'f':
-        gl_screen.fullscreen = 1;
-        break;
-      case 'F':
-        if(optarg != NULL) show_fps = atoi(optarg);
-        else max_fps = !show_fps;
-        break;
-      case 'd':
-        data = strdup(optarg);
-        break;
-      case 'j':
-        indjoystick = atoi(optarg);
-        break;
-      case 'J':
-        namjoystick = strdup(optarg);
-        break;
-      case 'v':
-        LOG(APPNAME": version %d.%d.%d", VMAJOR, VMINOR, VREV);
-      case 'h':
-        print_usage(argv);
-        exit(EXIT_SUCCESS);
-    }
-  }
-  
-  // Check if the data file is valid.
-  if(pack_check(data)) {
-    ERR("Data file '%s'not found", data);
-    WARN("You should specify which data file to use with '-d'");
-    WARN("See -h or --help for more information.");
-    SDL_Quit();
-    exit(EXIT_FAILURE);
-  }
-
-  data_name(); // Loads the data's name and friends.
+  // Load the data basics.
+  data_name();
   LOG(" %s", dataname);
   DEBUG();
   
@@ -182,6 +119,7 @@ int main(int argc, char** argv) {
   if(ai_init())
     WARN("Error initializing AI");
 
+  // Misc openGL init stuff.
   gl_fontInit(NULL, NULL, FONT_SIZE); // Init default font size.
   gui_init(); // Init the GUI crap.
 
@@ -206,10 +144,18 @@ int main(int argc, char** argv) {
     // Event loop.
     while(SDL_PollEvent(&event)) {
       if(event.type == SDL_QUIT) quit = 1; // Handle quit.
-      input_handle(&event); // handles all the events the player keybinds.
+
+      if(space) // Player is flying around happily.
+        input_handle(&event); // handles all the events the player keybinds.
+    }
+
+    if(space) {
+      // Player is flying around.
+      update_space();
+      glClear(GL_COLOR_BUFFER_BIT);
+      render_space();
+      SDL_GL_SwapBuffers();
     }
-    update_all();
-    render_all();
   }
 
   // Unload data.
@@ -229,13 +175,15 @@ int main(int argc, char** argv) {
   joystick_exit();  // Release joystick.
   input_exit();     // Clean up keybindings.
   gl_exit();        // Kills video output.
+  
+  // All is good.
   exit(EXIT_SUCCESS);
 }
 
-// Updates everything.
+// Updates the game.
 static double fps_dt = 1.;
 static double dt = 0.;
-static void update_all(void) {
+static void update_space(void) {
   // dt in ms/1000.
   dt = (double)(SDL_GetTicks() - time) / 1000.;
   time = SDL_GetTicks();
@@ -257,7 +205,7 @@ static void update_all(void) {
   pilots_update(dt);
 }
 
-// == Renders everything. ==================================
+// == Renders the game. ==================================
 // Blitting order. (layers)
 //
 //    BG  | Stars and planets.
@@ -272,9 +220,7 @@ static void update_all(void) {
 //        | Foreground particles.
 //        | Text and GUI.
 // ========================================================
-static void render_all(void) {
-  glClear(GL_COLOR_BUFFER_BIT);
-
+static void render_space(void) {
   // BG.
   space_render(dt);
   planets_render();
@@ -285,8 +231,6 @@ static void render_all(void) {
   // FG.  
   player_render();
   display_fps(dt);
-
-  SDL_GL_SwapBuffers();
 }
 
 
@@ -310,6 +254,15 @@ static void display_fps(const double dt) {
 static void data_name(void) {
   uint32_t bufsize;
   char* buf;
+  
+  // Check if data file is valid.
+  if(pack_check(DATA)) {
+    ERR("Data file '%s' not found", data);
+    WARN("You should specify which data file to use with '-d'");
+    WARN("See -h or --help for more information");
+    SDL_Quit();
+    exit(EXIT_FAILURE);
+  }
 
   // Check the version.
   buf = pack_readfile(DATA, VERSION_FILE, &bufsize);