Hi there! I'd been looking at a tutorial to make a simple screenshot function, and it had suggested to use threads so game performance wasn't impacted. Currently I'm using the C++11 threads. I was having a terrible time trying to get this to work, but found that you have to disable the OpenGL context when dealing with a window in another thread with the RenderWindow's setActive().
The problem with this, at least as I understand it, I have to wait for the function\thread to be done so I can set the window active again and therefore I suffer from the 1-2 second pause of taking a screenshot that I was trying to avoid because the capture() is slow (according to the SFML wiki)
Is there any way around this?
To give you an idea of what I have:
void AMEngine::ScreenCapture()
{
sf::Image screenshot = MainWindow->capture();
time_t epoch_time;
struct tm tm_p;
errno_t err;
epoch_time = time(NULL);
err = localtime_s(&tm_p, &epoch_time);
//ToDo: Improve screenshot name formatting
std::string screenshotName = std::to_string(
tm_p.tm_mday) +
"-" +
std::to_string(tm_p.tm_mon) +
"-" +
std::to_string(tm_p.tm_year) +
" " +
std::to_string(tm_p.tm_hour) +
std::to_string(tm_p.tm_min) +
std::to_string(tm_p.tm_sec);
screenshot.saveToFile("screenshots/" + screenshotName + ".png");
MainWindow->display();
}
void AMEngine::Update()
{
sf::Event event;
while (MainWindow->pollEvent(event))
{
if (event.type == sf::Event::Closed)
m_bIsRunning = false;
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
m_bIsRunning = false;
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::F5)
{
MainWindow->setActive(false);
std::thread tSC(&AMEngine::ScreenCapture, this);
tSC.join();
MainWindow->setActive(true);
}
}
}
Or is this a pipe dream?
I tried searching about screenshots with threads, but ironically, there's a thread on this forum called the Screenshot Thread. XD