mirror of
https://github.com/SFML/SFML.git
synced 2025-02-18 06:18:01 +08:00
Return std::optional<Event>
from sf::WindowBase::pollEvent
This commit is contained in:
parent
1c873112ec
commit
068ae6f575
4
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
4
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
@ -57,8 +57,10 @@ body:
|
|||||||
|
|
||||||
while (window.isOpen())
|
while (window.isOpen())
|
||||||
{
|
{
|
||||||
for (sf::Event event; window.pollEvent(event);)
|
while (const auto maybeEvent = window.pollEvent())
|
||||||
{
|
{
|
||||||
|
const auto& event = *maybeEvent;
|
||||||
|
|
||||||
if (event.is<sf::Event::Closed>())
|
if (event.is<sf::Event::Closed>())
|
||||||
window.close();
|
window.close();
|
||||||
}
|
}
|
||||||
|
4
.github/ISSUE_TEMPLATE/feature_request.yml
vendored
4
.github/ISSUE_TEMPLATE/feature_request.yml
vendored
@ -54,8 +54,10 @@ body:
|
|||||||
|
|
||||||
while (window.isOpen())
|
while (window.isOpen())
|
||||||
{
|
{
|
||||||
for (sf::Event event; window.pollEvent(event);)
|
while (const auto maybeEvent = window.pollEvent())
|
||||||
{
|
{
|
||||||
|
const auto& event = *maybeEvent;
|
||||||
|
|
||||||
if (event.is<sf::Event::Closed>())
|
if (event.is<sf::Event::Closed>())
|
||||||
window.close();
|
window.close();
|
||||||
}
|
}
|
||||||
|
4
.github/pull_request_template.md
vendored
4
.github/pull_request_template.md
vendored
@ -39,8 +39,10 @@ int main()
|
|||||||
|
|
||||||
while (window.isOpen())
|
while (window.isOpen())
|
||||||
{
|
{
|
||||||
for (sf::Event event; window.pollEvent(event);)
|
while (const auto maybeEvent = window.pollEvent())
|
||||||
{
|
{
|
||||||
|
const auto& event = *maybeEvent;
|
||||||
|
|
||||||
if (event.is<sf::Event::Closed>())
|
if (event.is<sf::Event::Closed>())
|
||||||
window.close();
|
window.close();
|
||||||
}
|
}
|
||||||
|
@ -44,8 +44,10 @@
|
|||||||
/// while (window.isOpen())
|
/// while (window.isOpen())
|
||||||
/// {
|
/// {
|
||||||
/// // Process events
|
/// // Process events
|
||||||
/// for (sf::Event event; window.pollEvent(event);)
|
/// while (const auto maybeEvent = window.pollEvent())
|
||||||
/// {
|
/// {
|
||||||
|
/// const auto& event = *maybeEvent;
|
||||||
|
///
|
||||||
/// // Close window: exit
|
/// // Close window: exit
|
||||||
/// if (event.is<sf::Event::Closed>())
|
/// if (event.is<sf::Event::Closed>())
|
||||||
/// window.close();
|
/// window.close();
|
||||||
|
@ -113,8 +113,10 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
while (window.isOpen())
|
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())
|
switch (event.getType())
|
||||||
{
|
{
|
||||||
case sf::Event::Type::Closed:
|
case sf::Event::Type::Closed:
|
||||||
|
@ -179,8 +179,10 @@ int main()
|
|||||||
while (window.isOpen())
|
while (window.isOpen())
|
||||||
{
|
{
|
||||||
// Handle events
|
// 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
|
// Window closed or escape key pressed: exit
|
||||||
if (event.is<sf::Event::Closed>() ||
|
if (event.is<sf::Event::Closed>() ||
|
||||||
(event.is<sf::Event::KeyPressed>() && event.get<sf::Event::KeyPressed>().code == sf::Keyboard::Escape))
|
(event.is<sf::Event::KeyPressed>() && event.get<sf::Event::KeyPressed>().code == sf::Keyboard::Escape))
|
||||||
|
@ -159,8 +159,10 @@ int main()
|
|||||||
while (window.isOpen())
|
while (window.isOpen())
|
||||||
{
|
{
|
||||||
// Handle events
|
// 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
|
// Window closed or escape key pressed: exit
|
||||||
if (event.is<sf::Event::Closed>() ||
|
if (event.is<sf::Event::Closed>() ||
|
||||||
(event.is<sf::Event::KeyPressed>() && event.get<sf::Event::KeyPressed>().code == sf::Keyboard::Escape))
|
(event.is<sf::Event::KeyPressed>() && event.get<sf::Event::KeyPressed>().code == sf::Keyboard::Escape))
|
||||||
|
@ -204,8 +204,10 @@ int main()
|
|||||||
while (window.isOpen())
|
while (window.isOpen())
|
||||||
{
|
{
|
||||||
// Process events
|
// Process events
|
||||||
for (sf::Event event; window.pollEvent(event);)
|
while (const auto maybeEvent = window.pollEvent())
|
||||||
{
|
{
|
||||||
|
const auto& event = *maybeEvent;
|
||||||
|
|
||||||
// Close window: exit
|
// Close window: exit
|
||||||
if (event.is<sf::Event::Closed>())
|
if (event.is<sf::Event::Closed>())
|
||||||
{
|
{
|
||||||
|
@ -390,8 +390,10 @@ int main()
|
|||||||
while (window.isOpen())
|
while (window.isOpen())
|
||||||
{
|
{
|
||||||
// Process events
|
// Process events
|
||||||
for (sf::Event event; window.pollEvent(event);)
|
while (const auto maybeEvent = window.pollEvent())
|
||||||
{
|
{
|
||||||
|
const auto& event = *maybeEvent;
|
||||||
|
|
||||||
// Close window: exit
|
// Close window: exit
|
||||||
if (event.is<sf::Event::Closed>())
|
if (event.is<sf::Event::Closed>())
|
||||||
window.close();
|
window.close();
|
||||||
|
@ -111,8 +111,10 @@ int main()
|
|||||||
while (window.isOpen())
|
while (window.isOpen())
|
||||||
{
|
{
|
||||||
// Handle events
|
// 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
|
// Window closed or escape key pressed: exit
|
||||||
if (event.is<sf::Event::Closed>() ||
|
if (event.is<sf::Event::Closed>() ||
|
||||||
(event.is<sf::Event::KeyPressed>() && event.get<sf::Event::KeyPressed>().code == sf::Keyboard::Escape))
|
(event.is<sf::Event::KeyPressed>() && event.get<sf::Event::KeyPressed>().code == sf::Keyboard::Escape))
|
||||||
|
@ -2541,8 +2541,10 @@ public:
|
|||||||
while (window.isOpen())
|
while (window.isOpen())
|
||||||
{
|
{
|
||||||
// Process events
|
// Process events
|
||||||
for (sf::Event event; window.pollEvent(event);)
|
while (const auto maybeEvent = window.pollEvent())
|
||||||
{
|
{
|
||||||
|
const auto& event = *maybeEvent;
|
||||||
|
|
||||||
// Close window: exit
|
// Close window: exit
|
||||||
if (event.is<sf::Event::Closed>())
|
if (event.is<sf::Event::Closed>())
|
||||||
window.close();
|
window.close();
|
||||||
|
@ -141,8 +141,10 @@ int main()
|
|||||||
while (window.isOpen())
|
while (window.isOpen())
|
||||||
{
|
{
|
||||||
// Process events
|
// Process events
|
||||||
for (sf::Event event; window.pollEvent(event);)
|
while (const auto maybeEvent = window.pollEvent())
|
||||||
{
|
{
|
||||||
|
const auto& event = *maybeEvent;
|
||||||
|
|
||||||
// Close window: exit
|
// Close window: exit
|
||||||
if (event.is<sf::Event::Closed>())
|
if (event.is<sf::Event::Closed>())
|
||||||
window.close();
|
window.close();
|
||||||
|
@ -214,9 +214,11 @@ private:
|
|||||||
/// // The main loop - ends as soon as the window is closed
|
/// // The main loop - ends as soon as the window is closed
|
||||||
/// while (window.isOpen())
|
/// while (window.isOpen())
|
||||||
/// {
|
/// {
|
||||||
/// // Event processing
|
/// // Event processing
|
||||||
/// for (sf::Event event; window.pollEvent(event);)
|
/// while (const auto maybeEvent = window.pollEvent())
|
||||||
/// {
|
/// {
|
||||||
|
/// const auto& event = *maybeEvent;
|
||||||
|
///
|
||||||
/// // Request for closing the window
|
/// // Request for closing the window
|
||||||
/// if (event.is<sf::Event::Closed>())
|
/// if (event.is<sf::Event::Closed>())
|
||||||
/// window.close();
|
/// window.close();
|
||||||
|
@ -91,8 +91,10 @@ SFML_WINDOW_API void setString(const String& text);
|
|||||||
/// sf::String string = sf::Clipboard::getString();
|
/// sf::String string = sf::Clipboard::getString();
|
||||||
///
|
///
|
||||||
/// // or use it in the event loop
|
/// // 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<sf::Event::Closed>())
|
/// if(event.is<sf::Event::Closed>())
|
||||||
/// window.close();
|
/// window.close();
|
||||||
/// if(event.is<sf::Event::KeyPressed>())
|
/// if(event.is<sf::Event::KeyPressed>())
|
||||||
|
@ -441,8 +441,10 @@ const T* Event::getIf() const
|
|||||||
/// statement to process events.
|
/// statement to process events.
|
||||||
///
|
///
|
||||||
/// \code
|
/// \code
|
||||||
/// for (sf::Event event; window.pollEvent(event);)
|
/// while (const auto maybeEvent = window.pollEvent())
|
||||||
/// {
|
/// {
|
||||||
|
/// const auto& event = *maybeEvent;
|
||||||
|
///
|
||||||
/// switch (event.getType())
|
/// switch (event.getType())
|
||||||
/// {
|
/// {
|
||||||
/// // Request for closing the window
|
/// // Request for closing the window
|
||||||
@ -475,8 +477,10 @@ const T* Event::getIf() const
|
|||||||
/// process events in a series of if/else if blocks.
|
/// process events in a series of if/else if blocks.
|
||||||
///
|
///
|
||||||
/// \code
|
/// \code
|
||||||
/// for (sf::Event event; window.pollEvent(event);)
|
/// while (const auto maybeEvent = window.pollEvent())
|
||||||
/// {
|
/// {
|
||||||
|
/// const auto& event = *maybeEvent;
|
||||||
|
///
|
||||||
/// // Request for closing the window
|
/// // Request for closing the window
|
||||||
/// if (event.is<sf::Event::Closed>())
|
/// if (event.is<sf::Event::Closed>())
|
||||||
/// window.close();
|
/// window.close();
|
||||||
|
@ -321,8 +321,10 @@ private:
|
|||||||
/// while (window.isOpen())
|
/// while (window.isOpen())
|
||||||
/// {
|
/// {
|
||||||
/// // Event processing
|
/// // Event processing
|
||||||
/// for (sf::Event event; window.pollEvent(event);)
|
/// while (const auto maybeEvent = window.pollEvent())
|
||||||
/// {
|
/// {
|
||||||
|
/// const auto& event = *maybeEvent;
|
||||||
|
///
|
||||||
/// // Request for closing the window
|
/// // Request for closing the window
|
||||||
/// if (event.is<sf::Event::Closed>())
|
/// if (event.is<sf::Event::Closed>())
|
||||||
/// window.close();
|
/// window.close();
|
||||||
|
@ -167,7 +167,7 @@ public:
|
|||||||
/// thus you should always call this function in a loop
|
/// thus you should always call this function in a loop
|
||||||
/// to make sure that you process every pending event.
|
/// to make sure that you process every pending event.
|
||||||
/// \code
|
/// \code
|
||||||
/// for (sf::Event event; window.pollEvent(event);)
|
/// while (const auto maybeEvent = window.pollEvent())
|
||||||
/// {
|
/// {
|
||||||
/// // process event...
|
/// // process event...
|
||||||
/// }
|
/// }
|
||||||
@ -180,7 +180,7 @@ public:
|
|||||||
/// \see waitEvent
|
/// \see waitEvent
|
||||||
///
|
///
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
[[nodiscard]] bool pollEvent(Event& event);
|
[[nodiscard]] std::optional<Event> pollEvent();
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
/// \brief Wait for an event and return it
|
/// \brief Wait for an event and return it
|
||||||
@ -193,8 +193,7 @@ public:
|
|||||||
/// is dedicated to events handling: you want to make this thread
|
/// is dedicated to events handling: you want to make this thread
|
||||||
/// sleep as long as no new event is received.
|
/// sleep as long as no new event is received.
|
||||||
/// \code
|
/// \code
|
||||||
/// sf::Event event;
|
/// if (window.waitEvent())
|
||||||
/// if (window.waitEvent(event))
|
|
||||||
/// {
|
/// {
|
||||||
/// // process event...
|
/// // process event...
|
||||||
/// }
|
/// }
|
||||||
@ -207,7 +206,7 @@ public:
|
|||||||
/// \see pollEvent
|
/// \see pollEvent
|
||||||
///
|
///
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
[[nodiscard]] bool waitEvent(Event& event);
|
[[nodiscard]] std::optional<Event> waitEvent();
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
/// \brief Get the position of the window
|
/// \brief Get the position of the window
|
||||||
@ -532,8 +531,10 @@ private:
|
|||||||
/// while (window.isOpen())
|
/// while (window.isOpen())
|
||||||
/// {
|
/// {
|
||||||
/// // Event processing
|
/// // Event processing
|
||||||
/// for (sf::Event event; window.pollEvent(event);)
|
/// while (const auto maybeEvent = window.pollEvent())
|
||||||
/// {
|
/// {
|
||||||
|
/// const auto& event = *maybeEvent;
|
||||||
|
///
|
||||||
/// // Request for closing the window
|
/// // Request for closing the window
|
||||||
/// if (event.is<sf::Event::Closed>())
|
/// if (event.is<sf::Event::Closed>())
|
||||||
/// window.close();
|
/// window.close();
|
||||||
|
@ -157,36 +157,30 @@ bool WindowBase::isOpen() const
|
|||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
bool WindowBase::pollEvent(Event& event)
|
std::optional<Event> WindowBase::pollEvent()
|
||||||
{
|
{
|
||||||
if (!m_impl)
|
if (!m_impl)
|
||||||
return false;
|
return std::nullopt;
|
||||||
|
|
||||||
if (const auto maybeEvent = m_impl->popEvent(false))
|
const auto event = m_impl->popEvent(false);
|
||||||
{
|
if (event)
|
||||||
filterEvent(*maybeEvent);
|
filterEvent(*event);
|
||||||
event = *maybeEvent;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
bool WindowBase::waitEvent(Event& event)
|
std::optional<Event> WindowBase::waitEvent()
|
||||||
{
|
{
|
||||||
if (!m_impl)
|
if (!m_impl)
|
||||||
return false;
|
return std::nullopt;
|
||||||
|
|
||||||
if (const auto maybeEvent = m_impl->popEvent(true))
|
const auto event = m_impl->popEvent(true);
|
||||||
{
|
if (event)
|
||||||
filterEvent(*maybeEvent);
|
filterEvent(*event);
|
||||||
event = *maybeEvent;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -81,15 +81,13 @@ TEST_CASE("[Window] sf::WindowBase", runDisplayTests())
|
|||||||
SECTION("pollEvent()")
|
SECTION("pollEvent()")
|
||||||
{
|
{
|
||||||
sf::WindowBase windowBase;
|
sf::WindowBase windowBase;
|
||||||
sf::Event event;
|
CHECK(!windowBase.pollEvent());
|
||||||
CHECK(!windowBase.pollEvent(event));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("waitEvent()")
|
SECTION("waitEvent()")
|
||||||
{
|
{
|
||||||
sf::WindowBase windowBase;
|
sf::WindowBase windowBase;
|
||||||
sf::Event event;
|
CHECK(!windowBase.waitEvent());
|
||||||
CHECK(!windowBase.waitEvent(event));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Set/get position")
|
SECTION("Set/get position")
|
||||||
|
@ -65,8 +65,10 @@ int main()
|
|||||||
while (window.isOpen())
|
while (window.isOpen())
|
||||||
{
|
{
|
||||||
// Process events
|
// Process events
|
||||||
for (sf::Event event; window.pollEvent(event);)
|
while (const auto maybeEvent = window.pollEvent())
|
||||||
{
|
{
|
||||||
|
const auto& event = *maybeEvent;
|
||||||
|
|
||||||
// Close window: exit
|
// Close window: exit
|
||||||
if (event.is<sf::Event::Closed>())
|
if (event.is<sf::Event::Closed>())
|
||||||
{
|
{
|
||||||
|
@ -63,8 +63,10 @@ int main()
|
|||||||
while (window.isOpen())
|
while (window.isOpen())
|
||||||
{
|
{
|
||||||
// Process events
|
// Process events
|
||||||
for (sf::Event event; window.pollEvent(event);)
|
while (const auto maybeEvent = window.pollEvent())
|
||||||
{
|
{
|
||||||
|
const auto& event = *maybeEvent;
|
||||||
|
|
||||||
// Close window: exit
|
// Close window: exit
|
||||||
if (event.is<sf::Event::Closed>())
|
if (event.is<sf::Event::Closed>())
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user