aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-09 15:29:18 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-09 15:29:18 +0100
commita80477f2e7f4c18adcc6441828d17582aad2598f (patch)
treefc26007bf3c9ea463cb4572bc26cf176d4b85d63 /src/crepe/facade
parent2ff3369a7aaf98936bf93f1c7c0dbfcaa38c31d7 (diff)
get_keyboard_state working
Diffstat (limited to 'src/crepe/facade')
-rw-r--r--src/crepe/facade/SDLContext.cpp29
-rw-r--r--src/crepe/facade/SDLContext.h36
2 files changed, 34 insertions, 31 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{};
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index b65ba9c..5182dca 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -110,7 +110,19 @@ public:
MouseData mouse_data;
WindowData window_data;
};
-
+ /**
+ * \brief Retrieves the current state of the keyboard.
+ *
+ * This method returns the state of all keys on the keyboard, represented as a
+ * `std::array` of boolean values. Each element of the array corresponds to a
+ * specific key defined in the `Keycode` enum, and the value indicates whether
+ * the key is currently pressed (true) or not pressed (false).
+ *
+ * \return A `std::array<bool, Keycode::NUM_KEYCODES>` representing the state of
+ * each key on the keyboard, where `true` means the key is pressed, and
+ * `false` means it is not pressed.
+ */
+ std::array<bool, Keycode::NUM_KEYCODES> get_keyboard_state();
/**
* \brief Gets the singleton instance of SDLContext.
* \return Reference to the SDLContext instance.
@@ -144,28 +156,16 @@ private:
void handle_window_event(const SDL_WindowEvent & window_event,
std::vector<SDLContext::EventData> & event_list);
/**
- * \brief Converts an SDL key code to the custom Keycode type.
+ * \brief Converts an SDL scan code to the custom Keycode type.
*
- * This method maps an SDL key code to the corresponding `Keycode` enum value,
+ * This method maps an SDL scan code to the corresponding `Keycode` enum value,
* which is used internally by the system to identify the keys.
*
- * \param sdl_key The SDL key code to convert.
+ * \param sdl_key The SDL scan code to convert.
* \return The corresponding `Keycode` value or `Keycode::NONE` if the key is unrecognized.
*/
- Keycode sdl_to_keycode(SDL_Keycode sdl_key);
- /**
- * \brief Retrieves the current state of the keyboard.
- *
- * This method returns the state of all keys on the keyboard, represented as a
- * `std::array` of boolean values. Each element of the array corresponds to a
- * specific key defined in the `Keycode` enum, and the value indicates whether
- * the key is currently pressed (true) or not pressed (false).
- *
- * \return A `std::array<bool, Keycode::NUM_KEYCODES>` representing the state of
- * each key on the keyboard, where `true` means the key is pressed, and
- * `false` means it is not pressed.
- */
- std::array<bool, Keycode::NUM_KEYCODES> get_keyboard_state();
+ Keycode sdl_to_keycode(SDL_Scancode sdl_key);
+
/**
* \brief Converts an SDL mouse button code to the custom MouseButton type.
*