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?
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.