diff options
Diffstat (limited to 'src/crepe/facade')
-rw-r--r-- | src/crepe/facade/EventData.h | 55 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 188 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 80 |
3 files changed, 175 insertions, 148 deletions
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::EventData> SDLContext::get_events() { - std::vector<SDLContext::EventData> event_list; +std::vector<EventData> SDLContext::get_events() { + std::vector<EventData> event_list; SDL_Event event; const CameraAuxiliaryData & cam = this->cam_aux_data; while (SDL_PollEvent(&event)) { @@ -283,77 +286,83 @@ std::vector<SDLContext::EventData> 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::EventData> SDLContext::get_events() { } void SDLContext::handle_window_event(const SDL_WindowEvent & window_event, - std::vector<SDLContext::EventData> & event_list) { + std::vector<EventData> & 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<SDLContext::EventData> get_events(); + std::vector<EventData> 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<SDLContext::EventData> & event_list); + std::vector<EventData> & 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<Keycode, bool> 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<SDL_Scancode, Keycode> LOOKUP_TABLE = {{SDL_SCANCODE_SPACE, Keycode::SPACE}, {SDL_SCANCODE_APOSTROPHE, Keycode::APOSTROPHE}, |