nomlib
Loading...
Searching...
No Matches
EventHandler.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_SYSTEM_EVENT_HANDLER_HPP
30#define NOMLIB_SYSTEM_EVENT_HANDLER_HPP
31
32#include <deque>
33#include <vector>
34
35#include "nomlib/config.hpp"
36#include "nomlib/system/Event.hpp"
37
38// Forward declarations (third-party)
39union SDL_Event;
40
41namespace nom {
42
43// Forward declarations
44struct event_watcher;
47
49class EventHandler
50{
51 public:
52 typedef EventHandler self_type;
53
54 enum JoystickHandlerType
55 {
56 NO_EVENT_HANDLER = 0,
57 SDL_JOYSTICK_EVENT_HANDLER,
58 GAME_CONTROLLER_EVENT_HANDLER,
59 };
60
61 EventHandler();
62
63 ~EventHandler();
64
66 EventHandler(const EventHandler& rhs) = delete;
67
69 EventHandler& operator =(const EventHandler& rhs) = delete;
70
72 nom::size_type num_events() const;
73
75 nom::size_type num_event_watchers() const;
76
84
93
94 JoystickHandlerType joystick_event_type() const;
95
100
105
108
111
115 bool poll_event(Event& ev);
116
120 void push_event(const Event& ev);
121
126
131
134
139 void append_event_watch(const event_filter& filter, void* data);
140
142 void remove_event_watch(const event_filter& filter);
143
146
147 private:
148 bool pop_event(Event& ev);
149
152
154 void process_event(const SDL_Event* ev);
155
156 void process_joystick_event(const SDL_Event* ev);
157 void process_game_controller_event(const SDL_Event* ev);
158
162 std::deque<Event> events_;
163
164 std::vector<std::unique_ptr<event_watcher>> event_watchers_;
165
168 nom::size_type max_events_count_ = 0;
169
170 void* joystick_event_handler_ = nullptr;
171 JoystickHandlerType joystick_event_type_ = NO_EVENT_HANDLER;
172};
173
174Event create_key_press(int32 sym, uint16 mod, uint8 repeat);
175Event create_key_release(int32 sym, uint16 mod, uint8 repeat);
176Event create_mouse_button_click(uint8 button, uint8 clicks, uint32 window_id);
177Event create_mouse_button_release(uint8 button, uint8 clicks, uint32 window_id);
178
179Event create_joystick_button_press(JoystickID id, uint8 button);
180Event create_joystick_button_release(JoystickID id, uint8 button);
181
182Event create_joystick_hat_motion(JoystickID id, uint8 hat, uint8 value);
183
184Event create_game_controller_button_press(JoystickID id, uint8 button);
185Event create_game_controller_button_release(JoystickID id, uint8 button);
186
187Event create_user_event(int32 code, void* data1, void* data2, uint32 window_id);
188
189Event create_quit_event(void* data1, void* data2);
190
191} // namespace nom
192
193#endif // include guard defined
194
void flush_events(Event::EventType type)
Clear events from the queue.
EventHandler & operator=(const EventHandler &rhs)=delete
Disabled copy assignment operator.
void flush_events()
Clear the events queue.
EventHandler(const EventHandler &rhs)=delete
Disabled copy constructor.
void process_event(const SDL_Event *ev)
Enumerate the available events from the underlying platform.
bool enable_game_controller_polling()
Initialize game controller events polling.
void process_events()
Enumerate the available events from the underlying platform.
void append_event_watch(const event_filter &filter, void *data)
Add an event listener.
bool poll_event(Event &ev)
Enumerate the available events.
void flush_event(Event::EventType type)
Clear an event from the queue.
JoystickEventHandler * joystick_event_handler() const
Get a non-owned pointer to the joystick device event handler.
void disable_game_controller_polling()
Shutdown the game controller events polling subsystem.
nom::size_type num_events() const
Get the total number of enqueued events.
GameControllerEventHandler * game_controller_event_handler() const
Get a non-owned pointer to the game controller device event handler.
void remove_event_watchers()
Erase all event watchers.
nom::size_type max_events_count_
The maximum number of events processed per queue cycle – i.e.: one frame of the game's update loop.
void disable_joystick_polling()
Shutdown the joystick events polling subsystem.
void push_event(const Event &ev)
Enqueue an event.
void remove_event_watch(const event_filter &filter)
Erase an event listener.
bool enable_joystick_polling()
Initialize joystick events polling.
std::deque< Event > events_
Enqueued events.
nom::size_type num_event_watchers() const
Get the total number of event watchers.
Internal management of hot-pluggable joystick devices handling.
Internal management of hot-pluggable joystick devices handling.
Event handling types.
Definition Event.hpp:457