diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-18 16:55:21 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-18 16:55:21 +0100 |
commit | 92e83ded2b6afb26082d7661c28af1d9d4e950a2 (patch) | |
tree | f03fe4f0f19efb4a96d79615d98e2af501550f00 | |
parent | 0e8fcca228e4a55c9553afc5dfed3a38f4c53a91 (diff) |
added mouse and key event triggering
-rw-r--r-- | src/crepe/api/Event.h | 16 | ||||
-rw-r--r-- | src/crepe/api/LoopManager.h | 13 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 114 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 1 | ||||
-rw-r--r-- | src/example/events.cpp | 4 | ||||
-rw-r--r-- | src/example/gameloop.cpp | 33 |
6 files changed, 151 insertions, 30 deletions
diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index d5ddf0a..ef6a791 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -87,6 +87,10 @@ public: //! Y-coordinate of the mouse position at the time of the event. int mouse_y = 0; + // Relative movement in x + int rel_x; + // Relative movement in y + int rel_y; }; /** @@ -113,3 +117,15 @@ public: class ShutDownEvent : public Event { public: }; + +class MouseScrollEvent : public Event { +public: + //! X-coordinate of the mouse position at the time of the event. + int scroll_x = 0; + + //! Y-coordinate of the mouse position at the time of the event. + int scroll_y = 0; + + int direction = 0; +}; + diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index f6904be..b18c9d1 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -10,6 +10,12 @@ namespace crepe { class LoopManager { public: void start(); + /** + * \brief Set game running variable + * + * \param running running (false = game shutdown, true = game running) + */ + void set_running(bool running); LoopManager(); private: @@ -53,12 +59,7 @@ private: * This function updates physics and game logic based on LoopTimer's fixed_delta_time. */ void fixed_update(); - /** - * \brief Set game running variable - * - * \param running running (false = game shutdown, true = game running) - */ - void set_running(bool running); + /** * \brief Function for executing render-related systems. * diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index bd2d33b..8a02f4c 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -82,34 +82,84 @@ SDLContext::~SDLContext() { SDL_Quit(); } -void SDLContext::handle_events(bool & running) { - EventManager& event_manager = EventManager::get_instance(); - //TODO: wouter i need events - SDL_Event event; - SDL_PollEvent(&event); - switch (event.type) { - case SDL_QUIT: - running = false; - break; - case SDL_KEYDOWN: - event_manager.trigger_event(KeyPressEvent{ - .key = this->sdl_to_keycode(event.key.keysym.sym), - .repeat = event.key.repeat - }); - break; - case SDL_MOUSEBUTTONDOWN: - int x, y; - SDL_GetMouseState(&x, &y); - triggerEvent(MousePressedEvent(x, y)); - break; - } +void SDLContext::handle_events(bool &running) { + EventManager& event_manager = EventManager::get_instance(); + SDL_Event event; + + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_QUIT: + running = false; + event_manager.trigger_event(ShutDownEvent{}); + break; + + case SDL_KEYDOWN: + std::cout << "keyDown: " << event.key.keysym.sym << std::endl; + event_manager.trigger_event<KeyPressEvent>(KeyPressEvent{ + .repeat = event.key.repeat, + .key = this->sdl_to_keycode(event.key.keysym.scancode) + }); + break; + + case SDL_KEYUP: + event_manager.trigger_event<KeyReleaseEvent>(KeyReleaseEvent{ + .key = this->sdl_to_keycode(event.key.keysym.scancode), + }); + break; + + case SDL_MOUSEBUTTONDOWN: + { + int x, y; + SDL_GetMouseState(&x, &y); + event_manager.trigger_event<MousePressEvent>(MousePressEvent{ + .mouse_x = x, + .mouse_y = y, + .button = this->sdl_to_mousebutton(event.button.button) + }); + } + break; + + case SDL_MOUSEBUTTONUP: + { + int x, y; + SDL_GetMouseState(&x, &y); + event_manager.trigger_event<MouseReleaseEvent>(MouseReleaseEvent{ + .mouse_x = x, + .mouse_y = y, + .button = this->sdl_to_mousebutton(event.button.button) + }); + } + break; + + case SDL_MOUSEMOTION: + event_manager.trigger_event<MouseMoveEvent>(MouseMoveEvent{ + .mouse_x = event.motion.x, + .mouse_y = event.motion.y, + .rel_x = event.motion.xrel, + .rel_y = event.motion.yrel + }); + break; + + case SDL_MOUSEWHEEL: + event_manager.trigger_event<MouseScrollEvent>(MouseScrollEvent{ + .scroll_x = event.wheel.x, + .scroll_y = event.wheel.y, + .direction = (event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED ? -1 : 1) + }); + break; + + // Add more events as needed for your application. + } + } } + Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) { static const std::array<Keycode, SDL_NUM_SCANCODES> LOOKUP_TABLE = [] { std::array<Keycode, SDL_NUM_SCANCODES> table{}; - table.fill(Keycode::NONE); // Default to NONE for unmapped keys + // Default to NONE for unmapped keys + table.fill(Keycode::NONE); table[SDL_SCANCODE_SPACE] = Keycode::SPACE; table[SDL_SCANCODE_APOSTROPHE] = Keycode::APOSTROPHE; @@ -219,7 +269,27 @@ Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) { return LOOKUP_TABLE[sdl_key]; } +MouseButton SDLContext::sdl_to_mousebutton(Uint8 sdl_button) { + static const std::array<MouseButton, 8> MOUSE_BUTTON_LOOKUP_TABLE = [] { + std::array<MouseButton, 8> table{}; + table.fill(MouseButton::NONE); // Default to NONE for unmapped buttons + + 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 mapped button +} void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer.get()); } void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index ef7161b..cce2fb6 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -55,6 +55,7 @@ private: */ void handle_events(bool & running); Keycode sdl_to_keycode(SDL_Keycode sdlKey); + MouseButton sdl_to_mousebutton(Uint8 sdl_button); private: //! Will only use get_ticks 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<int>(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<int>(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 <iostream> #include "crepe/api/LoopManager.h" +#include <crepe/api/EventManager.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/IKeyListener.h> +#include <crepe/api/IMouseListener.h> using namespace crepe; +class TestKeyListener : public IKeyListener { +public: + bool on_key_pressed(const KeyPressEvent & event) override { + std::cout << "TestKeyListener: Key Pressed - Code: " << static_cast<int>(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<int>(event.key) + << std::endl; + return false; + } +}; +bool on_key_pressed(const KeyPressEvent & event){ + std::cout << "TestKeyListener: Key Pressed - Code: " << static_cast<int>(event.key) + << std::endl; + if(event.key == Keycode::ESCAPE){ + return true; + } + return false; + } int main() { LoopManager gameloop; + TestKeyListener key_listener; + EventManager::get_instance().subscribe<KeyPressEvent>(on_key_pressed); gameloop.start(); + + return 1; } |