From 4e572b704c9b415a8ac895e7a8af952565c56bab Mon Sep 17 00:00:00 2001 From: Allanis Date: Sat, 24 May 2014 21:19:28 +0100 Subject: [PATCH] [Add] Created lstd for portable SDLKey isalpha functions and such. --- src/lstd.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/lstd.h | 6 ++++++ src/options.c | 23 +++-------------------- src/toolkit.c | 28 +++------------------------- 4 files changed, 57 insertions(+), 45 deletions(-) create mode 100644 src/lstd.c create mode 100644 src/lstd.h diff --git a/src/lstd.c b/src/lstd.c new file mode 100644 index 0000000..6de1e7e --- /dev/null +++ b/src/lstd.c @@ -0,0 +1,45 @@ +/** + * @file lstd.c + * + * @brief Portable replacements for some functions. + */ +#include "lstd.h" + +/** + * @brief Check to see if a key is alpha. + * @param k Key to check. + * @return 1 if is alpha. + */ +int lstd_isalpha(SDLKey k) { + int ret; + + ret = 0; + + /* Alpha. */ + if((k >= SDLK_a) && (k <= SDLK_z)) + ret = 1; + + return ret; +} + +/** + * @brief Like isalnum but for keysyms. + * @param k Key to check. + * @return 1 if is alnum. + */ +int lstd_isalnum(SDLKey k) { + int ret; + + ret = 0; + + /* Alpha. */ + if((k >= SDLK_a) && (k <= SDLK_z)) + ret = 1; + + /* Number. */ + if((k >= SDLK_0) && (k <= SDLK_9)) + ret = 1; + + return ret; +} + diff --git a/src/lstd.h b/src/lstd.h new file mode 100644 index 0000000..217700e --- /dev/null +++ b/src/lstd.h @@ -0,0 +1,6 @@ +#pragma once +#include + +int lstd_isalpha(SDLKey k); +int lstd_isalnum(SDLKey k); + diff --git a/src/options.c b/src/options.c index eead090..a7a0e28 100644 --- a/src/options.c +++ b/src/options.c @@ -13,6 +13,7 @@ #include "toolkit.h" #include "sound.h" #include "music.h" +#include "lstd.h" #include "options.h" #define KEYBINDS_WIDTH 440 /**< Options menu width. */ @@ -27,30 +28,12 @@ /* Extern. */ extern const char* keybindNames[]; /* From input.c */ -static int opt_isalpha(SDLKey k); static const char* modToText(SDLMod mod); static void menuKeybinds_update(unsigned int wid, char* name); static void opt_setSFXLevel(unsigned int wid, char* str); static void opt_setMusicLevel(unsigned int wid, char* str); -/** - * @brief Check to see if a key is alpha. - * @param k Key to check. - * @return 1 if is alpha. - */ -static int opt_isalpha(SDLKey k) { - int ret; - - ret = 0; - - /* Alpha. */ - if((k >= SDLK_a) && (k <= SDLK_z)) - ret = 1; - - return ret; -} - /** * @brief Opens the keybindings menu. */ @@ -84,7 +67,7 @@ void opt_menuKeybinds(void) { switch(type) { case KEYBIND_KEYBOARD: /* SDL_GetKeyName returns lowercase which is ugly. */ - if(opt_isalpha(key)) + if(lstd_isalpha(key)) snprintf(str[j], 64, "%s <%c>", keybindNames[j], toupper(key)); else snprintf(str[j], 64, "%s <%s>", keybindNames[j], SDL_GetKeyName(key)); @@ -165,7 +148,7 @@ static void menuKeybinds_update(unsigned int wid, char* name) { break; case KEYBIND_KEYBOARD: /* SDL_GetKeyName returns lowercase which is ugly. */ - if(opt_isalpha(key)) + if(lstd_isalpha(key)) snprintf(bind, 32, "keyboard: %s%s%c", (mod != KMOD_NONE) ? modToText(mod) : "", (mod != KMOD_NONE) ? " + " : "", diff --git a/src/toolkit.c b/src/toolkit.c index 5849955..fa6564f 100644 --- a/src/toolkit.c +++ b/src/toolkit.c @@ -11,6 +11,7 @@ #include "pause.h" #include "opengl.h" #include "input.h" +#include "lstd.h" #include "toolkit.h" #define INPUT_DELAY 500 @@ -198,8 +199,6 @@ static void toolkit_clip(double x, double y, double w, double h); static void toolkit_unclip(void); static void toolkit_drawRect(double x, double y, double w, double h, glColour* c, glColour* lc); -/* Misc. */ -static int toolkit_isalnum(SDLKey k); /** * @brief Set the internal widget position. @@ -1971,7 +1970,7 @@ static int toolkit_inputInput(Uint8 type, Widget* inp, SDLKey key) { #endif /* Only catch some keys. */ - if(!toolkit_isalnum(key) && (key != SDLK_BACKSPACE) && + if(!lstd_isalnum(key) && (key != SDLK_BACKSPACE) && (key != SDLK_SPACE) && (key != SDLK_RETURN)) return 0; @@ -2211,27 +2210,6 @@ static void toolkit_clearKey(void) { input_keyCounter = 0; } -/** - * @brief Like isalnum but for keysyms. - * @param k Key to check. - * @return 1 if is alnum. - */ -static int toolkit_isalnum(SDLKey k) { - int ret; - - ret = 0; - - /* Alpha. */ - if((k >= SDLK_a) && (k <= SDLK_z)) - ret = 1; - - /* Number. */ - if((k >= SDLK_0) && (k <= SDLK_9)) - ret = 1; - - return ret; -} - /** * @brief Handles keyboard events. * @param event Keyboard event to handle. @@ -2251,7 +2229,7 @@ static int toolkit_keyEvent(SDL_Event* event) { key = event->key.keysym.sym; /* Hack to simulate key repetition. */ - if((key == SDLK_BACKSPACE) || toolkit_isalnum(key)) { + if((key == SDLK_BACKSPACE) || lstd_isalnum(key)) { if(event->type == SDL_KEYDOWN) toolkit_regKey(key); else if(event->type == SDL_KEYUP) toolkit_unregKey(key); }