pats
pats

Reputation: 1291

SpriteBatch and ShapeRenderer draw ordering in libgdx

I have to order sprites and shapes drawn on the screen.

is it ok to call begin and end of SpriteBatch and ShapeRenderer mulitple times in render method.

Is there away to avoid this?

My requirement.

shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
shapeRenderer.setProjectionMatrix(batch.getProjectionMatrix());
shapeRenderer.rect(....);
shapeRenderer.end();


batch.begin();
for (int i = 0; i < spriteList.size(); i++) {
     spriteList.get(i).render(batch);
}
batch.end();

shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setProjectionMatrix(batch.getProjectionMatrix());
shapeRenderer.setColor(Color.GRAY.r,Color.GRAY.g, Color.GRAY.b, 1);
shapeRenderer.rect(....);
shapeRenderer.end();

batch.begin();
score.render(batch);
batch.end();

Upvotes: 3

Views: 685

Answers (1)

Zoe - Save the data dump
Zoe - Save the data dump

Reputation: 28238

No there isn't. If you have a specific order in which you want to draw things and that requires different batches, you have to call end and begin repeatedly. It's terrible design IMO (from the library's side) but that's how it works.

There are no workarounds on it, and I doubt there will every be any. So you're stuck with calling begin and end every time you change the renderer

Upvotes: 3

Related Questions