Search code examples
androidlibgdxtexturestexture-atlas

TextureAtlas and renderCalls in libgdx


I'm creating two textures and put them in the same TextureAtlas as shown in the code:

public void create () {
    pix = new Pixmap(100, 100, Pixmap.Format.RGBA8888);
    textureAtlas = new TextureAtlas();

    pix.setColor(Color.WHITE);
    pix.fill();
    textureAtlas.addRegion("white", new TextureRegion(new Texture(pix)));
    pix.setColor(Color.RED);
    pix.fill();
    textureAtlas.addRegion("red", new TextureRegion(new Texture(pix)));

    tr_list = textureAtlas.getRegions();

    pix.dispose()
}

and then when rendering:

public void render () {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    for (int i = 0; i < 200; ++i) {
        batch.draw(tr_list.get(i % tr_list.size), (int)(Math.random()* 500), (int)(Math.random()* 500));
    }
    font.draw(batch, "FPS: " + Integer.toString(Gdx.graphics.getFramesPerSecond()), 0, Gdx.graphics.getHeight() - 20);
    font.draw(batch, "Render Calls: " + Integer.toString(batch.renderCalls), 0, Gdx.graphics.getHeight() - 60);
    batch.end();
}

I was expecting batch.renderCalls to be equal to 1, since the textures are in the same TextureAtlas, but instead it's equal to 200. What am I doing wrong?


Solution

  • In order to draw your TextureRegions in a single render call, each of them must be a part of the same Texture, not TextureAtlas.

    TextureAtlas can contain TextureRegions of different Textures, which actually happens in your case. Even though you use the same Pixmap, you create two different Textures from it.