SFML community forums

Help => Graphics => Topic started by: andreasxp on September 07, 2020, 12:38:16 pm

Title: sf::RenderTexture breaks after an unrelated sf::RenderWindow closes
Post 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.
Title: Re: sf::RenderTexture breaks after an unrelated sf::RenderWindow closes
Post by: Hapax on September 07, 2020, 07:21:58 pm
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.
Title: Re: sf::RenderTexture breaks after an unrelated sf::RenderWindow closes
Post by: andreasxp on September 07, 2020, 07:28:02 pm
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.
Title: Re: sf::RenderTexture breaks after an unrelated sf::RenderWindow closes
Post by: Hapax on September 12, 2020, 08:39:54 pm
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;
Title: Re: sf::RenderTexture breaks after an unrelated sf::RenderWindow closes
Post by: andreasxp on September 14, 2020, 02:13:07 am
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.
Title: Re: sf::RenderTexture breaks after an unrelated sf::RenderWindow closes
Post by: binary1248 on September 14, 2020, 08:19:42 pm
Can you try using the master branch? This might have been fixed in the last 2 years.
Title: Re: sf::RenderTexture breaks after an unrelated sf::RenderWindow closes
Post by: eXpl0it3r on September 14, 2020, 10:07:28 pm
You can also find pre-built binaries of the master branch here: https://artifacts.sfml-dev.org/by-branch/master/