From 193d5bf4d9577e63202cec45b21ed85a76a89f09 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Fri, 13 Dec 2024 08:16:48 +0100 Subject: keyboard state implemented --- src/crepe/api/Event.h | 4 ---- src/crepe/api/KeyCodes.h | 2 +- src/crepe/facade/SDLContext.cpp | 10 ++++------ src/crepe/facade/SDLContext.h | 12 ++++-------- 4 files changed, 9 insertions(+), 19 deletions(-) diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index d222e51..66dd0cc 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -2,8 +2,6 @@ // TODO discussing the location of these events #include -#include - #include "KeyCodes.h" #include "types.h" @@ -25,7 +23,6 @@ public: //! The key that was pressed. Keycode key = Keycode::NONE; - keyboard_state_t keyboard_state; }; /** @@ -35,7 +32,6 @@ class KeyReleaseEvent : public Event { public: //! The key that was released. Keycode key = Keycode::NONE; - keyboard_state_t keyboard_state; }; /** diff --git a/src/crepe/api/KeyCodes.h b/src/crepe/api/KeyCodes.h index 4a3bf57..fa0d17c 100644 --- a/src/crepe/api/KeyCodes.h +++ b/src/crepe/api/KeyCodes.h @@ -157,5 +157,5 @@ typedef enum { NUM_KEYCODES = 512, } Keycode; -typedef std::unordered_map keyboard_state_t; + } // namespace crepe diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index d70a7bb..6e47561 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -84,7 +84,7 @@ Keycode SDLContext::sdl_to_keycode(SDL_Scancode sdl_key) { return Keycode::NONE; } -keyboard_state_t SDLContext::get_keyboard_state() { +void SDLContext::update_keyboard_state() { // Array to hold the key states (true if pressed, false if not) std::array keyState{}; SDL_PumpEvents(); @@ -94,11 +94,10 @@ keyboard_state_t SDLContext::get_keyboard_state() { Keycode key = sdl_to_keycode(static_cast(i)); if (key != Keycode::NONE) { - keyboard_state[key] = current_state[i] != 0; + this->keyboard_state[key] = current_state[i] != 0; } } - return keyboard_state; } MouseButton SDLContext::sdl_to_mousebutton(Uint8 sdl_button) { @@ -293,25 +292,24 @@ std::vector SDLContext::get_events() { break; case SDL_KEYDOWN: { - + this->update_keyboard_state(); EventData transfer_event; transfer_event.event_type = SDLContext::EventType::KEYDOWN; transfer_event.data.key_data = KeyData{ .key = sdl_to_keycode(event.key.keysym.scancode), .key_repeat = event.key.repeat != 0, - .keyboard_state = this->get_keyboard_state(), }; event_list.push_back(transfer_event); } break; case SDL_KEYUP: { + this->update_keyboard_state(); EventData transfer_event; transfer_event.event_type = SDLContext::EventType::KEYUP; transfer_event.data.key_data = KeyData{ .key = sdl_to_keycode(event.key.keysym.scancode), .key_repeat = false, - .keyboard_state = this->get_keyboard_state(), }; event_list.push_back(transfer_event); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index feb51f4..b10bcfe 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -125,16 +125,12 @@ public: /** * \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 + * This method updates the state of all keys on the keyboard. Each element of the unordered map 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 `keyboard_state_t` representing the state of - * each key on the keyboard, where `true` means the key is pressed, and - * `false` means it is not pressed. + * */ - keyboard_state_t get_keyboard_state(); + void update_keyboard_state(); /** * \brief Gets the singleton instance of SDLContext. * \return Reference to the SDLContext instance. @@ -296,7 +292,7 @@ private: */ CameraAuxiliaryData cam_aux_data; private: - keyboard_state_t keyboard_state; + std::unordered_map keyboard_state; const std::unordered_map LOOKUP_TABLE = { {SDL_SCANCODE_SPACE, Keycode::SPACE}, {SDL_SCANCODE_APOSTROPHE, Keycode::APOSTROPHE}, -- cgit v1.2.3