mirror of
https://github.com/libSDL2pp/libSDL2pp.git
synced 2025-08-03 19:05:59 -04:00
Changed event polling functions from templates to use std::function
This commit is contained in:
parent
24c172d7ad
commit
945e435ad5
@ -21,6 +21,8 @@
|
||||
|
||||
#include <SDL2pp/EventPolling.hh>
|
||||
|
||||
using std::function;
|
||||
|
||||
namespace SDL2pp {
|
||||
namespace Event {
|
||||
bool PollEvent() {
|
||||
@ -30,6 +32,17 @@ namespace SDL2pp {
|
||||
return result;
|
||||
}
|
||||
|
||||
bool PollEvent(function<void(const SDL_Event &)> eventHandler) {
|
||||
SDL_Event event;
|
||||
if (!SDL_PollEvent(&event)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
eventHandler(event);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int PollAllEvents() {
|
||||
int result;
|
||||
|
||||
@ -37,5 +50,11 @@ namespace SDL2pp {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int PollAllEvents(function<void(const SDL_Event &)> eventHandler) {
|
||||
int result;
|
||||
for (result = 0; PollEvent(eventHandler); result++);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,6 +26,8 @@
|
||||
|
||||
#include <SDL2pp/Export.hh>
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace SDL2pp {
|
||||
namespace Event {
|
||||
////////////////////////////////////////////////////////////
|
||||
@ -56,6 +58,19 @@ namespace SDL2pp {
|
||||
/// retrieved the event handler is not called and this function
|
||||
/// returns false.
|
||||
///
|
||||
/// This function accepts free functions, lambdas and callable objects.
|
||||
///
|
||||
/// If an instance of a callable struct/class needs to be passed
|
||||
/// by reference, std::ref() needs to be used:
|
||||
/// \code
|
||||
/// struct EventHandler {
|
||||
/// void operator()(const SDL_Event &);
|
||||
/// };
|
||||
///
|
||||
/// EventHandler myEventHandler;
|
||||
/// PollEvent(std::ref(myEventHandler));
|
||||
/// \endcode
|
||||
///
|
||||
/// \ingroup events
|
||||
///
|
||||
/// \headerfile SDL2pp/EventPolling.hh
|
||||
@ -69,17 +84,7 @@ namespace SDL2pp {
|
||||
/// \see https://wiki.libsdl.org/SDL_PollEvent
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
bool PollEvent(T&& eventHandler) {
|
||||
SDL_Event event;
|
||||
if (!SDL_PollEvent(&event)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
eventHandler(event);
|
||||
|
||||
return true;
|
||||
}
|
||||
SDL2PP_EXPORT bool PollEvent(std::function<void(const SDL_Event &)> eventHandler);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Polls all the events from the event queue
|
||||
@ -106,6 +111,19 @@ namespace SDL2pp {
|
||||
/// using the polled event as an argument. This function returns the
|
||||
/// amount of events that were polled.
|
||||
///
|
||||
/// This function accepts free functions, lambdas and callable objects.
|
||||
///
|
||||
/// If an instance of a callable struct/class needs to be passed
|
||||
/// by reference, std::ref() needs to be used:
|
||||
/// \code
|
||||
/// struct EventHandler {
|
||||
/// void operator()(const SDL_Event &);
|
||||
/// };
|
||||
///
|
||||
/// EventHandler myEventHandler;
|
||||
/// PollAllEvents(std::ref(myEventHandler));
|
||||
/// \endcode
|
||||
///
|
||||
/// \ingroup events
|
||||
///
|
||||
/// \headerfile SDL2pp/EventPolling.hh
|
||||
@ -119,12 +137,7 @@ namespace SDL2pp {
|
||||
/// \see https://wiki.libsdl.org/SDL_PollEvent
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
int PollAllEvents(T&& eventHandler) {
|
||||
int result;
|
||||
for (result = 0; PollEvent(eventHandler); result++);
|
||||
return result;
|
||||
}
|
||||
SDL2PP_EXPORT int PollAllEvents(std::function<void(const SDL_Event &)> eventHandler);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -101,7 +101,7 @@ BEGIN_TEST(int, char*[])
|
||||
|
||||
const SDL_Event expectedEvent = PushUserEvent(45);
|
||||
|
||||
EXPECT_TRUE(PollEvent(eventHandler) == true);
|
||||
EXPECT_TRUE(PollEvent(std::ref(eventHandler)) == true);
|
||||
EXPECT_TRUE(eventHandler.events.size() == 1);
|
||||
|
||||
const SDL_Event result = eventHandler.events[0];
|
||||
@ -195,7 +195,7 @@ BEGIN_TEST(int, char*[])
|
||||
}
|
||||
int totalExpectedEvents = static_cast<int>(expectedEvents.size());
|
||||
|
||||
EXPECT_TRUE(PollAllEvents(eventHandler) == totalExpectedEvents);
|
||||
EXPECT_TRUE(PollAllEvents(std::ref(eventHandler)) == totalExpectedEvents);
|
||||
EXPECT_TRUE(eventHandler.events.size() == expectedEvents.size());
|
||||
|
||||
for (int n = 0; n < totalExpectedEvents; n++) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user