Could someone kindly tell me why this doesn't work, and if it is considered a bug or not?
The following code does not correctly take a screenshot, the PNG file rendered is blank with RGBA (0, 0, 0, 0).
I have the following code:
#include <SFML/Graphics.hpp>
const int ScreenWidth = 3000/4;
const int ScreenHeight = 2000/4;
int main()
{
sf::RenderWindow window(sf::VideoMode(ScreenWidth, ScreenHeight), "SFML Animation"/*, sf::Style::None*/);
sf::RectangleShape rect(sf::Vector2f(100, 100));
rect.setPosition(sf::Vector2f(100, 100));
while(window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::Blue);
window.draw(rect);
window.display();
//static int i = 0;
//if (i++ > 10)
//{
sf::Image screenshot = window.capture();
screenshot.saveToFile("test_big000.png");
return 0;
//}
}
}
If I uncomment the last lines to be the following, to allow more frames to be processed before a screenshot, a correct screenshot is made:
static int i = 0;
if (i++ > 10) // just to make it have to process the loop a few times
{
sf::Image screenshot = window.capture();
screenshot.saveToFile("test_big000.png");
return 0;
}
Short version:
Why does the capture() function not work on the first frame of the program?
Is this intended behavior? Couldn't find anything with search to verify this claim.
Also note that the screenshot.saveToFile() is returning true.
I'm on Windows 7, if that matters, using SFML 2.2 compiled from github about a week ago.
I tested this for you. I found similar - but not equal - results. On the first cycle, it saves a pure blue screenshot (for me).
I confirmed that it has nothing to do with it forcing the window closed so soon afterwards by changing the screenshot code to:
static bool done = false;
if (!done)
{
sf::Image screenshot = window.capture();
screenshot.saveToFile("G:/Resource Pool/temp/test_big000.png");
done = true;
}
which keeps the window open after taking the screenshot. The screenshot (again, for me) is blue only.
I would suspect that this is closely related to the fact that the window isn't actually drawn instantly so the screenshot is capturing the image before the OS has actually finished displaying the window.