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

Author Topic: Changing Texture does not affect Sprite ?  (Read 3276 times)

0 Members and 2 Guests are viewing this topic.

Dude234

  • Newbie
  • *
  • Posts: 10
    • View Profile
Changing Texture does not affect Sprite ?
« on: June 30, 2018, 03:56:03 pm »
Sprite spr = new Sprite();

Texture tex = new Texture("tex1.png");

spr.Texture = tex; // does not matter if inside constructor or prop

tex = new Texture("tex2.png");
 

I expected that changing the texture itself would also change the texture of the Sprite,
because I expected that the texture is referenced and not copied.
I really hope I do this wrong.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Changing Texture does not affect Sprite ?
« Reply #1 on: June 30, 2018, 06:39:06 pm »
The sprite still references the first texture, not the second one. This is .Net, not C++: when you write "tex = new Texture(...)" you make the tex variable reference another texture object, you're not modifying the texture object that tex was referencing before.

In brief: your code create two textures, and at the end spr references the first one, tex the second one.
Laurent Gomila - SFML developer

Dude234

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Changing Texture does not affect Sprite ?
« Reply #2 on: June 30, 2018, 07:34:10 pm »
Yes, that was a C# fail on my side and it's quite a limitation.

Sure there are workarounds to re-assign the texture of all concerning instances, when the change is needed.
But that does not seem to be straightforward for now.

The next idea would be to change the members of the texture instance instead, updating its data.
So we are editing the texture instance itself, all other objects, which are referencing it would display the changes!

The only access we get is: Texture.Update().

Image img1 = new Image("img1.png");

Image img2 = new Image("img2.png");

Texture tex = new Texture(img1);

Sprite spr = new Sprite(tex);

tex.Update(img2);
 

I expected the texture to change, since the instance is updated.
But the first image is still shown.

Dude234

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Changing Texture does not affect Sprite ?
« Reply #3 on: June 30, 2018, 07:37:45 pm »
Alright. I got it working! ;D It needed to be the same size as stated in the docs. Sorry!

Quote
No additional check is performed on the size of the image, passing an invalid combination of image size and offset will lead to an undefined behavior. This function does nothing if the texture was not previously created.