SFML community forums
Help => Graphics => Topic started by: andreasxp on September 07, 2020, 12:38:16 pm
-
Hello, I'm having trouble with a RenderTexture. It seems that in a program where an unrelated RenderWindow has been closed, during the last iteration of the game loop, the RenderTexture gets drawn incorrectly and essentially has nothing on it except for the color used in clear().
I narrowed it down to this piece of code, which draws a simple circle on a green RenderTexture. After the close button has been pressed, The program captures the texture 5 times at different points. On my machine, captures 1 and 2 have the circle, while 3-5 are just green with no graphics.
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
sf::CircleShape shape(200);
shape.setPosition(150, 150);
// Create a new render-texture
sf::RenderTexture texture;
if (!texture.create(500, 500))
return -1;
// The main loop
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
texture.getTexture().copyToImage().saveToFile("test1.png");
window.close();
texture.getTexture().copyToImage().saveToFile("test2.png");
}
}
// Clear the whole texture with green color
texture.clear(sf::Color::Green);
// Draw stuff to the texture
texture.draw(shape); // shape is a sf::Shape
// We're done drawing to the texture
texture.display();
if (!window.isOpen()) texture.getTexture().copyToImage().saveToFile("test3.png");
// Now we start rendering to the window, clear it first
window.clear();
// Draw the texture
sf::Sprite sprite(texture.getTexture());
if (!window.isOpen()) texture.getTexture().copyToImage().saveToFile("test4.png");
window.draw(sprite);
// End the current frame and display its contents on screen
window.display();
if (!window.isOpen()) texture.getTexture().copyToImage().saveToFile("test5.png");
}
}
Is there something I'm doing wrong? Is this a bug in SFML? Thanks.
My specs are: SFML 2.5.1, Windows 10 update 2004, Nvidia graphics with latest drivers. Tested on two machines with similar results.
-
I would presume that test3 - test5 are remnants from a previous test. This code will not be creating those. Try deleting all of the files and run it again. It should only create the first two. The first two are what you are expecting, it seems.
-
Sorry, my mistake, I forgot to delete a line before posting the example. The code I meant to post does not have the line "if (!window.isOpen()) break;". I edited the original post with correct code.
-
With the modified code, what is the result you are experiencing and how does it differ from what you expect (and what do you expect to happen?)?
Also, do me a favour. Try creating an empty context before the window and let me know if that changes anything.
sf::Context context;
-
The result I'm experiencing: captures 1 and 2 have a circle on green background, while 3-5 are just green with no graphics.
The result I expected: all captures have a circle on green background.
Why I expect this result: nothing about the drawing process changes after the window is closed. All drawing is performed on an unrelated RenderTexture. There is a call to texture.draw(shape); just like all other times, and yet the circle does not appear.
Creating a context did not change anything.
-
Can you try using the master branch? This might have been fixed in the last 2 years.
-
You can also find pre-built binaries of the master branch here: https://artifacts.sfml-dev.org/by-branch/master/