SFML community forums

Bindings - other languages => Java => Topic started by: Groogy on October 27, 2012, 12:50:17 pm

Title: Final members on vertices and vectors
Post by: Groogy 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));
Title: Re: Final members on vertices and vectors
Post by: pdinklag 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.
Title: Re: Final members on vertices and vectors
Post by: Groogy 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.