diff --git a/BBGE/ActionInput.cpp b/BBGE/ActionInput.cpp index bf12a9b..f3f4ce5 100644 --- a/BBGE/ActionInput.cpp +++ b/BBGE/ActionInput.cpp @@ -23,7 +23,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "ActionMapper.h" #include "Core.h" #include "SDL.h" -#include "LegacyKeycodes.h" +#include "GameKeyNames.h" static std::string inputcode2string(int k) @@ -32,35 +32,34 @@ static std::string inputcode2string(int k) return std::string(); if(k < KEY_MAXARRAY) { - // See parseKey() below - std::stringstream os; - os << "K:"; -#ifdef BBGE_BUILD_SDL2 - int keycode = SDL_GetKeyFromScancode((SDL_Scancode)k); - os << keycode << "," << k; -#else - os << k; -#endif + // Returns KEY_* or NULL + const std::string& str = getKeyNameFromInputCode(k); + if(str.length()) + return str; + + // fallback + std::ostringstream os; + os << "K:" << k; return os.str(); } if(k >= JOY_BUTTON_0 && k < JOY_BUTTON_END) { - std::stringstream os; + std::ostringstream os; os << "JB:" << (k - JOY_BUTTON_0); return os.str(); } if(k >= JOY_AXIS_0_POS && k < JOY_AXIS_END_POS) { - std::stringstream os; + std::ostringstream os; os << "AX:+" << (k - JOY_AXIS_0_POS); return os.str(); } if(k >= JOY_AXIS_0_NEG && k < JOY_AXIS_END_NEG) { - std::stringstream os; + std::ostringstream os; os << "AX:-" << (k - JOY_AXIS_0_NEG); return os.str(); } @@ -97,14 +96,10 @@ static const char *jbtnname(int joystickID, int btn) return j ? j->getButtonName(btn) : NULL; } - - std::string getInputCodeToString(int k) { std::string s = inputcode2string(k); - if(s.empty()) - return "NONE"; - return spacesToUnderscores(s); + return s.empty() ? "0" : s; } std::string getInputCodeToUserString(unsigned int k, size_t joystickID) @@ -117,6 +112,7 @@ std::string getInputCodeToUserString(unsigned int k, size_t joystickID) if(k < KEY_MAXARRAY) { #ifdef BBGE_BUILD_SDL2 + pretty = SDL_GetScancodeName((SDL_Scancode)k); const SDL_Keycode kcode = SDL_GetKeyFromScancode((SDL_Scancode)k); if(kcode != SDLK_UNKNOWN) pretty = SDL_GetKeyName(kcode); @@ -145,38 +141,8 @@ std::string getInputCodeToUserString(unsigned int k, size_t joystickID) return s; } - return inputcode2string(k); -} - -// two comma-separated ints -// first is the keycode, second the scancode -// (Keymap-independent) Scancodes are used when built with SDL2 support and specified -// (Keymap-dependent) Keycode is used otherwise -static int parseKey(const char *ks) -{ - int k = 0; - -#ifdef BBGE_BUILD_SDL2 - if(const char *comma = strchr(ks, ',')) - { - k = atoi(comma + 1); - if(k && k < KEY_MAXARRAY) - return k; - } -#endif - - // Use the keycode - k = atoi(ks); - if(k < KEY_MAXARRAY) - { -#ifdef BBGE_BUILD_SDL2 - // But when we're on SDL2, don't forget to turn they keycode back into a scancode, since we work with scancodes internally - k = SDL_GetScancodeFromKey(k); -#endif - return k; - } - - return 0; + std::string s = inputcode2string(k); + return s.empty() ? "-" : s; } int getStringToInputCode(const std::string& s) @@ -188,7 +154,7 @@ int getStringToInputCode(const std::string& s) if(s == "MMB") return MOUSE_BUTTON_MIDDLE; if(!strncmp(s.c_str(), "K:", 2)) - return parseKey(s.c_str() + 2); + return atoi(s.c_str() + 2); if(!strncmp(s.c_str(), "JB:", 3)) return JOY_BUTTON_0 + atoi(s.c_str() + 3); if(!strncmp(s.c_str(), "MB:", 3)) @@ -203,12 +169,10 @@ int getStringToInputCode(const std::string& s) default: return 0; } } - if(s == "NONE") - return 0; // Maybe we're upgrading from an old config? - // Note that this returns 0 for "0", which was considered "no key" - if(int k = getInputCodeFromLegacyName(s.c_str())) + // This handles KEY_* and some old mouse/joystick names. + if(int k = getInputCodeFromKeyName(s.c_str())) return k; return 0; @@ -218,9 +182,8 @@ int getStringToInputCode(const std::string& s) ActionInput::ActionInput() { - for (int i = 0; i < INP_MSESIZE; i++) data.single.mse[i] = 0; - for (int i = 0; i < INP_KEYSIZE; i++) data.single.key[i] = 0; - for (int i = 0; i < INP_JOYSIZE; i++) data.single.joy[i] = 0; + for (int i = 0; i < INP_COMBINED_SIZE; i++) + data.all[i] = 0; } std::string ActionInput::toString() const diff --git a/BBGE/LegacyKeycodes.cpp b/BBGE/GameKeyNames.cpp similarity index 57% rename from BBGE/LegacyKeycodes.cpp rename to BBGE/GameKeyNames.cpp index cfef9b9..b0562f0 100644 --- a/BBGE/LegacyKeycodes.cpp +++ b/BBGE/GameKeyNames.cpp @@ -1,11 +1,12 @@ -#include "LegacyKeycodes.h" +#include "GameKeyNames.h" #include "ActionStatus.h" +#include #include -#include typedef std::map InputCodeMap; InputCodeMap inputCodeMap; +static std::string keyNames[KEY_MAXARRAY]; static void initInputCodeMap() { @@ -84,6 +85,22 @@ static void initInputCodeMap() K(KEY_RETURN) K(KEY_TAB) K(KEY_ESCAPE) + K(KEY_SPACE) + K(KEY_BACKSPACE) + K(KEY_NUMPADMINUS) + K(KEY_NUMPADPERIOD) + K(KEY_NUMPADPLUS) + K(KEY_NUMPADSLASH) + K(KEY_NUMPADSTAR) + K(KEY_PGDN) + K(KEY_PGUP) + K(KEY_APOSTROPHE) + K(KEY_EQUALS) + K(KEY_SEMICOLON) + K(KEY_LBRACKET) + K(KEY_RBRACKET) + K(KEY_TILDE) + K(MOUSE_BUTTON_LEFT) K(MOUSE_BUTTON_RIGHT) @@ -92,20 +109,30 @@ static void initInputCodeMap() for (int jb = JOY_BUTTON_0; jb < JOY_BUTTON_END; jb++) { - std::ostringstream os; - os << "JOY_BUTTON_" << jb - JOY_BUTTON_0; - inputCodeMap[os.str()] = jb; + char buf[32]; + sprintf(buf, "JOY_BUTTON_%d", jb - JOY_BUTTON_0); + inputCodeMap[buf] = jb; } + + // ---------------------- + + // Can just use pointers to the strings in the map; they'll stay where they are in memory + for(InputCodeMap::iterator it = inputCodeMap.begin(); it != inputCodeMap.end(); ++it) + keyNames[it->second] = it->first; } -int getInputCodeFromLegacyName(const char *name) +int getInputCodeFromKeyName(const char *name) { return inputCodeMap[name]; } +const std::string& getKeyNameFromInputCode(int k) +{ + return keyNames[k]; +} -struct LegacyKeymapInitializer +struct KeyNameInitializer { - LegacyKeymapInitializer() { initInputCodeMap(); } + KeyNameInitializer() { initInputCodeMap(); } }; -static LegacyKeymapInitializer s_kinit; +static KeyNameInitializer s_kinit; diff --git a/BBGE/GameKeyNames.h b/BBGE/GameKeyNames.h new file mode 100644 index 0000000..f83e6f3 --- /dev/null +++ b/BBGE/GameKeyNames.h @@ -0,0 +1,9 @@ +#ifndef BBGE_LEGACYKEYCODES_H +#define BBGE_LEGACYKEYCODES_H + +#include + +int getInputCodeFromKeyName(const char *name); +const std::string& getKeyNameFromInputCode(int k); + +#endif diff --git a/BBGE/LegacyKeycodes.h b/BBGE/LegacyKeycodes.h deleted file mode 100644 index 9506570..0000000 --- a/BBGE/LegacyKeycodes.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef BBGE_LEGACYKEYCODES_H -#define BBGE_LEGACYKEYCODES_H - -int getInputCodeFromLegacyName(const char *name); - -#endif diff --git a/CMakeLists.txt b/CMakeLists.txt index 6140198..2c358d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -432,6 +432,7 @@ SET(AQUARIA_SRCS ${SRCDIR}/Entity.cpp ${SRCDIR}/FlockEntity.cpp ${SRCDIR}/Game.cpp + ${SRCDIR}/GameKeyNames.cpp ${SRCDIR}/GameStructs.cpp ${SRCDIR}/GameplayVariables.cpp ${SRCDIR}/GasCloud.cpp diff --git a/win/vc90/BBGE.vcproj b/win/vc90/BBGE.vcproj index 1d1ebb1..cfd6b81 100644 --- a/win/vc90/BBGE.vcproj +++ b/win/vc90/BBGE.vcproj @@ -362,6 +362,14 @@ RelativePath="..\..\BBGE\FrameBuffer.h" > + + + + @@ -390,14 +398,6 @@ RelativePath="..\..\BBGE\Joystick.h" > - - - -