Search code examples
libgdx

does the spritebatch draws multiple stages with a single batch? Libgdx


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.


Solution

  • 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.