mirror of
https://github.com/SFML/SFML.git
synced 2024-11-24 20:31:05 +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())
|
||||
{
|
||||
for (sf::Event event; window.pollEvent(event);)
|
||||
while (const auto maybeEvent = window.pollEvent())
|
||||
{
|
||||
const auto& event = *maybeEvent;
|
||||
|
||||
if (event.is<sf::Event::Closed>())
|
||||
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())
|
||||
{
|
||||
for (sf::Event event; window.pollEvent(event);)
|
||||
while (const auto maybeEvent = window.pollEvent())
|
||||
{
|
||||
const auto& event = *maybeEvent;
|
||||
|
||||
if (event.is<sf::Event::Closed>())
|
||||
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())
|
||||
{
|
||||
for (sf::Event event; window.pollEvent(event);)
|
||||
while (const auto maybeEvent = window.pollEvent())
|
||||
{
|
||||
const auto& event = *maybeEvent;
|
||||
|
||||
if (event.is<sf::Event::Closed>())
|
||||
window.close();
|
||||
}
|
||||
|
@ -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<sf::Event::Closed>())
|
||||
/// window.close();
|
||||
|
@ -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:
|
||||
|
@ -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<sf::Event::Closed>() ||
|
||||
(event.is<sf::Event::KeyPressed>() && event.get<sf::Event::KeyPressed>().code == sf::Keyboard::Escape))
|
||||
|
@ -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<sf::Event::Closed>() ||
|
||||
(event.is<sf::Event::KeyPressed>() && event.get<sf::Event::KeyPressed>().code == sf::Keyboard::Escape))
|
||||
|
@ -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<sf::Event::Closed>())
|
||||
{
|
||||
|
@ -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<sf::Event::Closed>())
|
||||
window.close();
|
||||
|
@ -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<sf::Event::Closed>() ||
|
||||
(event.is<sf::Event::KeyPressed>() && event.get<sf::Event::KeyPressed>().code == sf::Keyboard::Escape))
|
||||
|
@ -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<sf::Event::Closed>())
|
||||
window.close();
|
||||
|
@ -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<sf::Event::Closed>())
|
||||
window.close();
|
||||
|
@ -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<sf::Event::Closed>())
|
||||
/// window.close();
|
||||
|
@ -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<sf::Event::Closed>())
|
||||
/// window.close();
|
||||
/// if(event.is<sf::Event::KeyPressed>())
|
||||
|
@ -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<sf::Event::Closed>())
|
||||
/// window.close();
|
||||
|
@ -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<sf::Event::Closed>())
|
||||
/// window.close();
|
||||
|
@ -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<Event> 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<Event> 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<sf::Event::Closed>())
|
||||
/// window.close();
|
||||
|
@ -157,36 +157,30 @@ bool WindowBase::isOpen() const
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
bool WindowBase::pollEvent(Event& event)
|
||||
std::optional<Event> 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<Event> 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;
|
||||
}
|
||||
|
||||
|
||||
|
@ -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")
|
||||
|
@ -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<sf::Event::Closed>())
|
||||
{
|
||||
|
@ -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<sf::Event::Closed>())
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user