Skip to content

Instantly share code, notes, and snippets.

@eXpl0it3r
Created October 31, 2015 01:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eXpl0it3r/6a0d9c911540ec9ac142 to your computer and use it in GitHub Desktop.
Save eXpl0it3r/6a0d9c911540ec9ac142 to your computer and use it in GitHub Desktop.
Non-blocking screenshots with SFML
#include <SFML/Graphics.hpp>
#include <thread>
int main()
{
sf::RenderWindow window({1920, 1080}, "Screenshot!");
sf::Texture tex;
tex.create(1920, 1080);
sf::Clock cl;
while(window.isOpen())
{
sf::Time dt = cl.restart();
window.setTitle(std::to_string(1.f/dt.asSeconds()));
sf::Event event;
while(window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
window.close();
else if(event.type == sf::Event::KeyPressed)
{
tex.update(window);
sf::Image img = tex.copyToImage();
std::thread t([img]() {
img.saveToFile("test.png");
});
t.detach();
}
}
window.clear(sf::Color(0xFF0000FF));
window.display();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment