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/Script.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/crepe/api/Script.h') 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: -- cgit v1.2.3 From dbe779ca850ec0bf5d9b5066a7a6df44f3762e4d Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sat, 14 Dec 2024 13:52:56 +0100 Subject: make format --- src/crepe/api/Event.h | 10 ++--- src/crepe/api/Script.cpp | 6 +-- src/crepe/api/Script.h | 4 +- src/crepe/facade/EventData.h | 89 ++++++++++++++++++++-------------------- src/crepe/facade/SDLContext.cpp | 9 ++-- src/crepe/facade/SDLContext.h | 8 ++-- src/crepe/system/InputSystem.cpp | 10 ++--- src/example/button.cpp | 12 +++--- src/test/InputTest.cpp | 3 +- 9 files changed, 72 insertions(+), 79 deletions(-) (limited to 'src/crepe/api/Script.h') diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index 4e57b45..73bf461 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 in game units + //! mouse position in world coordinates (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 in game units + //! mouse position in world coordinates (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 in game units + //! mouse position in world coordinates (game units). vec2 mouse_pos = {0, 0}; //! The mouse button that was released. @@ -75,7 +75,7 @@ public: */ class MouseMoveEvent : public Event { public: - //! mouse position in game units + //! mouse position in world coordinates (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 in game units when the scroll happened. + //! mouse position in world coordinates (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/Script.cpp b/src/crepe/api/Script.cpp index 7b56f61..638cca7 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 "../manager/SceneManager.h" #include "Script.h" using namespace crepe; @@ -26,7 +26,7 @@ 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; +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 4fbf344..dcecc07 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -2,11 +2,11 @@ #include +#include "../api/KeyCodes.h" #include "../manager/EventManager.h" #include "../manager/Mediator.h" #include "../system/CollisionSystem.h" #include "../types.h" -#include "../api/KeyCodes.h" #include "../util/OptionalRef.h" namespace crepe { @@ -142,7 +142,7 @@ protected: * \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; + const keyboard_state_t & get_keyboard_state() const; //! \} private: diff --git a/src/crepe/facade/EventData.h b/src/crepe/facade/EventData.h index d0ca07f..a7526b4 100644 --- a/src/crepe/facade/EventData.h +++ b/src/crepe/facade/EventData.h @@ -3,53 +3,52 @@ #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, - }; +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 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 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; +//! 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 e5b0284..7ccc243 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -75,18 +75,17 @@ SDLContext::~SDLContext() { } Keycode SDLContext::sdl_to_keycode(SDL_Scancode sdl_key) { - if (!LOOKUP_TABLE.contains(sdl_key)) - return Keycode::NONE; + if (!LOOKUP_TABLE.contains(sdl_key)) return Keycode::NONE; return LOOKUP_TABLE.at(sdl_key); } -const keyboard_state_t& SDLContext::get_keyboard_state(){ +const keyboard_state_t & SDLContext::get_keyboard_state() { 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(i)); if (key != Keycode::NONE) { this->keyboard_state[key] = current_state[i] != 0; @@ -283,7 +282,7 @@ std::vector SDLContext::get_events() { case SDL_QUIT: event_list.push_back({.event_type = EventType::SHUTDOWN}); break; - case SDL_KEYDOWN: + case SDL_KEYDOWN: event_list.push_back(EventData{ .event_type = EventType::KEY_DOWN, .data = { diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 3531680..8f4760e 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -5,19 +5,18 @@ #include #include #include -#include #include #include #include #include #include +#include "../types.h" #include "api/Camera.h" #include "api/Color.h" #include "api/KeyCodes.h" #include "api/Sprite.h" #include "api/Transform.h" -#include "../types.h" #include "EventData.h" @@ -71,7 +70,6 @@ public: }; public: - /** * \brief Gets the singleton instance of SDLContext. * \return Reference to the SDLContext instance. @@ -146,8 +144,8 @@ public: * \return A constant reference to the `keyboard_state_t`, which holds the state * of each key (true = pressed, false = not pressed). */ - const keyboard_state_t& get_keyboard_state(); - + const keyboard_state_t & get_keyboard_state(); + public: /** * \brief Gets the current SDL ticks since the program started. diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index 1427adf..fca540f 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -1,7 +1,7 @@ #include "../api/Button.h" +#include "../facade/SDLContext.h" #include "../manager/ComponentManager.h" #include "../manager/EventManager.h" -#include "../facade/SDLContext.h" #include "util/Log.h" #include "InputSystem.h" @@ -47,8 +47,8 @@ void InputSystem::update() { } } -void InputSystem::handle_mouse_event(const EventData & event, - const vec2 & camera_origin, const Camera & current_cam) { +void InputSystem::handle_mouse_event(const EventData & event, const vec2 & camera_origin, + const Camera & current_cam) { EventManager & event_mgr = this->mediator.event_manager; vec2 adjusted_mouse; adjusted_mouse.x = event.data.mouse_data.mouse_position.x + camera_origin.x; @@ -153,9 +153,7 @@ void InputSystem::handle_non_mouse_event(const EventData & event) { } } - -void InputSystem::handle_move(const EventData & event_data, - const vec2 & mouse_pos) { +void InputSystem::handle_move(const EventData & event_data, const vec2 & mouse_pos) { ComponentManager & mgr = this->mediator.component_manager; RefVector