diff options
Diffstat (limited to 'src/crepe/facade/SDLContext.cpp')
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 722c165..1e8af41 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -78,8 +78,7 @@ SDLContext::~SDLContext() { SDL_Quit(); } -// Function that uses the shared table -Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) { +Keycode SDLContext::sdl_to_keycode(SDL_Scancode sdl_key) { static const std::array<Keycode, SDL_NUM_SCANCODES> LOOKUP_TABLE = [] { std::array<Keycode, SDL_NUM_SCANCODES> table{}; table.fill(Keycode::NONE); @@ -192,19 +191,23 @@ Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) { return LOOKUP_TABLE[sdl_key]; } std::array<bool, Keycode::NUM_KEYCODES> SDLContext::get_keyboard_state() { - // Array to hold the key states (true if pressed, false if not) - std::array<bool, SDL_NUM_SCANCODES> keyState; - - const Uint8 * current_state = SDL_GetKeyboardState(nullptr); - - for (int i = 0; i < SDL_NUM_SCANCODES; ++i) { - // Set true if the key is pressed, false if not - keyState[i] = current_state[i] != 0; - } - - return keyState; + // Array to hold the key states (true if pressed, false if not) + std::array<bool, Keycode::NUM_KEYCODES> keyState{}; + SDL_PumpEvents(); + const Uint8 *current_state = SDL_GetKeyboardState(nullptr); + + for (int i = 0; i < SDL_NUM_SCANCODES; ++i) { + Keycode key = sdl_to_keycode(static_cast<SDL_Scancode>(i)); + + if (key != Keycode::NONE) { + keyState[key] = current_state[i] != 0; + } + } + + return keyState; } + MouseButton SDLContext::sdl_to_mousebutton(Uint8 sdl_button) { static const std::array<MouseButton, 5> MOUSE_BUTTON_LOOKUP_TABLE = [] { std::array<MouseButton, 5> table{}; |