SFML community forums

Help => Graphics => Topic started by: LucasShadow on September 16, 2012, 12:26:59 am

Title: Sprites with Different Textures
Post by: LucasShadow on September 16, 2012, 12:26:59 am
Hello. I have a need to change the texture of a sprite from one to another. Now that is easy to do with a simple .setTexture(texture). However, it seems that if the textures differ in size, there is a problem.

For example, if I initialize a sprite with a small texture and then change it to a large texture, only a portion of the large texture is assigned to the sprite, a portion exactly the same size as the smaller texture.
Small:
(http://puu.sh/15sjM)
Large:
(http://puu.sh/15skv)

If I initialize the texture with a large texture and change it to a small texture, the small texture will display with horizonal and vertical lines;
Large:
(http://puu.sh/15sbc)
Small:
(http://puu.sh/15sij)

Additionally, it seems that the sprite's dimensions can not be changed from what they are initialized to with the first texture. Is there a way to change the textures correctly and be able to force a resize of the sprite to the correct dimensions?

Info: large_texture is a 65 x 65 .jpg, small_texture is a 32 x 32 .jpg

Source code that replicates my problem, using SFML 2.0:
#include <windows.h>
#include <SFML/Graphics.hpp>

using namespace std;

 int main()
 {
     sf::RenderWindow App(sf::VideoMode(800, 600), "SFML window");

     sf::Texture large_texture;
     large_texture.loadFromFile("res\\Items\\stone.png");
     sf::Texture small_texture;
     small_texture.loadFromFile("res\\Items\\stonedust.png");

     sf::Sprite sprite1;
     sprite1.setTexture(small_texture);//change to either small or large
                                       //for initializing
     bool UsingLargeTexture(false);

     while (App.isOpen())
     {
         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
         {
             bool decision(false);
             if(UsingLargeTexture == true && decision == false)
             {
                 sprite1.setTexture(small_texture);
                 UsingLargeTexture = false;
                 decision = true;
             }
             if(UsingLargeTexture == false && decision == false)
             {
                 sprite1.setTexture(large_texture);
                 UsingLargeTexture = true;
                 decision = true;
             }
             Sleep(500);
         }

         App.clear();

         App.draw(sprite1);

         App.display();
     }

     return EXIT_SUCCESS;
 }
 
Title: Re: Sprites with Different Textures
Post by: masskiller on September 16, 2012, 12:39:36 am
I think that's because the texture uses a texture rect, as you don't change the texture rect what will happen is that if you load the small one first, the big one will only be visible on the area defined by the default texture rect (which is the size of the image that you load first), if you use the big one first you'll have a huge texture for a small image, therefore it might have unnecessary pixels to it. You have to reset the texture rect to the size of the image accordingly, else it won't work as you want it to.

I think the documentation has more details about it, I just gave the basic brush up.

Edit: I don't know what you are trying to do with it, but why not use both textures? That way you could use both of them as many times as you like without constantly charging an image and setting a new rect.
Title: Re: Sprites with Different Textures
Post by: LucasShadow on September 16, 2012, 01:11:29 am
Thank you, specifying rects fixed my problem :)

Edit: I don't know what you are trying to do with it, but why not use both textures? That way you could use both of them as many times as you like without constantly charging an image and setting a new rect.

For what I am doing, I am not sure it would work, but Ill give it a go :)
Title: Re: Sprites with Different Textures
Post by: FRex on September 16, 2012, 01:12:58 am
All is in documentation
http://www.sfml-dev.org/documentation/2.0/classsf_1_1Sprite.php#a3729c88d88ac38c19317c18e87242560
Title: Re: Sprites with Different Textures
Post by: Haikarainen on September 16, 2012, 04:57:38 pm
SetTexture takes 1 extra optional argument, so do this:
Sprite.SetTexture(tex, true);

to automatically recalculate the rect. I think I brought it up with laurent sometime that it should be the default behaviour but I cant remember what the reason not to was.