From dd5cbe614b7cf0300fb163c9aa7ddacecb517f59 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Sat, 2 Feb 2013 00:16:06 +0000
Subject: [PATCH] [Add] Lua dependancy.   -- Lua parses config file.

---
 bin/Makefile | 12 ++++++++++--
 src/main.c   | 34 ++++++++++++++++++++++++++++++++--
 src/opengl.c | 46 ++++++++++++++++++++++++----------------------
 3 files changed, 66 insertions(+), 26 deletions(-)

diff --git a/bin/Makefile b/bin/Makefile
index 4cd4473..5d51494 100644
--- a/bin/Makefile
+++ b/bin/Makefile
@@ -9,14 +9,22 @@ OBJS := $(OBJS:../src/%.c=../src/%.o)
 DATA = data
 DATAFILES = $(shell find ../gfx/ ../dat/ -name '*.png' -o -name '*.xml' -print)
 
-CFLAGS = -Wall `sdl-config --cflags` `xml2-config --cflags` $(VERSION)
+CLUA = -I/usr/include/lua5.1
+CSDL = `sdl-config --cflags`
+CXML = `xml2-config --cflags`
+CGL  =
+CFLAGS = -Wall $(CLUA) $(CSDL) $(CXML) $(CGL) $(VERSION)
 ifdef DEBUG
 CFLAGS += -g3 -DDEBUG
 else
 CFLAGS += -O2
 endif
 
-LDFLAGS = -lm `sdl-config --libs` `xml2-config --libs` -lSDL_image -lGL
+LDLUA = -llua5.1
+LDSDL = `sdl-config --libs` -lSDL_image
+LDXML = `xml2-config --libs`
+LDGL	= -lGL
+LDFLAGS = -lm $(LDLUA) $(LDSDL) $(LDXML) $(LDGL)
 
 %.o: ../src/%.c
 	@gcc -c $(CFLAGS) -o $@ $<
diff --git a/src/main.c b/src/main.c
index a77c1bd..3a80964 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,4 +1,7 @@
 #include <SDL.h>
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
 #include <unistd.h>
 #include <string.h>
 
@@ -14,12 +17,16 @@
 #include "rng.h"
 #include "pilot.h"
 
+#define CONF_FILE "conf"
+
 static int quit = 0;
 
 static unsigned int time = 0;
 
 // Prototypes.
 
+void print_usage(char** argv);
+
 // Update.
 static void update_all(void);
 
@@ -37,8 +44,6 @@ void print_usage(char** argv) {
 }
 
 int main(int argc, char** argv) {
-  SDL_Event event;
-
   // Default values..
   gl_screen.w = 800;
   gl_screen.h = 640;
@@ -47,6 +52,30 @@ int main(int argc, char** argv) {
   int indjoystick = -1;
   char* namjoystick = NULL;
 
+  // Use Lua to parse configuration file.
+  lua_State* L = luaL_newstate();
+  if(luaL_dofile(L, CONF_FILE) == 0) {
+    // OpenGL.
+    lua_getglobal(L, "width");
+    if(lua_isnumber(L, -1))
+      gl_screen.w = (int)lua_tonumber(L, -1);
+    lua_getglobal(L, "height");
+    if(lua_isnumber(L, -1))
+      gl_screen.h = (int)lua_tonumber(L, -1);
+    lua_getglobal(L, "fullscreen");
+    if(lua_isnumber(L, -1))
+      if((int)lua_tonumber(L, -1) == 1)
+        gl_screen.fullscreen = 1;
+
+    // Joystick.
+    lua_getglobal(L, "joystick");
+    if(lua_isnumber(L, -1))
+      indjoystick = (int)lua_tonumber(L, -1);
+    else if(lua_isstring(L, -1))
+      namjoystick = strdup((char*)lua_tostring(L, -1));
+  }
+  lua_close(L);
+
   // Parse arguments.
   int c = 0;
   while((c = getopt(argc, argv, "fJ:j:hv")) != -1) {
@@ -104,6 +133,7 @@ int main(int argc, char** argv) {
   time = SDL_GetTicks();
 
   // Main looops.
+  SDL_Event event;
   while(!quit) {
     // Event loop.
     while(SDL_PollEvent(&event)) {
diff --git a/src/opengl.c b/src/opengl.c
index c880447..31f6468 100644
--- a/src/opengl.c
+++ b/src/opengl.c
@@ -272,30 +272,32 @@ int gl_init(void) {
   //SDL_ShowCursor(SDL_DISABLE);
 
   // Get available fullscreen modes.
-  modes = SDL_ListModes(NULL, SDL_OPENGL | SDL_FULLSCREEN);
-  if(modes == NULL) {
-    WARN("No fullscreen modes available");
-    if(flags & SDL_FULLSCREEN) {
-      WARN("Disabling fullscreen mode");
-      flags ^= SDL_FULLSCREEN;
+  if(gl_screen.fullscreen) {
+    modes = SDL_ListModes(NULL, SDL_OPENGL | SDL_FULLSCREEN);
+    if(modes == NULL) {
+      WARN("No fullscreen modes available");
+      if(flags & SDL_FULLSCREEN) {
+        WARN("Disabling fullscreen mode");
+        flags ^= SDL_FULLSCREEN;
+      }
     }
-  }
-  else if(modes == (SDL_Rect**) -1)
-    DEBUG("All fullscreen modes available.");
-  else {
-    DEBUG("Available fullscreen modes:");
-    for(i = 0; modes[i]; ++i) {
-      DEBUG("\t\t%d x %d", modes[i]->w, modes[i]->h);
-      if(flags & SDL_FULLSCREEN && modes[i]->w == gl_screen.w && modes[i]->h == gl_screen.h)
-        supported = 1;
+    else if(modes == (SDL_Rect**)-1)
+      DEBUG("All fullscreen modes available");
+    else {
+      DEBUG("Available fullscreen modes:");
+      for(i = 0; modes[i]; ++i) {
+        DEBUG("\t%dx%d", modes[i]->w, modes[i]->h);
+        if(flags & SDL_FULLSCREEN && modes[i]->w == gl_screen.w && modes[i]->h == gl_screen.h)
+          supported = 1;
+      }
+    }
+    // Make sure fullscreen mode is supported.
+    if(flags & SDL_FULLSCREEN && !supported) {
+      WARN("Fullscreen mode %dx%d is not supported by your current setup, switching to another mode",
+            gl_screen.w, gl_screen.h);
+      gl_screen.w = modes[0]->w;
+      gl_screen.h = modes[0]->h;
     }
-  }
-  // Make sure fullscreen mode is supported.
-  if((flags & SDL_FULLSCREEN) && (!supported)) {
-    WARN("Fullscreen mode %d x %d is not supported by your current setup, switching to another mode.",
-          gl_screen.w, gl_screen.h);
-    gl_screen.w = modes[0]->w;
-    gl_screen.h = modes[0]->h;
   }
 
   // Test the setup.