0

It's been a while since I used LibGdx, so I feel like I'm just missing something obvious.

My render method in MyGdxGame looks like this, calling for the stage to draw itself (along with its actors), and then I try to draw a set of textures for debugging purposes.

@Override
    public void render()
    {        
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        StageManager.getCurrentStage().act();

        spriteBatch.begin();
        StageManager.getCurrentStage().draw();
        for(int i = 0; i < 100; i++) 
        {
            spriteBatch.draw(TextureManager.getPlayerTexture(), 50*i, 50*i);
        }
    }

The stage is drawn along with its one actor, but the other textures are not being drawn.

What I've tried: setting batch projection matrix off the stage camera(after calling update), making sure the texture coordinates should be visible.

The actor gets its texture from the same TextureManager.getPlayerTexture so I don't think its a texture issue.

What else should I check for to get the textures drawn as well?

DoubleDouble
  • 1,493
  • 10
  • 25
  • 1
    Is this your actual code? calling `spriteBatch.begin()` followed by `stage.draw()` will throw a RuntimeException. – Tenfour04 Jul 08 '16 at 16:18
  • 1
    `stage.draw()` internally calls `begin` and `end` on the batch. To draw additional stuff, you need to wrap those draw calls in `begin()` and `end()`. – Tenfour04 Jul 08 '16 at 16:18
  • Strange. It is my actual code, I dont get an exception. Moving batch.begin below the draw call does fix the issue though. (Post as an answer so I can accept please) – DoubleDouble Jul 08 '16 at 16:30
  • Can you show your `create` method? Maybe you have two sprite batches in use. Stage instantiates its own if one is not passed to its constructor. – Tenfour04 Jul 08 '16 at 17:07
  • I instantiate Stage with an empty constructor, so I do probably have two in use, which I will correct - thanks for the tip! – DoubleDouble Jul 08 '16 at 18:06
  • I'm grateful you kept asking, I would have had many stages, each with its own SpriteBatch, and probably would have struggled for a while to figure out the memory usage at some future point. Thanks again! – DoubleDouble Jul 08 '16 at 20:26

1 Answers1

1

Normally, your code would have caused a RuntimeException, because you're calling begin on the batch before drawing the stage. But since you actually have two sprite batches (one internal to the Stage because you didn't share the original with it), no actual errors occur.

You're missing a call to spriteBatch.end() after drawing your texture(s). And the call to spriteBatch.begin() needs to move after stage.draw().

And you should pass that sprite batch into the Stage constructor, because it is wasteful to have more than one sprite batch. Each sprite batch uses a fair amount of memory and compiles a shader.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154