LibGDX drawing of sprites?

So, I have a grid of tiles I wish to render, along with various other objects and bits / pieces atop those tiles. I’ve determined that the way I should go about it is to only render the tiles around the player’s position, which isn’t too hard.

The thing is, LibGDX’s SpriteBatch has a maximum size of 5460 sprites per drawing call, correct? There might well be more than that at any given point, considering the tiles are 8x8 and the screen is 640 x 480, coming up to 4800 sprites in tiles alone. On top of those might be items and entities and other such nonsense.

Does anyone have any advice for me, whether to use separate spritebatches or something like that? Would it be more efficient? Or can I increase the maximum size of a spritebatch and would THAT be more efficient?

Thanks!

When rendering large amounts of the same thing you shouldn’t have an instance for each object. What I mean is that your map should store only the type of each tile and when drawing you use the tile type to get a texture for that.

You do it like this (pseudo-code):


SomeTable tileTypes = new SomeTable<TileType, Texture>(); //Consider HashTable
tileTypes.put(TileType.GRASS, new Texture("grass.png"));
tileTypes.put(TileType.DIRT, new Texture("dirt.png"));
tileTypes.put(TileType.ROCK, new Texture("rocknroll.png"));

public void drawTiles(){
    for(Tile t : tiles){
        batch.draw(tileTypes.get(t.getType()), calculateXforThisTile, calculateYforThisTile);
    }
}

I’ve tested this for you. Running the following code I’ve managed to draw 16000 things on one draw call:


public class BatchDrawingTest extends ApplicationAdapter {
	SpriteBatch			batch;
	OrthographicCamera	camera;
	TextureRegion		thing;

	@Override
	public void create() {
		camera = new OrthographicCamera(800.0f, 600.0f);
		camera.translate(400.0f, 300.0f);
		Assets.load();

		batch = new SpriteBatch();

		thing = new TextureRegion(new Texture(Gdx.files.internal("launcher.png")));
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		Gdx.gl.glClear(16384);

		batch.setProjectionMatrix(camera.combined);
		camera.update();

		batch.begin();
		for (int i = 0; i < 16000; i++)
			batch.draw(thing, ((i / 16000f) * 800), (i % 50) * 50, 20, 20);
		batch.end();
	}

	@Override
	public void dispose() {}
}

Try doing something like this. If you need more help just reply here =)

When SpriteBatch reaches it’s limit, it will automatically flush and begin a new batch. You shouldn’t worry about this though, as the batch will need to be flushed on many other occasions, such as changing shaders, changing uniform variables in the shaders and many more.

EDIT—
If your game doesn’t have more than 100 draw calls per frame, I don’t think you have to worry about anything yet. You could probably have up to 500 or 1000 draw calls and game would still run at 60 fps on not very good machines.