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

Author Topic: Final members on vertices and vectors  (Read 1792 times)

0 Members and 1 Guest are viewing this topic.

Groogy

  • Hero Member
  • *****
  • Posts: 1470
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Final members on vertices and vectors
« on: October 27, 2012, 12:50:17 pm »
Hi! The members of vertices and vectors(and probably other classes) are final. I understand why you've done hat but can you somehow get that removed? Cause it's really annoying to work with.

In the end it will also be inefficient if I have to allocate new objects at runtime just because I want to change vertices at runtime, let's say, a custom animation?

Is it possible to have it read-write somehow? Maybe add set/get methods? I don't really know the function call overhead on Java but I guess declaring the class or the methods final might help with that?

Edit: Just so you don't think I only want this for performance reason. I don't really care that much about it and I know the GC among other things are pretty fast here with temporary objects. But it's just inconvenient to always allocate a new object instead of being able to change it. In some cases it makes sense, vectors can make sense with it but vertices is just plain hard to work with if I can't change it's members.

This is just painful:
this.vertices = new Vertex[4];
this.vertices[0] = new Vertex(new Vector2f(-0.5f, -0.5f), new Vector2f(0.0f, 0.0f));
this.vertices[1] = new Vertex(new Vector2f( 0.5f, -0.5f), new Vector2f(1.0f, 0.0f));
this.vertices[2] = new Vertex(new Vector2f( 0.5f,  0.5f), new Vector2f(1.0f, 1.0f));
this.vertices[3] = new Vertex(new Vector2f(-0.5f,  0.5f), new Vector2f(0.0f, 1.0f));
« Last Edit: October 27, 2012, 01:13:47 pm by Groogy »
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

pdinklag

  • Sr. Member
  • ****
  • Posts: 330
  • JSFML Developer
    • View Profile
    • JSFML Website
Re: Final members on vertices and vectors
« Reply #1 on: October 28, 2012, 07:21:35 am »
Efficiency / performance had absolutely nothing to do with this decision or really any design decision.

I made data structure classes like these immutable a while ago because they should be. There is no such thing as "const" in Java, so whenever you pass a vertex anywhere, it could be modified and break stuff. That's bad design, that's why I made this change.

Also, how is
Quote from: Groogy
this.vertices = new Vertex[4];
this.vertices[0] = new Vertex(new Vector2f(-0.5f, -0.5f), new Vector2f(0.0f, 0.0f));
this.vertices[1] = new Vertex(new Vector2f( 0.5f, -0.5f), new Vector2f(1.0f, 0.0f));
this.vertices[2] = new Vertex(new Vector2f( 0.5f,  0.5f), new Vector2f(1.0f, 1.0f));
this.vertices[3] = new Vertex(new Vector2f(-0.5f,  0.5f), new Vector2f(0.0f, 1.0f));
any more painful than, say,
this.vertices = new Vertex[4];
this.vertices[0] = new Vertex();
this.vertices[0].setPosition(new Vector2f(-0.5f, -0.5f));
this.vertices[0].setTexCoords(new Vector2f(-0.5f, -0.5f));
this.vertices[1] = new Vertex();
this.vertices[1].setPosition(new Vector2f(-0.5f, -0.5f));
this.vertices[1].setTexCoords(new Vector2f(-0.5f, -0.5f));
this.vertices[2] = new Vertex();
this.vertices[2].setPosition(new Vector2f(-0.5f, -0.5f));
this.vertices[2].setTexCoords(new Vector2f(-0.5f, -0.5f));
this.vertices[3] = new Vertex();
this.vertices[3].setPosition(new Vector2f(-0.5f, -0.5f));
this.vertices[3].setTexCoords(new Vector2f(-0.5f, -0.5f));
? (Nevermind the wrong coordinates, I just wanted to illustrate that you'd end up with 3 times as many lines)

The only point where this really gets painful is when you re-iterate and want to change vertices. Instead of changing single properties, you'll have to create a new instance. That's a little annoying, yes, but safe and predictable code should be preferred over slightly shorter code.
JSFML - The Java binding to SFML.

Groogy

  • Hero Member
  • *****
  • Posts: 1470
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Re: Final members on vertices and vectors
« Reply #2 on: October 29, 2012, 06:59:50 am »
The only point where this really gets painful is when you re-iterate and want to change vertices. Instead of changing single properties, you'll have to create a new instance. That's a little annoying, yes, but safe and predictable code should be preferred over slightly shorter code.

That's what I do actually in a later stage but ah well. I guess I can live with it.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio