package com.badlogic.gdx.maps.tiled.renderers;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.MapLayer;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Disposable;

public abstract class BatchTiledMapRenderer implements TiledMapRenderer, Disposable {
	
	protected TiledMap map;

	protected float unitScale;
	
	protected SpriteBatch spriteBatch;
	
	protected Rectangle viewBounds; 

	public TiledMap getMap() {
		return map;			
	}
	
	public float getUnitScale() {
		return unitScale;
	}
	
	public SpriteBatch getSpriteBatch() {
		return spriteBatch;
	}

	public Rectangle getViewBounds() {
		return viewBounds;
	}
	
	public BatchTiledMapRenderer(TiledMap map) {
		this.map = map;
		this.unitScale = 1;
		this.spriteBatch = new SpriteBatch();
		this.viewBounds = new Rectangle();
	}
	
	public BatchTiledMapRenderer(TiledMap map, float unitScale) {
		this.map = map;
		this.unitScale = unitScale;
		this.viewBounds = new Rectangle();
		this.spriteBatch = new SpriteBatch();
	}
	
	@Override
	public void setView(OrthographicCamera camera) {
		spriteBatch.setProjectionMatrix(camera.combined);
		float width = camera.viewportWidth * camera.zoom;
		float height = camera.viewportHeight * camera.zoom;
		viewBounds.set(camera.position.x - width / 2, camera.position.y - height / 2, width, height);
	}
	
	@Override
	public void setView (Matrix4 projection, float x, float y, float width, float height) {
		spriteBatch.setProjectionMatrix(projection);
		viewBounds.set(x, y, width, height);
	}
	
	@Override
	public void render () {
		spriteBatch.begin();
		for (MapLayer layer : map.getLayers()) {
			if (layer.isVisible()) {
				if (layer instanceof TiledMapTileLayer) {
					renderTileLayer((TiledMapTileLayer) layer);
				} else {
					for (MapObject object : layer.getObjects()) {
						renderObject(object);
					}
				}					
			}				
		}
		spriteBatch.end();
	}
	
	@Override
	public void render (int[] layers) {
		spriteBatch.begin();
		for (int layerIdx : layers) {
			MapLayer layer = map.getLayers().get(layerIdx);
			if (layer.isVisible()) {
				if (layer instanceof TiledMapTileLayer) {
					renderTileLayer((TiledMapTileLayer) layer);
				} else {
					for (MapObject object : layer.getObjects()) {
						renderObject(object);
					}
				}					
			}				
		}		
		spriteBatch.end();
	}

	@Override
	public void dispose () {
		spriteBatch.dispose();
	}
	
}
@@ -732,24 +732,64 @@
 Bounds; %0D%0A%0D%0A
+%09protected boolean ownsSpriteBatch;%0D%0A%09%0D%0A
 %09public Tile
@@ -822,32 +822,95 @@
 map;%09%09%09%0D%0A%09%7D%0D%0A%09%0D%0A
+%09public void setMap(TiledMap map) %7B%0D%0A%09%09this.map = map;%0D%0A%09%7D%0D%0A%09%0D%0A
 %09public float ge
@@ -1145,121 +1145,366 @@
 this
-.map = map;%0D%0A%09%09this.unitScale = 1;%0D%0A%09%09this.spriteBatch = new SpriteBatch();%0D%0A%09%09this.viewBounds = new Rectangle();
+(map, 1.0f);%0D%0A%09%7D%0D%0A%09%0D%0A%09public BatchTiledMapRenderer(TiledMap map, float unitScale) %7B%0D%0A%09%09this.map = map;%0D%0A%09%09this.unitScale = unitScale;%0D%0A%09%09this.viewBounds = new Rectangle();%0D%0A%09%09this.spriteBatch = new SpriteBatch();%0D%0A%09%09this.ownsSpriteBatch = true;%0D%0A%09%7D%0D%0A%09%0D%0A%09public BatchTiledMapRenderer(TiledMap map, SpriteBatch spriteBatch) %7B%0D%0A%09%09this(map, 1.0f, spriteBatch);%09%09
 %0D%0A%09%7D
@@ -1559,32 +1559,57 @@
  float unitScale
+, SpriteBatch spriteBatch
 ) %7B%0D%0A%09%09this.map 
@@ -1698,36 +1698,57 @@
 s.spriteBatch = 
-new 
+spriteBatch;%0D%0A%09%09this.owns
 SpriteBatch();%0D%0A
@@ -1738,26 +1738,32 @@
 sSpriteBatch
-()
+ = false
 ;%0D%0A%09%7D%0D%0A%09%0D%0A%09@
@@ -3156,24 +3156,51 @@
 spose () %7B%0D%0A
+%09%09if (ownsSpriteBatch) %7B%0D%0A%09
 %09%09spriteBatc
@@ -3211,16 +3211,24 @@
 spose();
+%09%0D%0A%09%09%7D%09%09
 %0D%0A%09%7D%0D%0A%09%0D

Add flexibility for BatchTiledMapRenderer users.