From 5584d1324558b907bff30fff9d3454753f5ba301 Mon Sep 17 00:00:00 2001 From: Vraiment Date: Sat, 15 Jul 2017 13:29:09 -0700 Subject: [PATCH 01/16] Added EventPolling files --- CMakeLists.txt | 2 ++ SDL2pp/EventPolling.cc | 22 ++++++++++++++++++++++ SDL2pp/EventPolling.hh | 29 +++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 SDL2pp/EventPolling.cc create mode 100644 SDL2pp/EventPolling.hh diff --git a/CMakeLists.txt b/CMakeLists.txt index 51eb91e..27822a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -126,6 +126,7 @@ SET(LIBRARY_SOURCES SDL2pp/AudioLock.cc SDL2pp/AudioSpec.cc SDL2pp/Color.cc + SDL2pp/EventPolling.cc SDL2pp/Exception.cc SDL2pp/Point.cc SDL2pp/RWops.cc @@ -145,6 +146,7 @@ SET(LIBRARY_HEADERS SDL2pp/AudioSpec.hh SDL2pp/Color.hh SDL2pp/ContainerRWops.hh + SDL2pp/EventPolling.hh SDL2pp/Exception.hh SDL2pp/Optional.hh SDL2pp/Point.hh diff --git a/SDL2pp/EventPolling.cc b/SDL2pp/EventPolling.cc new file mode 100644 index 0000000..6c4fab2 --- /dev/null +++ b/SDL2pp/EventPolling.cc @@ -0,0 +1,22 @@ +/* + libSDL2pp - C++11 bindings/wrapper for SDL2 + Copyright (C) 2017 Vraiment + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include diff --git a/SDL2pp/EventPolling.hh b/SDL2pp/EventPolling.hh new file mode 100644 index 0000000..9de3d63 --- /dev/null +++ b/SDL2pp/EventPolling.hh @@ -0,0 +1,29 @@ +/* + libSDL2pp - C++11 bindings/wrapper for SDL2 + Copyright (C) 2017 Vraiment + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef SDL2PP_EVENTPOLLING_HH +#define SDL2PP_EVENTPOLLING_HH + +namespace SDL2pp { + +} + +#endif From 0da7b3c96299c7750649bd4d241098180c912914 Mon Sep 17 00:00:00 2001 From: Vraiment Date: Sat, 15 Jul 2017 13:46:43 -0700 Subject: [PATCH 02/16] Added testing files for event polling --- tests/CMakeLists.txt | 1 + tests/test_eventpolling.cc | 10 ++++++++++ 2 files changed, 11 insertions(+) create mode 100644 tests/test_eventpolling.cc diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1c14256..4669ccd 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,6 +2,7 @@ SET(CLI_TESTS test_color test_color_constexpr + test_eventpolling test_error test_optional test_pointrect diff --git a/tests/test_eventpolling.cc b/tests/test_eventpolling.cc new file mode 100644 index 0000000..292ed5f --- /dev/null +++ b/tests/test_eventpolling.cc @@ -0,0 +1,10 @@ +#include + +#include + +#include "testing.h" + +using namespace SDL2pp; + +BEGIN_TEST(int, char*[]) +END_TEST() From 088eaa873cca10791064bc6cca7d4ecd71bdc226 Mon Sep 17 00:00:00 2001 From: Vraiment Date: Sat, 15 Jul 2017 17:43:14 -0700 Subject: [PATCH 03/16] Added PollEvent() --- SDL2pp/EventPolling.cc | 9 +++++++++ SDL2pp/EventPolling.hh | 4 +++- tests/test_eventpolling.cc | 27 +++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/SDL2pp/EventPolling.cc b/SDL2pp/EventPolling.cc index 6c4fab2..de81211 100644 --- a/SDL2pp/EventPolling.cc +++ b/SDL2pp/EventPolling.cc @@ -20,3 +20,12 @@ */ #include + +namespace SDL2pp { + bool PollEvent() { + SDL_Event event; + auto result = SDL_PollEvent(&event); + + return result; + } +} diff --git a/SDL2pp/EventPolling.hh b/SDL2pp/EventPolling.hh index 9de3d63..cf2ecf2 100644 --- a/SDL2pp/EventPolling.hh +++ b/SDL2pp/EventPolling.hh @@ -22,8 +22,10 @@ #ifndef SDL2PP_EVENTPOLLING_HH #define SDL2PP_EVENTPOLLING_HH -namespace SDL2pp { +#include +namespace SDL2pp { + bool PollEvent(); } #endif diff --git a/tests/test_eventpolling.cc b/tests/test_eventpolling.cc index 292ed5f..20e4d16 100644 --- a/tests/test_eventpolling.cc +++ b/tests/test_eventpolling.cc @@ -1,10 +1,37 @@ #include #include +#include #include "testing.h" using namespace SDL2pp; +inline SDL_Event PushUserEvent(Sint32 userCode = 0, void *data1 = nullptr, void *data2 = nullptr) { + SDL_Event event; + event.type = SDL_USEREVENT; + event.user.code = userCode; + event.user.data1 = data1; + event.user.data2 = data2; + + if (SDL_PushEvent(&event) < 0) { + throw Exception("SDL_PushEvent"); + } + + return event; +} + BEGIN_TEST(int, char*[]) + // With no callback and no polled event + { + EXPECT_TRUE(PollEvent() == false); + } + + // With no callback and a polled event + { + PushUserEvent(); + + EXPECT_TRUE(PollEvent() == true); + EXPECT_TRUE(PollEvent() == false); + } END_TEST() From 78cc589fc9e17ab1487643fa4e7ded33e4ae1622 Mon Sep 17 00:00:00 2001 From: Vraiment Date: Sat, 15 Jul 2017 18:39:48 -0700 Subject: [PATCH 04/16] Added PollEvent(T eventHandler) --- SDL2pp/EventPolling.hh | 12 ++++++ tests/test_eventpolling.cc | 77 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/SDL2pp/EventPolling.hh b/SDL2pp/EventPolling.hh index cf2ecf2..4bb77bf 100644 --- a/SDL2pp/EventPolling.hh +++ b/SDL2pp/EventPolling.hh @@ -26,6 +26,18 @@ namespace SDL2pp { bool PollEvent(); + + template + bool PollEvent(T& eventHandler) { + SDL_Event event; + if (!SDL_PollEvent(&event)) { + return false; + } + + eventHandler(event); + + return true; + } } #endif diff --git a/tests/test_eventpolling.cc b/tests/test_eventpolling.cc index 20e4d16..3e9a692 100644 --- a/tests/test_eventpolling.cc +++ b/tests/test_eventpolling.cc @@ -5,7 +5,10 @@ #include "testing.h" +#include + using namespace SDL2pp; +using namespace std; inline SDL_Event PushUserEvent(Sint32 userCode = 0, void *data1 = nullptr, void *data2 = nullptr) { SDL_Event event; @@ -21,6 +24,22 @@ inline SDL_Event PushUserEvent(Sint32 userCode = 0, void *data1 = nullptr, void return event; } +namespace StaticEventHandler { + vector events; + + inline void EventHandler(const SDL_Event& event) { + events.push_back(event); + } +} + +struct CallableEventHandler { + vector events; + + void operator()(const SDL_Event& event) { + events.push_back(event); + } +}; + BEGIN_TEST(int, char*[]) // With no callback and no polled event { @@ -34,4 +53,62 @@ BEGIN_TEST(int, char*[]) EXPECT_TRUE(PollEvent() == true); EXPECT_TRUE(PollEvent() == false); } + + + // With function as an event handler + { + StaticEventHandler::events.clear(); + + const SDL_Event expected = PushUserEvent(15); + + EXPECT_TRUE(PollEvent(StaticEventHandler::EventHandler) == true); + EXPECT_TRUE(StaticEventHandler::events.size() == 1); + + const SDL_Event result = StaticEventHandler::events[0]; + EXPECT_TRUE(result.type == expected.type); + EXPECT_TRUE(result.user.code == expected.user.code); + + // Verify no further events + EXPECT_TRUE(PollEvent(StaticEventHandler::EventHandler) == false); + EXPECT_TRUE(StaticEventHandler::events.size() == 1); + } + + // With lambda as an event handler + { + vector events; + auto eventHandler = [&events](const SDL_Event& event) { + events.push_back(event); + }; + + const SDL_Event expectedEvent = PushUserEvent(22); + + EXPECT_TRUE(PollEvent(eventHandler) == true); + EXPECT_TRUE(events.size() == 1); + + const SDL_Event result = events[0]; + EXPECT_TRUE(result.type == expectedEvent.type); + EXPECT_TRUE(result.user.code == expectedEvent.user.code); + + // Verify no further events + EXPECT_TRUE(PollEvent(eventHandler) == false); + EXPECT_TRUE(events.size() == 1); + } + + // With callable object + { + CallableEventHandler eventHandler; + + const SDL_Event expectedEvent = PushUserEvent(45); + + EXPECT_TRUE(PollEvent(eventHandler) == true); + EXPECT_TRUE(eventHandler.events.size() == 1); + + const SDL_Event result = eventHandler.events[0]; + EXPECT_TRUE(result.type == expectedEvent.type); + EXPECT_TRUE(result.user.code == expectedEvent.user.code); + + // Verify no further events + EXPECT_TRUE(PollEvent(eventHandler) == false); + EXPECT_TRUE(eventHandler.events.size() == 1); + } END_TEST() From c10ffbb8672abcec65c56e52b65d9f50183c25af Mon Sep 17 00:00:00 2001 From: Vraiment Date: Sat, 15 Jul 2017 18:55:11 -0700 Subject: [PATCH 05/16] Added PollAllEvents() --- SDL2pp/EventPolling.cc | 8 ++++++++ SDL2pp/EventPolling.hh | 2 ++ tests/test_eventpolling.cc | 17 +++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/SDL2pp/EventPolling.cc b/SDL2pp/EventPolling.cc index de81211..ece01e8 100644 --- a/SDL2pp/EventPolling.cc +++ b/SDL2pp/EventPolling.cc @@ -28,4 +28,12 @@ namespace SDL2pp { return result; } + + int PollAllEvents() { + int result; + + for (result = 0; PollEvent(); result++); + + return result; + } } diff --git a/SDL2pp/EventPolling.hh b/SDL2pp/EventPolling.hh index 4bb77bf..1b13ede 100644 --- a/SDL2pp/EventPolling.hh +++ b/SDL2pp/EventPolling.hh @@ -38,6 +38,8 @@ namespace SDL2pp { return true; } + + int PollAllEvents(); } #endif diff --git a/tests/test_eventpolling.cc b/tests/test_eventpolling.cc index 3e9a692..efd0945 100644 --- a/tests/test_eventpolling.cc +++ b/tests/test_eventpolling.cc @@ -111,4 +111,21 @@ BEGIN_TEST(int, char*[]) EXPECT_TRUE(PollEvent(eventHandler) == false); EXPECT_TRUE(eventHandler.events.size() == 1); } + + // With no callback and no polled events + { + EXPECT_TRUE(PollAllEvents() == 0); + } + + // With no callback and several polled events + { + constexpr int totalEvents = 5; + for (int n = 0; n < totalEvents; ++n) { + PushUserEvent(); + } + + EXPECT_TRUE(PollAllEvents() == totalEvents); + // Verify no further events + EXPECT_TRUE(PollEvent() == false); + } END_TEST() From ce876eaa6d38845c0df04c7be5d0561b4fa6b62c Mon Sep 17 00:00:00 2001 From: Vraiment Date: Sat, 15 Jul 2017 19:25:10 -0700 Subject: [PATCH 06/16] Added PollAllEvents(T eventHandler) --- SDL2pp/EventPolling.hh | 7 ++++ tests/test_eventpolling.cc | 81 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/SDL2pp/EventPolling.hh b/SDL2pp/EventPolling.hh index 1b13ede..40cff9a 100644 --- a/SDL2pp/EventPolling.hh +++ b/SDL2pp/EventPolling.hh @@ -40,6 +40,13 @@ namespace SDL2pp { } int PollAllEvents(); + + template + int PollAllEvents(T& eventHandler) { + int result; + for (result = 0; PollEvent(eventHandler); result++); + return result; + } } #endif diff --git a/tests/test_eventpolling.cc b/tests/test_eventpolling.cc index efd0945..3983a06 100644 --- a/tests/test_eventpolling.cc +++ b/tests/test_eventpolling.cc @@ -128,4 +128,85 @@ BEGIN_TEST(int, char*[]) // Verify no further events EXPECT_TRUE(PollEvent() == false); } + + // With function as an event handler + { + StaticEventHandler::events.clear(); + + vector expectedEvents; + for (const auto eventCode : { 15, 32, 99, 85 }) { + expectedEvents.push_back(PushUserEvent(eventCode)); + } + int totalExpectedEvents = static_cast(expectedEvents.size()); + + EXPECT_TRUE(PollAllEvents(StaticEventHandler::EventHandler) == totalExpectedEvents); + EXPECT_TRUE(StaticEventHandler::events.size() == expectedEvents.size()); + + for (int n = 0; n < totalExpectedEvents; n++) { + const SDL_Event result = StaticEventHandler::events[n]; + const SDL_Event expected = expectedEvents[n]; + + EXPECT_TRUE(result.type == expected.type); + EXPECT_TRUE(result.user.code == expected.user.code); + } + + // Verify no further events + EXPECT_TRUE(PollEvent(StaticEventHandler::EventHandler) == false); + EXPECT_TRUE(StaticEventHandler::events.size() == expectedEvents.size()); + } + + // With lambda as an event handler + { + vector handledEvents; + auto eventHandler = [&handledEvents](const SDL_Event& event) { + handledEvents.push_back(event); + }; + + vector expectedEvents; + for (const auto eventCode : { 37, 88, 42, 63, 23, 19 }) { + expectedEvents.push_back(PushUserEvent(eventCode)); + } + int totalExpectedEvents = static_cast(expectedEvents.size()); + + EXPECT_TRUE(PollAllEvents(eventHandler) == totalExpectedEvents); + EXPECT_TRUE(handledEvents.size() == expectedEvents.size()); + + for (int n = 0; n < totalExpectedEvents; n++) { + const SDL_Event result = handledEvents[n]; + const SDL_Event expected = expectedEvents[n]; + + EXPECT_TRUE(result.type == expected.type); + EXPECT_TRUE(result.user.code == expected.user.code); + } + + // Verify no further events + EXPECT_TRUE(PollEvent(eventHandler) == false); + EXPECT_TRUE(handledEvents.size() == expectedEvents.size()); + } + + // With callable object as an event handler + { + CallableEventHandler eventHandler; + + vector expectedEvents; + for (const auto eventCode : { 11, 98, 62, 35, 71 }) { + expectedEvents.push_back(PushUserEvent(eventCode)); + } + int totalExpectedEvents = static_cast(expectedEvents.size()); + + EXPECT_TRUE(PollAllEvents(eventHandler) == totalExpectedEvents); + EXPECT_TRUE(eventHandler.events.size() == expectedEvents.size()); + + for (int n = 0; n < totalExpectedEvents; n++) { + const SDL_Event result = eventHandler.events[n]; + const SDL_Event expected = expectedEvents[n]; + + EXPECT_TRUE(result.type == expected.type); + EXPECT_TRUE(result.user.code == expected.user.code); + } + + // Verify no further events + EXPECT_TRUE(PollEvent(eventHandler) == false); + EXPECT_TRUE(eventHandler.events.size() == expectedEvents.size()); + } END_TEST() From 902075453f6e50976a602033661bda4638c3aa2a Mon Sep 17 00:00:00 2001 From: Vraiment Date: Sat, 15 Jul 2017 20:02:34 -0700 Subject: [PATCH 07/16] Added event polling documentation and the include in SDL2pp.hh --- SDL2pp/EventPolling.hh | 81 ++++++++++++++++++++++++++++++++++++++++-- SDL2pp/SDL2pp.hh | 8 +++++ 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/SDL2pp/EventPolling.hh b/SDL2pp/EventPolling.hh index 40cff9a..deba7b5 100644 --- a/SDL2pp/EventPolling.hh +++ b/SDL2pp/EventPolling.hh @@ -24,9 +24,50 @@ #include +#include + namespace SDL2pp { - bool PollEvent(); + //////////////////////////////////////////////////////////// + /// \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 + /// returns true, if not it returns false. + /// + /// \ingroup events + /// + /// \headerfile SDL2pp/EventPolling.hh + /// + /// \returns True if an event was polled, false otherwise + /// + /// \see https://wiki.libsdl.org/SDL_PollEvent + /// + //////////////////////////////////////////////////////////// + SDL2PP_EXPORT bool PollEvent(); + //////////////////////////////////////////////////////////// + /// \brief Polls and handles a single event + /// + /// This function tries to poll a single event from the event + /// queue using SDL_PollEvent(). If an event was polled the + /// event handler is called using the retrieved SDL_Event as an + /// argument then this function returns true. If no event was + /// retrieved the event handler is not called and this function + /// returns false. + /// + /// \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 True if an event was polled, false otherwise + /// + /// \see https://wiki.libsdl.org/SDL_Event + /// \see https://wiki.libsdl.org/SDL_PollEvent + /// + //////////////////////////////////////////////////////////// template bool PollEvent(T& eventHandler) { SDL_Event event; @@ -39,8 +80,44 @@ namespace SDL2pp { return true; } - int PollAllEvents(); + //////////////////////////////////////////////////////////// + /// \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(); + //////////////////////////////////////////////////////////// + /// \brief Polls and handles all the events from the event queue + /// + /// This function calls SDL_PollEvent() until the event queue is empty. + /// Then for each event that was polled the event handler is called + /// using the polled event as an argument. This function returns the + /// 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 int PollAllEvents(T& eventHandler) { int result; diff --git a/SDL2pp/SDL2pp.hh b/SDL2pp/SDL2pp.hh index 22110b4..d8e9fb6 100644 --- a/SDL2pp/SDL2pp.hh +++ b/SDL2pp/SDL2pp.hh @@ -97,6 +97,14 @@ #include #include +//////////////////////////////////////////////////////////// +/// \defgroup events Event handling +/// +/// \brief Functions and classes to easily manage SDL events +/// +//////////////////////////////////////////////////////////// +#include + #ifdef SDL2PP_WITH_TTF //////////////////////////////////////////////////////////// /// \defgroup ttf SDL_ttf From b7e977638b59794f92a992beaebaa1bbe722b08c Mon Sep 17 00:00:00 2001 From: Vraiment Date: Sat, 22 Jul 2017 15:13:12 -0700 Subject: [PATCH 08/16] Moved Polling methods to SDL2pp::Event namespace --- SDL2pp/EventPolling.cc | 26 ++--- SDL2pp/EventPolling.hh | 188 +++++++++++++++++++------------------ tests/test_eventpolling.cc | 1 + 3 files changed, 110 insertions(+), 105 deletions(-) diff --git a/SDL2pp/EventPolling.cc b/SDL2pp/EventPolling.cc index ece01e8..06b684d 100644 --- a/SDL2pp/EventPolling.cc +++ b/SDL2pp/EventPolling.cc @@ -22,18 +22,20 @@ #include namespace SDL2pp { - bool PollEvent() { - SDL_Event event; - auto result = SDL_PollEvent(&event); + namespace Event { + bool PollEvent() { + SDL_Event event; + auto result = SDL_PollEvent(&event); + + return result; + } - return result; - } - - int PollAllEvents() { - int result; - - for (result = 0; PollEvent(); result++); - - return result; + int PollAllEvents() { + int result; + + for (result = 0; PollEvent(); result++); + + return result; + } } } diff --git a/SDL2pp/EventPolling.hh b/SDL2pp/EventPolling.hh index deba7b5..77cf0fe 100644 --- a/SDL2pp/EventPolling.hh +++ b/SDL2pp/EventPolling.hh @@ -27,102 +27,104 @@ #include namespace SDL2pp { - //////////////////////////////////////////////////////////// - /// \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 - /// returns true, if not it returns false. - /// - /// \ingroup events - /// - /// \headerfile SDL2pp/EventPolling.hh - /// - /// \returns True if an event was polled, false otherwise - /// - /// \see https://wiki.libsdl.org/SDL_PollEvent - /// - //////////////////////////////////////////////////////////// - SDL2PP_EXPORT bool PollEvent(); - - //////////////////////////////////////////////////////////// - /// \brief Polls and handles a single event - /// - /// This function tries to poll a single event from the event - /// queue using SDL_PollEvent(). If an event was polled the - /// event handler is called using the retrieved SDL_Event as an - /// argument then this function returns true. If no event was - /// retrieved the event handler is not called and this function - /// returns false. - /// - /// \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 True if an event was polled, false otherwise - /// - /// \see https://wiki.libsdl.org/SDL_Event - /// \see https://wiki.libsdl.org/SDL_PollEvent - /// - //////////////////////////////////////////////////////////// - template - bool PollEvent(T& eventHandler) { - SDL_Event event; - if (!SDL_PollEvent(&event)) { - return false; + namespace 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 + /// returns true, if not it returns false. + /// + /// \ingroup events + /// + /// \headerfile SDL2pp/EventPolling.hh + /// + /// \returns True if an event was polled, false otherwise + /// + /// \see https://wiki.libsdl.org/SDL_PollEvent + /// + //////////////////////////////////////////////////////////// + SDL2PP_EXPORT bool PollEvent(); + + //////////////////////////////////////////////////////////// + /// \brief Polls and handles a single event + /// + /// This function tries to poll a single event from the event + /// queue using SDL_PollEvent(). If an event was polled the + /// event handler is called using the retrieved SDL_Event as an + /// argument then this function returns true. If no event was + /// retrieved the event handler is not called and this function + /// returns false. + /// + /// \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 True if an event was polled, false otherwise + /// + /// \see https://wiki.libsdl.org/SDL_Event + /// \see https://wiki.libsdl.org/SDL_PollEvent + /// + //////////////////////////////////////////////////////////// + template + bool PollEvent(T& eventHandler) { + SDL_Event event; + if (!SDL_PollEvent(&event)) { + 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 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(); - - //////////////////////////////////////////////////////////// - /// \brief Polls and handles all the events from the event queue - /// - /// This function calls SDL_PollEvent() until the event queue is empty. - /// Then for each event that was polled the event handler is called - /// using the polled event as an argument. This function returns the - /// 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 - int PollAllEvents(T& eventHandler) { - int result; - for (result = 0; PollEvent(eventHandler); result++); - return result; + //////////////////////////////////////////////////////////// + /// \brief Polls and handles all the events from the event queue + /// + /// This function calls SDL_PollEvent() until the event queue is empty. + /// Then for each event that was polled the event handler is called + /// using the polled event as an argument. This function returns the + /// 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 + int PollAllEvents(T& eventHandler) { + int result; + for (result = 0; PollEvent(eventHandler); result++); + return result; + } } } diff --git a/tests/test_eventpolling.cc b/tests/test_eventpolling.cc index 3983a06..7026270 100644 --- a/tests/test_eventpolling.cc +++ b/tests/test_eventpolling.cc @@ -8,6 +8,7 @@ #include using namespace SDL2pp; +using namespace SDL2pp::Event; using namespace std; inline SDL_Event PushUserEvent(Sint32 userCode = 0, void *data1 = nullptr, void *data2 = nullptr) { From 9db15981bf5c06093d1904c8d0ac87d8a97fdfeb Mon Sep 17 00:00:00 2001 From: Vraiment Date: Sat, 22 Jul 2017 15:16:41 -0700 Subject: [PATCH 09/16] Added support for lambdas in place for EventPolling --- SDL2pp/EventPolling.hh | 4 ++-- tests/test_eventpolling.cc | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/SDL2pp/EventPolling.hh b/SDL2pp/EventPolling.hh index 77cf0fe..16b0613 100644 --- a/SDL2pp/EventPolling.hh +++ b/SDL2pp/EventPolling.hh @@ -70,7 +70,7 @@ namespace SDL2pp { /// //////////////////////////////////////////////////////////// template - bool PollEvent(T& eventHandler) { + bool PollEvent(T&& eventHandler) { SDL_Event event; if (!SDL_PollEvent(&event)) { return false; @@ -120,7 +120,7 @@ namespace SDL2pp { /// //////////////////////////////////////////////////////////// template - int PollAllEvents(T& eventHandler) { + int PollAllEvents(T&& eventHandler) { int result; for (result = 0; PollEvent(eventHandler); result++); return result; diff --git a/tests/test_eventpolling.cc b/tests/test_eventpolling.cc index 7026270..66e4451 100644 --- a/tests/test_eventpolling.cc +++ b/tests/test_eventpolling.cc @@ -77,13 +77,11 @@ BEGIN_TEST(int, char*[]) // With lambda as an event handler { vector events; - auto eventHandler = [&events](const SDL_Event& event) { - events.push_back(event); - }; - const SDL_Event expectedEvent = PushUserEvent(22); - EXPECT_TRUE(PollEvent(eventHandler) == true); + EXPECT_TRUE(PollEvent([&events](const SDL_Event& event) { + events.push_back(event); + }) == true); EXPECT_TRUE(events.size() == 1); const SDL_Event result = events[0]; @@ -91,7 +89,9 @@ BEGIN_TEST(int, char*[]) EXPECT_TRUE(result.user.code == expectedEvent.user.code); // Verify no further events - EXPECT_TRUE(PollEvent(eventHandler) == false); + EXPECT_TRUE(PollEvent([&events](const SDL_Event& event) { + events.push_back(event); + }) == false); EXPECT_TRUE(events.size() == 1); } @@ -159,17 +159,15 @@ BEGIN_TEST(int, char*[]) // With lambda as an event handler { vector handledEvents; - auto eventHandler = [&handledEvents](const SDL_Event& event) { - handledEvents.push_back(event); - }; - vector expectedEvents; for (const auto eventCode : { 37, 88, 42, 63, 23, 19 }) { expectedEvents.push_back(PushUserEvent(eventCode)); } int totalExpectedEvents = static_cast(expectedEvents.size()); - EXPECT_TRUE(PollAllEvents(eventHandler) == totalExpectedEvents); + EXPECT_TRUE(PollAllEvents([&handledEvents](const SDL_Event& event) { + handledEvents.push_back(event); + }) == totalExpectedEvents); EXPECT_TRUE(handledEvents.size() == expectedEvents.size()); for (int n = 0; n < totalExpectedEvents; n++) { @@ -181,7 +179,9 @@ BEGIN_TEST(int, char*[]) } // Verify no further events - EXPECT_TRUE(PollEvent(eventHandler) == false); + EXPECT_TRUE(PollEvent([&handledEvents](const SDL_Event& event) { + handledEvents.push_back(event); + }) == false); EXPECT_TRUE(handledEvents.size() == expectedEvents.size()); } From 648754505f15f98d794bf9c84ba2ef392aeb9b2d Mon Sep 17 00:00:00 2001 From: Vraiment Date: Sat, 22 Jul 2017 19:44:54 -0700 Subject: [PATCH 10/16] Added event wait skeleton --- CMakeLists.txt | 2 ++ SDL2pp/EventWait.cc | 27 +++++++++++++++++++++++++++ SDL2pp/EventWait.hh | 32 ++++++++++++++++++++++++++++++++ SDL2pp/SDL2pp.hh | 1 + tests/CMakeLists.txt | 1 + tests/test_eventwait.cc | 11 +++++++++++ 6 files changed, 74 insertions(+) create mode 100644 SDL2pp/EventWait.cc create mode 100644 SDL2pp/EventWait.hh create mode 100644 tests/test_eventwait.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 27822a2..32ccd15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -127,6 +127,7 @@ SET(LIBRARY_SOURCES SDL2pp/AudioSpec.cc SDL2pp/Color.cc SDL2pp/EventPolling.cc + SDL2pp/EventWait.cc SDL2pp/Exception.cc SDL2pp/Point.cc SDL2pp/RWops.cc @@ -147,6 +148,7 @@ SET(LIBRARY_HEADERS SDL2pp/Color.hh SDL2pp/ContainerRWops.hh SDL2pp/EventPolling.hh + SDL2pp/EventWait.hh SDL2pp/Exception.hh SDL2pp/Optional.hh SDL2pp/Point.hh diff --git a/SDL2pp/EventWait.cc b/SDL2pp/EventWait.cc new file mode 100644 index 0000000..4bfe084 --- /dev/null +++ b/SDL2pp/EventWait.cc @@ -0,0 +1,27 @@ +/* + libSDL2pp - C++11 bindings/wrapper for SDL2 + Copyright (C) 2017 Vraiment + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include + +namespace SDL2pp { + namespace Event { + } +} diff --git a/SDL2pp/EventWait.hh b/SDL2pp/EventWait.hh new file mode 100644 index 0000000..d29154e --- /dev/null +++ b/SDL2pp/EventWait.hh @@ -0,0 +1,32 @@ +/* + libSDL2pp - C++11 bindings/wrapper for SDL2 + Copyright (C) 2017 Vraiment + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef SDL2PP_EVENTWAIT_HH +#define SDL2PP_EVENTWAIT_HH + +#include + +namespace SDL2pp { + namespace Event { + } +} + +#endif diff --git a/SDL2pp/SDL2pp.hh b/SDL2pp/SDL2pp.hh index d8e9fb6..340d3a2 100644 --- a/SDL2pp/SDL2pp.hh +++ b/SDL2pp/SDL2pp.hh @@ -104,6 +104,7 @@ /// //////////////////////////////////////////////////////////// #include +#include #ifdef SDL2PP_WITH_TTF //////////////////////////////////////////////////////////// diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4669ccd..22646aa 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -3,6 +3,7 @@ SET(CLI_TESTS test_color test_color_constexpr test_eventpolling + test_eventwait test_error test_optional test_pointrect diff --git a/tests/test_eventwait.cc b/tests/test_eventwait.cc new file mode 100644 index 0000000..3d82755 --- /dev/null +++ b/tests/test_eventwait.cc @@ -0,0 +1,11 @@ +#include + +#include + +#include "testing.h" + +using namespace SDL2pp; +using namespace SDL2pp::Event; + +BEGIN_TEST(int, char*[]) +END_TEST() From c641f5602b1a680ff7cc6e2fdf29e2391ead4c95 Mon Sep 17 00:00:00 2001 From: Vraiment Date: Sun, 23 Jul 2017 01:08:34 -0700 Subject: [PATCH 11/16] Added EventWait code (definitions/logic) --- SDL2pp/EventWait.cc | 26 ++++++++++++++++++++++++++ SDL2pp/EventWait.hh | 5 +++++ 2 files changed, 31 insertions(+) diff --git a/SDL2pp/EventWait.cc b/SDL2pp/EventWait.cc index 4bfe084..6d59407 100644 --- a/SDL2pp/EventWait.cc +++ b/SDL2pp/EventWait.cc @@ -21,7 +21,33 @@ #include +#include + namespace SDL2pp { namespace Event { + SDL_Event WaitEvent() { + SDL_Event result; + if (SDL_WaitEvent(&result) == 0) { + throw Exception("SDL_WaitEvent"); + } + return result; + } + + Optional WaitEvent(int timeout) { + SDL_Event result; + SDL_ClearError(); // Necessary to ensure SDL_WaitEventTimeout + // returned with no errors + if (SDL_WaitEventTimeout(&result, timeout) == 0) { + auto error = SDL_GetError(); + // Check if the error message is empty + if (*error == 0) { + return Optional(); + } else { + throw Exception("SDL_WaitEventTimeout"); + } + } + + return result; + } } } diff --git a/SDL2pp/EventWait.hh b/SDL2pp/EventWait.hh index d29154e..bbc8338 100644 --- a/SDL2pp/EventWait.hh +++ b/SDL2pp/EventWait.hh @@ -24,8 +24,13 @@ #include +#include + namespace SDL2pp { namespace Event { + SDL_Event WaitEvent(); + + Optional WaitEvent(int timeout); } } From 10c653b8158aa95f022b1b9a122fcad56fdfc6c9 Mon Sep 17 00:00:00 2001 From: Vraiment Date: Sun, 23 Jul 2017 01:08:42 -0700 Subject: [PATCH 12/16] Added event wait tests --- tests/test_eventwait.cc | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/test_eventwait.cc b/tests/test_eventwait.cc index 3d82755..8487771 100644 --- a/tests/test_eventwait.cc +++ b/tests/test_eventwait.cc @@ -8,4 +8,34 @@ using namespace SDL2pp; using namespace SDL2pp::Event; BEGIN_TEST(int, char*[]) + // Test wait event with no timeout + { + SDL_Event expected; + expected.type = SDL_USEREVENT; + expected.user.code = 99; + SDL_PushEvent(&expected); + + auto result = WaitEvent(); + EXPECT_TRUE(result.type == expected.type); + EXPECT_TRUE(result.user.code == expected.user.code); + } + + // Test wait event with timeout and with event + { + SDL_Event expected; + expected.type = SDL_USEREVENT; + expected.user.code = 32; + SDL_PushEvent(&expected); + + auto result = WaitEvent(10); + EXPECT_TRUE(!(result == NullOpt)); + EXPECT_TRUE(result->type == expected.type); + EXPECT_TRUE(result->user.code == expected.user.code); + } + + // Test wait event with timeout and no event + { + auto result = WaitEvent(10); + EXPECT_TRUE(result == NullOpt); + } END_TEST() From 7d3e54a29f428c50ab216e8d7fb4dc011e14105e Mon Sep 17 00:00:00 2001 From: Vraiment Date: Sun, 23 Jul 2017 01:19:59 -0700 Subject: [PATCH 13/16] Added EventWait documentation --- SDL2pp/EventWait.hh | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/SDL2pp/EventWait.hh b/SDL2pp/EventWait.hh index bbc8338..dc55bd0 100644 --- a/SDL2pp/EventWait.hh +++ b/SDL2pp/EventWait.hh @@ -28,8 +28,47 @@ namespace SDL2pp { namespace Event { + //////////////////////////////////////////////////////////// + /// \brief Waits indefinitely for an event + /// + /// This function waits for the next available event. This + /// function WILL STOP the program execution until an event + /// is available. + /// + /// \ingroup events + /// + /// \headerfile SDL2pp/EventWait.hh + /// + /// \returns The event that was available + /// + /// \throws SDL2pp::Exception + /// + /// \see https://wiki.libsdl.org/SDL_WaitEvent + /// + //////////////////////////////////////////////////////////// SDL_Event WaitEvent(); + //////////////////////////////////////////////////////////// + /// \brief Waits a given amount of time for an event + /// + /// This function waits for the next available event for + /// specified amount of time. This function WILL STOP the + /// program execution until the given amount of time runs out + /// or an event is available. + /// + /// \ingroup events + /// + /// \headerfile SDL2pp/EventWait.hh + /// + /// \param[in] timeout The amount of time to wait for an event + /// + /// \returns The last event that was available + /// + /// \throws SDL2pp::Exception + /// + /// \see https://wiki.libsdl.org/SDL_WaitEvent + /// + //////////////////////////////////////////////////////////// Optional WaitEvent(int timeout); } } From 5a2c8133001f0d289bfe0434d6dfb47ef1c58879 Mon Sep 17 00:00:00 2001 From: Vraiment Date: Sun, 23 Jul 2017 01:25:16 -0700 Subject: [PATCH 14/16] Export EventWait functions --- SDL2pp/EventWait.hh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/SDL2pp/EventWait.hh b/SDL2pp/EventWait.hh index dc55bd0..c8a3e42 100644 --- a/SDL2pp/EventWait.hh +++ b/SDL2pp/EventWait.hh @@ -24,6 +24,7 @@ #include +#include #include namespace SDL2pp { @@ -46,7 +47,7 @@ namespace SDL2pp { /// \see https://wiki.libsdl.org/SDL_WaitEvent /// //////////////////////////////////////////////////////////// - SDL_Event WaitEvent(); + SDL2PP_EXPORT SDL_Event WaitEvent(); //////////////////////////////////////////////////////////// /// \brief Waits a given amount of time for an event @@ -69,7 +70,7 @@ namespace SDL2pp { /// \see https://wiki.libsdl.org/SDL_WaitEvent /// //////////////////////////////////////////////////////////// - Optional WaitEvent(int timeout); + SDL2PP_EXPORT Optional WaitEvent(int timeout); } } From 24c172d7ad67efb3a79d5173e2d06e7bb5a87d8e Mon Sep 17 00:00:00 2001 From: Vraiment Date: Sun, 23 Jul 2017 01:55:26 -0700 Subject: [PATCH 15/16] Fix EventWait tests --- tests/test_eventwait.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_eventwait.cc b/tests/test_eventwait.cc index 8487771..4e3ea8b 100644 --- a/tests/test_eventwait.cc +++ b/tests/test_eventwait.cc @@ -1,5 +1,6 @@ -#include +#include +#include #include #include "testing.h" @@ -8,6 +9,8 @@ using namespace SDL2pp; using namespace SDL2pp::Event; BEGIN_TEST(int, char*[]) + const SDL sdl(SDL_INIT_EVENTS); + // Test wait event with no timeout { SDL_Event expected; From 945e435ad521734ddebc2c88a5a56acf6f71af26 Mon Sep 17 00:00:00 2001 From: Vraiment Date: Sun, 23 Jul 2017 12:59:14 -0700 Subject: [PATCH 16/16] Changed event polling functions from templates to use std::function --- SDL2pp/EventPolling.cc | 19 +++++++++++++++ SDL2pp/EventPolling.hh | 47 ++++++++++++++++++++++++-------------- tests/test_eventpolling.cc | 4 ++-- 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/SDL2pp/EventPolling.cc b/SDL2pp/EventPolling.cc index 06b684d..9df99a6 100644 --- a/SDL2pp/EventPolling.cc +++ b/SDL2pp/EventPolling.cc @@ -21,6 +21,8 @@ #include +using std::function; + namespace SDL2pp { namespace Event { bool PollEvent() { @@ -30,6 +32,17 @@ namespace SDL2pp { return result; } + bool PollEvent(function 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 eventHandler) { + int result; + for (result = 0; PollEvent(eventHandler); result++); + return result; + } } } diff --git a/SDL2pp/EventPolling.hh b/SDL2pp/EventPolling.hh index 16b0613..eed2d2b 100644 --- a/SDL2pp/EventPolling.hh +++ b/SDL2pp/EventPolling.hh @@ -26,6 +26,8 @@ #include +#include + 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 - bool PollEvent(T&& eventHandler) { - SDL_Event event; - if (!SDL_PollEvent(&event)) { - return false; - } - - eventHandler(event); - - return true; - } + SDL2PP_EXPORT bool PollEvent(std::function 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 - int PollAllEvents(T&& eventHandler) { - int result; - for (result = 0; PollEvent(eventHandler); result++); - return result; - } + SDL2PP_EXPORT int PollAllEvents(std::function eventHandler); } } diff --git a/tests/test_eventpolling.cc b/tests/test_eventpolling.cc index 66e4451..a7a662e 100644 --- a/tests/test_eventpolling.cc +++ b/tests/test_eventpolling.cc @@ -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(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++) {