Created
November 11, 2019 16:08
-
-
Save bl4ckb0ne/0ee2a8dec13e503c5febfe799675b0ad to your computer and use it in GitHub Desktop.
optional_sfml_events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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