SFML community forums

Bindings - other languages => DotNet => Topic started by: SuperV1234 on September 23, 2012, 11:47:08 am

Title: Nothing happens when transforming a sprite [bug?]
Post by: SuperV1234 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++;
    }          
}
Title: Re: Nothing happens when transforming a sprite [bug?]
Post by: Laurent 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);
Title: Re: Nothing happens when transforming a sprite [bug?]
Post by: FRex 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?)
Title: Re: Nothing happens when transforming a sprite [bug?]
Post by: Laurent 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 :)
Title: Re: Nothing happens when transforming a sprite [bug?]
Post by: SuperV1234 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.
Title: Re: Nothing happens when transforming a sprite [bug?]
Post by: Laurent 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?
Title: Re: Nothing happens when transforming a sprite [bug?]
Post by: SuperV1234 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 :)
Title: Re: Nothing happens when transforming a sprite [bug?]
Post by: FRex 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?
Title: Re: Nothing happens when transforming a sprite [bug?]
Post by: Laurent on September 23, 2012, 03:51:32 pm
Sorry, you're right. 0 is a valid scale factor.