diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index 49f871afb..18c789cfc 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -57,8 +57,10 @@ body: while (window.isOpen()) { - for (sf::Event event; window.pollEvent(event);) + while (const auto maybeEvent = window.pollEvent()) { + const auto& event = *maybeEvent; + if (event.is()) window.close(); } diff --git a/.github/ISSUE_TEMPLATE/feature_request.yml b/.github/ISSUE_TEMPLATE/feature_request.yml index d1661aa2a..789586d40 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.yml +++ b/.github/ISSUE_TEMPLATE/feature_request.yml @@ -54,8 +54,10 @@ body: while (window.isOpen()) { - for (sf::Event event; window.pollEvent(event);) + while (const auto maybeEvent = window.pollEvent()) { + const auto& event = *maybeEvent; + if (event.is()) window.close(); } diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 4475cea18..ed5ce20de 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -39,8 +39,10 @@ int main() while (window.isOpen()) { - for (sf::Event event; window.pollEvent(event);) + while (const auto maybeEvent = window.pollEvent()) { + const auto& event = *maybeEvent; + if (event.is()) window.close(); } diff --git a/doc/mainpage.hpp b/doc/mainpage.hpp index abd0f4824..7f916253e 100644 --- a/doc/mainpage.hpp +++ b/doc/mainpage.hpp @@ -44,8 +44,10 @@ /// while (window.isOpen()) /// { /// // Process events -/// for (sf::Event event; window.pollEvent(event);) +/// while (const auto maybeEvent = window.pollEvent()) /// { +/// const auto& event = *maybeEvent; +/// /// // Close window: exit /// if (event.is()) /// window.close(); diff --git a/examples/android/app/src/main/jni/main.cpp b/examples/android/app/src/main/jni/main.cpp index 26afec3da..f99e6c54e 100644 --- a/examples/android/app/src/main/jni/main.cpp +++ b/examples/android/app/src/main/jni/main.cpp @@ -113,8 +113,10 @@ int main(int argc, char* argv[]) while (window.isOpen()) { - for (sf::Event event; active ? window.pollEvent(event) : window.waitEvent(event);) + while (const auto maybeEvent = active ? window.pollEvent() : window.waitEvent()) { + const auto& event = *maybeEvent; + switch (event.getType()) { case sf::Event::Type::Closed: diff --git a/examples/island/Island.cpp b/examples/island/Island.cpp index eaf6445c4..e5bbc284b 100644 --- a/examples/island/Island.cpp +++ b/examples/island/Island.cpp @@ -179,8 +179,10 @@ int main() while (window.isOpen()) { // Handle events - for (sf::Event event; window.pollEvent(event);) + while (const auto maybeEvent = window.pollEvent()) { + const auto& event = *maybeEvent; + // Window closed or escape key pressed: exit if (event.is() || (event.is() && event.get().code == sf::Keyboard::Escape)) diff --git a/examples/joystick/Joystick.cpp b/examples/joystick/Joystick.cpp index 9fd61ab58..6f1c1847b 100644 --- a/examples/joystick/Joystick.cpp +++ b/examples/joystick/Joystick.cpp @@ -159,8 +159,10 @@ int main() while (window.isOpen()) { // Handle events - for (sf::Event event; window.pollEvent(event);) + while (const auto maybeEvent = window.pollEvent()) { + const auto& event = *maybeEvent; + // Window closed or escape key pressed: exit if (event.is() || (event.is() && event.get().code == sf::Keyboard::Escape)) diff --git a/examples/opengl/OpenGL.cpp b/examples/opengl/OpenGL.cpp index 0cfcafc43..f828f9f0d 100644 --- a/examples/opengl/OpenGL.cpp +++ b/examples/opengl/OpenGL.cpp @@ -204,8 +204,10 @@ int main() while (window.isOpen()) { // Process events - for (sf::Event event; window.pollEvent(event);) + while (const auto maybeEvent = window.pollEvent()) { + const auto& event = *maybeEvent; + // Close window: exit if (event.is()) { diff --git a/examples/shader/Shader.cpp b/examples/shader/Shader.cpp index 0d559089e..7969687e6 100644 --- a/examples/shader/Shader.cpp +++ b/examples/shader/Shader.cpp @@ -390,8 +390,10 @@ int main() while (window.isOpen()) { // Process events - for (sf::Event event; window.pollEvent(event);) + while (const auto maybeEvent = window.pollEvent()) { + const auto& event = *maybeEvent; + // Close window: exit if (event.is()) window.close(); diff --git a/examples/tennis/Tennis.cpp b/examples/tennis/Tennis.cpp index ea058aa1e..8b98fec86 100644 --- a/examples/tennis/Tennis.cpp +++ b/examples/tennis/Tennis.cpp @@ -111,8 +111,10 @@ int main() while (window.isOpen()) { // Handle events - for (sf::Event event; window.pollEvent(event);) + while (const auto maybeEvent = window.pollEvent()) { + const auto& event = *maybeEvent; + // Window closed or escape key pressed: exit if (event.is() || (event.is() && event.get().code == sf::Keyboard::Escape)) diff --git a/examples/vulkan/Vulkan.cpp b/examples/vulkan/Vulkan.cpp index aeb95fe65..5dbf5a8e3 100644 --- a/examples/vulkan/Vulkan.cpp +++ b/examples/vulkan/Vulkan.cpp @@ -2541,8 +2541,10 @@ public: while (window.isOpen()) { // Process events - for (sf::Event event; window.pollEvent(event);) + while (const auto maybeEvent = window.pollEvent()) { + const auto& event = *maybeEvent; + // Close window: exit if (event.is()) window.close(); diff --git a/examples/window/Window.cpp b/examples/window/Window.cpp index ecd883b99..89b95122f 100644 --- a/examples/window/Window.cpp +++ b/examples/window/Window.cpp @@ -141,8 +141,10 @@ int main() while (window.isOpen()) { // Process events - for (sf::Event event; window.pollEvent(event);) + while (const auto maybeEvent = window.pollEvent()) { + const auto& event = *maybeEvent; + // Close window: exit if (event.is()) window.close(); diff --git a/include/SFML/Graphics/RenderWindow.hpp b/include/SFML/Graphics/RenderWindow.hpp index 90815d060..4711ccd8d 100644 --- a/include/SFML/Graphics/RenderWindow.hpp +++ b/include/SFML/Graphics/RenderWindow.hpp @@ -214,9 +214,11 @@ private: /// // The main loop - ends as soon as the window is closed /// while (window.isOpen()) /// { -/// // Event processing -/// for (sf::Event event; window.pollEvent(event);) -/// { +/// // Event processing +/// while (const auto maybeEvent = window.pollEvent()) +/// { +/// const auto& event = *maybeEvent; +/// /// // Request for closing the window /// if (event.is()) /// window.close(); diff --git a/include/SFML/Window/Clipboard.hpp b/include/SFML/Window/Clipboard.hpp index 44a9b88e0..8e9503ace 100644 --- a/include/SFML/Window/Clipboard.hpp +++ b/include/SFML/Window/Clipboard.hpp @@ -91,8 +91,10 @@ SFML_WINDOW_API void setString(const String& text); /// sf::String string = sf::Clipboard::getString(); /// /// // or use it in the event loop -/// for (sf::Event event; window.pollEvent(event);) +/// while (const auto maybeEvent = window.pollEvent()) /// { +/// const auto& event = *maybeEvent; +/// /// if(event.is()) /// window.close(); /// if(event.is()) diff --git a/include/SFML/Window/Event.hpp b/include/SFML/Window/Event.hpp index 3f665e0ae..b1b85696a 100644 --- a/include/SFML/Window/Event.hpp +++ b/include/SFML/Window/Event.hpp @@ -441,8 +441,10 @@ const T* Event::getIf() const /// statement to process events. /// /// \code -/// for (sf::Event event; window.pollEvent(event);) +/// while (const auto maybeEvent = window.pollEvent()) /// { +/// const auto& event = *maybeEvent; +/// /// switch (event.getType()) /// { /// // Request for closing the window @@ -475,8 +477,10 @@ const T* Event::getIf() const /// process events in a series of if/else if blocks. /// /// \code -/// for (sf::Event event; window.pollEvent(event);) +/// while (const auto maybeEvent = window.pollEvent()) /// { +/// const auto& event = *maybeEvent; +/// /// // Request for closing the window /// if (event.is()) /// window.close(); diff --git a/include/SFML/Window/Window.hpp b/include/SFML/Window/Window.hpp index c5286c987..65297fc22 100644 --- a/include/SFML/Window/Window.hpp +++ b/include/SFML/Window/Window.hpp @@ -321,8 +321,10 @@ private: /// while (window.isOpen()) /// { /// // Event processing -/// for (sf::Event event; window.pollEvent(event);) +/// while (const auto maybeEvent = window.pollEvent()) /// { +/// const auto& event = *maybeEvent; +/// /// // Request for closing the window /// if (event.is()) /// window.close(); diff --git a/include/SFML/Window/WindowBase.hpp b/include/SFML/Window/WindowBase.hpp index e5bd77ca2..3f57e7760 100644 --- a/include/SFML/Window/WindowBase.hpp +++ b/include/SFML/Window/WindowBase.hpp @@ -167,7 +167,7 @@ public: /// thus you should always call this function in a loop /// to make sure that you process every pending event. /// \code - /// for (sf::Event event; window.pollEvent(event);) + /// while (const auto maybeEvent = window.pollEvent()) /// { /// // process event... /// } @@ -180,7 +180,7 @@ public: /// \see waitEvent /// //////////////////////////////////////////////////////////// - [[nodiscard]] bool pollEvent(Event& event); + [[nodiscard]] std::optional pollEvent(); //////////////////////////////////////////////////////////// /// \brief Wait for an event and return it @@ -193,8 +193,7 @@ public: /// is dedicated to events handling: you want to make this thread /// sleep as long as no new event is received. /// \code - /// sf::Event event; - /// if (window.waitEvent(event)) + /// if (window.waitEvent()) /// { /// // process event... /// } @@ -207,7 +206,7 @@ public: /// \see pollEvent /// //////////////////////////////////////////////////////////// - [[nodiscard]] bool waitEvent(Event& event); + [[nodiscard]] std::optional waitEvent(); //////////////////////////////////////////////////////////// /// \brief Get the position of the window @@ -532,8 +531,10 @@ private: /// while (window.isOpen()) /// { /// // Event processing -/// for (sf::Event event; window.pollEvent(event);) +/// while (const auto maybeEvent = window.pollEvent()) /// { +/// const auto& event = *maybeEvent; +/// /// // Request for closing the window /// if (event.is()) /// window.close(); diff --git a/src/SFML/Window/WindowBase.cpp b/src/SFML/Window/WindowBase.cpp index deb7d37c4..a205028fc 100644 --- a/src/SFML/Window/WindowBase.cpp +++ b/src/SFML/Window/WindowBase.cpp @@ -157,36 +157,30 @@ bool WindowBase::isOpen() const //////////////////////////////////////////////////////////// -bool WindowBase::pollEvent(Event& event) +std::optional WindowBase::pollEvent() { if (!m_impl) - return false; + return std::nullopt; - if (const auto maybeEvent = m_impl->popEvent(false)) - { - filterEvent(*maybeEvent); - event = *maybeEvent; - return true; - } + const auto event = m_impl->popEvent(false); + if (event) + filterEvent(*event); - return false; + return event; } //////////////////////////////////////////////////////////// -bool WindowBase::waitEvent(Event& event) +std::optional WindowBase::waitEvent() { if (!m_impl) - return false; + return std::nullopt; - if (const auto maybeEvent = m_impl->popEvent(true)) - { - filterEvent(*maybeEvent); - event = *maybeEvent; - return true; - } + const auto event = m_impl->popEvent(true); + if (event) + filterEvent(*event); - return false; + return event; } diff --git a/test/Window/WindowBase.test.cpp b/test/Window/WindowBase.test.cpp index dcab1e7d5..efc5f6dcf 100644 --- a/test/Window/WindowBase.test.cpp +++ b/test/Window/WindowBase.test.cpp @@ -81,15 +81,13 @@ TEST_CASE("[Window] sf::WindowBase", runDisplayTests()) SECTION("pollEvent()") { sf::WindowBase windowBase; - sf::Event event; - CHECK(!windowBase.pollEvent(event)); + CHECK(!windowBase.pollEvent()); } SECTION("waitEvent()") { sf::WindowBase windowBase; - sf::Event event; - CHECK(!windowBase.waitEvent(event)); + CHECK(!windowBase.waitEvent()); } SECTION("Set/get position") diff --git a/tools/xcode/templates/SFML/SFML App.xctemplate/main.cpp b/tools/xcode/templates/SFML/SFML App.xctemplate/main.cpp index 4dd6d18f4..8074cf637 100644 --- a/tools/xcode/templates/SFML/SFML App.xctemplate/main.cpp +++ b/tools/xcode/templates/SFML/SFML App.xctemplate/main.cpp @@ -65,8 +65,10 @@ int main() while (window.isOpen()) { // Process events - for (sf::Event event; window.pollEvent(event);) + while (const auto maybeEvent = window.pollEvent()) { + const auto& event = *maybeEvent; + // Close window: exit if (event.is()) { diff --git a/tools/xcode/templates/SFML/SFML CLT.xctemplate/main.cpp b/tools/xcode/templates/SFML/SFML CLT.xctemplate/main.cpp index d90dddc5d..be3cacecb 100644 --- a/tools/xcode/templates/SFML/SFML CLT.xctemplate/main.cpp +++ b/tools/xcode/templates/SFML/SFML CLT.xctemplate/main.cpp @@ -63,8 +63,10 @@ int main() while (window.isOpen()) { // Process events - for (sf::Event event; window.pollEvent(event);) + while (const auto maybeEvent = window.pollEvent()) { + const auto& event = *maybeEvent; + // Close window: exit if (event.is()) {