Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (799)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (865)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [LibGDX] Least costly way to render lines?  (Read 16244 times)
0 Members and 1 Guest are viewing this topic.
Offline MrPork
« Posted 2015-03-23 23:09:21 »

              Using so much as 50 methods with Shape Renderer drops performance horrendously.

"f**k it, maybe it'll work." -Me
Offline chrislo27
« Reply #1 - Posted 2015-03-24 00:24:32 »

If they're only up-down and left-right (right angles only), you could make a Texture out of a 1x1 Pixmap and render the texture with x, y, width, and height. Change width and height to create a line in some direction.
Offline craftm

JGO Coder


Medals: 15
Projects: 1


_Keep Trying


« Reply #2 - Posted 2015-03-24 01:18:42 »

Are you using 50 different Shape Renderers or only one with a custom method to create it?

You can create a Texture 1x1 (as @chrislo27 said), Ninepatch, others..

Something like this:
1  
2  
3  
4  
batch.begin();
for (int i = 0; i < listRectangle.size(); i++)
   batch.draw(texture, listRectangle.get.(i).getX(), listRectangle.get.(i).getY(), listRectangle.get.(i).getWidth(), listRectangle.get.(i).getHeight());
batch.end();

1  
2  
3  
4  
shape.begin(ShapeType.Line);
for (int i = 0; i < listRectangle.size(); i++)
   shape.rect(listRectangle.get.(i).getX(), listRectangle.get.(i).getY(), listRectangle.get.(i).getWidth(), listRectangle.get.(i).getHeight());  
shape.end();

_ Wink
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline syszee
« Reply #3 - Posted 2015-03-24 02:26:33 »

If your objects are in an array, you can easily just do

1  
2  
3  
4  
batch.begin();
for(Object o : listRectangle.size())
    //Draw Your Shape
batch.end();


Where as "Object" is whatever the type of variable you are using, obviously. This is essentially what @craftm suggestion, but a bit shorter.

Offline Buddhaman
« Reply #4 - Posted 2015-03-24 23:50:08 »

For lines that aren't axis aligned, you can draw them as quads. Like this

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public static void drawLine(SpriteBatch batch, float x1, float y1, float x2, float y2, float lineWidth, AtlasRegion lineTexture) {
      float xdif = x2-x1;
      float ydif = y2-y1;
      float l2 = xdif*xdif+ydif*ydif;
      float invl = (float)(1/Math.sqrt(l2));
      //dif is vector with length linewidth from first to second vertex
      xdif*=invl*lineWidth;
      ydif*=invl*lineWidth;
     
      float floatBits = batch.getColor().toFloatBits();
      //draw quad with width of linewidth*2 through (x1, y1) and (x2, y2)
      float[] verts = new float[]{x1+ydif, y1-xdif, floatBits, lineTexture.getU(), lineTexture.getV(),
                           x1-ydif, y1+xdif, floatBits, lineTexture.getU2(), lineTexture.getV(),
                           x2-ydif, y2+xdif, floatBits, lineTexture.getU2(), lineTexture.getV2(),
                           x2+ydif, y2-xdif, floatBits, lineTexture.getU(), lineTexture.getV2()};
      batch.draw(lineTexture.getTexture(), verts, 0, 20);
   }
Offline Cero
« Reply #5 - Posted 2015-03-25 00:22:27 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public static void drawLine(SpriteBatch batch, float _x1, float _y1, float _x2, float _y2, float thickness, Texture tex)
   {
      float length = Trigonometrics.getDistanceAccurate(_x1, _y1, _x2, _y2); // get distance between those 2 points
      float dx = _x1;
      float dy = _y1;
      dx = dx - _x2;
      dy = dy - _y2;
      float angle = MathUtils.radiansToDegrees*MathUtils.atan2(dy, dx);
      angle = angle-180;
      batch.draw(tex, _x1, _y1, 0f, thickness*0.5f, length, thickness, 1f, 1f, angle, 0, 0, tex.getWidth(), tex.getHeight(), false, false);
   }

texture would be a 1x1 white texture, in this case, but could vary

Offline klaus
« Reply #6 - Posted 2015-04-01 21:26:15 »

Performance only drops if you "begin" and "end" the ShapeRenderer in those 50 methods you mentioned every time as @craftm said.

Slow:

1  
2  
3  
4  
5  
6  
7  
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
shapeRenderer.line(x, y, x2, y2);
shapeRenderer.end();
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
shapeRenderer.line(x, y, x2, y2);
shapeRenderer.end();
...


Fast:
1  
2  
3  
4  
5  
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
shapeRenderer.line(x, y, x2, y2);
shapeRenderer.line(x, y, x2, y2);
shapeRenderer.end();
...
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (204 views)
2019-09-04 15:33:17

hadezbladez (4941 views)
2018-11-16 13:46:03

hadezbladez (1820 views)
2018-11-16 13:41:33

hadezbladez (5218 views)
2018-11-16 13:35:35

hadezbladez (1028 views)
2018-11-16 13:32:03

EgonOlsen (4404 views)
2018-06-10 19:43:48

EgonOlsen (5235 views)
2018-06-10 19:43:44

EgonOlsen (2977 views)
2018-06-10 19:43:20

DesertCoockie (3879 views)
2018-05-13 18:23:11

nelsongames (4311 views)
2018-04-24 18:15:36
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!