6 #include <SDL_events.h> 12 #include <begin_code.h> 22 #pragma warning(disable : 26495) 40 SDL_KeyboardEvent
key;
62 #if SDL_VERSION_ATLEAST(2, 0, 9) 83 sizeof(
Event) ==
sizeof(SDL_Event),
84 "Hey, it's likely that the bits in sdl::Event and SDL_Event don't " 85 "lineup. Please check you are using a supported version of SDL2!!");
87 static_assert(offsetof(
Event, common.timestamp) == offsetof(SDL_Event, common.timestamp));
89 static_assert(offsetof(
Event, user.windowID) == offsetof(SDL_Event, user.windowID));
91 memset(
this, 0,
sizeof(SDL_Event));
110 operator SDL_Event()
const {
return *
reinterpret_cast<SDL_Event const*
>(
this); }
113 SDL_Event
const*
ptr()
const {
return reinterpret_cast<SDL_Event const*
>(
this); }
116 SDL_Event*
ptr() {
return reinterpret_cast<SDL_Event*
>(
this); }
132 if (!SDL_WaitEvent(
ptr()))
throw Exception{
"SDL_WaitEvent"};
139 if (!SDL_WaitEventTimeout(
ptr(), timeout))
149 if (!SDL_PushEvent(const_cast<SDL_Event*>(
ptr())))
158 if (SDL_PeepEvents(
ptr(), 1, SDL_PEEKEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) < 0)
165 inline bool has_events() {
return SDL_HasEvents(SDL_FIRSTEVENT, SDL_LASTEVENT); }
169 inline bool has_events(Uint32 type) {
return SDL_HasEvent(type); }
176 return SDL_HasEvents(minType, maxType);
186 inline void flush_events(Uint32 minType, Uint32 maxType) { SDL_FlushEvents(minType, maxType); }
198 inline void add_events(std::vector<Event>
const& events, Uint32 minType, Uint32 maxType)
201 auto array =
const_cast<SDL_Event*
>(
reinterpret_cast<SDL_Event const*
>(&events[0]));
202 if (SDL_PeepEvents(array,
int(events.size()), SDL_ADDEVENT, minType, maxType) < 0)
212 add_events(events, SDL_FIRSTEVENT, SDL_LASTEVENT);
218 inline void add_events(std::vector<Event>
const& events, Uint32 type)
227 inline std::vector<Event>
peek_events(
size_t maxEvents, Uint32 minType, Uint32 maxType)
229 auto res = std::vector<Event>(maxEvents);
230 auto array =
reinterpret_cast<SDL_Event*
>(&res[0]);
231 if (SDL_PeepEvents(array,
int(maxEvents), SDL_PEEKEVENT, minType, maxType) < 0)
241 return peek_events(maxEvents, SDL_FIRSTEVENT, SDL_LASTEVENT);
246 inline std::vector<Event>
peek_events(
size_t maxEvents, Uint32 type)
255 inline std::vector<Event>
get_events(
size_t maxEvents, Uint32 minType, Uint32 maxType)
257 auto res = std::vector<Event>(maxEvents);
258 auto array =
reinterpret_cast<SDL_Event*
>(&res[0]);
259 if (SDL_PeepEvents(array,
int(maxEvents), SDL_GETEVENT, minType, maxType) < 0)
271 return get_events(maxEvents, SDL_FIRSTEVENT, SDL_LASTEVENT);
277 inline std::vector<Event>
get_events(
size_t maxEvents, Uint32 type)
287 void* userdata_ =
nullptr;
289 bool isWatcher_ =
false;
297 if (isWatcher_) del_watcher();
308 void set() { SDL_SetEventFilter(&call_filter, userdata_); }
310 static void unset() { SDL_FilterEvents(
nullptr,
nullptr); }
314 SDL_AddEventWatch(&call_filter,
this);
320 SDL_DelEventWatch(&call_filter,
this);
327 return static_cast<Event::State>(SDL_GetEventState(type));
332 SDL_EventState(type,
int(state));
337 #include <close_code.h> void flush_events()
Clear all events from the event queue.
SDL_ControllerButtonEvent cbutton
Game Controller button event data.
SDL_DisplayEvent display
Sensor event data.
SDL_JoyHatEvent jhat
Joystick hat event data.
static Event const & ref_from(SDL_Event const *e)
& ref_from(SDL_Event const* e)
std::vector< Event > get_events(size_t maxEvents)
Get events from the queue.
SDL_ControllerAxisEvent caxis
Game Controller axis event data.
SDL_JoyAxisEvent jaxis
Joystick axis event data.
void wait()
Wait until next event occur. This will stop the execution of your code until something happens...
SDL_CommonEvent common
Common event data.
std::vector< Event > peek_events(size_t maxEvents, Uint32 minType, Uint32 maxType)
Peek at multiple future events.
static Event & ref_from(SDL_Event *e)
ref_from(SDL_Event& e)
void wait(int timeout)
Wait until next event occur, or until the given duration expired.
SDL_AudioDeviceEvent adevice
Audio device event data.
SDL_WindowEvent window
Window event data.
SDL_MultiGestureEvent mgesture
Gesture event data.
Uint32 type
Event type, shared with all events.
void add_events(std::vector< Event > const &events)
Add events to the queue.
bool has_events(Uint32 type)
Return true if there are events of a specific type in the queue.
Event::State event_state(Uint32 type)
bool has_events()
Return true if there are events in the queue.
SDL_MouseWheelEvent wheel
Mouse wheel event data.
SDL_MouseMotionEvent motion
Mouse motion event data.
SDL_SysWMEvent syswm
System dependent window event data.
SDL_KeyboardEvent key
Keyboard event data.
Object that represent an event captured by SDL.
SDL_DollarGestureEvent dgesture
Gesture event data.
SDL_MouseButtonEvent button
Mouse button event data.
SDL_TextEditingEvent edit
Text editing event data.
bool poll()
Pool for events, return false when there are no more events to poll.
Event()
Default ctor an event.
static int call_filter(void *data, SDL_Event *event)
bool(*)(void *, Event &) func_type
SDL_Event * ptr()
Get a pointer to an SDL_Event.
void peek()
Peek the next event in the list.
void flush_events(Uint32 minType, Uint32 maxType)
Clear events of a range of types from the event queue.
SDL_QuitEvent quit
Quit request event data.
Define to deactivate exception support.
void push() const
Push the current event to the list of event to process.
State
For type safety, we will use these scoped enum values instead of raw numbers like the C api...
void pump_events()
Pump the event loop from the OS event system.
SDL_JoyBallEvent jball
Joystick ball event data.
SDL_Event const * ptr() const
Get a pointer to an SDL_Event.
bool has_events(Uint32 minType, Uint32 maxType)
Return true if there are events of a specific range of types in the queue.
std::vector< Event > peek_events(size_t maxEvents)
Peek at future events.
SDL_ControllerDeviceEvent cdevice
Game Controller device event data.
std::vector< Event > get_events(size_t maxEvents, Uint32 type)
Get events from a specific type.
SDL_TouchFingerEvent tfinger
Touch finger event data.
std::vector< Event > get_events(size_t maxEvents, Uint32 minType, Uint32 maxType)
Get events from the queue maxEvents max number of events to get.
void add_events(std::vector< Event > const &events, Uint32 minType, Uint32 maxType)
Add events of a specific range of types to the event queue.
SDL_DropEvent drop
Drag and drop event data.
SDL_TextInputEvent text
Text input event data.
static Event const & ref_from(SDL_Event const &e)
Get a const reference to an sdl::Event from an SDL_Event.
std::vector< Event > peek_events(size_t maxEvents, Uint32 type)
Peek events from a specific type.
EventFilter(func_type filter)
SDL_JoyButtonEvent jbutton
Joystick button event data.
SDL_UserEvent user
Custom event data.
void flush_events(Uint32 type)
Clear events from a specific type from the event queue.
static Event & ref_from(SDL_Event &e)
Get a non-const reference to an sdl::Event from an SDL_Event.
Event(SDL_Event const &e)
converting ctor to create an sdl::Event from an SDL_Event struct
Define to deactivate exception support.
void add_events(std::vector< Event > const &events, Uint32 type)
Add events of a specific type to the queue.
SDL_JoyDeviceEvent jdevice
Joystick device change event data.
Uint8 padding[56]
Window event data.
void set_event_state(Uint32 type, Event::State state)
EventFilter(func_type filter, void *userdata)