TextureRegion

Hi,

I’m drawing 64x32 tiles - each calls spritebatch draw method, runs 60fps. Now, is this expensive to do this? Should I draw using texture regions, although my
map is procedural?

Any advice on using one draw call to draw lots of tiles would be appreciated.

Thanks

Don’t worry, the SpriteBatch batches the render calls already, it does it like this:


render(...){
    if(texture changed)
        change texture and flush // => draw call

    if(batch full)
        flush // => draw call

    add sprite to an array of vertex data // => Internal Buffer fills up, but no draw call!
}

If you really need to optimize, sort the sprites by texture, so that you do not need to switch textures and flush so often.
But save this for when you get performance problems, everything else would be premature optimization.

Hey that is good news :slight_smile: Thanks

[quote=“steveyg90,post:1,topic:56434”]
You really should be using texture regions, or better: use texture atlases (you need to render texture regions). Pack your tile textures into an atlas using TexturePacker. Then draw all the tiles (and nothing else in between, also no batch.begin / batch.end in between tile draw calls). Also, make sure that all the tiles on your map are on a single texture page. Switching between different textures is an expensive operation, and by drawing all your tiles from the same texture page (which is loaded into the graphics card as one texture) you will not need to switch textures for each tile.

Hi,

All my tiles are in one texture. I then use sprite batch to render them.


		batch.begin();

		// Draw the map - only draw what camera sees
		worldMap.drawMap(debug, batch, regions, camX - 2, camY, endX + 2, endY,
				WORLDWIDTH, WORLDHEIGHT);

		batch.end();


and my drawMap method:


public void drawMap(boolean bDebug, SpriteBatch batch,
			TextureRegion[][] region, int sx, int sy, int ex, int ey, int w,
			int h) {

		if (bDebug) { // debug mode - brute force draw everything
			for (int row = 0; row < h; row++)
				for (int col = 0; col < w; col++)
					if (getEntity(col, row) != null)
						worldMap[col][row].draw(batch, region);
		} else { // draw what we can see
			for (int row = sy; row < ey + 1; row++)
				for (int col = sx; col < ex + 1; col++)
					if (getEntity(col, row) != null) // THIS NEEDS REMOVING AT
														// SOME POINT!!!!!
					{
						BlankEntity entity = getEntity(col, row);

						if (entity instanceof WaterEntity)
							batch.setColor(0, 0, 0.4f, 1);
						else if (entity.lightUp)
							batch.setColor(entity.r, entity.g, entity.b, 1);
						else
							batch.setColor(0.4f, 0.4f, 0.8f, 1);
						worldMap[col][row].draw(batch, region);
					}
		}
	}

and draw method for each entity is like this (t[.][.] is location of the tile in the texture region):


	public void draw(Batch batch, TextureRegion[][] t) {
		batch.draw(t[1][0], x << size, y << size); // 0,0
	}

I assume this is ok? The setColor code in here is a dirty way of lighting blocks up near the player.

Thanks