[LibGdx] Massive fps drop when loading more than 1 texture.

Hello, JGO!
I have created world. World size is 250 * 250 tiles and tile information is stored in 2D object array.
At first i was generating randomly 2 tiles - dirt and empty tile. There was no problems and had like 60fps without culling.
After that i created another tile(stone) and then it started to lag(~5fps). If i generate 250x250 with only dirt tile/stone tile then fps is normal.

Writing this up i remembered that it might lag, because there is 2 different Tile objects now, but why would it lag?

If you need any code, say it. And if you don’t understand me, i will try to tell this differently.

Tile and rendering code would be good.

Tile rendering.


/**
	 * Checks every tile -> Gets tile id -> Draws correct texture.
	 * If Tile is empty, ignore it.
	 * @param batch
	 */
	public void drawTiles(SpriteBatch batch) {
		for(int x = 0; x < WORLD_X; x++) {
			for(int y = 0; y < WORLD_Y; y++) {
				if(getTileId(x, y) != 0) {
					batch.draw(Tiles.getTile(getTileId(x, y)).getTileImg(), 16 * x, 16 * y);
				}
			}
		}
	}

Generating world



//Contains map information
	private Tile[][] map = new Tile[WORLD_X][WORLD_Y];
	
	/**
	 * Generates new map
	 */
	public TerrainGenerator() {
		for(int x = 0; x < WORLD_X; x++) {
			for(int y = 0; y < WORLD_Y; y++) {
				map[x][y] = new Tile(Misc.random(0, 2));
			}
		}
	 }


Tile class



public class Tile {
	
	private int id;
	private int durability;
	
	public Tile(int tileId) {
		this.id = tileId;
		this.durability = Tiles.getTile(tileId).getDurability();
	}
	
	public int getId() {
		return this.id;
	}
	
	public int setId(int tileId) {
		return this.id = tileId;
	}
	
	
	public int getDurability() {
		return this.durability;
	}
}


Contains tile information.



public enum Tiles {
	
	EMPTY(0, null, -1),
	DIRT(1, new Texture("tiles/sand.png"), 5),
	ROCK(2, new Texture("tiles/rock.png"), 2);
	
	//Tile id
	private int id;
	//Tile image
	private Texture tileImg;
	//Tile durability
	private int durability;
	
	Tiles(int id, Texture tileImg, int durability) {
		this.id = id;
		this.tileImg = tileImg;
		this.durability = durability;
	}
	
	/**
	 * Returns Specified Tile data.
	 * @param tileId
	 * @return
	 */
	public static Tiles getTile(int tileId) {
		for(Tiles t : Tiles.values()) {
			if(t.getId() == tileId) {
				return t;
			}
		}
		return null;
	}
	
	/**
	 * Returns Tile id
	 * @return
	 */
	public int getId() {
		return id;
	}
	
	/**
	 * Returns Tiles image
	 * @return
	 */
	public Texture getTileImg() {
		return tileImg;
	}
	
	public int getDurability() {
		return durability;
	}

}


mind if I see the batch code?

Sorry, you want to see SpriteBatch.java or what?
I think it’s not batching problem. It might be something with tile creating or anything else.

You can only batch sprites that use same texture. Look the amount of drawcalls spriteBatch do per frame and you see that this is the problem.

Solution to this BTW is TextureAtlas.

Thanks, fixed. You can close this. :slight_smile:

Just edit and put [Solved] in the title.