summaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorancarola <raffaele.ancarola@epfl.ch>2019-02-03 21:52:52 +0100
committerNao Pross <naopross@thearcway.org>2019-02-06 23:33:11 +0100
commit0d30bb843d3aeeacae211e1d0ba3cbf5faa01cd7 (patch)
treebbe7c5b43fb2f6b451336f69bef27153a84bbc24 /include
parentAdd basic texture wrapper (diff)
downloadlibwsdl2-0d30bb843d3aeeacae211e1d0ba3cbf5faa01cd7.tar.gz
libwsdl2-0d30bb843d3aeeacae211e1d0ba3cbf5faa01cd7.zip
Create an interface to SDL eventsHEADmaster
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
Diffstat (limited to 'include')
-rw-r--r--include/event.hpp183
-rw-r--r--include/video.hpp9
2 files changed, 180 insertions, 12 deletions
diff --git a/include/event.hpp b/include/event.hpp
index 9932cb8..12e898f 100644
--- a/include/event.hpp
+++ b/include/event.hpp
@@ -1,24 +1,189 @@
#pragma once
-#include <optional>
+#include "mm/mmvec.hpp"
+#include <memory>
extern "C" {
#include <SDL2/SDL_events.h>
}
namespace wsdl2 {
- class event {
- public:
- using type = SDL_EventType;
- event(const SDL_Event& e);
+ // forward declaration
+ class window;
+}
- SDL_Event& sdl() { return m_event; }
+namespace wsdl2::event {
+
+ class event_t {
- private:
SDL_Event m_event;
+
+ protected:
+
+ SDL_Event& sdl() { return m_event; }
+
+ const SDL_Event& sdl() const { return m_event; }
+
+ public:
+
+ event_t(const SDL_Event& e);
+
+ // polymorphic
+ virtual ~event_t() {}
+
+ // copy constructor
+ event_t(const event_t& e);
+
+ uint32_t type() const;
+ };
+
+ struct e_key : public event_t
+ {
+ enum class action_t : unsigned
+ {
+ up = SDL_KEYUP,
+ down = SDL_KEYDOWN
+ };
+
+ using event_t::event_t;
+ e_key(const event_t& e) : event_t(e) {}
+
+ action_t action() const;
+
+ // key pressed or released
+ SDL_Keycode get() const;
+ };
+
+ namespace mouse {
+
+ enum class action_t : unsigned
+ {
+ button_up = SDL_MOUSEBUTTONUP,
+ button_down = SDL_MOUSEBUTTONDOWN,
+ motion = SDL_MOUSEMOTION,
+ wheel = SDL_MOUSEWHEEL
+ };
+
+ struct e_mouse : public event_t
+ {
+ virtual ~e_mouse() {}
+
+ using event_t::event_t;
+ e_mouse(const event_t& e) : event_t(e) {}
+
+ action_t action() const;
+
+ virtual mm::vec2<int> location() const = 0;
+ };
+
+ struct e_button : public e_mouse
+ {
+ using e_mouse::e_mouse;
+ e_button(const event_t& e) : e_mouse(e) {}
+
+ enum class button_t : unsigned
+ {
+ left = SDL_BUTTON_LEFT,
+ middle = SDL_BUTTON_MIDDLE,
+ right = SDL_BUTTON_RIGHT,
+ x1 = SDL_BUTTON_X1,
+ x2 = SDL_BUTTON_X2
+ };
+
+ mm::vec2<int> clicks() const;
+
+ button_t which() const;
+
+ virtual mm::vec2<int> location() const override;
+ };
+
+ struct e_motion : public e_mouse
+ {
+ using e_mouse::e_mouse;
+ e_motion(const event_t& e) : e_mouse(e) {}
+
+ // delta (x, y)
+ mm::vec2<int> movement() const;
+
+ virtual mm::vec2<int> location() const override;
+ };
+
+ struct e_wheel : public e_mouse
+ {
+ using e_mouse::e_mouse;
+ e_wheel(const event_t& e) : e_mouse(e) {}
+
+ int horizontal() const;
+ int vertical() const;
+
+ virtual mm::vec2<int> location() const override;
+ };
+
+ }
+
+ struct e_quit : public event_t
+ {
+ using event_t::event_t;
+ e_quit(const event_t& e) : event_t(e) {}
};
+ namespace window {
+
+ enum class action_t : unsigned
+ {
+ shown = SDL_WINDOWEVENT_SHOWN,
+ hidden = SDL_WINDOWEVENT_HIDDEN,
+ exposed = SDL_WINDOWEVENT_EXPOSED,
+ moved = SDL_WINDOWEVENT_MOVED,
+ resized = SDL_WINDOWEVENT_RESIZED,
+ size_changed = SDL_WINDOWEVENT_SIZE_CHANGED,
+ minimized = SDL_WINDOWEVENT_MINIMIZED,
+ maximized = SDL_WINDOWEVENT_MAXIMIZED,
+ restored = SDL_WINDOWEVENT_RESTORED,
+ enter = SDL_WINDOWEVENT_ENTER,
+ leave = SDL_WINDOWEVENT_LEAVE,
+ focus_gained = SDL_WINDOWEVENT_FOCUS_GAINED,
+ focus_lost = SDL_WINDOWEVENT_FOCUS_LOST,
+ close = SDL_WINDOWEVENT_CLOSE,
+ take_focus = SDL_WINDOWEVENT_TAKE_FOCUS,
+ hit_test = SDL_WINDOWEVENT_HIT_TEST
+ };
+
+
+ struct e_window : public event_t
+ {
+
+ using event_t::event_t;
+ e_window(const event_t& e) : event_t(e) {}
+
+ // TODO, mapping sdl window id with wsdl2 object
+ wsdl2::window * window();
+
+ action_t action() const;
+ };
+
+ // window positional event
+ struct e_move : public e_window
+ {
+ using e_window::e_window;
+ e_move(const event_t& e) : e_window(e) {}
+
+ mm::vec2<uint32_t> position();
+ };
+
+ // window bound event
+ struct e_resize : public e_window
+ {
+ using e_window::e_window;
+ e_resize(const event_t& e) : e_window(e) {}
+
+ mm::vec2<uint32_t> size();
+ };
+ }
+
+ // TODO other handlers
+
+ std::shared_ptr<event_t> poll_event();
+}
- std::optional<event> poll_event();
-} \ No newline at end of file
diff --git a/include/video.hpp b/include/video.hpp
index 1d71d21..7fbf859 100644
--- a/include/video.hpp
+++ b/include/video.hpp
@@ -4,6 +4,7 @@
#include <string>
#include <array>
+#include <map>
#include <type_traits>
extern "C" {
@@ -236,6 +237,9 @@ namespace wsdl2 {
/// a basic wrapper around a SDL window
class window {
+
+ static std::map<Uint8, window*> win_map;
+
public:
friend class renderer;
@@ -261,6 +265,8 @@ namespace wsdl2 {
renderer& get_renderer() { return m_renderer; }
void update();
+ static window * get(Uint8 id);
+
private:
bool m_open;
renderer m_renderer;
@@ -269,7 +275,4 @@ namespace wsdl2 {
// dirty C code
SDL_Window* sdl();
};
-
-
-
}