Moved Polling methods to SDL2pp::Event namespace

This commit is contained in:
Vraiment 2017-07-22 15:13:12 -07:00
parent 902075453f
commit b7e977638b
3 changed files with 110 additions and 105 deletions

View File

@ -22,18 +22,20 @@
#include <SDL2pp/EventPolling.hh> #include <SDL2pp/EventPolling.hh>
namespace SDL2pp { namespace SDL2pp {
bool PollEvent() { namespace Event {
SDL_Event event; bool PollEvent() {
auto result = SDL_PollEvent(&event); SDL_Event event;
auto result = SDL_PollEvent(&event);
return result; return result;
} }
int PollAllEvents() { int PollAllEvents() {
int result; int result;
for (result = 0; PollEvent(); result++); for (result = 0; PollEvent(); result++);
return result; return result;
}
} }
} }

View File

@ -27,102 +27,104 @@
#include <SDL2pp/Export.hh> #include <SDL2pp/Export.hh>
namespace SDL2pp { namespace SDL2pp {
//////////////////////////////////////////////////////////// namespace Event {
/// \brief Polls a single event ////////////////////////////////////////////////////////////
/// /// \brief Polls a single event
/// This function tries to poll a single event from the event ///
/// queue using SDL_PollEvent(). If an event was polled it /// This function tries to poll a single event from the event
/// returns true, if not it returns false. /// queue using SDL_PollEvent(). If an event was polled it
/// /// returns true, if not it returns false.
/// \ingroup events ///
/// /// \ingroup events
/// \headerfile SDL2pp/EventPolling.hh ///
/// /// \headerfile SDL2pp/EventPolling.hh
/// \returns True if an event was polled, false otherwise ///
/// /// \returns True if an event was polled, false otherwise
/// \see https://wiki.libsdl.org/SDL_PollEvent ///
/// /// \see https://wiki.libsdl.org/SDL_PollEvent
//////////////////////////////////////////////////////////// ///
SDL2PP_EXPORT bool PollEvent(); ////////////////////////////////////////////////////////////
SDL2PP_EXPORT bool PollEvent();
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Polls and handles a single event /// \brief Polls and handles a single event
/// ///
/// This function tries to poll a single event from the event /// This function tries to poll a single event from the event
/// queue using SDL_PollEvent(). If an event was polled the /// queue using SDL_PollEvent(). If an event was polled the
/// event handler is called using the retrieved SDL_Event as an /// event handler is called using the retrieved SDL_Event as an
/// argument then this function returns true. If no event was /// argument then this function returns true. If no event was
/// retrieved the event handler is not called and this function /// retrieved the event handler is not called and this function
/// returns false. /// returns false.
/// ///
/// \ingroup events /// \ingroup events
/// ///
/// \headerfile SDL2pp/EventPolling.hh /// \headerfile SDL2pp/EventPolling.hh
/// ///
/// \param[in] eventHandler Object that can be used as eventHandler(event) /// \param[in] eventHandler Object that can be used as eventHandler(event)
/// where event is an instance of SDL_Event /// where event is an instance of SDL_Event
/// ///
/// \returns True if an event was polled, false otherwise /// \returns True if an event was polled, false otherwise
/// ///
/// \see https://wiki.libsdl.org/SDL_Event /// \see https://wiki.libsdl.org/SDL_Event
/// \see https://wiki.libsdl.org/SDL_PollEvent /// \see https://wiki.libsdl.org/SDL_PollEvent
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
template <typename T> template <typename T>
bool PollEvent(T& eventHandler) { bool PollEvent(T& eventHandler) {
SDL_Event event; SDL_Event event;
if (!SDL_PollEvent(&event)) { if (!SDL_PollEvent(&event)) {
return false; return false;
}
eventHandler(event);
return true;
} }
eventHandler(event); ////////////////////////////////////////////////////////////
/// \brief Polls all the events from the event queue
///
/// This function calls SDL_PollEvent() until the event queue is empty.
/// Returns the amount of events that were polled from the queue.
///
/// \ingroup events
///
/// \headerfile SDL2pp/EventPolling.hh
///
/// \returns The amount of polled events (can be zero)
///
/// \see https://wiki.libsdl.org/SDL_PollEvent
///
////////////////////////////////////////////////////////////
SDL2PP_EXPORT int PollAllEvents();
return true; ////////////////////////////////////////////////////////////
} /// \brief Polls and handles all the events from the event queue
///
//////////////////////////////////////////////////////////// /// This function calls SDL_PollEvent() until the event queue is empty.
/// \brief Polls all the events from the event queue /// Then for each event that was polled the event handler is called
/// /// using the polled event as an argument. This function returns the
/// This function calls SDL_PollEvent() until the event queue is empty. /// amount of events that were polled.
/// Returns the amount of events that were polled from the queue. ///
/// /// \ingroup events
/// \ingroup events ///
/// /// \headerfile SDL2pp/EventPolling.hh
/// \headerfile SDL2pp/EventPolling.hh ///
/// /// \param[in] eventHandler Object that can be used as eventHandler(event)
/// \returns The amount of polled events (can be zero) /// where event is an instance of SDL_Event
/// ///
/// \see https://wiki.libsdl.org/SDL_PollEvent /// \returns The amount of polled events (can be zero)
/// ///
//////////////////////////////////////////////////////////// /// \see https://wiki.libsdl.org/SDL_Event
SDL2PP_EXPORT int PollAllEvents(); /// \see https://wiki.libsdl.org/SDL_PollEvent
///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Polls and handles all the events from the event queue template <typename T>
/// int PollAllEvents(T& eventHandler) {
/// This function calls SDL_PollEvent() until the event queue is empty. int result;
/// Then for each event that was polled the event handler is called for (result = 0; PollEvent(eventHandler); result++);
/// using the polled event as an argument. This function returns the return result;
/// amount of events that were polled. }
///
/// \ingroup events
///
/// \headerfile SDL2pp/EventPolling.hh
///
/// \param[in] eventHandler Object that can be used as eventHandler(event)
/// where event is an instance of SDL_Event
///
/// \returns The amount of polled events (can be zero)
///
/// \see https://wiki.libsdl.org/SDL_Event
/// \see https://wiki.libsdl.org/SDL_PollEvent
///
////////////////////////////////////////////////////////////
template <typename T>
int PollAllEvents(T& eventHandler) {
int result;
for (result = 0; PollEvent(eventHandler); result++);
return result;
} }
} }

View File

@ -8,6 +8,7 @@
#include <vector> #include <vector>
using namespace SDL2pp; using namespace SDL2pp;
using namespace SDL2pp::Event;
using namespace std; using namespace std;
inline SDL_Event PushUserEvent(Sint32 userCode = 0, void *data1 = nullptr, void *data2 = nullptr) { inline SDL_Event PushUserEvent(Sint32 userCode = 0, void *data1 = nullptr, void *data2 = nullptr) {