Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::RenderTexture breaks after an unrelated sf::RenderWindow closes  (Read 4034 times)

0 Members and 1 Guest are viewing this topic.

andreasxp

  • Newbie
  • *
  • Posts: 3
    • View Profile
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.
« Last Edit: September 07, 2020, 07:27:30 pm by andreasxp »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: sf::RenderTexture breaks after an unrelated sf::RenderWindow closes
« Reply #1 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

andreasxp

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: sf::RenderTexture breaks after an unrelated sf::RenderWindow closes
« Reply #2 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.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: sf::RenderTexture breaks after an unrelated sf::RenderWindow closes
« Reply #3 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;
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

andreasxp

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: sf::RenderTexture breaks after an unrelated sf::RenderWindow closes
« Reply #4 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.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: sf::RenderTexture breaks after an unrelated sf::RenderWindow closes
« Reply #5 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.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: sf::RenderTexture breaks after an unrelated sf::RenderWindow closes
« Reply #6 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/
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything