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

Author Topic: [Solved]Transform... a class? or maybe an struct?  (Read 16582 times)

0 Members and 1 Guest are viewing this topic.

NinjaFighter

  • Jr. Member
  • **
  • Posts: 91
    • View Profile
    • http://www.sebagames.wordpress.com
[Solved]Transform... a class? or maybe an struct?
« on: May 05, 2012, 06:32:12 pm »
yesterday I saw that Transform is a Class instead of a Struct. Is this ok? I mean, there are lot of moments where we use a new Transform (for example, on a DisplayObject hierarchy), or store previous transforms, etc. Maybe, this could be much more efficiently if can Transform be an structure. (for example, in XNA, there are Matrix structures, that can be combined (just like SFML's Transform), but if there are structures instead classes, the GarbageCollector don't be affected at all)
« Last Edit: May 22, 2012, 09:31:33 pm by NinjaFighter »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Transform... a class? or maybe an struct?
« Reply #1 on: May 05, 2012, 06:39:09 pm »
I don't know ;D
Laurent Gomila - SFML developer

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Re: Transform... a class? or maybe an struct?
« Reply #2 on: May 05, 2012, 06:57:00 pm »
I didn't understand what would be the advantages of a struct over a class  :-\
Want to play movies in your SFML application? Check out sfeMovie!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Transform... a class? or maybe an struct?
« Reply #3 on: May 05, 2012, 07:05:32 pm »
Instances of classes are allocated on the heap and managed by the garbage collector. Structures are more lightweight: they are allocated on the stack and their lifetime is thus managed automatically without bothering the garbage collector.

Another important difference is that structures are handled by value while classes are handled by reference (without a visible difference in the syntax like there is in C++).

(correct me if I'm wrong, I'm not a .Net programmer)
Laurent Gomila - SFML developer

NinjaFighter

  • Jr. Member
  • **
  • Posts: 91
    • View Profile
    • http://www.sebagames.wordpress.com
Re: Transform... a class? or maybe an struct?
« Reply #4 on: May 05, 2012, 07:11:57 pm »
Yeah =)
Then, I think that could be good to make them a struct instead of a class. There's no reason to be a class... even, various methods return "this", for example "Rotate,Scale or Translate", so.. in a class, this doesn't make sense.

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Re: Transform... a class? or maybe an struct?
« Reply #5 on: May 05, 2012, 07:17:51 pm »
Oh, I didn't even notice I was in the C# section ;D . But still good to know, thanks :) .
Want to play movies in your SFML application? Check out sfeMovie!

NinjaFighter

  • Jr. Member
  • **
  • Posts: 91
    • View Profile
    • http://www.sebagames.wordpress.com
Re: Transform... a class? or maybe an struct?
« Reply #6 on: May 05, 2012, 07:25:34 pm »
Simply consider that: "There's no good practice, to use new classes during updates or renders."
This should be on the front of the book of CSharp's good practices. ;)
These practices will overload the GarbageCollector in a unnecessary way.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Transform... a class? or maybe an struct?
« Reply #7 on: May 05, 2012, 10:36:12 pm »
If you're absolutely sure that I should do the modification, then I'm ready to trust you. Remember, I'm not an experienced .Net programmer at all.

Quote
even, various methods return "this", for example "Rotate,Scale or Translate", so.. in a class, this doesn't make sense
May I ask why?
Laurent Gomila - SFML developer

NinjaFighter

  • Jr. Member
  • **
  • Posts: 91
    • View Profile
    • http://www.sebagames.wordpress.com
Re: Transform... a class? or maybe an struct?
« Reply #8 on: May 05, 2012, 10:59:35 pm »
Because, if you call: "myTransformation.Rotate(45f)" you don't need to know "myTransformation".
The same rule is logic for Structs too... but! in some cases this is specially useful with structs.
For example, if you have a List<Transformation>, or array.

Imagine that:
Code: [Select]
//Rotate 45 all transformations
for(int i = 0; i<mTransList.Count; i++){
   mTransList[i].Rotate(45f);
}

//Note: if Transform is struct, this is like do nothing, otherwise, if is a class, all Transformations will be succesfully rotated.

The correct code to modify all Translation if they are structs, is more short if you returns "this" on all modification functions.

Code: [Select]
//Rotate 45 all STRUCT transformations
for(int i = 0; i<mTransList.Count; i++){
   mTransList[i] = mTransList[i].Rotate(45f);
}

In that case, returns "this" make sense for structs, and not for classes.  ;)

 Anyway, I prefer not returning "this" in these methods and solve by this way:
Code: [Select]
//Rotate 45 all STRUCT transformations
Translation t;
for(int i = 0; i<mTransList.Count; i++){
   t = mTransList[i];
   t.Rotate(45f);
   mTransList[i] = t;
}

And problem solved... never copy unnesesary data (becase return "this" on struct, returns a copy).
« Last Edit: May 05, 2012, 11:08:56 pm by NinjaFighter »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Transform... a class? or maybe an struct?
« Reply #9 on: May 06, 2012, 09:28:00 am »
But this is returned so that you can chain calls:
transform.Rotate(45).Translate(10, 20).Scale(2, 2);

So this wouldn't work with structs, because "this" would be returned as a copy and not as a reference? If so, then Transform definitely needs to be a class.
Laurent Gomila - SFML developer

NinjaFighter

  • Jr. Member
  • **
  • Posts: 91
    • View Profile
    • http://www.sebagames.wordpress.com
Re: Transform... a class? or maybe an struct?
« Reply #10 on: May 06, 2012, 05:19:54 pm »
I figured what you say, but keep in mind that Transform not need to be a class.
You can do:

Code: [Select]
transform = transform.Rotate(45).Translate(10, 20).Scale(2, 2);

That is the way with structs =)

Any way, returning "this" or not, could be great if transform can be structs, and be copied more freely.
For example:
Code: [Select]
//Basic DisplayObjects hierarchy
Transform parentTransform = mySprite.Transform;
Transform childTransform;

foreach(Sprite child in mySpriteChilds){
   childTransform = parentTransform * child.Transform;
   //TODO: Draw child with childTransform
}

If Transform is a class, this practice is much more complicated, and you will loss a lot of versatility.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Transform... a class? or maybe an struct?
« Reply #11 on: May 06, 2012, 05:27:36 pm »
Quote
If Transform is a class, this practice is much more complicated, and you will loss a lot of versatility.
You're right... And it's true that the "chained calls" syntax can be slightly modified accordingly.

Laurent Gomila - SFML developer

NinjaFighter

  • Jr. Member
  • **
  • Posts: 91
    • View Profile
    • http://www.sebagames.wordpress.com
Re: Transform... a class? or maybe an struct?
« Reply #12 on: May 06, 2012, 09:09:57 pm »
Please, consider that if you make Transform a struct:
If you return this on modification methods, all data has been copied, but if you no use it... there are a minimal waste of processes. Personally I recomend you to do some classic methods, for example, SetRST, SetSRT, etc

Another thing to remember is (this is very important for classes too), when you contains an array, this array will not be copied with a structure (or a class).
For example:
Code: [Select]
struct Matrix{
   float[] Values{get;set;}
   float NotArrayValue{get;set;}
}

If you copy the Matrix structure... "Values" is like a pointer, so... if you make this:
Code: [Select]
Matrix m1 = new Matrix{ Values = new float[]{ 1f, 2f, 3f }, NotArrayValue = 25f };
Matrix m2 = m1;

m1.Values[0] += 100f;
m1.NotArrayValue += 100f;

Console.Writeline(m2.Values[0]);
Console.Writeline(m2.NotArrayValue);


the result is:
Code: [Select]
101
25

So, is like with arrays, C# manages a pointer to array, so, please consider that if you make Transformations be structures, remember to do a Marshal.Copy (or Clone) with this array, I recommend you to make a "Clone" function. (this is the standar that c# use with arrays)

Instead of write "m2 = m1;" you must write "m2 = m1.Clone();" (only needed if you want to modify internal matrix values.)
« Last Edit: May 07, 2012, 12:06:28 am by NinjaFighter »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Transform... a class? or maybe an struct?
« Reply #13 on: May 08, 2012, 08:57:22 am »
I think it's ok because internally, a Transform has just a pointer to the C API object (like all other classes). There's no managed member.
Laurent Gomila - SFML developer

NinjaFighter

  • Jr. Member
  • **
  • Posts: 91
    • View Profile
    • http://www.sebagames.wordpress.com
Re: Transform... a class? or maybe an struct?
« Reply #14 on: May 10, 2012, 12:34:04 am »
Ok.. anyway... do you agree to make Transform a struct instead of a class?

 

anything