SFML community forums

Help => Graphics => Topic started by: patilanz on August 29, 2016, 12:26:30 pm

Title: Change size of texture, sprite without scale [SOLUCIONADO]
Post by: patilanz 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?
Title: Re: Change size of texture, sprite without scale
Post by: Hapax 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.
Title: Re: Change size of texture, sprite without scale
Post by: Laurent on August 29, 2016, 03:11:53 pm
... or use the second argument of the Sprite::setTexture function.
Title: Re: Change size of texture, sprite without scale
Post by: patilanz on September 02, 2016, 05:21:21 pm
Gracias solucionado con setTextureRect