Skip to content

Instantly share code, notes, and snippets.

@bl4ckb0ne
Created November 11, 2019 16:08
Show Gist options
  • Save bl4ckb0ne/0ee2a8dec13e503c5febfe799675b0ad to your computer and use it in GitHub Desktop.
Save bl4ckb0ne/0ee2a8dec13e503c5febfe799675b0ad to your computer and use it in GitHub Desktop.
optional_sfml_events
std::optional<const sf::Event> poll(sf::RenderWindow &rw)
{
if (f::Event e; rw.pollEvent(e))
{
return e;
}
return std::nullopt;
}
int main()
{
sf::RenderWindow rw; //...
while (const std::optional<const sf::Event> e{poll(rw)})
{
if e->type == sf::Event::Closed)
{
rw.close();
}
}
return 0;
}
template<typename F>
void poll(sf::RenderWindow &rw, F &&f)
{
sf::Event e;
while(rw.ppollEvent(e))
{
f(e);
}
}
int main()
{
sf::RenderWindow rw; //...
poll(rw, [&rw](const sf::Event &e)
{
if (e.type == sf::Event::Closed)
{
rw.close();
}
}
);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment