diff options
-rw-r--r-- | src/crepe/api/Config.h | 1 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 29 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 2 | ||||
-rw-r--r-- | src/crepe/system/InputSystem.h | 2 | ||||
-rw-r--r-- | src/test/InputTest.cpp | 42 |
5 files changed, 37 insertions, 39 deletions
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 7b1a5ca..d2be3f3 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -91,7 +91,6 @@ public: //! The maximum number of pixels the mouse can move between MouseDown and MouseUp events to be considered a click. int tolerance = 5; } click_tolerance; - }; } // namespace crepe diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 1e8af41..4504b95 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -191,22 +191,21 @@ Keycode SDLContext::sdl_to_keycode(SDL_Scancode 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, 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; -} + // 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 = [] { diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 5182dca..053aa59 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -165,7 +165,7 @@ private: * \return The corresponding `Keycode` value or `Keycode::NONE` if the key is unrecognized. */ Keycode sdl_to_keycode(SDL_Scancode sdl_key); - + /** * \brief Converts an SDL mouse button code to the custom MouseButton type. * diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h index 1d6e79c..5da8f32 100644 --- a/src/crepe/system/InputSystem.h +++ b/src/crepe/system/InputSystem.h @@ -1,9 +1,9 @@ #pragma once +#include "../api/Config.h" #include "../facade/SDLContext.h" #include "../types.h" #include "../util/OptionalRef.h" -#include "../api/Config.h" #include "System.h" diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp index e9dc645..5150960 100644 --- a/src/test/InputTest.cpp +++ b/src/test/InputTest.cpp @@ -332,30 +332,30 @@ TEST_F(InputTest, WindowMoveTest) { EXPECT_TRUE(callback_triggered); } TEST_F(InputTest, KeyboardStateTest) { - SDLContext& sdl_context = this->mediator.sdl_context; - // Simulate pressing a key - SDL_Event key_down_event; - SDL_zero(key_down_event); - key_down_event.type = SDL_KEYDOWN; - key_down_event.key.keysym.scancode = SDL_SCANCODE_A; + SDLContext & sdl_context = this->mediator.sdl_context; + // Simulate pressing a key + SDL_Event key_down_event; + SDL_zero(key_down_event); + key_down_event.type = SDL_KEYDOWN; + key_down_event.key.keysym.scancode = SDL_SCANCODE_A; key_down_event.key.keysym.sym = SDL_SCANCODE_A; - SDL_PushEvent(&key_down_event); + SDL_PushEvent(&key_down_event); - // Check the keyboard state - auto keyboard_state = sdl_context.get_keyboard_state(); + // Check the keyboard state + auto keyboard_state = sdl_context.get_keyboard_state(); - // Verify the state of the 'A' key - EXPECT_TRUE(keyboard_state[Keycode::A]); + // Verify the state of the 'A' key + EXPECT_TRUE(keyboard_state[Keycode::A]); - // Simulate releasing a key - SDL_Event key_up_event; - SDL_zero(key_up_event); - key_up_event.type = SDL_KEYUP; - key_up_event.key.keysym.scancode = SDL_SCANCODE_A; // Simulate releasing 'A' key - SDL_PushEvent(&key_up_event); - // Check the keyboard state again - keyboard_state = sdl_context.get_keyboard_state(); + // Simulate releasing a key + SDL_Event key_up_event; + SDL_zero(key_up_event); + key_up_event.type = SDL_KEYUP; + key_up_event.key.keysym.scancode = SDL_SCANCODE_A; // Simulate releasing 'A' key + SDL_PushEvent(&key_up_event); + // Check the keyboard state again + keyboard_state = sdl_context.get_keyboard_state(); - // Verify the state of the 'A' key - EXPECT_FALSE(keyboard_state[Keycode::A]); + // Verify the state of the 'A' key + EXPECT_FALSE(keyboard_state[Keycode::A]); } |