diff --git a/src/conf.c b/src/conf.c index 51eeb14..1e3dbd8 100644 --- a/src/conf.c +++ b/src/conf.c @@ -130,7 +130,8 @@ int conf_loadConfig(const char* file) { int i; double d; char* str, *mod; - int type, key, reverse; + SDLKey key; + int type, reverse; int w, h, fsaa; SDLMod m; @@ -229,10 +230,14 @@ int conf_loadConfig(const char* file) { lua_gettable(L, -2); if(lua_isstring(L, -1)) str = (char*)lua_tostring(L, -1); - else { + else if(lua_isnil(L, -1)) { WARN("Found keybind with no type field!"); str = "null"; } + else { + WARN("Found keybind with invalid type field!"); + str = "null"; + } lua_remove(L, -1); /* Get the key. */ @@ -240,9 +245,14 @@ int conf_loadConfig(const char* file) { lua_gettable(L, -2); if(lua_isnumber(L, -1)) key = (int)lua_tonumber(L, -1); - else { + else if(lua_isstring(L, -1)) + key = input_keyConv((char*)lua_tostring(L, -1)); + else if(lua_isnil(L, -1)) { WARN("Found keybind with no key field!"); - key = -1; + key = SDLK_UNKNOWN; + } else { + WARN("Found keybind with invalid key field!"); + key = SDLK_UNKNOWN; } lua_remove(L, -1); @@ -266,8 +276,7 @@ int conf_loadConfig(const char* file) { mod = NULL; lua_remove(L, -1); - - if((key != -1) && (str != NULL)) { + if((key != SDLK_UNKNOWN) && (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; diff --git a/src/input.c b/src/input.c index d0160c5..2446430 100644 --- a/src/input.c +++ b/src/input.c @@ -104,6 +104,11 @@ unsigned int input_afterburnSensibility = 200; /**< ms between taps to afterbur /* From player.c */ extern double player_turn; +static char* keyconv[SDLK_LAST]; /**< Key conversion table. */ + +static void input_keyConvGen(void); +static void input_keyConvDestroy(void); + /** * @fn void input_setDefault(void) * @@ -173,6 +178,8 @@ void input_init(void) { tmp->reverse = 1.; input_keybinds[i] = tmp; } + + input_keyConvGen(); } /** @@ -185,6 +192,41 @@ void input_exit(void) { for(i = 0; strcmp(keybindNames[i], "end"); i++) free(input_keybinds[i]); free(input_keybinds); + + input_keyConvDestroy(); +} + +/** + * @brief Create the key conversion table. + */ +static void input_keyConvGen(void) { + SDLKey k; + + for(k == SDLK_FIRST; k < SDLK_LAST; k++) + keyconv[k] = SDL_GetKeyName(k); +} + +/** + * @brief Destroy the key conversion table. + */ +static void input_keyConvDestroy(void) { + +} + +/** + * @brief Get the key id from its name. + * @param name Name of the key to get id from. + * @return ID of the key. + */ +SDLKey input_keyConv(char* name) { + SDLKey k; + + for(k = SDLK_FIRST; k < SDLK_LAST; k++) + if(strcmp(name, keyconv[k])==0) + return k; + + WARN("Keyname '%s' doesn't match any key.", name); + return SDLK_UNKNOWN; } /** diff --git a/src/input.h b/src/input.h index 640bcb7..b2a1e61 100644 --- a/src/input.h +++ b/src/input.h @@ -13,6 +13,7 @@ typedef enum { /* Set input. */ void input_setDefault(void); +SDLKey input_keyConv(char* name); void input_setKeybind(char* keybind, KeybindType type, int key, SDLMod mod, int reverse); SDLKey input_getKeybind(const char* keybind, KeybindType* type, SDLMod* mod, int* reverse); const char* input_getKeybindDescription(char* keybind); diff --git a/src/lephisto.c b/src/lephisto.c index 28e0a6e..ac7fabf 100644 --- a/src/lephisto.c +++ b/src/lephisto.c @@ -136,6 +136,12 @@ int main(int argc, char** argv) { if(lfile_dirMakeExist("%s", lfile_basePath())) WARN("Unable to create lephisto directory '%s'", lfile_basePath()); + /* Must be initialized befre input init is called. */ + if(SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) { + WARN("Unable to initialize SDL video: %s", SDL_GetError()); + return -1; + } + /* Input must be initialized for config to work. */ input_init();