[Add] Split configuration processing into seperate files, namely conf.*
-- Managed to clean up main.c a little.
This commit is contained in:
parent
334730287c
commit
2d797b9d52
141
src/conf.c
Normal file
141
src/conf.c
Normal file
@ -0,0 +1,141 @@
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
#include "main.h"
|
||||
#include "log.h"
|
||||
#include "player.h"
|
||||
#include "opengl.h"
|
||||
#include "conf.h"
|
||||
|
||||
#define conf_loadInt(n,i) \
|
||||
lua_getglobal(L,n); \
|
||||
if(lua_isnumber(L, -1)) { \
|
||||
i = (int)lua_tonumber(L, -1); \
|
||||
lua_remove(L, -1); \
|
||||
}
|
||||
|
||||
#define conf_loadBool(n,b) \
|
||||
lua_getglobal(L,n); \
|
||||
if(lua_isnumber(L, -1)) \
|
||||
if((int)lua_tonumber(L, -1) == 1) { \
|
||||
b = 1; \
|
||||
lua_remove(L, -1); \
|
||||
} \
|
||||
|
||||
#define conf_loadString(n,s) \
|
||||
lua_getglobal(L,n); \
|
||||
if(lua_isstring(L, -1)) { \
|
||||
s = strdup((char*)lua_tostring(L, -1)); \
|
||||
lua_remove(L, -1); \
|
||||
}
|
||||
|
||||
// Some crap from main.
|
||||
extern int show_fps;
|
||||
extern int max_fps;
|
||||
extern int indjoystick;
|
||||
extern char* namjoystick;
|
||||
// From player.c
|
||||
extern const char* keybindNames[]; // Keybindings.
|
||||
|
||||
// Set the default configuration.
|
||||
void conf_setDefaults(void) {
|
||||
// Global.
|
||||
data = DATA_DEF;
|
||||
// GL.
|
||||
gl_screen.w = 800;
|
||||
gl_screen.h = 640;
|
||||
gl_screen.fullscreen = 0;
|
||||
// Joystick.
|
||||
indjoystick = -1;
|
||||
namjoystick = NULL;
|
||||
// Input.
|
||||
input_setDefault();
|
||||
}
|
||||
|
||||
// Ok.. Parse a config file plox.
|
||||
int conf_loadConfig(const char* file) {
|
||||
int i;
|
||||
|
||||
lua_State* L = luaL_newstate();
|
||||
if(luaL_dofile(L, file) == 0) {
|
||||
// Conf file exists indeed.
|
||||
// Global.
|
||||
conf_loadString("data", data);
|
||||
|
||||
// OpenGL properties..
|
||||
conf_loadInt("width", gl_screen.w);
|
||||
conf_loadInt("height", gl_screen.h);
|
||||
conf_loadBool("fullscreen", gl_screen.fullscreen);
|
||||
conf_loadBool("showfps", show_fps);
|
||||
conf_loadInt("maxfps", max_fps);
|
||||
|
||||
// Joystick.
|
||||
lua_getglobal(L, "joystick");
|
||||
if(lua_isnumber(L, -1)) {
|
||||
indjoystick = (int)lua_tonumber(L, -1);
|
||||
lua_remove(L, -1);
|
||||
}
|
||||
else if(lua_isstring(L, -1)) {
|
||||
namjoystick = strdup((char*)lua_tostring(L, -1));
|
||||
lua_remove(L, -1);
|
||||
}
|
||||
|
||||
// If there are any keybindings. Grab them.
|
||||
char* str;
|
||||
int type, key, reverse;
|
||||
for(i = 0; strcmp(keybindNames[i], "end"); i++) {
|
||||
lua_getglobal(L, keybindNames[i]);
|
||||
str = NULL;
|
||||
key = -1;
|
||||
reverse = 0;
|
||||
if(lua_istable(L, -1)) {
|
||||
// It's a gawd damn table!!
|
||||
lua_pushstring(L, "type");
|
||||
lua_gettable(L, -2);
|
||||
if(lua_isstring(L, -1))
|
||||
str = (char*)lua_tostring(L, -1);
|
||||
|
||||
// Get the key.
|
||||
lua_pushstring(L, "key");
|
||||
lua_gettable(L, -3);
|
||||
if(lua_isnumber(L, -1))
|
||||
key = (int)lua_tonumber(L, -1);
|
||||
|
||||
// Is it reversed? Only used for axis.
|
||||
lua_pushstring(L, "reverse");
|
||||
lua_gettable(L, -4);
|
||||
if(lua_isnumber(L, -1))
|
||||
reverse = 1;
|
||||
|
||||
if(key != -1 && str != NULL) {
|
||||
// Then the keybind is valid. Get the type.
|
||||
if(strcmp(str, "null")==0) type = KEYBIND_NULL;
|
||||
else if(strcmp(str, "keyboard")==0) type = KEYBIND_KEYBOARD;
|
||||
else if(strcmp(str, "jaxis")==0) type = KEYBIND_JAXIS;
|
||||
else if(strcmp(str, "jbutton")==0) type = KEYBIND_JBUTTON;
|
||||
else {
|
||||
WARN("Unknown keybinding of type %s", str);
|
||||
continue;
|
||||
}
|
||||
// Set the keybind.
|
||||
input_setKeybind((char*)keybindNames[i], type, key, reverse);
|
||||
} else
|
||||
WARN("Malformed keybind in %s", file);
|
||||
|
||||
// Clean up after table crap.
|
||||
lua_remove(L,-1);
|
||||
lua_remove(L,-1);
|
||||
lua_remove(L,-1);
|
||||
lua_remove(L,-1);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Failed to load the config file..
|
||||
DEBUG("Config file '%s' not found.", file);
|
||||
return 1;
|
||||
}
|
||||
lua_close(L);
|
||||
return 0;
|
||||
}
|
||||
|
5
src/conf.h
Normal file
5
src/conf.h
Normal file
@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
void conf_setDefaults(void);
|
||||
int conf_loadConfig(const char* file);
|
||||
|
145
src/main.c
145
src/main.c
@ -1,12 +1,10 @@
|
||||
#include <SDL.h>
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#include "main.h"
|
||||
#include "conf.h"
|
||||
#include "log.h"
|
||||
#include "physics.h"
|
||||
#include "opengl.h"
|
||||
@ -28,22 +26,23 @@
|
||||
#define START_DATA "../dat/start.xml"
|
||||
|
||||
#define CONF_FILE "conf"
|
||||
#define VERSION_FILE "VERSION"
|
||||
#define VERSION_LEN 10
|
||||
#define MINIMUM_FPS 0.5
|
||||
|
||||
#define FONT_SIZE 12
|
||||
|
||||
extern const char* keybindNames[]; // Keybindings.
|
||||
|
||||
static int quit = 0; // Primary loop.
|
||||
static unsigned int time = 0; // Calculate FPS and movement.
|
||||
static char version[VERSION_LEN];
|
||||
|
||||
// Just some default crap.
|
||||
#define DATA_DEF "data" // Default data pack file.
|
||||
#define DATA_NAME_LEN 25 // Max length of data name.
|
||||
char* data = NULL;
|
||||
char dataname[DATA_NAME_LEN];
|
||||
static int show_fps = 1; // Default - True.
|
||||
static int max_fps = 0;
|
||||
int show_fps = 1; // Default - True.
|
||||
int max_fps = 0;
|
||||
int indjoystick = -1;
|
||||
char* namjoystick = NULL;
|
||||
|
||||
// Prototypes.
|
||||
|
||||
@ -69,121 +68,22 @@ static void print_usage(char** argv) {
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
int i;
|
||||
|
||||
// Print the version.
|
||||
LOG(""APPNAME" v%d.%d.%d", VMAJOR, VMINOR, VREV);
|
||||
snprintf(version, VERSION_LEN, "%d.%d.%d", VMAJOR, VMINOR, VREV);
|
||||
LOG(" "APPNAME" v%s", version);
|
||||
|
||||
// Initialize SDL for possible warnings.
|
||||
SDL_Init(0);
|
||||
// Default values..
|
||||
// Global.
|
||||
data = DATA_DEF;
|
||||
gl_screen.w = 800;
|
||||
gl_screen.h = 640;
|
||||
gl_screen.fullscreen = 0;
|
||||
// Joystick.
|
||||
int indjoystick = -1;
|
||||
char* namjoystick = NULL;
|
||||
|
||||
// input.
|
||||
// Input must be initialized for config to work.
|
||||
input_init();
|
||||
input_setDefault(); // Call input crap from player.c
|
||||
|
||||
// Use Lua to parse configuration file.
|
||||
lua_State* L = luaL_newstate();
|
||||
if(luaL_dofile(L, CONF_FILE) == 0) { // Conf file exists.
|
||||
// Global.
|
||||
lua_getglobal(L, "data");
|
||||
if(lua_isstring(L, -1)) {
|
||||
data = strdup((char*)lua_tostring(L, -1));
|
||||
lua_remove(L, -1);
|
||||
}
|
||||
// OpenGL properties.
|
||||
lua_getglobal(L, "width");
|
||||
if(lua_isnumber(L, -1)) {
|
||||
gl_screen.w = (int)lua_tonumber(L, -1);
|
||||
lua_remove(L, -1);
|
||||
}
|
||||
lua_getglobal(L, "height");
|
||||
if(lua_isnumber(L, -1)) {
|
||||
gl_screen.h = (int)lua_tonumber(L, -1);
|
||||
lua_remove(L, -1);
|
||||
}
|
||||
lua_getglobal(L, "fullscreen");
|
||||
if(lua_isnumber(L, -1))
|
||||
if((int)lua_tonumber(L, -1) == 1) {
|
||||
gl_screen.fullscreen = 1;
|
||||
lua_remove(L, -1);
|
||||
}
|
||||
// Set the default config values.
|
||||
conf_setDefaults();
|
||||
|
||||
lua_getglobal(L, "fps");
|
||||
if(lua_isnumber(L, -1)) {
|
||||
max_fps = (int)lua_tonumber(L, -1);
|
||||
lua_remove(L, -1);
|
||||
}
|
||||
|
||||
// Joystick.
|
||||
lua_getglobal(L, "joystick");
|
||||
if(lua_isnumber(L, -1)) {
|
||||
indjoystick = (int)lua_tonumber(L, -1);
|
||||
lua_remove(L, -1);
|
||||
}
|
||||
else if(lua_isstring(L, -1)) {
|
||||
namjoystick = strdup((char*)lua_tostring(L, -1));
|
||||
lua_remove(L, -1);
|
||||
}
|
||||
|
||||
// Grab the keybindings if there are any.
|
||||
char* str;
|
||||
int type, key, reverse;
|
||||
for(i = 0; strcmp(keybindNames[i],"end"); i++) {
|
||||
lua_getglobal(L, keybindNames[i]);
|
||||
str = NULL;
|
||||
key = -1;
|
||||
reverse = 0;
|
||||
if(lua_istable(L, -1)) { // It's a table alright.
|
||||
// Get the event type.
|
||||
lua_pushstring(L, "type");
|
||||
lua_gettable(L, -2);
|
||||
if(lua_isstring(L, -1))
|
||||
str = (char*)lua_tostring(L, -1);
|
||||
|
||||
// Get the key.
|
||||
lua_pushstring(L, "key");
|
||||
lua_gettable(L, -3);
|
||||
if(lua_isnumber(L, -1))
|
||||
key = (int)lua_tonumber(L, -1);
|
||||
|
||||
// Is it reversed? Only useful for axis.
|
||||
lua_pushstring(L, "reverse");
|
||||
lua_gettable(L, -4);
|
||||
if(lua_isnumber(L, -1))
|
||||
reverse = 1;
|
||||
|
||||
if(key != -1 && str != NULL) { // Keybind is valid!
|
||||
// Get the type.
|
||||
if(strcmp(str, "null")==0) type = KEYBIND_NULL;
|
||||
else if(strcmp(str, "keyboard")==0) type = KEYBIND_KEYBOARD;
|
||||
else if(strcmp(str, "jaxis")==0) type = KEYBIND_JAXIS;
|
||||
else if(strcmp(str, "jbutton")==0) type = KEYBIND_JBUTTON;
|
||||
else {
|
||||
WARN("Unknown keybinding of type %s", str);
|
||||
continue;
|
||||
}
|
||||
// Set the keybind.
|
||||
input_setKeybind((char*)keybindNames[i], type, key, reverse);
|
||||
} else WARN("Malformed keybind in %s", CONF_FILE);
|
||||
|
||||
// Clean up after table stuff.
|
||||
lua_remove(L, -1);
|
||||
lua_remove(L, -1);
|
||||
lua_remove(L, -1);
|
||||
lua_remove(L, -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
lua_close(L);
|
||||
// Have Lua parse the config file.
|
||||
conf_loadConfig(CONF_FILE);
|
||||
|
||||
// Parse arguments.
|
||||
static struct option long_options[] = {
|
||||
@ -409,7 +309,20 @@ static void display_fps(const double dt) {
|
||||
|
||||
static void data_name(void) {
|
||||
uint32_t bufsize;
|
||||
char* buf = pack_readfile(DATA, START_DATA, &bufsize);
|
||||
char* buf;
|
||||
|
||||
// Check the version.
|
||||
buf = pack_readfile(DATA, VERSION_FILE, &bufsize);
|
||||
|
||||
if(strncmp(buf, version, bufsize) != 0) {
|
||||
WARN("Lephisto version and data module version differ!");
|
||||
WARN("Lephisto is v%s, data is for v%s", version, buf);
|
||||
}
|
||||
|
||||
free(buf);
|
||||
|
||||
// Load the datafiles name.
|
||||
buf = pack_readfile(DATA, START_DATA, &bufsize);
|
||||
|
||||
xmlNodePtr node;
|
||||
xmlDocPtr doc = xmlParseMemory(buf, bufsize);
|
||||
|
@ -10,6 +10,7 @@
|
||||
#define MAX(x,y) (((x)>(y))?(x):(y))
|
||||
#define MIN(x,y) (((x)>(y))?(y):(x))
|
||||
|
||||
#define DATA_DEF "data" // Default data packfile.
|
||||
extern char* data; // Modifiable datafile.
|
||||
#define DATA data // Data file.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user