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

Author Topic: Can't change sprite image  (Read 8547 times)

0 Members and 1 Guest are viewing this topic.

Jaenis

  • Newbie
  • *
  • Posts: 48
    • View Profile
Can't change sprite image
« on: August 11, 2009, 03:02:35 pm »
Hi

Found that I can't change sprite source image in SFML2

Here's simple example:
Code: [Select]
// 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:
Code: [Select]
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:
Code: [Select]
   // 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Can't change sprite image
« Reply #1 on: August 11, 2009, 03:14:52 pm »
Quote
Is there some reason for this behavior or is it just a small bug?

There is a very good reason, which I already explained on this forum a few times.
Laurent Gomila - SFML developer

Jaenis

  • Newbie
  • *
  • Posts: 48
    • View Profile
Can't change sprite image
« Reply #2 on: August 11, 2009, 03:20:48 pm »
Okay, could you also mention it on the documentation pages please :)
Couldn't find it there

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Can't change sprite image
« Reply #3 on: August 11, 2009, 03:25:19 pm »
Quote
Okay, could you also mention it on the documentation pages please

Mention what? That "image" and "subrect" are two separate properties? I thought it was obvious enough... :P
Laurent Gomila - SFML developer

Jaenis

  • Newbie
  • *
  • Posts: 48
    • View Profile
Can't change sprite image
« Reply #4 on: August 11, 2009, 03:35:29 pm »
I haven't even set the subrect for my sprite, so I wondered why it is displaying wrong after I change to another image.
It might be clear for you that this will be the case, but it will be confusing for users who do not know about this feature. Just mentioning that one should set the subrect after setting the image in documentation would clarify the issue.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Can't change sprite image
« Reply #5 on: August 11, 2009, 03:41:15 pm »
That's right. Anyway, I'm going to improve the documentation a lot in the SFML 2 branch, everything will be much more detailed :)
Laurent Gomila - SFML developer

Jaenis

  • Newbie
  • *
  • Posts: 48
    • View Profile
Can't change sprite image
« Reply #6 on: August 11, 2009, 03:44:22 pm »
Great, that sounds good :)
Thanks

Antidote

  • Newbie
  • *
  • Posts: 35
    • View Profile
Can't change sprite image
« Reply #7 on: August 12, 2009, 11:49:39 pm »
Laurent should get some lackey to revise parts of the Documentation so that it's absolutely clear whats what. Also it always good to have an extra set of eyes when writing a documentation.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Can't change sprite image
« Reply #8 on: August 13, 2009, 03:38:54 pm »
Jaenis, now that you know it, just write a global SetImage(sf::Sprite&, const sf::Image&) function which also adjusts the subrect. Like this, you never have to think of that again... ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Can't change sprite image
« Reply #9 on: August 14, 2009, 02:00:32 am »
But others still have to.

Laurent, what do you think of an additional parameter for sf::Sprite::SetImage(), like "resize" which defaults to "true"? Up to now I've wanted to resize a sprite more often than keeping its subrect, that's why I ask. ;)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Can't change sprite image
« Reply #10 on: August 14, 2009, 08:41:36 am »
I can add such a parameter, but it would default to false so that it doesn't break the previous behaviour and doesn't confuse people who don't expect the subrect to change.
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Can't change sprite image
« Reply #11 on: August 14, 2009, 11:27:53 am »
I've implemented this new parameter in the sfml2 branch.

Enjoy :)
Laurent Gomila - SFML developer

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Can't change sprite image
« Reply #12 on: August 14, 2009, 11:30:14 am »
I also meant "false" as the default value, sorry. ;) Thanks for the implementation!

forrestcupp

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Can't change sprite image
« Reply #13 on: August 14, 2009, 09:03:14 pm »
Quote from: "Laurent"
I've implemented this new parameter in the sfml2 branch.

Enjoy :)
Sweet!  Thank you.


In the meantime, you can just call
Code: [Select]
sprite.SetImage(sf::Image());before you set it with the new image, and it will clear the old subrect settings along with everything else from the old image.

Jaenis

  • Newbie
  • *
  • Posts: 48
    • View Profile
Can't change sprite image
« Reply #14 on: August 15, 2009, 07:57:18 am »
Quote from: "Laurent"
I've implemented this new parameter in the sfml2 branch.

Enjoy :)

Whee, thanks!  :D