From 0d30bb843d3aeeacae211e1d0ba3cbf5faa01cd7 Mon Sep 17 00:00:00 2001 From: ancarola Date: Sun, 3 Feb 2019 21:52:52 +0100 Subject: Create an interface to SDL events List of basic events interfaces in namespace wsdl2::event - event_t - e_key - mouse::e_mouse - mouse::e_motion - mouse::e_button - e_quit (pure SDL quit callback) - window::e_window - window::e_resize - window::e_move --- event.cpp | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 92 insertions(+), 8 deletions(-) (limited to 'event.cpp') diff --git a/event.cpp b/event.cpp index 60d432e..a6830e6 100644 --- a/event.cpp +++ b/event.cpp @@ -1,21 +1,105 @@ #include "event.hpp" - -#include +#include "video.hpp" extern "C" { #include } -wsdl2::event::event(const SDL_Event& e) { - m_event = e; +using namespace wsdl2::event; + +event_t::event_t(const SDL_Event& e) : m_event(e) {} + +event_t::event_t(const event_t& e) : m_event(e.m_event) {} + +uint32_t event_t::type() const +{ + return m_event.type; } -std::optional wsdl2::poll_event() { +std::shared_ptr poll_event() { + SDL_Event ev; +#define EV_PTR(__type__) std::make_shared(ev) + + using namespace wsdl2::event; + if (SDL_PollEvent(&ev) != 0) { - return event(ev); + + switch (ev.type) + { + + // keyboard events + case SDL_KEYUP: + return EV_PTR(e_key); + case SDL_KEYDOWN: + return EV_PTR(e_key); + + // mouse events + case SDL_MOUSEMOTION: + return EV_PTR(mouse::e_motion); + case SDL_MOUSEBUTTONDOWN: + return EV_PTR(mouse::e_button); + case SDL_MOUSEBUTTONUP: + return EV_PTR(mouse::e_button); + case SDL_MOUSEWHEEL: + return EV_PTR(mouse::e_wheel); + + // sdl quit event + case SDL_QUIT: + return EV_PTR(e_quit); + + // window events + case SDL_WINDOWEVENT: + + switch (ev.window.event) + { + case (SDL_WINDOWEVENT_MOVED): + return EV_PTR(window::e_move); + case (SDL_WINDOWEVENT_RESIZED): + return EV_PTR(window::e_resize); + case (SDL_WINDOWEVENT_SIZE_CHANGED): + return EV_PTR(window::e_resize); + default: + return EV_PTR(window::e_window); + } + } + + return nullptr; } - return std::nullopt; -} \ No newline at end of file + return nullptr; +} + + +/* + * Keyboard + */ + +e_key::action_t e_key::action() const +{ + return static_cast(sdl().type); +} + +SDL_Keycode e_key::get() const +{ + return sdl().key.keysym.sym; +} + +/* + * Mouse + */ + +mouse::action_t mouse::e_mouse::action() const +{ + return static_cast(sdl().type); +} + +mm::vec2 mouse::e_mouse::location() const +{ + // TODO + return mm::vec2({}); +} + +// TODO other structures +// -- cgit v1.2.1