From 92e83ded2b6afb26082d7661c28af1d9d4e950a2 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 18 Nov 2024 16:55:21 +0100 Subject: added mouse and key event triggering --- src/example/events.cpp | 4 ++-- src/example/gameloop.cpp | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) (limited to 'src/example') diff --git a/src/example/events.cpp b/src/example/events.cpp index 6431c67..1b9ea45 100644 --- a/src/example/events.cpp +++ b/src/example/events.cpp @@ -56,12 +56,12 @@ public: bool on_key_pressed(const KeyPressEvent & event) override { std::cout << "TestKeyListener: Key Pressed - Code: " << static_cast(event.key) << std::endl; - return true; // Return true if the listener should remain active + return false; } bool on_key_released(const KeyReleaseEvent & event) override { std::cout << "TestKeyListener: Key Released - Code: " << static_cast(event.key) << std::endl; - return true; + return false; } }; int main() { diff --git a/src/example/gameloop.cpp b/src/example/gameloop.cpp index a676f20..d45b3ce 100644 --- a/src/example/gameloop.cpp +++ b/src/example/gameloop.cpp @@ -1,7 +1,40 @@ +#include #include "crepe/api/LoopManager.h" +#include +#include +#include +#include using namespace crepe; +class TestKeyListener : public IKeyListener { +public: + bool on_key_pressed(const KeyPressEvent & event) override { + std::cout << "TestKeyListener: Key Pressed - Code: " << static_cast(event.key) + << std::endl; + if(event.key == Keycode::ESCAPE){ + + } + return false; + } + bool on_key_released(const KeyReleaseEvent & event) override { + std::cout << "TestKeyListener: Key Released - Code: " << static_cast(event.key) + << std::endl; + return false; + } +}; +bool on_key_pressed(const KeyPressEvent & event){ + std::cout << "TestKeyListener: Key Pressed - Code: " << static_cast(event.key) + << std::endl; + if(event.key == Keycode::ESCAPE){ + return true; + } + return false; + } int main() { LoopManager gameloop; + TestKeyListener key_listener; + EventManager::get_instance().subscribe(on_key_pressed); gameloop.start(); + + return 1; } -- cgit v1.2.3 From 48015cd425b26eb68eb07f4e4b1adf71e81e11b1 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 25 Nov 2024 11:48:39 +0100 Subject: make format --- mwe/events/include/event.h | 2 +- src/crepe/api/Button.cpp | 2 +- src/crepe/api/Button.h | 28 +-- src/crepe/api/Event.h | 6 +- src/crepe/api/KeyCodes.h | 284 +++++++++++++++---------------- src/crepe/api/LoopManager.cpp | 6 +- src/crepe/api/UiObject.h | 16 +- src/crepe/facade/SDLContext.cpp | 357 +++++++++++++++++++-------------------- src/crepe/facade/SDLContext.h | 16 +- src/crepe/system/InputSystem.cpp | 259 ++++++++++++++-------------- src/crepe/system/InputSystem.h | 43 ++--- src/example/gameloop.cpp | 22 ++- src/test/EventTest.cpp | 74 ++++---- src/test/inputTest.cpp | 307 +++++++++++++++++---------------- src/test/loopTimerTest.cpp | 25 ++- 15 files changed, 714 insertions(+), 733 deletions(-) (limited to 'src/example') diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index ee1bf52..e1b220b 100644 --- a/mwe/events/include/event.h +++ b/mwe/events/include/event.h @@ -148,7 +148,7 @@ private: }; class ShutDownEvent : public Event { public: - ShutDownEvent() : Event("ShutDownEvent") {}; + ShutDownEvent() : Event("ShutDownEvent"){}; REGISTER_EVENT_TYPE(ShutDownEvent) diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp index 70f749d..547c0fc 100644 --- a/src/crepe/api/Button.cpp +++ b/src/crepe/api/Button.cpp @@ -2,4 +2,4 @@ using namespace crepe; -Button::Button(game_object_id_t id) : UiObject(id){} +Button::Button(game_object_id_t id) : UiObject(id) {} diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h index f769d58..0056238 100644 --- a/src/crepe/api/Button.h +++ b/src/crepe/api/Button.h @@ -12,33 +12,33 @@ namespace crepe { */ class Button : public UiObject { public: - /** + /** * \brief Constructs a Button with the specified game object ID. * \param id The unique ID of the game object associated with this button. */ - Button(game_object_id_t id); + Button(game_object_id_t id); - //! Indicates if the button is interactable (can be clicked). - bool interactable = true; + //! Indicates if the button is interactable (can be clicked). + bool interactable = true; - //! Indicates if the button is a toggle button (can be pressed and released). - bool is_toggle = false; + //! Indicates if the button is a toggle button (can be pressed and released). + bool is_toggle = false; - //! Indicates whether the button is currently pressed. - bool is_pressed = false; + //! Indicates whether the button is currently pressed. + bool is_pressed = false; - //! Indicates whether the mouse is currently hovering over the button. - bool hover = false; + //! Indicates whether the mouse is currently hovering over the button. + bool hover = false; - //! The callback function to be executed when the button is clicked. - std::function on_click; + //! The callback function to be executed when the button is clicked. + std::function on_click; public: - /** + /** * \brief Retrieves the maximum number of instances allowed for this button type. * \return Always returns 1, as only a single instance is allowed. */ - virtual int get_instances_max() const override { return 1; } + virtual int get_instances_max() const override { return 1; } }; } // namespace crepe diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index 46dd86b..2018d52 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -89,9 +89,9 @@ public: //! Y-coordinate of the mouse position at the time of the event. int mouse_y = 0; // Relative movement in x - int rel_x; + int rel_x; // Relative movement in y - int rel_y; + int rel_y; }; /** @@ -127,4 +127,4 @@ class ShutDownEvent : public Event { public: }; -} +} // namespace crepe diff --git a/src/crepe/api/KeyCodes.h b/src/crepe/api/KeyCodes.h index 4b026c5..fcfc080 100644 --- a/src/crepe/api/KeyCodes.h +++ b/src/crepe/api/KeyCodes.h @@ -1,154 +1,154 @@ #pragma once namespace crepe { - //! Enumeration for mouse button inputs, including standard and extended buttons. - enum class MouseButton { - NONE = 0, //!< No mouse button input. - LEFT_MOUSE = 1, //!< Left mouse button. - RIGHT_MOUSE = 2, //!< Right mouse button. - MIDDLE_MOUSE = 3, //!< Middle mouse button (scroll wheel press). - X1_MOUSE = 4, //!< First extended mouse button. - X2_MOUSE = 5, //!< Second extended mouse button. - SCROLL_UP = 6, //!< Scroll wheel upward movement. - SCROLL_DOWN = 7, //!< Scroll wheel downward movement. - }; +//! Enumeration for mouse button inputs, including standard and extended buttons. +enum class MouseButton { + NONE = 0, //!< No mouse button input. + LEFT_MOUSE = 1, //!< Left mouse button. + RIGHT_MOUSE = 2, //!< Right mouse button. + MIDDLE_MOUSE = 3, //!< Middle mouse button (scroll wheel press). + X1_MOUSE = 4, //!< First extended mouse button. + X2_MOUSE = 5, //!< Second extended mouse button. + SCROLL_UP = 6, //!< Scroll wheel upward movement. + SCROLL_DOWN = 7, //!< Scroll wheel downward movement. +}; - //! Enumeration for keyboard key inputs, including printable characters, function keys, and keypad keys. - enum class Keycode { - NONE = 0, //!< No key input. - SPACE = 32, //!< Spacebar. - APOSTROPHE = 39, //!< Apostrophe ('). - COMMA = 44, //!< Comma (,). - MINUS = 45, //!< Minus (-). - PERIOD = 46, //!< Period (.). - SLASH = 47, //!< Slash (/). - D0 = 48, //!< Digit 0. - D1 = 49, //!< Digit 1. - D2 = 50, //!< Digit 2. - D3 = 51, //!< Digit 3. - D4 = 52, //!< Digit 4. - D5 = 53, //!< Digit 5. - D6 = 54, //!< Digit 6. - D7 = 55, //!< Digit 7. - D8 = 56, //!< Digit 8. - D9 = 57, //!< Digit 9. - SEMICOLON = 59, //!< Semicolon (;). - EQUAL = 61, //!< Equal sign (=). - A = 65, //!< Key 'A'. - B = 66, //!< Key 'B'. - C = 67, //!< Key 'C'. - D = 68, //!< Key 'D'. - E = 69, //!< Key 'E'. - F = 70, //!< Key 'F'. - G = 71, //!< Key 'G'. - H = 72, //!< Key 'H'. - I = 73, //!< Key 'I'. - J = 74, //!< Key 'J'. - K = 75, //!< Key 'K'. - L = 76, //!< Key 'L'. - M = 77, //!< Key 'M'. - N = 78, //!< Key 'N'. - O = 79, //!< Key 'O'. - P = 80, //!< Key 'P'. - Q = 81, //!< Key 'Q'. - R = 82, //!< Key 'R'. - S = 83, //!< Key 'S'. - T = 84, //!< Key 'T'. - U = 85, //!< Key 'U'. - V = 86, //!< Key 'V'. - W = 87, //!< Key 'W'. - X = 88, //!< Key 'X'. - Y = 89, //!< Key 'Y'. - Z = 90, //!< Key 'Z'. - LEFT_BRACKET = 91, //!< Left bracket ([). - BACKSLASH = 92, //!< Backslash (\). - RIGHT_BRACKET = 93, //!< Right bracket (]). - GRAVE_ACCENT = 96, //!< Grave accent (`). - WORLD1 = 161, //!< Non-US key #1. - WORLD2 = 162, //!< Non-US key #2. - ESCAPE = 256, //!< Escape key. - ENTER = 257, //!< Enter key. - TAB = 258, //!< Tab key. - BACKSPACE = 259, //!< Backspace key. - INSERT = 260, //!< Insert key. - DELETE = 261, //!< Delete key. - RIGHT = 262, //!< Right arrow key. - LEFT = 263, //!< Left arrow key. - DOWN = 264, //!< Down arrow key. - UP = 265, //!< Up arrow key. - PAGE_UP = 266, //!< Page Up key. - PAGE_DOWN = 267, //!< Page Down key. - HOME = 268, //!< Home key. - END = 269, //!< End key. - CAPS_LOCK = 280, //!< Caps Lock key. - SCROLL_LOCK = 281, //!< Scroll Lock key. - NUM_LOCK = 282, //!< Num Lock key. - PRINT_SCREEN = 283, //!< Print Screen key. - PAUSE = 284, //!< Pause key. - /** +//! Enumeration for keyboard key inputs, including printable characters, function keys, and keypad keys. +enum class Keycode { + NONE = 0, //!< No key input. + SPACE = 32, //!< Spacebar. + APOSTROPHE = 39, //!< Apostrophe ('). + COMMA = 44, //!< Comma (,). + MINUS = 45, //!< Minus (-). + PERIOD = 46, //!< Period (.). + SLASH = 47, //!< Slash (/). + D0 = 48, //!< Digit 0. + D1 = 49, //!< Digit 1. + D2 = 50, //!< Digit 2. + D3 = 51, //!< Digit 3. + D4 = 52, //!< Digit 4. + D5 = 53, //!< Digit 5. + D6 = 54, //!< Digit 6. + D7 = 55, //!< Digit 7. + D8 = 56, //!< Digit 8. + D9 = 57, //!< Digit 9. + SEMICOLON = 59, //!< Semicolon (;). + EQUAL = 61, //!< Equal sign (=). + A = 65, //!< Key 'A'. + B = 66, //!< Key 'B'. + C = 67, //!< Key 'C'. + D = 68, //!< Key 'D'. + E = 69, //!< Key 'E'. + F = 70, //!< Key 'F'. + G = 71, //!< Key 'G'. + H = 72, //!< Key 'H'. + I = 73, //!< Key 'I'. + J = 74, //!< Key 'J'. + K = 75, //!< Key 'K'. + L = 76, //!< Key 'L'. + M = 77, //!< Key 'M'. + N = 78, //!< Key 'N'. + O = 79, //!< Key 'O'. + P = 80, //!< Key 'P'. + Q = 81, //!< Key 'Q'. + R = 82, //!< Key 'R'. + S = 83, //!< Key 'S'. + T = 84, //!< Key 'T'. + U = 85, //!< Key 'U'. + V = 86, //!< Key 'V'. + W = 87, //!< Key 'W'. + X = 88, //!< Key 'X'. + Y = 89, //!< Key 'Y'. + Z = 90, //!< Key 'Z'. + LEFT_BRACKET = 91, //!< Left bracket ([). + BACKSLASH = 92, //!< Backslash (\). + RIGHT_BRACKET = 93, //!< Right bracket (]). + GRAVE_ACCENT = 96, //!< Grave accent (`). + WORLD1 = 161, //!< Non-US key #1. + WORLD2 = 162, //!< Non-US key #2. + ESCAPE = 256, //!< Escape key. + ENTER = 257, //!< Enter key. + TAB = 258, //!< Tab key. + BACKSPACE = 259, //!< Backspace key. + INSERT = 260, //!< Insert key. + DELETE = 261, //!< Delete key. + RIGHT = 262, //!< Right arrow key. + LEFT = 263, //!< Left arrow key. + DOWN = 264, //!< Down arrow key. + UP = 265, //!< Up arrow key. + PAGE_UP = 266, //!< Page Up key. + PAGE_DOWN = 267, //!< Page Down key. + HOME = 268, //!< Home key. + END = 269, //!< End key. + CAPS_LOCK = 280, //!< Caps Lock key. + SCROLL_LOCK = 281, //!< Scroll Lock key. + NUM_LOCK = 282, //!< Num Lock key. + PRINT_SCREEN = 283, //!< Print Screen key. + PAUSE = 284, //!< Pause key. + /** * \name Function keys (F1-F25). * \{ */ - F1 = 290, - F2 = 291, - F3 = 292, - F4 = 293, - F5 = 294, - F6 = 295, - F7 = 296, - F8 = 297, - F9 = 298, - F10 = 299, - F11 = 300, - F12 = 301, - F13 = 302, - F14 = 303, - F15 = 304, - F16 = 305, - F17 = 306, - F18 = 307, - F19 = 308, - F20 = 309, - F21 = 310, - F22 = 311, - F23 = 312, - F24 = 313, - F25 = 314, - /// \} - /** + F1 = 290, + F2 = 291, + F3 = 292, + F4 = 293, + F5 = 294, + F6 = 295, + F7 = 296, + F8 = 297, + F9 = 298, + F10 = 299, + F11 = 300, + F12 = 301, + F13 = 302, + F14 = 303, + F15 = 304, + F16 = 305, + F17 = 306, + F18 = 307, + F19 = 308, + F20 = 309, + F21 = 310, + F22 = 311, + F23 = 312, + F24 = 313, + F25 = 314, + /// \} + /** * \name Keypad digits and operators. * \{ */ - KP0 = 320, - KP1 = 321, - KP2 = 322, - KP3 = 323, - KP4 = 324, - KP5 = 325, - KP6 = 326, - KP7 = 327, - KP8 = 328, - KP9 = 329, - KP_DECIMAL = 330, - KP_DIVIDE = 331, - KP_MULTIPLY = 332, - KP_SUBTRACT = 333, - KP_ADD = 334, - KP_ENTER = 335, - KP_EQUAL = 336, - /// \} - /** + KP0 = 320, + KP1 = 321, + KP2 = 322, + KP3 = 323, + KP4 = 324, + KP5 = 325, + KP6 = 326, + KP7 = 327, + KP8 = 328, + KP9 = 329, + KP_DECIMAL = 330, + KP_DIVIDE = 331, + KP_MULTIPLY = 332, + KP_SUBTRACT = 333, + KP_ADD = 334, + KP_ENTER = 335, + KP_EQUAL = 336, + /// \} + /** * \name Modifier keys. * \{ */ - LEFT_SHIFT = 340, - LEFT_CONTROL = 341, - LEFT_ALT = 342, - LEFT_SUPER = 343, - RIGHT_SHIFT = 344, - RIGHT_CONTROL = 345, - RIGHT_ALT = 346, - RIGHT_SUPER = 347, - /// \} - MENU = 348, //!< Menu key. - }; -} + LEFT_SHIFT = 340, + LEFT_CONTROL = 341, + LEFT_ALT = 342, + LEFT_SUPER = 343, + RIGHT_SHIFT = 344, + RIGHT_CONTROL = 345, + RIGHT_ALT = 346, + RIGHT_SUPER = 347, + /// \} + MENU = 348, //!< Menu key. +}; +} // namespace crepe diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 9599943..b343250 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -2,11 +2,11 @@ #include "../system/AnimatorSystem.h" #include "../system/CollisionSystem.h" +#include "../system/InputSystem.h" #include "../system/ParticleSystem.h" #include "../system/PhysicsSystem.h" #include "../system/RenderSystem.h" #include "../system/ScriptSystem.h" -#include "../system/InputSystem.h" #include "LoopManager.h" #include "LoopTimer.h" @@ -24,9 +24,7 @@ LoopManager::LoopManager() { this->load_system(); } -void LoopManager::process_input() { - this->get_system().update(); -} +void LoopManager::process_input() { this->get_system().update(); } void LoopManager::start() { this->setup(); diff --git a/src/crepe/api/UiObject.h b/src/crepe/api/UiObject.h index ae2e744..7bd1c2e 100644 --- a/src/crepe/api/UiObject.h +++ b/src/crepe/api/UiObject.h @@ -10,24 +10,24 @@ namespace crepe { */ class UiObject : public Component { public: - /** + /** * \brief Constructs a UiObject with the specified game object ID. * \param id The unique ID of the game object associated with this UI object. */ - UiObject(game_object_id_t id); + UiObject(game_object_id_t id); - //! The width of the UI object. - int width = 0; + //! The width of the UI object. + int width = 0; - //! The height of the UI object. - int height = 0; + //! The height of the UI object. + int height = 0; public: - /** + /** * \brief Retrieves the maximum number of instances allowed for this UI object type. * /return Always returns 1, as only a single instance is allowed. */ - virtual int get_instances_max() const override { return 1; } + virtual int get_instances_max() const override { return 1; } }; } // namespace crepe diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index a37392f..8ed3654 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -5,23 +5,22 @@ #include #include #include +#include #include #include #include #include #include #include -#include #include "../api/Camera.h" +#include "../api/EventManager.h" #include "../api/Sprite.h" #include "../api/Texture.h" #include "../api/Transform.h" -#include "../api/EventManager.h" #include "../api/Vector2.h" #include "../util/Log.h" - #include "SDLContext.h" using namespace crepe; @@ -77,138 +76,138 @@ SDLContext::~SDLContext() { } Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) { - static const std::array LOOKUP_TABLE = [] { - std::array table{}; - table.fill(Keycode::NONE); - - table[SDL_SCANCODE_SPACE] = Keycode::SPACE; - table[SDL_SCANCODE_APOSTROPHE] = Keycode::APOSTROPHE; - table[SDL_SCANCODE_COMMA] = Keycode::COMMA; - table[SDL_SCANCODE_MINUS] = Keycode::MINUS; - table[SDL_SCANCODE_PERIOD] = Keycode::PERIOD; - table[SDL_SCANCODE_SLASH] = Keycode::SLASH; - table[SDL_SCANCODE_0] = Keycode::D0; - table[SDL_SCANCODE_1] = Keycode::D1; - table[SDL_SCANCODE_2] = Keycode::D2; - table[SDL_SCANCODE_3] = Keycode::D3; - table[SDL_SCANCODE_4] = Keycode::D4; - table[SDL_SCANCODE_5] = Keycode::D5; - table[SDL_SCANCODE_6] = Keycode::D6; - table[SDL_SCANCODE_7] = Keycode::D7; - table[SDL_SCANCODE_8] = Keycode::D8; - table[SDL_SCANCODE_9] = Keycode::D9; - table[SDL_SCANCODE_SEMICOLON] = Keycode::SEMICOLON; - table[SDL_SCANCODE_EQUALS] = Keycode::EQUAL; - table[SDL_SCANCODE_A] = Keycode::A; - table[SDL_SCANCODE_B] = Keycode::B; - table[SDL_SCANCODE_C] = Keycode::C; - table[SDL_SCANCODE_D] = Keycode::D; - table[SDL_SCANCODE_E] = Keycode::E; - table[SDL_SCANCODE_F] = Keycode::F; - table[SDL_SCANCODE_G] = Keycode::G; - table[SDL_SCANCODE_H] = Keycode::H; - table[SDL_SCANCODE_I] = Keycode::I; - table[SDL_SCANCODE_J] = Keycode::J; - table[SDL_SCANCODE_K] = Keycode::K; - table[SDL_SCANCODE_L] = Keycode::L; - table[SDL_SCANCODE_M] = Keycode::M; - table[SDL_SCANCODE_N] = Keycode::N; - table[SDL_SCANCODE_O] = Keycode::O; - table[SDL_SCANCODE_P] = Keycode::P; - table[SDL_SCANCODE_Q] = Keycode::Q; - table[SDL_SCANCODE_R] = Keycode::R; - table[SDL_SCANCODE_S] = Keycode::S; - table[SDL_SCANCODE_T] = Keycode::T; - table[SDL_SCANCODE_U] = Keycode::U; - table[SDL_SCANCODE_V] = Keycode::V; - table[SDL_SCANCODE_W] = Keycode::W; - table[SDL_SCANCODE_X] = Keycode::X; - table[SDL_SCANCODE_Y] = Keycode::Y; - table[SDL_SCANCODE_Z] = Keycode::Z; - table[SDL_SCANCODE_LEFTBRACKET] = Keycode::LEFT_BRACKET; - table[SDL_SCANCODE_BACKSLASH] = Keycode::BACKSLASH; - table[SDL_SCANCODE_RIGHTBRACKET] = Keycode::RIGHT_BRACKET; - table[SDL_SCANCODE_GRAVE] = Keycode::GRAVE_ACCENT; - table[SDL_SCANCODE_ESCAPE] = Keycode::ESCAPE; - table[SDL_SCANCODE_RETURN] = Keycode::ENTER; - table[SDL_SCANCODE_TAB] = Keycode::TAB; - table[SDL_SCANCODE_BACKSPACE] = Keycode::BACKSPACE; - table[SDL_SCANCODE_INSERT] = Keycode::INSERT; - table[SDL_SCANCODE_DELETE] = Keycode::DELETE; - table[SDL_SCANCODE_RIGHT] = Keycode::RIGHT; - table[SDL_SCANCODE_LEFT] = Keycode::LEFT; - table[SDL_SCANCODE_DOWN] = Keycode::DOWN; - table[SDL_SCANCODE_UP] = Keycode::UP; - table[SDL_SCANCODE_PAGEUP] = Keycode::PAGE_UP; - table[SDL_SCANCODE_PAGEDOWN] = Keycode::PAGE_DOWN; - table[SDL_SCANCODE_HOME] = Keycode::HOME; - table[SDL_SCANCODE_END] = Keycode::END; - table[SDL_SCANCODE_CAPSLOCK] = Keycode::CAPS_LOCK; - table[SDL_SCANCODE_SCROLLLOCK] = Keycode::SCROLL_LOCK; - table[SDL_SCANCODE_NUMLOCKCLEAR] = Keycode::NUM_LOCK; - table[SDL_SCANCODE_PRINTSCREEN] = Keycode::PRINT_SCREEN; - table[SDL_SCANCODE_PAUSE] = Keycode::PAUSE; - table[SDL_SCANCODE_F1] = Keycode::F1; - table[SDL_SCANCODE_F2] = Keycode::F2; - table[SDL_SCANCODE_F3] = Keycode::F3; - table[SDL_SCANCODE_F4] = Keycode::F4; - table[SDL_SCANCODE_F5] = Keycode::F5; - table[SDL_SCANCODE_F6] = Keycode::F6; - table[SDL_SCANCODE_F7] = Keycode::F7; - table[SDL_SCANCODE_F8] = Keycode::F8; - table[SDL_SCANCODE_F9] = Keycode::F9; - table[SDL_SCANCODE_F10] = Keycode::F10; - table[SDL_SCANCODE_F11] = Keycode::F11; - table[SDL_SCANCODE_F12] = Keycode::F12; - table[SDL_SCANCODE_KP_0] = Keycode::KP0; - table[SDL_SCANCODE_KP_1] = Keycode::KP1; - table[SDL_SCANCODE_KP_2] = Keycode::KP2; - table[SDL_SCANCODE_KP_3] = Keycode::KP3; - table[SDL_SCANCODE_KP_4] = Keycode::KP4; - table[SDL_SCANCODE_KP_5] = Keycode::KP5; - table[SDL_SCANCODE_KP_6] = Keycode::KP6; - table[SDL_SCANCODE_KP_7] = Keycode::KP7; - table[SDL_SCANCODE_KP_8] = Keycode::KP8; - table[SDL_SCANCODE_KP_9] = Keycode::KP9; - table[SDL_SCANCODE_LSHIFT] = Keycode::LEFT_SHIFT; - table[SDL_SCANCODE_LCTRL] = Keycode::LEFT_CONTROL; - table[SDL_SCANCODE_LALT] = Keycode::LEFT_ALT; - table[SDL_SCANCODE_LGUI] = Keycode::LEFT_SUPER; - table[SDL_SCANCODE_RSHIFT] = Keycode::RIGHT_SHIFT; - table[SDL_SCANCODE_RCTRL] = Keycode::RIGHT_CONTROL; - table[SDL_SCANCODE_RALT] = Keycode::RIGHT_ALT; - table[SDL_SCANCODE_RGUI] = Keycode::RIGHT_SUPER; - table[SDL_SCANCODE_MENU] = Keycode::MENU; - - return table; - }(); - - if (sdl_key < 0 || sdl_key >= SDL_NUM_SCANCODES) { - return Keycode::NONE; - } - - return LOOKUP_TABLE[sdl_key]; + static const std::array LOOKUP_TABLE = [] { + std::array table{}; + table.fill(Keycode::NONE); + + table[SDL_SCANCODE_SPACE] = Keycode::SPACE; + table[SDL_SCANCODE_APOSTROPHE] = Keycode::APOSTROPHE; + table[SDL_SCANCODE_COMMA] = Keycode::COMMA; + table[SDL_SCANCODE_MINUS] = Keycode::MINUS; + table[SDL_SCANCODE_PERIOD] = Keycode::PERIOD; + table[SDL_SCANCODE_SLASH] = Keycode::SLASH; + table[SDL_SCANCODE_0] = Keycode::D0; + table[SDL_SCANCODE_1] = Keycode::D1; + table[SDL_SCANCODE_2] = Keycode::D2; + table[SDL_SCANCODE_3] = Keycode::D3; + table[SDL_SCANCODE_4] = Keycode::D4; + table[SDL_SCANCODE_5] = Keycode::D5; + table[SDL_SCANCODE_6] = Keycode::D6; + table[SDL_SCANCODE_7] = Keycode::D7; + table[SDL_SCANCODE_8] = Keycode::D8; + table[SDL_SCANCODE_9] = Keycode::D9; + table[SDL_SCANCODE_SEMICOLON] = Keycode::SEMICOLON; + table[SDL_SCANCODE_EQUALS] = Keycode::EQUAL; + table[SDL_SCANCODE_A] = Keycode::A; + table[SDL_SCANCODE_B] = Keycode::B; + table[SDL_SCANCODE_C] = Keycode::C; + table[SDL_SCANCODE_D] = Keycode::D; + table[SDL_SCANCODE_E] = Keycode::E; + table[SDL_SCANCODE_F] = Keycode::F; + table[SDL_SCANCODE_G] = Keycode::G; + table[SDL_SCANCODE_H] = Keycode::H; + table[SDL_SCANCODE_I] = Keycode::I; + table[SDL_SCANCODE_J] = Keycode::J; + table[SDL_SCANCODE_K] = Keycode::K; + table[SDL_SCANCODE_L] = Keycode::L; + table[SDL_SCANCODE_M] = Keycode::M; + table[SDL_SCANCODE_N] = Keycode::N; + table[SDL_SCANCODE_O] = Keycode::O; + table[SDL_SCANCODE_P] = Keycode::P; + table[SDL_SCANCODE_Q] = Keycode::Q; + table[SDL_SCANCODE_R] = Keycode::R; + table[SDL_SCANCODE_S] = Keycode::S; + table[SDL_SCANCODE_T] = Keycode::T; + table[SDL_SCANCODE_U] = Keycode::U; + table[SDL_SCANCODE_V] = Keycode::V; + table[SDL_SCANCODE_W] = Keycode::W; + table[SDL_SCANCODE_X] = Keycode::X; + table[SDL_SCANCODE_Y] = Keycode::Y; + table[SDL_SCANCODE_Z] = Keycode::Z; + table[SDL_SCANCODE_LEFTBRACKET] = Keycode::LEFT_BRACKET; + table[SDL_SCANCODE_BACKSLASH] = Keycode::BACKSLASH; + table[SDL_SCANCODE_RIGHTBRACKET] = Keycode::RIGHT_BRACKET; + table[SDL_SCANCODE_GRAVE] = Keycode::GRAVE_ACCENT; + table[SDL_SCANCODE_ESCAPE] = Keycode::ESCAPE; + table[SDL_SCANCODE_RETURN] = Keycode::ENTER; + table[SDL_SCANCODE_TAB] = Keycode::TAB; + table[SDL_SCANCODE_BACKSPACE] = Keycode::BACKSPACE; + table[SDL_SCANCODE_INSERT] = Keycode::INSERT; + table[SDL_SCANCODE_DELETE] = Keycode::DELETE; + table[SDL_SCANCODE_RIGHT] = Keycode::RIGHT; + table[SDL_SCANCODE_LEFT] = Keycode::LEFT; + table[SDL_SCANCODE_DOWN] = Keycode::DOWN; + table[SDL_SCANCODE_UP] = Keycode::UP; + table[SDL_SCANCODE_PAGEUP] = Keycode::PAGE_UP; + table[SDL_SCANCODE_PAGEDOWN] = Keycode::PAGE_DOWN; + table[SDL_SCANCODE_HOME] = Keycode::HOME; + table[SDL_SCANCODE_END] = Keycode::END; + table[SDL_SCANCODE_CAPSLOCK] = Keycode::CAPS_LOCK; + table[SDL_SCANCODE_SCROLLLOCK] = Keycode::SCROLL_LOCK; + table[SDL_SCANCODE_NUMLOCKCLEAR] = Keycode::NUM_LOCK; + table[SDL_SCANCODE_PRINTSCREEN] = Keycode::PRINT_SCREEN; + table[SDL_SCANCODE_PAUSE] = Keycode::PAUSE; + table[SDL_SCANCODE_F1] = Keycode::F1; + table[SDL_SCANCODE_F2] = Keycode::F2; + table[SDL_SCANCODE_F3] = Keycode::F3; + table[SDL_SCANCODE_F4] = Keycode::F4; + table[SDL_SCANCODE_F5] = Keycode::F5; + table[SDL_SCANCODE_F6] = Keycode::F6; + table[SDL_SCANCODE_F7] = Keycode::F7; + table[SDL_SCANCODE_F8] = Keycode::F8; + table[SDL_SCANCODE_F9] = Keycode::F9; + table[SDL_SCANCODE_F10] = Keycode::F10; + table[SDL_SCANCODE_F11] = Keycode::F11; + table[SDL_SCANCODE_F12] = Keycode::F12; + table[SDL_SCANCODE_KP_0] = Keycode::KP0; + table[SDL_SCANCODE_KP_1] = Keycode::KP1; + table[SDL_SCANCODE_KP_2] = Keycode::KP2; + table[SDL_SCANCODE_KP_3] = Keycode::KP3; + table[SDL_SCANCODE_KP_4] = Keycode::KP4; + table[SDL_SCANCODE_KP_5] = Keycode::KP5; + table[SDL_SCANCODE_KP_6] = Keycode::KP6; + table[SDL_SCANCODE_KP_7] = Keycode::KP7; + table[SDL_SCANCODE_KP_8] = Keycode::KP8; + table[SDL_SCANCODE_KP_9] = Keycode::KP9; + table[SDL_SCANCODE_LSHIFT] = Keycode::LEFT_SHIFT; + table[SDL_SCANCODE_LCTRL] = Keycode::LEFT_CONTROL; + table[SDL_SCANCODE_LALT] = Keycode::LEFT_ALT; + table[SDL_SCANCODE_LGUI] = Keycode::LEFT_SUPER; + table[SDL_SCANCODE_RSHIFT] = Keycode::RIGHT_SHIFT; + table[SDL_SCANCODE_RCTRL] = Keycode::RIGHT_CONTROL; + table[SDL_SCANCODE_RALT] = Keycode::RIGHT_ALT; + table[SDL_SCANCODE_RGUI] = Keycode::RIGHT_SUPER; + table[SDL_SCANCODE_MENU] = Keycode::MENU; + + return table; + }(); + + if (sdl_key < 0 || sdl_key >= SDL_NUM_SCANCODES) { + return Keycode::NONE; + } + + return LOOKUP_TABLE[sdl_key]; } MouseButton SDLContext::sdl_to_mousebutton(Uint8 sdl_button) { - static const std::array MOUSE_BUTTON_LOOKUP_TABLE = [] { - std::array table{}; - table.fill(MouseButton::NONE); - - table[SDL_BUTTON_LEFT] = MouseButton::LEFT_MOUSE; - table[SDL_BUTTON_RIGHT] = MouseButton::RIGHT_MOUSE; - table[SDL_BUTTON_MIDDLE] = MouseButton::MIDDLE_MOUSE; - table[SDL_BUTTON_X1] = MouseButton::X1_MOUSE; - table[SDL_BUTTON_X2] = MouseButton::X2_MOUSE; - - return table; - }(); - - if (sdl_button >= MOUSE_BUTTON_LOOKUP_TABLE.size()) { - // Return NONE for invalid or unmapped button - return MouseButton::NONE; - } + static const std::array MOUSE_BUTTON_LOOKUP_TABLE = [] { + std::array table{}; + table.fill(MouseButton::NONE); + + table[SDL_BUTTON_LEFT] = MouseButton::LEFT_MOUSE; + table[SDL_BUTTON_RIGHT] = MouseButton::RIGHT_MOUSE; + table[SDL_BUTTON_MIDDLE] = MouseButton::MIDDLE_MOUSE; + table[SDL_BUTTON_X1] = MouseButton::X1_MOUSE; + table[SDL_BUTTON_X2] = MouseButton::X2_MOUSE; + + return table; + }(); + + if (sdl_button >= MOUSE_BUTTON_LOOKUP_TABLE.size()) { + // Return NONE for invalid or unmapped button + return MouseButton::NONE; + } - return MOUSE_BUTTON_LOOKUP_TABLE[sdl_button]; + return MOUSE_BUTTON_LOOKUP_TABLE[sdl_button]; } void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer.get()); } void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); } @@ -310,73 +309,63 @@ int SDLContext::get_height(const Texture & ctx) const { } void SDLContext::delay(int ms) const { SDL_Delay(ms); } -std::vector SDLContext::get_events(){ +std::vector SDLContext::get_events() { std::vector event_list; SDL_Event event; while (SDL_PollEvent(&event)) { - switch (event.type) { - case SDL_QUIT: - event_list.push_back(EventData{ + switch (event.type) { + case SDL_QUIT: + event_list.push_back(EventData{ .event_type = SDLContext::Event::SHUTDOWN, }); break; - case SDL_KEYDOWN: + case SDL_KEYDOWN: event_list.push_back(EventData{ .event_type = SDLContext::Event::KEYDOWN, .key = sdl_to_keycode(event.key.keysym.scancode), .key_repeat = (event.key.repeat != 0), }); break; - case SDL_KEYUP: + case SDL_KEYUP: event_list.push_back(EventData{ .event_type = SDLContext::Event::KEYUP, .key = sdl_to_keycode(event.key.keysym.scancode), }); - break; - case SDL_MOUSEBUTTONDOWN: - { - int x,y; - SDL_GetMouseState(&x, &y); - event_list.push_back(EventData{ - .event_type = SDLContext::Event::MOUSEDOWN, - .mouse_button = sdl_to_mousebutton(event.button.button), - .mouse_position = {event.button.x,event.button.y}, - }); - } - break; - case SDL_MOUSEBUTTONUP: - { - int x,y; - SDL_GetMouseState(&x, &y); - event_list.push_back(EventData{ - .event_type = SDLContext::Event::MOUSEUP, - .mouse_button = sdl_to_mousebutton(event.button.button), - .mouse_position = {event.button.x,event.button.y}, - }); - } - break; - - case SDL_MOUSEMOTION: - { - event_list.push_back(EventData{ - .event_type = SDLContext::Event::MOUSEMOVE, - .mouse_position = {event.motion.x,event.motion.y}, - .rel_mouse_move = {event.motion.xrel,event.motion.yrel} - }); - } - break; - - case SDL_MOUSEWHEEL: - { - event_list.push_back(EventData{ - .event_type = SDLContext::Event::MOUSEWHEEL, - .mouse_position = {event.motion.x,event.motion.y}, - .wheel_delta = event.wheel.y, - }); - } - break; - } - } + break; + case SDL_MOUSEBUTTONDOWN: { + int x, y; + SDL_GetMouseState(&x, &y); + event_list.push_back(EventData{ + .event_type = SDLContext::Event::MOUSEDOWN, + .mouse_button = sdl_to_mousebutton(event.button.button), + .mouse_position = {event.button.x, event.button.y}, + }); + } break; + case SDL_MOUSEBUTTONUP: { + int x, y; + SDL_GetMouseState(&x, &y); + event_list.push_back(EventData{ + .event_type = SDLContext::Event::MOUSEUP, + .mouse_button = sdl_to_mousebutton(event.button.button), + .mouse_position = {event.button.x, event.button.y}, + }); + } break; + + case SDL_MOUSEMOTION: { + event_list.push_back( + EventData{.event_type = SDLContext::Event::MOUSEMOVE, + .mouse_position = {event.motion.x, event.motion.y}, + .rel_mouse_move = {event.motion.xrel, event.motion.yrel}}); + } break; + + case SDL_MOUSEWHEEL: { + event_list.push_back(EventData{ + .event_type = SDLContext::Event::MOUSEWHEEL, + .mouse_position = {event.motion.x, event.motion.y}, + .wheel_delta = event.wheel.y, + }); + } break; + } + } return event_list; } - diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 1b881d1..7ec11fd 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -6,15 +6,15 @@ #include #include #include -#include #include #include +#include -#include "../api/Sprite.h" +#include "../api/Event.h" #include "../api/KeyCodes.h" +#include "../api/Sprite.h" #include "../api/Transform.h" #include "../api/Vector2.h" -#include "../api/Event.h" #include "api/Camera.h" #include "types.h" @@ -36,7 +36,7 @@ class InputSystem; class SDLContext { public: - enum Event{ + enum Event { NONE = 0, MOUSEDOWN, MOUSEUP, @@ -52,9 +52,9 @@ public: Keycode key = Keycode::NONE; bool key_repeat = false; MouseButton mouse_button = MouseButton::NONE; - std::pair mouse_position = {-1,-1}; + std::pair mouse_position = {-1, -1}; int wheel_delta = -1; - std::pair rel_mouse_move = {-1,-1}; + std::pair rel_mouse_move = {-1, -1}; }; /** * \brief Gets the singleton instance of SDLContext. @@ -75,11 +75,12 @@ private: * \param running Reference to a boolean flag that controls the main loop. */ std::vector get_events(); - + Keycode get_key(); Keycode get_mouse(); Keycode sdl_to_keycode(SDL_Keycode sdlKey); MouseButton sdl_to_mousebutton(Uint8 sdl_button); + private: //! Will only use get_ticks friend class AnimatorSystem; @@ -197,5 +198,4 @@ private: SDL_Rect viewport = {0, 0, 640, 480}; }; - } // namespace crepe diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index 1b455cd..7f7f9ec 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -1,154 +1,155 @@ -#include "ComponentManager.h" -#include "../api/EventManager.h" #include "../api/Event.h" - +#include "../api/EventManager.h" +#include "ComponentManager.h" #include "system/InputSystem.h" using namespace crepe; void InputSystem::update() { - EventManager &event_mgr = EventManager::get_instance(); - std::vector event_list = SDLContext::get_instance().get_events(); - - for (const SDLContext::EventData &event : event_list) { - switch (event.event_type) { - case SDLContext::Event::KEYDOWN: { - event_mgr.queue_event(KeyPressEvent{ - .repeat = event.key_repeat, - .key = event.key, - }); - break; - } - case SDLContext::Event::KEYUP: { - event_mgr.queue_event(KeyReleaseEvent{ - .key = event.key, - }); - break; - } - case SDLContext::Event::MOUSEDOWN: { - event_mgr.queue_event(MousePressEvent{ - .mouse_x = event.mouse_position.first, - .mouse_y = event.mouse_position.second, - .button = event.mouse_button, - }); - last_mouse_down_position = event.mouse_position; - last_mouse_button = event.mouse_button; - break; - } - case SDLContext::Event::MOUSEUP: { - MouseReleaseEvent mouse_release_event = MouseReleaseEvent{ - .mouse_x = event.mouse_position.first, - .mouse_y = event.mouse_position.second, - .button = event.mouse_button, - }; - event_mgr.queue_event(mouse_release_event); - - // Calculate deltas for click detection - int delta_x = event.mouse_position.first - last_mouse_down_position.first; - int delta_y = event.mouse_position.second - last_mouse_down_position.second; - - if (last_mouse_button == event.mouse_button && - std::abs(delta_x) <= click_tolerance && - std::abs(delta_y) <= click_tolerance) { - event_mgr.queue_event(MouseClickEvent{ - .mouse_x = event.mouse_position.first, - .mouse_y = event.mouse_position.second, - .button = event.mouse_button, - }); - - handle_click(event); - } - break; - } - case SDLContext::Event::MOUSEMOVE: { - event_mgr.queue_event(MouseMoveEvent{ - .mouse_x = event.mouse_position.first, - .mouse_y = event.mouse_position.second, - .rel_x = event.rel_mouse_move.first, - .rel_y = event.rel_mouse_move.second, - }); - handle_move(event); - break; - } - case SDLContext::Event::MOUSEWHEEL: { - event_mgr.queue_event(MouseScrollEvent{ - .scroll_x = event.wheel_delta, - .scroll_y = 0, - .direction = event.wheel_delta, - }); - break; - } - case SDLContext::Event::SHUTDOWN: { - event_mgr.queue_event(ShutDownEvent{}); - break; - } - default: - break; - } - } + EventManager & event_mgr = EventManager::get_instance(); + std::vector event_list = SDLContext::get_instance().get_events(); + + for (const SDLContext::EventData & event : event_list) { + switch (event.event_type) { + case SDLContext::Event::KEYDOWN: { + event_mgr.queue_event(KeyPressEvent{ + .repeat = event.key_repeat, + .key = event.key, + }); + break; + } + case SDLContext::Event::KEYUP: { + event_mgr.queue_event(KeyReleaseEvent{ + .key = event.key, + }); + break; + } + case SDLContext::Event::MOUSEDOWN: { + event_mgr.queue_event(MousePressEvent{ + .mouse_x = event.mouse_position.first, + .mouse_y = event.mouse_position.second, + .button = event.mouse_button, + }); + last_mouse_down_position = event.mouse_position; + last_mouse_button = event.mouse_button; + break; + } + case SDLContext::Event::MOUSEUP: { + MouseReleaseEvent mouse_release_event = MouseReleaseEvent{ + .mouse_x = event.mouse_position.first, + .mouse_y = event.mouse_position.second, + .button = event.mouse_button, + }; + event_mgr.queue_event(mouse_release_event); + + // Calculate deltas for click detection + int delta_x = event.mouse_position.first - last_mouse_down_position.first; + int delta_y = event.mouse_position.second - last_mouse_down_position.second; + + if (last_mouse_button == event.mouse_button + && std::abs(delta_x) <= click_tolerance + && std::abs(delta_y) <= click_tolerance) { + event_mgr.queue_event(MouseClickEvent{ + .mouse_x = event.mouse_position.first, + .mouse_y = event.mouse_position.second, + .button = event.mouse_button, + }); + + handle_click(event); + } + break; + } + case SDLContext::Event::MOUSEMOVE: { + event_mgr.queue_event(MouseMoveEvent{ + .mouse_x = event.mouse_position.first, + .mouse_y = event.mouse_position.second, + .rel_x = event.rel_mouse_move.first, + .rel_y = event.rel_mouse_move.second, + }); + handle_move(event); + break; + } + case SDLContext::Event::MOUSEWHEEL: { + event_mgr.queue_event(MouseScrollEvent{ + .scroll_x = event.wheel_delta, + .scroll_y = 0, + .direction = event.wheel_delta, + }); + break; + } + case SDLContext::Event::SHUTDOWN: { + event_mgr.queue_event(ShutDownEvent{}); + break; + } + default: + break; + } + } } -void InputSystem::handle_move(const SDLContext::EventData &event_data) { - ComponentManager &mgr = this->component_manager; +void InputSystem::handle_move(const SDLContext::EventData & event_data) { + ComponentManager & mgr = this->component_manager; - std::vector> buttons = mgr.get_components_by_type