2
votes

The title says it all.

does calling multiple Stages draw() method with the same SpriteBatcher object happens only in a single batch or it is one Stage = one batch?

I have a situation where i dont want to use actors for the background of the game, i just draw it directly. But the algo looks like this.

  1. begin batch
  2. draw background
  3. end batch
  4. call all stage draw

If your answer is: the spritebatch will draw multiple stages in one batch, then I can say that it is more efficient to put each sprites as a member to an Actor object and then add all Actor object to the Stage object, then call the stage draw. This way, i can minimize the call to begin batch and end batch which can improve performance as what i know.

1
Modern low end phones can do roughly 50 draw calls before frame rate drops below 60 FPS. Of course, there are other factors that can affect that, but it's almost certainly not worth complicating your code to save one draw call.Tenfour04
an additional information. great. thanks.paulj
One other thing. Buttons and Tables in libgdx also cause a batch flush by default, so you will get an extra draw call on each of them unless you call setTransform(false); on them, which is fine as long as you don't plan to scale or rotate them. Those really can add up if you use a lot of buttons.Tenfour04

1 Answers

5
votes

Looking at the Stages code, it seems like it calls batch.begin() and batch.end():

public void draw () {
    Camera camera = viewport.getCamera();
    camera.update();

    if (!root.isVisible()) return;

    Batch batch = this.batch;
    if (batch != null) {
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        root.draw(batch, 1);
        batch.end();
    }

    if (debug) drawDebug();
}

So adding your Actors to the Stage might reduce the draw calls.
But remember, that if the Batch is full, it will be flush()ed anyways, which results in a draw call.
So it does not necessarily decrease the number of draw calls. Also don't think about performance to much, as long as you don't have perforrmance problems. If the other solutions seams "cleaner", i guess it is not worth changing it.
One important thing to remeber, when working with more then one Batch is, to call end() for one, before calling begin() for the other. You should never have 2 Batches "running" at the same time.