From ccbfb97a11cd931655f2762443ffc36f5f25e86f Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sat, 14 Dec 2024 12:38:22 +0100 Subject: removed sdlcontext from header and implemented initiliser list for EventData --- src/crepe/api/Event.h | 10 +-- src/crepe/api/KeyCodes.h | 4 +- src/crepe/api/Script.cpp | 7 +- src/crepe/api/Script.h | 10 ++- src/crepe/facade/EventData.h | 55 ++++++++++++ src/crepe/facade/SDLContext.cpp | 188 +++++++++++++++++++++------------------ src/crepe/facade/SDLContext.h | 80 ++++------------- src/crepe/system/InputSystem.cpp | 51 +++++------ src/crepe/system/InputSystem.h | 16 ++-- src/test/InputTest.cpp | 1 + 10 files changed, 228 insertions(+), 194 deletions(-) create mode 100644 src/crepe/facade/EventData.h (limited to 'src') diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index 17ae809..d353a5b 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -39,7 +39,7 @@ public: */ class MousePressEvent : public Event { public: - //! mouse position + //! mouse position in game units vec2 mouse_pos = {0, 0}; //! The mouse button that was pressed. @@ -51,7 +51,7 @@ public: */ class MouseClickEvent : public Event { public: - //! mouse position + //! mouse position in game units vec2 mouse_pos = {0, 0}; //! The mouse button that was clicked. @@ -63,7 +63,7 @@ public: */ class MouseReleaseEvent : public Event { public: - //! mouse position + //! mouse position in game units vec2 mouse_pos = {0, 0}; //! The mouse button that was released. @@ -75,7 +75,7 @@ public: */ class MouseMoveEvent : public Event { public: - //! new mouse position + //! mouse position in game units vec2 mouse_pos = {0, 0}; //! The change in mouse position relative to the last position (in pixels). ivec2 mouse_delta = {0, 0}; @@ -86,7 +86,7 @@ public: */ class MouseScrollEvent : public Event { public: - //! mouse position when the scroll happened. + //! mouse position in game units when the scroll happened. vec2 mouse_pos = {0, 0}; //! scroll direction (-1 = down, 1 = up) int scroll_direction = 0; diff --git a/src/crepe/api/KeyCodes.h b/src/crepe/api/KeyCodes.h index dc3219a..1b9573a 100644 --- a/src/crepe/api/KeyCodes.h +++ b/src/crepe/api/KeyCodes.h @@ -3,6 +3,7 @@ #include namespace crepe { + //! Enumeration for mouse button inputs, including standard and extended buttons. enum class MouseButton { NONE = 0, //!< No mouse button input. @@ -154,5 +155,6 @@ enum class Keycode { /// \} MENU = 348, //!< Menu key. }; - +//! Typedef for keyboard state. +typedef std::unordered_map keyboard_state_t; } // namespace crepe diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp index 753a9e3..7b56f61 100644 --- a/src/crepe/api/Script.cpp +++ b/src/crepe/api/Script.cpp @@ -1,7 +1,7 @@ #include #include "../manager/SceneManager.h" - +#include "../facade/SDLContext.h" #include "Script.h" using namespace crepe; @@ -25,3 +25,8 @@ void Script::set_next_scene(const string & name) { } SaveManager & Script::get_save_manager() const { return this->mediator->save_manager; } + +const keyboard_state_t& Script::get_keyboard_state() const{ + SDLContext& sdl_context = this->mediator->sdl_context; + return sdl_context.get_keyboard_state(); +} diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 668e5d1..4fbf344 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -6,6 +6,7 @@ #include "../manager/Mediator.h" #include "../system/CollisionSystem.h" #include "../types.h" +#include "../api/KeyCodes.h" #include "../util/OptionalRef.h" namespace crepe { @@ -134,7 +135,14 @@ protected: //! Retrieve SaveManager reference SaveManager & get_save_manager() const; - + /** + * \brief Utility function to retrieve the keyboard state + * \see SDLContext::get_keyboard_state + * + * \return current keyboard state map with Keycode as key and bool as value(true = pressed, false = not pressed) + * + */ + const keyboard_state_t& get_keyboard_state() const; //! \} private: diff --git a/src/crepe/facade/EventData.h b/src/crepe/facade/EventData.h new file mode 100644 index 0000000..d0ca07f --- /dev/null +++ b/src/crepe/facade/EventData.h @@ -0,0 +1,55 @@ +#pragma once +#include "../api/KeyCodes.h" +#include "../types.h" +namespace crepe { +//! EventType enum for passing eventType + enum EventType { + NONE = 0, + MOUSE_DOWN, + MOUSE_UP, + MOUSE_MOVE, + MOUSE_WHEEL, + KEY_UP, + KEY_DOWN, + SHUTDOWN, + WINDOW_MINIMIZE, + WINDOW_MAXIMIZE, + WINDOW_FOCUS_GAIN, + WINDOW_FOCUS_LOST, + WINDOW_MOVE, + WINDOW_RESIZE, + WINDOW_EXPOSE, + }; + + //! Struct for storing key data. + struct KeyData { + Keycode key = Keycode::NONE; + bool key_repeat = false; + }; + + //! Struct for storing mouse data. + struct MouseData { + MouseButton mouse_button = MouseButton::NONE; + ivec2 mouse_position = {-1, -1}; + int scroll_direction = -1; + float scroll_delta = INFINITY; + ivec2 rel_mouse_move = {-1, -1}; + }; + + //! Struct for storing window data. + struct WindowData { + ivec2 move_delta; + ivec2 resize_dimension; + }; + + //! EventData struct for passing event data from facade + struct EventData { + EventType event_type = EventType::NONE; + union { + KeyData key_data; + MouseData mouse_data; + WindowData window_data; + } data; + + }; +} // namespace crepe diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 7e19ede..bb65e3b 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -76,13 +76,16 @@ SDLContext::~SDLContext() { } Keycode SDLContext::sdl_to_keycode(SDL_Scancode sdl_key) { - auto it = LOOKUP_TABLE.find(sdl_key); - if (it != LOOKUP_TABLE.end()) { - return it->second; - } + if (!LOOKUP_TABLE.contains(sdl_key)) + return Keycode::NONE; + + return LOOKUP_TABLE.at(sdl_key); +} - return Keycode::NONE; +const keyboard_state_t& SDLContext::get_keyboard_state() const{ + return this->keyboard_state; } + void SDLContext::update_keyboard_state() { // Array to hold the key states (true if pressed, false if not) SDL_PumpEvents(); @@ -273,8 +276,8 @@ ivec2 SDLContext::get_size(const Texture & ctx) { return size; } -std::vector SDLContext::get_events() { - std::vector event_list; +std::vector SDLContext::get_events() { + std::vector event_list; SDL_Event event; const CameraAuxiliaryData & cam = this->cam_aux_data; while (SDL_PollEvent(&event)) { @@ -283,77 +286,83 @@ std::vector SDLContext::get_events() { mouse_pos.y = (event.button.y - cam.bar_size.y) / cam.render_scale.y; switch (event.type) { case SDL_QUIT: - event_list.push_back({.event_type = SDLContext::EventType::SHUTDOWN}); + event_list.push_back({.event_type = EventType::SHUTDOWN}); break; - case SDL_KEYDOWN: { + 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, - }; - event_list.push_back(transfer_event); + event_list.push_back(EventData{ + .event_type = EventType::KEY_DOWN, + .data = { + .key_data = { + .key = this->sdl_to_keycode(event.key.keysym.scancode), + .key_repeat = event.key.repeat != 0, + }, + }, + }); break; - } - case SDL_KEYUP: { + 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, - }; - event_list.push_back(transfer_event); + event_list.push_back(EventData{ + .event_type = EventType::KEY_UP, + .data = { + .key_data = { + .key = this->sdl_to_keycode(event.key.keysym.scancode), + .key_repeat = event.key.repeat != 0, + }, + }, + }); break; - } - - case SDL_MOUSEBUTTONDOWN: { - EventData transfer_event; - transfer_event.event_type = SDLContext::EventType::MOUSEDOWN; - transfer_event.data.mouse_data = MouseData{ - .mouse_button = sdl_to_mousebutton(event.button.button), - .mouse_position = mouse_pos, - }; - event_list.push_back(transfer_event); + + case SDL_MOUSEBUTTONDOWN: + event_list.push_back(EventData{ + .event_type = EventType::MOUSE_DOWN, + .data = { + .mouse_data = { + .mouse_button = this->sdl_to_mousebutton(event.button.button), + .mouse_position = mouse_pos, + }, + }, + }); break; - } - case SDL_MOUSEBUTTONUP: { - EventData transfer_event; - transfer_event.event_type = SDLContext::EventType::MOUSEUP; - transfer_event.data.mouse_data = MouseData{ - .mouse_button = sdl_to_mousebutton(event.button.button), - .mouse_position = mouse_pos, - }; - event_list.push_back(transfer_event); + case SDL_MOUSEBUTTONUP: + event_list.push_back(EventData{ + .event_type = EventType::MOUSE_UP, + .data = { + .mouse_data = { + .mouse_button = this->sdl_to_mousebutton(event.button.button), + .mouse_position = mouse_pos, + }, + }, + }); break; - } - - case SDL_MOUSEMOTION: { - EventData transfer_event; - transfer_event.event_type = SDLContext::EventType::MOUSEMOVE; - transfer_event.data.mouse_data = MouseData{ - .mouse_position = mouse_pos, - .rel_mouse_move = {event.motion.xrel, event.motion.yrel}, - }; - event_list.push_back(transfer_event); + + case SDL_MOUSEMOTION: + event_list.push_back(EventData{ + .event_type = EventType::MOUSE_MOVE, + .data = { + .mouse_data = { + .mouse_position = mouse_pos, + .rel_mouse_move = {event.motion.xrel, event.motion.yrel}, + }, + }, + }); break; - } - - case SDL_MOUSEWHEEL: { - EventData transfer_event; - transfer_event.event_type = SDLContext::EventType::MOUSEWHEEL; - transfer_event.data.mouse_data = MouseData{ - .mouse_position = mouse_pos, - .scroll_direction = event.wheel.y < 0 ? -1 : 1, - .scroll_delta = event.wheel.preciseY, - }; - event_list.push_back(transfer_event); + + case SDL_MOUSEWHEEL: + event_list.push_back(EventData{ + .event_type = EventType::MOUSE_WHEEL, + .data = { + .mouse_data = { + .mouse_position = mouse_pos, + .scroll_direction = event.wheel.y < 0 ? -1 : 1, + .scroll_delta = event.wheel.preciseY, + }, + }, + }); break; - } case SDL_WINDOWEVENT: - handle_window_event(event.window, event_list); + this->handle_window_event(event.window, event_list); break; } } @@ -362,38 +371,43 @@ std::vector SDLContext::get_events() { } void SDLContext::handle_window_event(const SDL_WindowEvent & window_event, - std::vector & event_list) { + std::vector & event_list) { switch (window_event.event) { case SDL_WINDOWEVENT_EXPOSED: - event_list.push_back(EventData{SDLContext::EventType::WINDOW_EXPOSE}); + event_list.push_back(EventData{EventType::WINDOW_EXPOSE}); break; - case SDL_WINDOWEVENT_RESIZED: { - EventData transfer_event; - transfer_event.event_type = SDLContext::EventType::WINDOW_RESIZE; - transfer_event.data.window_data - = WindowData{.resize_dimension = {window_event.data1, window_event.data2}}; - event_list.push_back(transfer_event); + case SDL_WINDOWEVENT_RESIZED: + event_list.push_back(EventData{ + .event_type = EventType::WINDOW_RESIZE, + .data = { + .window_data = { + .resize_dimension = {window_event.data1, window_event.data2} + }, + }, + }); break; - } - case SDL_WINDOWEVENT_MOVED: { - EventData transfer_event; - transfer_event.event_type = SDLContext::EventType::WINDOW_MOVE; - transfer_event.data.window_data - = WindowData{.move_delta = {window_event.data1, window_event.data2}}; - event_list.push_back(transfer_event); + case SDL_WINDOWEVENT_MOVED: + event_list.push_back(EventData{ + .event_type = EventType::WINDOW_MOVE, + .data = { + .window_data = { + .move_delta = {window_event.data1, window_event.data2} + }, + }, + }); break; - } + case SDL_WINDOWEVENT_MINIMIZED: - event_list.push_back(EventData{SDLContext::EventType::WINDOW_MINIMIZE}); + event_list.push_back(EventData{EventType::WINDOW_MINIMIZE}); break; case SDL_WINDOWEVENT_MAXIMIZED: - event_list.push_back(EventData{SDLContext::EventType::WINDOW_MAXIMIZE}); + event_list.push_back(EventData{EventType::WINDOW_MAXIMIZE}); break; case SDL_WINDOWEVENT_FOCUS_GAINED: - event_list.push_back(EventData{SDLContext::EventType::WINDOW_FOCUS_GAIN}); + event_list.push_back(EventData{EventType::WINDOW_FOCUS_GAIN}); break; case SDL_WINDOWEVENT_FOCUS_LOST: - event_list.push_back(EventData{SDLContext::EventType::WINDOW_FOCUS_LOST}); + event_list.push_back(EventData{EventType::WINDOW_FOCUS_LOST}); break; } } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 7ccefeb..34a4a09 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -17,11 +17,11 @@ #include "api/KeyCodes.h" #include "api/Sprite.h" #include "api/Transform.h" +#include "../types.h" -#include "types.h" +#include "EventData.h" namespace crepe { - class Texture; class Mediator; @@ -71,61 +71,7 @@ public: }; public: - //! EventType enum for passing eventType - enum EventType { - NONE = 0, - MOUSEDOWN, - MOUSEUP, - MOUSEMOVE, - MOUSEWHEEL, - KEYUP, - KEYDOWN, - SHUTDOWN, - WINDOW_MINIMIZE, - WINDOW_MAXIMIZE, - WINDOW_FOCUS_GAIN, - WINDOW_FOCUS_LOST, - WINDOW_MOVE, - WINDOW_RESIZE, - WINDOW_EXPOSE, - }; - struct KeyData { - Keycode key = Keycode::NONE; - bool key_repeat = false; - }; - struct MouseData { - MouseButton mouse_button = MouseButton::NONE; - ivec2 mouse_position = {-1, -1}; - int scroll_direction = -1; - float scroll_delta = INFINITY; - ivec2 rel_mouse_move = {-1, -1}; - }; - struct WindowData { - ivec2 move_delta; - ivec2 resize_dimension; - }; - //! EventData struct for passing event data from facade - struct EventData { - SDLContext::EventType event_type = SDLContext::EventType::NONE; - - union EventDataUnion { - KeyData key_data; - MouseData mouse_data; - WindowData window_data; - - EventDataUnion() {} - ~EventDataUnion() {} - } data; - }; - /** - * \brief Retrieves the current state of the keyboard. - * - * 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). - * - */ - void update_keyboard_state(); + /** * \brief Gets the singleton instance of SDLContext. * \return Reference to the SDLContext instance. @@ -161,7 +107,7 @@ public: * * \return Events that occurred since last call to `get_events()` */ - std::vector get_events(); + std::vector get_events(); /** * \brief Fills event_list with triggered window events * @@ -169,7 +115,7 @@ public: * */ void handle_window_event(const SDL_WindowEvent & window_event, - std::vector & event_list); + std::vector & event_list); /** * \brief Converts an SDL scan code to the custom Keycode type. * @@ -191,7 +137,8 @@ public: * \return The corresponding `MouseButton` value or `MouseButton::NONE` if the key is unrecognized */ MouseButton sdl_to_mousebutton(Uint8 sdl_button); - + const keyboard_state_t& get_keyboard_state() const; + public: /** * \brief Gets the current SDL ticks since the program started. @@ -288,7 +235,18 @@ private: CameraAuxiliaryData cam_aux_data; private: - std::unordered_map keyboard_state; + /** + * \brief Retrieves the current state of the keyboard. + * + * 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). + * + */ + void update_keyboard_state(); + //! variable to store the state of each key (true = pressed, false = not pressed) + keyboard_state_t keyboard_state; + //! lookup table for converting SDL_SCANCODES to Keycodes const std::unordered_map LOOKUP_TABLE = {{SDL_SCANCODE_SPACE, Keycode::SPACE}, {SDL_SCANCODE_APOSTROPHE, Keycode::APOSTROPHE}, diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index 8de700b..1427adf 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -1,7 +1,7 @@ #include "../api/Button.h" #include "../manager/ComponentManager.h" #include "../manager/EventManager.h" -#include "facade/SDLContext.h" +#include "../facade/SDLContext.h" #include "util/Log.h" #include "InputSystem.h" @@ -12,7 +12,7 @@ void InputSystem::update() { ComponentManager & mgr = this->mediator.component_manager; SDLContext & context = this->mediator.sdl_context; - std::vector event_list = context.get_events(); + std::vector event_list = context.get_events(); RefVector