SFML community forums

Help => Graphics => Topic started by: tuttoscorre on May 05, 2013, 12:08:27 am

Title: Change texture image for a sprite during runtime
Post by: tuttoscorre on May 05, 2013, 12:08:27 am
Hi! I'm doing a enhanched version of Pong, and I want to make the ball became a fireball when I press "W" key. Everything is working good, in the costructor of my ball class I load the texture like this:
void Load(std::string filename) {
              if(_image.loadFromFile(filename) == false)
        {
                _filename = "";
                _isLoaded = false;
        }
        else
        {
                _filename = filename;
                _sprite.setTexture(_image);
                _isLoaded = true;
        }
}
 

Ok. Now I have to say that my first loading (default) .png texture is 20x20; while the second (fireball one) is 40x40.

When I change the sprite like this (on "W" press event):
Load("fireball.png");
 
yes, it changes, but the drawing area is the one of 20x20 default sprite, so I see only a piece of my fireball sprite! How can I solve this?

ps. If I load first the biggest texture it draws it well!

Thanks a lot.
Title: Re: Change texture image for a sprite during runtime
Post by: eXpl0it3r on May 05, 2013, 12:48:10 am
You'll have to adjust the texture rect of the sprite. This can be done, either up on setting the texture (spr.setTexture(tex, true);) or by using the setTextureRect function of the the sprite.

Besides, you should in most cases preload the texture, so you don't waste valuable time, loading an image over and over again from the disk.
Title: Re: Change texture image for a sprite during runtime
Post by: tuttoscorre on May 05, 2013, 01:21:11 pm
You'll have to adjust the texture rect of the sprite. This can be done, either up on setting the texture (spr.setTexture(tex, true);) or by using the setTextureRect function of the the sprite.

Besides, you should in most cases preload the texture, so you don't waste valuable time, loading an image over and over again from the disk.

Now it works, thanks a lot!!