SFML community forums

Help => Graphics => Topic started by: eglomer on October 28, 2011, 02:30:38 pm

Title: [SOLVED] Sprite resize error
Post by: eglomer on October 28, 2011, 02:30:38 pm
I load an image with:
Code: [Select]
sf::Image img;
sf::Sprite sprite;

img.LoadFromFile("myimage.png"); // An image of 480x530 px
img.SetSmooth(0);

sprite.SetImage(img);
sprite.SetPosition(0, 0);
sprite.Resize(480, 530);


and when I show it with App.Draw (sprite); there's no problem. But then I change the image with:
Code: [Select]
img.LoadFromFile("biggerimage.png"); // An image of 500x500 px
sprite.SetImage(img);
sprite.Resize(480, 530);


then the image looks 500x500 size but cutted to 480x530, so it looks ugly.

How can I change an sprite image into a bigger image and resize it correctly?
Title: [SOLVED] Sprite resize error
Post by: TheCake on October 28, 2011, 04:14:57 pm
Hi

Your first call to sprite.Resize() is actually useless. When you set an image of a sprite for the first time, the sprite will take the size of the image.

Then, the Resize() method is used to change the size of the sprite itself (with scale handling), not the surface that is drawn from the image.

To modify the image surface drawn by the sprite, you shall use the SetSubRect() method : http://www.sfml-dev.org/documentation/1.6/classsf_1_1Sprite.php#a54bf1e6b425c40b00dd544a9c4fb77df

If you want to change the image drawn of a sprite, this could help you :
Code: [Select]
mySprite.SetImage(myNewImage);
mySprite.SetSubRect(sf::IntRect(0, 0, myNewImage.GetWidth(), myNewImage.GetHeight()));
Title: [SOLVED] Sprite resize error
Post by: eglomer on November 02, 2011, 01:26:04 pm
Thank you, TheCake.

I finally solved it using a temporal sprite and then copying it to my sprite variable.

 :D