Emre801
Emre801

Reputation: 3203

Rotate texture sfml and save output to file

I want to start out by saying I am a complete c++ novice. I do not know any of the vocabulary associated with it so if this question is trivial I apologies, as I do not now the correct terms or phrases to google the solution myself.

Currently playing around with this code I found on github to capture gameplay from my 3ds. https://github.com/Gotos/Cute3DSCapture/blob/master/main.cpp

I am trying to add the ability to rotate the image and save it to a png file.

from main.cpp around lines 267 I am trying to add the following functionality.

sprite.setTexture(texture);
sprite.rotate(90);
texture = sprite.getTexture();
texture.copyToImage().saveToFile("Something/Place/img.png");

Current texture and sprite are defined as the following.

sf::Texture texture;
sf::Sprite sprite;

When I try to build and run I get the followin

main.cpp:269:25: error: no viable overloaded '='
                texture = sprite.getTexture();
                ~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~
/usr/local/include/SFML/Graphics/Texture.hpp:421:14: note: candidate function
      not viable: no known conversion from 'const sf::Texture *' to
      'const sf::Texture' for 1st argument; dereference the argument with *
    Texture& operator =(const Texture& right);

Any help would be greatly appreciated.

Upvotes: 1

Views: 1914

Answers (2)

NewGuy
NewGuy

Reputation: 1

You could just add another sf::Texture Newtexture = sprite.getTexture(); Newtexture.copyToImage().saveToFile("Something/Place/img.png");

Upvotes: 0

Hiura
Hiura

Reputation: 3530

sf::Sprite is not a texture manipulator, which means that the texture returned by sf::Sprite::getTexture() will not be modified. That's why the Sprite's ctor takes a reference to a const texture instance.

If you're solely interested in image manipulation, I'd recommend using something else than SFML as you might get better features/performance for specific operations. Probably something like imagemagick.

With SFML, you can do it roughly like this:

  • Create a RenderTexture of the desired size -- think of this as a virtual window that give you access to its texture when .display() has been called.
  • Create your sprite using your initial texture and some manipulation (e.g. rotation) -- make sure that the size of the render texture is correctly set to "display" the resulting image
  • Draw your sprite on the render texture and call display() on it.
  • Then you can call sf::RenderTexture::getTexture() and chain copyToImage() and saveToFile().

Look at the detailed description of sf::RenderTexture for an example of use.

Upvotes: 3

Related Questions