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

Author Topic: Nothing happens when transforming a sprite [bug?]  (Read 6053 times)

0 Members and 1 Guest are viewing this topic.

SuperV1234

  • SFML Team
  • Full Member
  • *****
  • Posts: 190
    • View Profile
Nothing happens when transforming a sprite [bug?]
« on: September 23, 2012, 11:47:08 am »
The displayed sprite doesn't rotate and doesn't scale at all.

Minimal code:
private static void Main()
{
    var renderWindow = new RenderWindow(new VideoMode(1024, 768), "Transform Bug");

    var image = new Image(64, 64, Color.White);
    var texture = new Texture(image);
    var sprite = new Sprite(texture);
    sprite.Position = new Vector2f(1024 / 2f, 768 / 2f);

    var i = 0;

    while(true)
    {
        renderWindow.Clear();
     
        sprite.Transform.Rotate(i);
        sprite.Transform.Scale(i, i);

        renderWindow.Draw(sprite);
        renderWindow.Display();

        i++;
    }          
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Nothing happens when transforming a sprite [bug?]
« Reply #1 on: September 23, 2012, 12:15:28 pm »
This is a drawback of the differences between C++ and C#, and me not being an expert in the latter :)

The Transform property of a sprite is a read-only attribute, you can't change it like this.

The correct code is:
sprite.Rotate(i);
sprite.Scale(i, i);
Laurent Gomila - SFML developer

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Nothing happens when transforming a sprite [bug?]
« Reply #2 on: September 23, 2012, 12:20:15 pm »
sprite.Scale(i, i);
Laurent, wouldn't calling that with 0 0 make sprite vanish and not show up after next scale calls?(or is c# api different?)
« Last Edit: September 23, 2012, 12:21:56 pm by FRex »
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Nothing happens when transforming a sprite [bug?]
« Reply #3 on: September 23, 2012, 12:22:40 pm »
0 is ignored as a scale factor.

And my code is wrong anyway, Scale and Rotation are properties in SFML.Net, not functions.

sprite.Rotation = i;
sprite.Scale = new Vector2f(i, i);

Sorry :)
Laurent Gomila - SFML developer

SuperV1234

  • SFML Team
  • Full Member
  • *****
  • Posts: 190
    • View Profile
Re: Nothing happens when transforming a sprite [bug?]
« Reply #4 on: September 23, 2012, 12:23:15 pm »
This is a drawback of the differences between C++ and C#, and me not being an expert in the latter :)

The Transform property of a sprite is a read-only attribute, you can't change it like this.

The correct code is:
sprite.Rotate(i);
sprite.Scale(i, i);

Oh. Could you make the Transform.Rotate(), Transform.Translate() methods private? It's counter-intuitive to have them accessible by the user.

Off-topic: I've also replied to the text rotation bug thread.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Nothing happens when transforming a sprite [bug?]
« Reply #5 on: September 23, 2012, 12:33:25 pm »
Quote
Oh. Could you make the Transform.Rotate(), Transform.Translate() methods private? It's counter-intuitive to have them accessible by the user.
No, they are usable if you have a custom Transform instance. What should be done here is that Sprite.Transform should return a const/read-only object -- but that's not possible in C#, right?
Laurent Gomila - SFML developer

SuperV1234

  • SFML Team
  • Full Member
  • *****
  • Posts: 190
    • View Profile
Re: Nothing happens when transforming a sprite [bug?]
« Reply #6 on: September 23, 2012, 12:47:53 pm »
Quote
Oh. Could you make the Transform.Rotate(), Transform.Translate() methods private? It's counter-intuitive to have them accessible by the user.
No, they are usable if you have a custom Transform instance. What should be done here is that Sprite.Transform should return a const/read-only object -- but that's not possible in C#, right?

I'll just quote this great answer by Jon Skeet (originally posted on http://stackoverflow.com/questions/939563/c-sharp-return-a-variable-as-read-only-from-get-set)

Quote
No, there's no way of doing this. For instance, if you return a List<string> (and it's not immutable) then callers will be able to add entries.

The normal way round this is to return an immutable wrapper, e.g. ReadOnlyCollection<T>.

For other mutable types, you may need to clone the value before returning it.

Note that just returning an immutable interface view (e.g. returning IEnumerable<T> instead of List<T>) won't stop a caller from casting back to the mutable type and mutating.

EDIT: Note that apart from anything else, this kind of concern is one of the reasons why immutable types make it easier to reason about code :)

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Nothing happens when transforming a sprite [bug?]
« Reply #7 on: September 23, 2012, 01:29:45 pm »
Quote
0 is ignored as a scale factor.
Not in c++. Is it supposed to behave different between languages?
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Nothing happens when transforming a sprite [bug?]
« Reply #8 on: September 23, 2012, 03:51:32 pm »
Sorry, you're right. 0 is a valid scale factor.
Laurent Gomila - SFML developer