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

Author Topic: Change size of texture, sprite without scale [SOLUCIONADO]  (Read 5569 times)

0 Members and 1 Guest are viewing this topic.

patilanz

  • Newbie
  • *
  • Posts: 2
    • View Profile
Change size of texture, sprite without scale [SOLUCIONADO]
« on: August 29, 2016, 12:26:30 pm »
Hello,
I am doing an animation which is changing the size and the color of a sprite with texture created by:

struct rect_s{
        int width;
        int height;
        sf::Color color;
        sf::Texture texture;
        sf::Sprite sprite;
} object;

sf::Uint8* updateMatrix(sf::Uint8 * matrix, sf::Color color, int size){
        if (matrix != 0)delete[]matrix;
        matrix = new sf::Uint8[size];
        for (int i = 0; i < size; i++){
                matrix[i++] = color.r;
                matrix[i++] = color.g;
                matrix[i++] = color.b;
                matrix[i] = color.a;
        }
        return matrix;
}
        object.width = 50;
        object.height = 50;
        object.color = sf::Color::Red;

        sf::Uint8 * matrix = 0;
        matrix = updateMatrix(matrix, object.color, object.width * object.height * 4);

       
        object.texture.create(object.width, object.height);
        object.texture.update(matrix);
        object.sprite.setTexture(object.texture);
 

Later i am doing that:

                if (((float)(clock() - last_t)/CLOCKS_PER_SEC) * 1000 > 30){

                        switch (control.cycle)
                        {
                        case 0:
                                object.color.g++;
                                object.color.r--;
                                object.width++;
                                if (object.color.g > 240)control.cycle = 1;
                                break;
......
//I update the texture size with calling create again but is not working
                        object.texture.create(object.width, object.height);
                        matrix = updateMatrix(matrix, object.color, object.width * object.height * 4);
                        object.texture.update(matrix);
 


How can i do that?
« Last Edit: September 02, 2016, 05:22:57 pm by patilanz »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Change size of texture, sprite without scale
« Reply #1 on: August 29, 2016, 02:57:37 pm »
Are you changing the size of the source texture and wanting to update the sprite to match that new size?
If so, you just need to set the sprite's texture rectangle to match.

For example, if your sprite and texture are both 30x20 and then you change your texture to 45x30, using:
sprite.setTextureRect(sf::IntRect(sf::Vector2i(0, 0), sf::Vector2i(sprite.getTexture().getSize())));
would set the sprite's texture rectangle to fit the texture's new size.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Change size of texture, sprite without scale
« Reply #2 on: August 29, 2016, 03:11:53 pm »
... or use the second argument of the Sprite::setTexture function.
Laurent Gomila - SFML developer

patilanz

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Change size of texture, sprite without scale
« Reply #3 on: September 02, 2016, 05:21:21 pm »
Gracias solucionado con setTextureRect