Skip to content

Using Shape Drawer

earlygrey edited this page Sep 26, 2019 · 5 revisions

Importing Shape Drawer into your project

See the README for up to date information about including shape drawer into your project.

Usage

To create a ShapeDrawer you just need a Batch and a TextureRegion. Typically this is a single white pixel so that you can easily colour it, and this is best packed into an atlas with your other textures. However for testing purposes you can make one programmatically:

Pixmap pixmap = new Pixmap(1, 1, Format.RGBA8888);
pixmap.setColor(Color.WHITE);
pixmap.drawPixel(0, 0);
texture = new Texture(pixmap); //remember to dispose of later
pixmap.dispose();
TextureRegion region = new TextureRegion(texture, 0, 0, 1, 1);

Then instantiate a ShapeDrawer using whatever Batch you're currently using:

ShapeDrawer drawer = new ShapeDrawer(batch, region);

To start drawing, simply call its drawing methods in between Batch#begin() and Batch#end(). Something like this:

batch.begin();
drawer.line(0, 0, 100, 100);
batch.end();

Types of Batches

Note that if you want to draw filled shapes, it is more efficient to use a batch that implements PolygonBatch (eg a PolygonSpriteBatch) instead of a Batch that does not (eg a SpriteBatch).

Updating

It's recommended to call the update() method whenever you change the projection or transformation matrices of the batch. This is because Shape Drawer keeps track of certain things internally, such as the size of a screen pixel in current batch coordinates. This is used internally for things like line endpoint snapping, estimating how many lines are required to draw an ellipse, and deciding whether to use a join type when drawing paths or polygons and one is not specified.