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

Author Topic: Change texture image for a sprite during runtime  (Read 3717 times)

0 Members and 2 Guests are viewing this topic.

tuttoscorre

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Change texture image for a sprite during runtime
« 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10914
    • View Profile
    • development blog
    • Email
Re: Change texture image for a sprite during runtime
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

tuttoscorre

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Change texture image for a sprite during runtime
« Reply #2 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!!

 

anything