Hi
Found that I can't change sprite source image in SFML2
Here's simple example:
// Load first image and create sprite
sf::Image Image;
Image.LoadFromFile("Logo.png"); // Size of 400x100
sf::Sprite Sprite(Image);
...time passes...
// Load second image and set it to sprite
sf::Image Img2;
Img2.LoadFromFile("Checkers.png"); // Size of 256x256
Sprite.SetImage(Img2);
After that last line the Sprite size is still 400x100 instead of 256x256 what I would expect.
This behavior comes from SFML/Graphics/Sprite.cpp:
void Sprite::SetImage(const Image& image)
{
// If there was no source image before and the new image is valid, adjust the source rectangle
if (!myImage && (image.GetWidth() > 0) && (image.GetHeight() > 0))
{
SetSubRect(IntRect(0, 0, image.GetWidth(), image.GetHeight()));
So, if image did already exist, then sprite size will not get changed.
This gets fixed by making this change to SFML: // If new image is valid, adjust the source rectangle
if (image.GetWidth() > 0) && (image.GetHeight() > 0)
Or calling Sprite.SetSubRect( sf::IntRect(0,0, Img2.GetWidth(), Img2.GetHeight()) ) after I change the image.
Is there some reason for this behavior or is it just a small bug?