diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-07 20:39:19 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-07 20:39:19 +0100 |
commit | 666292e33d6997a61d9eaff31e8180a602656474 (patch) | |
tree | dfe0f1cca9b3f8cecf0467fc598598caa32027af /src/crepe/facade | |
parent | fdb4c99e139a264d4e15e6913a3756fc6cccb2f2 (diff) |
save
Diffstat (limited to 'src/crepe/facade')
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 134 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 35 |
2 files changed, 106 insertions, 63 deletions
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 4cc2206..e3410cb 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -375,65 +375,83 @@ ivec2 SDLContext::get_size(const Texture & ctx) { void SDLContext::delay(int ms) const { SDL_Delay(ms); } std::vector<SDLContext::EventData> SDLContext::get_events() { - std::vector<SDLContext::EventData> event_list; - SDL_Event event; - while (SDL_PollEvent(&event)) { - switch (event.type) { - case SDL_QUIT: - event_list.push_back(EventData{ - .event_type = SDLContext::EventType::SHUTDOWN, - }); - break; - case SDL_KEYDOWN: - event_list.push_back(EventData{ - .event_type = SDLContext::EventType::KEYDOWN, - .key = sdl_to_keycode(event.key.keysym.scancode), - .key_repeat = (event.key.repeat != 0), - }); - break; - case SDL_KEYUP: - event_list.push_back(EventData{ - .event_type = SDLContext::EventType::KEYUP, - .key = sdl_to_keycode(event.key.keysym.scancode), - }); - break; - case SDL_MOUSEBUTTONDOWN: - event_list.push_back(EventData{ - .event_type = SDLContext::EventType::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::EventType::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::EventType::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::EventType::MOUSEWHEEL, - .mouse_position = {event.motion.x, event.motion.y}, - // TODO: why is this needed? - .scroll_direction = event.wheel.y < 0 ? -1 : 1, - .scroll_delta = event.wheel.preciseY, - }); - } break; - } - } - return event_list; + std::vector<SDLContext::EventData> event_list; + SDL_Event event; + + // Handle general SDL events + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_QUIT: + event_list.push_back({SDLContext::EventType::SHUTDOWN, {}, {}, {}}); + break; + case SDL_KEYDOWN: + event_list.push_back({SDLContext::EventType::KEYDOWN, + {sdl_to_keycode(event.key.keysym.scancode), event.key.repeat != 0}, {}, {}}); + break; + case SDL_KEYUP: + event_list.push_back({SDLContext::EventType::KEYUP, + {sdl_to_keycode(event.key.keysym.scancode), false}, {}, {}}); + break; + case SDL_MOUSEBUTTONDOWN: + event_list.push_back({SDLContext::EventType::MOUSEDOWN, {}, + {sdl_to_mousebutton(event.button.button), {event.button.x, event.button.y}}, {}}); + break; + case SDL_MOUSEBUTTONUP: + event_list.push_back({SDLContext::EventType::MOUSEUP, {}, + {sdl_to_mousebutton(event.button.button), {event.button.x, event.button.y}}, {}}); + break; + case SDL_MOUSEMOTION: + event_list.push_back({SDLContext::EventType::MOUSEMOVE, {}, + {{}, {event.motion.x, event.motion.y}, -1, INFINITY, {event.motion.xrel, event.motion.yrel}}, {}}); + break; + case SDL_MOUSEWHEEL: + event_list.push_back({SDLContext::EventType::MOUSEWHEEL, {}, + {{}, {}, event.wheel.y < 0 ? -1 : 1, event.wheel.preciseY, {}}, {}}); + break; + + // Forward window events for further processing + case SDL_WINDOWEVENT: + handle_window_event(event.window, event_list); + break; + } + } + + return event_list; +} + +// Separate function for SDL_WINDOWEVENT subtypes +void SDLContext::handle_window_event(const SDL_WindowEvent& window_event, + std::vector<SDLContext::EventData>& event_list) { + switch (window_event.event) { + case SDL_WINDOWEVENT_EXPOSED: + event_list.push_back({SDLContext::EventType::WINDOW_EXPOSE, {}, {}, {}}); + break; + case SDL_WINDOWEVENT_RESIZED: + event_list.push_back({SDLContext::EventType::WINDOW_RESIZE, {}, {}, + {{}, {window_event.data1, window_event.data2}}}); + break; + case SDL_WINDOWEVENT_MOVED: + event_list.push_back({SDLContext::EventType::WINDOW_MOVE, {}, {}, + {{window_event.data1, window_event.data2}, {}}}); + break; + case SDL_WINDOWEVENT_MINIMIZED: + event_list.push_back({SDLContext::EventType::WINDOW_MINIMIZE, {}, {}, {}}); + break; + case SDL_WINDOWEVENT_MAXIMIZED: + event_list.push_back({SDLContext::EventType::WINDOW_MAXIMIZE, {}, {}, {}}); + break; + case SDL_WINDOWEVENT_FOCUS_GAINED: + event_list.push_back({SDLContext::EventType::WINDOW_FOCUS_GAIN, {}, {}, {}}); + break; + case SDL_WINDOWEVENT_FOCUS_LOST: + event_list.push_back({SDLContext::EventType::WINDOW_FOCUS_LOST, {}, {}, {}}); + break; + } } + + + + void SDLContext::set_color_texture(const Texture & texture, const Color & color) { SDL_SetTextureColorMod(texture.texture.get(), color.r, color.g, color.b); SDL_SetTextureAlphaMod(texture.texture.get(), color.a); diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index e232511..1f83985 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -79,19 +79,37 @@ public: KEYUP, KEYDOWN, SHUTDOWN, - + WINDOW_MINIMIZE, + WINDOW_MAXIMIZE, + WINDOW_FOCUS_GAIN, + WINDOW_FOCUS_LOST, + WINDOW_MOVE, + WINDOW_RESIZE, + WINDOW_EXPOSE, }; - //! EventData struct for passing event data from facade - struct EventData { - SDLContext::EventType event_type = SDLContext::EventType::NONE; + 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; + KeyData key_data; + MouseData mouse_data; + WindowData window_data; + }; + /** * \brief Gets the singleton instance of SDLContext. * \return Reference to the SDLContext instance. @@ -116,7 +134,14 @@ private: * \return Events that occurred since last call to `get_events()` */ std::vector<SDLContext::EventData> get_events(); - + /** + * \brief Fills event_list with triggered window events + * + * This method checks if any window events are triggered and adds them to the event_list. + * + */ + void handle_window_event(const SDL_WindowEvent& window_event, + std::vector<SDLContext::EventData>& event_list); /** * \brief Converts an SDL key code to the custom Keycode type. * |