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

Author Topic: Sprites with Different Textures  (Read 5705 times)

0 Members and 1 Guest are viewing this topic.

LucasShadow

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Sprites with Different Textures
« 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:

Large:


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:

Small:


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;
 }
 

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Sprites with Different Textures
« Reply #1 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.
« Last Edit: September 16, 2012, 12:48:44 am by masskiller »
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

LucasShadow

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Re: Sprites with Different Textures
« Reply #2 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 :)

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Back to C++ gamedev with SFML in May 2023

Haikarainen

  • Guest
Re: Sprites with Different Textures
« Reply #4 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.

 

anything