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

Author Topic: Save to Image not working?  (Read 2540 times)

0 Members and 1 Guest are viewing this topic.

Yaxlat

  • Newbie
  • *
  • Posts: 14
    • View Profile
Save to Image not working?
« 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.

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Save to Image not working?
« Reply #1 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.

Yaxlat

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Save to Image not working?
« Reply #2 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

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Save to Image not working?
« Reply #3 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Save to Image not working?
« Reply #4 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.
Laurent Gomila - SFML developer