ShapeRenderer API changes

I just committed some changes to the repository that update the ShapeRenderer API. Yes, it’ll break you current code but the fixes will be trivial.

We’ve changed from having a large number of ShapeTypes (Rectangle, FilledRectangle, etc.) to having 3 shape types reflecting the underlying OpenGl primitives used: ShapeType.Point, ShapeType.Line and ShapeType.Filled.

This allows us to batch various shapes that previously would’ve caused a new batch. For example:

renderer.begin(ShapeType.Filled);
 
renderer.rect(0, 0, 1, 1);			
renderer.circle(0.2f, 0.2f, 0.5f, 40);			
renderer.cone(0.6f, 0.6f, 0, 0.3f, 0.75f, 20);			
renderer.triangle(-0.1f, 0.1f, -0.6f, -0.1f, -0.3f, 0.5f);
 
renderer.end();

Previously that would’ve required 4 batches and can now be done in one :)

There are a few exceptions to note: Line, Curve, Polygon & Box only work with ShapeType.Line. Point plotting only works with ShapeType.Point. The rest works for both ShapeType.Line & ShapeType.Filled. If you make a mistake here, it’ll throw an exception.

Sorry for breaking the API but I figured it was worth it for making it easier & faster to use ShapeRenderer.

  • http://uraniumlane.net kulpae

    Thank you! That’s more intuitive than it was before. Yeah!

  • t4ils

    Working great, good job ;)

    But you forgot to add that polygons can’t be filled either :)

  • http://www.thegreystudios.com bach

    Thanks t4ils, good point. Wanted to add the functionality – but for now I noted it in the post.

  • shinmai

    The new system looks awesome, however, I can’t get it to play nice. I can draw lines and points fine, but triangles, circles, rects and polygons all throw an ArrayIndexOutOfBoundsException at ….glutils.ImmediateModeRenderer20.color(ImmediateModeRenderer20.java:101) and at ….glutils.ShapeRenderer.rect(ShapeRenderer.java:370).
    Am I doing something wrong? (at this point I’m just beginning a filled shape, setting a color, drawing a rect and ending the shape, and no matter what I try, the application hangs at the rect-call).

  • http://www.thegreystudios.com bach

    @shinmai: I can’t reproduce your problem. Can you file an issue on the issue-tracker with the exact steps to reproduce this? Preferably with a sample project.

  • shinmai

    I would, but after updating to the latest nightly last night, I tried again today (having already hacked what I was trying to achieve with sprites and tinting) and now everything works as expected. I’d revert to my old code but I’m between development machines and didn’t have source controle set up at the time :(

    I can try to check out an older revision and see if I can reproduce, all I did was: create new libgdx project with libgdx-setup, change android projec to use GL2.0, create a shaperenderer object in create() and in render() begin a filled shape, set color to Color.RED, draw rect, end shape. Running the above on all occasions I tried between Feb 4th and last night would result in an ArrayIndexOutOfBound, but now works perfectly.

    Haven’t had time to look into this properly, but I’m extremely happy everything is working. Thanks for your hard work, the new API is absolutely awesome.

  • http://www.thegreystudios.com bach

    @shinmai: good to hear it’s working out for you! If you run into any problems with it again please do let us know, it’s much appreciated :)

  • Meddy M

    Ok I choose the bad day to update my project with the last GDX version ;)

    Had:

    [..]
    shapeRenderer.begin(ShapeType.FilledRectangle);
    [..]
    shapeRenderer.filledRect(0, 0, 1280, 752);
    [..]

    Changed to:

    shapeRenderer.begin(ShapeType.Filled);
    [..]
    shapeRenderer.rect(0, 0, 1280, 752);

    got an error:

    Exception in thread “LWJGL Application” com.badlogic.gdx.utils.GdxRuntimeException: java.lang.NoSuchFieldError: Filled
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:107)
    Caused by: java.lang.NoSuchFieldError: Filled

  • lostsoul

    This new update is cool but could you please update the new Box2D version. I’m having trouble with several of these which I want to implement into a game I’m making. It would be most appreciated.