0

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?

Sapp
  • 3
  • 1

2 Answers2

2

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.

Arctic45
  • 1,078
  • 1
  • 7
  • 17
  • Thanks! Now I get it. I thought TextureAtlas was taking care of putting different textures "together". – Sapp Dec 06 '17 at 17:11
0

You are drawing them in loop 200 times.

for (int i = 0; i < 200; ++i) {
    // this will be called 200 times
    batch.draw(tr_list.get(i % tr_list.size), (int)(Math.random()* 500), (int)(Math.random()* 500)); 
}

And also TextureAtlas class doesn't create pig picture of textures that you add to it. It is just container of TextureRegions how @Arctic23 noticed.

So main problem for you is to draw 2 textures in one render call. I wouldn't recommend you doing this. It is possible but it will be hard to work with them.

Calling batch.draw(..) every time you draw a texture is not a big performance problem. So i don't know why you detach this.

icarumbas
  • 1,777
  • 3
  • 17
  • 30