Updated the love key to SDL keycode mapping to be a static array instead of a std::map.

This commit is contained in:
Alex Szpakowski
2014-09-27 03:17:08 -03:00
parent b70f72acb3
commit e2354acc65
3 changed files with 10 additions and 14 deletions
+1 -1
View File
@@ -241,7 +241,7 @@ public:
KEY_EJECT, KEY_EJECT,
KEY_SLEEP, KEY_SLEEP,
KEY_MAX_ENUM = 512 KEY_MAX_ENUM
}; };
virtual ~Keyboard() {} virtual ~Keyboard() {}
+6 -7
View File
@@ -18,8 +18,6 @@
* 3. This notice may not be removed or altered from any source distribution. * 3. This notice may not be removed or altered from any source distribution.
**/ **/
#include "common/config.h"
#include "Keyboard.h" #include "Keyboard.h"
namespace love namespace love
@@ -55,8 +53,8 @@ bool Keyboard::isDown(Key *keylist) const
for (Key key = *keylist; key != KEY_MAX_ENUM; key = *(++keylist)) for (Key key = *keylist; key != KEY_MAX_ENUM; key = *(++keylist))
{ {
auto it = keys.find(key); SDL_Scancode scancode = SDL_GetScancodeFromKey(keymap[key]);
if (it != keys.end() && keystate[SDL_GetScancodeFromKey(it->second)]) if (keystate[scancode])
return true; return true;
} }
@@ -76,9 +74,10 @@ bool Keyboard::hasTextInput() const
return SDL_IsTextInputActive(); return SDL_IsTextInputActive();
} }
std::map<Keyboard::Key, SDL_Keycode> Keyboard::createKeyMap() const SDL_Keycode *Keyboard::createKeyMap()
{ {
std::map<Keyboard::Key, SDL_Keycode> k; // Array must be static so its lifetime continues once the function returns.
static SDL_Keycode k[Keyboard::KEY_MAX_ENUM] = {SDLK_UNKNOWN};
k[Keyboard::KEY_UNKNOWN] = SDLK_UNKNOWN; k[Keyboard::KEY_UNKNOWN] = SDLK_UNKNOWN;
@@ -287,7 +286,7 @@ std::map<Keyboard::Key, SDL_Keycode> Keyboard::createKeyMap()
return k; return k;
} }
std::map<Keyboard::Key, SDL_Keycode> Keyboard::keys = Keyboard::createKeyMap(); const SDL_Keycode *Keyboard::keymap = Keyboard::createKeyMap();
} // sdl } // sdl
} // keyboard } // keyboard
+3 -6
View File
@@ -26,10 +26,7 @@
#include "common/EnumMap.h" #include "common/EnumMap.h"
// SDL // SDL
#include <SDL.h> #include <SDL_keyboard.h>
// STL
#include <map>
namespace love namespace love
{ {
@@ -60,8 +57,8 @@ private:
// The real implementation is in love::event::sdl::Event::Convert. // The real implementation is in love::event::sdl::Event::Convert.
bool key_repeat; bool key_repeat;
static std::map<Key, SDL_Keycode> createKeyMap(); static const SDL_Keycode *createKeyMap();
static std::map<Key, SDL_Keycode> keys; static const SDL_Keycode *keymap;
}; // Keyboard }; // Keyboard