SFML community forums

Help => General => Topic started by: Yaxlat on March 22, 2015, 01:07:56 am

Title: Save to Image not working?
Post by: Yaxlat on March 22, 2015, 01:07:56 am
I have written the following code:


    string imageName = "levelImage";   
    sf::RenderTexture r;
    for(vector<GameObject*>::const_iterator i = gameObjects.begin(); i != gameObjects.end(); ++i) {
        (*i)->render(r);
    }
    r.getTexture().copyToImage().saveToFile(imageName);

Which should create an image of all my gameObjects. However, I am getting the error message: "failed to save image: "levelImage.png"" I am almost certain the problem is not within the for loop btw.
Title: Re: Save to Image not working?
Post by: dabbertorres on March 22, 2015, 01:29:45 am
You're forgetting sf::RenderTexture::clear() and sf::RenderTexture::display().

They're very similar to a sf::RenderWindow.
Title: Re: Save to Image not working?
Post by: Yaxlat on March 22, 2015, 01:37:19 am
You're forgetting sf::RenderTexture::clear() and sf::RenderTexture::display().

They're very similar to a sf::RenderWindow.

Actually I was forgetting sf::RenderTexture.create();  :)
clear and display are only for interacting with the screen, not saving an image
Title: Re: Save to Image not working?
Post by: dabbertorres on March 22, 2015, 02:17:44 am
If you want a sf::RenderTexture to display the stuff you have drawn to it, you need to at least call display.
Title: Re: Save to Image not working?
Post by: Laurent on March 22, 2015, 09:04:07 am
Quote
clear and display are only for interacting with the screen, not saving an image
I wonder where you've read that. All the doc says is that clear() and display() are needed for correct behaviour; stick to this.