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

Author Topic: Is there a more efficient way of updating VertexArrays?  (Read 5776 times)

0 Members and 2 Guests are viewing this topic.

Scipi

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Is there a more efficient way of updating VertexArrays?
« on: January 22, 2014, 07:27:02 pm »
Hello.

I'm working on a project in which I'm using VertexArray to draw moving lines as well as a particle system based off the code shown here:
http://www.sfml-dev.org/tutorials/2.0/graphics-vertex-array.php

It's an easy enough implementation, however I end up with code that looks like this:

Note, the Vector2f is my own implementation since the values in org.jsfml.system.Vector2f are final.

Code: [Select]
import org.jsfml.graphics.*;
import org.jsfml.system.Time;
import systemcrash.math.Vector2f;

class MachinegunBullet extends Bullet {
    private VertexArray shape;
   
    @Override
    public void tick(Time t) {
pos.x += direction.x * velocity * t.asSeconds();
pos.y += direction.y * velocity * t.asSeconds();

shape.set(0, new Vertex(new org.jsfml.system.Vector2f(pos.x, pos.y), new Color(255, 255, 0, 100)));
shape.set(1, new Vertex(new org.jsfml.system.Vector2f(pos.x + 5*direction.y, pos.y + 5*direction.y), Color.YELLOW));

    }

    @Override
    public void draw(RenderTarget rt, RenderStates rs) {
rt.draw(shape);
    }
   
    public MachinegunBullet(Vector2f pos, Vector2f direction, float angle) {
super(pos, direction, angle);
shape = new VertexArray(PrimitiveType.LINES);
shape.add(new Vertex(new org.jsfml.system.Vector2f(pos.x, pos.y), new Color(255, 255, 0, 100)));
shape.add(new Vertex(new org.jsfml.system.Vector2f(pos.x + 5*direction.y, pos.y + 5*direction.y), Color.YELLOW));
    }

   
   
}

Since Vertexes are final, I have to create new vertex objects with the updated positions as well as colors in some cases. So that leaves me wondering if there is a better method of updating the Vertices in the Array than throwing away two objects per frame per bullet?

Thanks.
I issa Cat Person =^w^=

pdinklag

  • Sr. Member
  • ****
  • Posts: 330
  • JSFML Developer
    • View Profile
    • JSFML Website
Re: Is there a more efficient way of updating VertexArrays?
« Reply #1 on: January 24, 2014, 08:32:01 am »
It's the way it's meant to be, so for right now, no.

For JSFML, I chose simple data structures to be immutable, ie they cannot be modified after construction. This can avoid a lot of potential pain when dealing with vectors and colors, for instance, passed as method parameters. I honestly would want to preserve consistency, but what I could do is introduce additional "copy"-like methods that return new instances. That might shorten the code a bit.

I know that it feels a little awkward to create new objects all the time, but that's probably the way of thinking when you come from C or similar worlds? Since you talk about "efficiency", I actually did a few benchmarks to compare this to the paradigm of publicly assignable fields. The performance difference was barely notable - the JVM is extremely efficient dealing with object re-allocation. I should repeat these benchmarks to get some referable data for this.
JSFML - The Java binding to SFML.

 

anything