package com.badlogic.gdx.maps.tiled;
+import static com.badlogic.gdx.graphics.g2d.SpriteBatch.*;
+
+import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
-import com.badlogic.gdx.graphics.OrthographicCamera;
+import com.badlogic.gdx.graphics.GL10;
+import com.badlogic.gdx.graphics.GL11;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
+import com.badlogic.gdx.graphics.g2d.SpriteCache;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
-import com.badlogic.gdx.maps.Map;
+import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.maps.MapLayer;
-import com.badlogic.gdx.maps.MapLayers;
-import com.badlogic.gdx.maps.MapRenderer;
+import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
import com.badlogic.gdx.math.Matrix4;
-import com.badlogic.gdx.utils.Disposable;
-
-import static com.badlogic.gdx.graphics.g2d.SpriteBatch.*;
-
-/**
- * @brief Logic for rendering TiledMap objects
- *
- * Includes several optimisations such as SpriteCache usage, fustrum culling etc.
- */
-public class TiledMapRenderer implements MapRenderer, Disposable {
+import com.badlogic.gdx.math.Polygon;
+import com.badlogic.gdx.math.Rectangle;
- private Map map;
-
- private SpriteBatch spriteBatch;
-
- private float unitScale = 1;
-
- private boolean ownsSpriteBatch = false;
-
- /**
- * @return map currently being used for rendering
- */
- public Map getMap() {
- return map;
- }
-
- /**
- * @return batch used for rendering
- */
- public SpriteBatch getSpriteBatch() {
- return spriteBatch;
- }
+public interface TiledMapRenderer {
- /**
- * @return world units per pixel used for rendering
- */
- public float getUnitScale() {
- return unitScale;
- }
+ public void setViewBounds(float x, float y, float width, float height);
- /**
- * Creates a renderer from a map. Will use own spritebatch and world units of 1.0 pixels
- *
- * @param map will use this map for rendering
- */
- public TiledMapRenderer(TiledMap map) {
- this(map, new SpriteBatch());
- ownsSpriteBatch = true;
- }
+ public void setProjectionMatrix(Matrix4 projection);
- /**
- * Creates a renderer from a map using the given world units. Will use a batch from its own.
- *
- * @param map will use this map for rendering
- * @param unitScale world units per pixel
- */
- public TiledMapRenderer(TiledMap map, float unitScale) {
- this(map, new SpriteBatch(), unitScale);
- ownsSpriteBatch = true;
- }
+ public void begin();
+ public void end();
- /**
- * @param map will use this map for rendering
- * @param spriteBatch batch that will be used for rendering
- */
- public TiledMapRenderer(TiledMap map, SpriteBatch spriteBatch) {
- this.map = map;
- this.spriteBatch = spriteBatch;
- this.ownsSpriteBatch = false;
- }
+ public void render();
+ public void renderObject(MapObject object);
+ public void renderTileLayer(TiledMapTileLayer layer);
- /**
- * @param map will use this map for rendering
- * @param spriteBatch batch that will be used for rendering
- * @param unitScale world units per pixel
- */
- public TiledMapRenderer(TiledMap map, SpriteBatch spriteBatch, float unitScale) {
- this.map = map;
- this.spriteBatch = spriteBatch;
- this.unitScale = unitScale;
- this.ownsSpriteBatch = false;
- }
+ public class BatchTiledMapRenderer implements TiledMapRenderer {
+
+ protected TiledMap map;
- /**
- * @param projection projection matrix that will be used for rendering the map
- */
- @Override
- public void setProjectionMatrix (Matrix4 projection) {
- spriteBatch.setProjectionMatrix(projection);
- }
+ 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 setViewBounds (float x, float y, float width, float height) {
+ viewBounds.set(x, y, width, height);
+ }
+
+ @Override
+ public void setProjectionMatrix (Matrix4 projection) {
+ spriteBatch.setProjectionMatrix(projection);
+ }
+
+ @Override
+ public void begin () {
+ spriteBatch.begin();
+ }
+
+ @Override
+ public void end () {
+ spriteBatch.end();
+ }
+
+ @Override
+ public void render () {
+ for (MapLayer layer : map.getLayers()) {
+ if (layer.getVisible()) {
+ if (layer instanceof TiledMapTileLayer) {
+ renderTileLayer((TiledMapTileLayer) layer);
+ } else {
+ for (MapObject object : layer.getObjects()) {
+ renderObject(object);
+ }
+ }
+ }
+ }
+ }
+
+ @Override
+ public void renderObject (MapObject object) {
+ // Do nothing
+ }
+
+ @Override
+ public void renderTileLayer (TiledMapTileLayer layer) {
+ // Do nothing
+ }
- /** Sets up the SpriteBatch for drawing.
- *
- * @see com.badlogic.gdx.maps.MapRenderer#begin()
- */
- public void begin() {
- spriteBatch.begin();
- }
-
- public void end() {
- spriteBatch.end();
}
- /**Convenience method. Calculates the view bounds from the provided OrthographicCamera.
- *
- * @param camera The OrthographicCamera from which to calculate the view bounds.
- */
- public void render(OrthographicCamera camera) {
+ public class CacheTiledMapRenderer implements TiledMapRenderer {
+ protected TiledMap map;
+
+ protected float unitScale;
- final float camTop = camera.position.y + camera.viewportHeight / 2 * camera.zoom;
- final float camLeft = camera.position.x - camera.viewportWidth / 2 * camera.zoom;
- final float camRight = camera.position.x + camera.viewportWidth / 2 * camera.zoom;
- final float camBottom = camera.position.y - camera.viewportHeight / 2 * camera.zoom;
+ protected SpriteCache spriteCache;
+
+ protected Rectangle viewBounds;
- for (MapLayer layer : map.getLayers()) {
- renderLayer(camLeft, camTop, camRight, camBottom, layer);
+ public TiledMap getMap() {
+ return map;
+ }
+
+ public float getUnitScale() {
+ return unitScale;
+ }
+
+ public SpriteCache getSpriteCache() {
+ return spriteCache;
}
- }
- /**Convenience method. Calculates the view bounds from the provided OrthographicCamera.
- *
- * @param camera The OrthographicCamera from which to calculate the view bounds.
- * @param layers The indices of the layers of the map to draw.
- */
- public void render(OrthographicCamera camera, int[] layers) {
- final float camTop = camera.position.y + camera.viewportHeight / 2 * camera.zoom;
- final float camLeft = camera.position.x - camera.viewportWidth / 2 * camera.zoom;
- final float camRight = camera.position.x + camera.viewportWidth / 2 * camera.zoom;
- final float camBottom = camera.position.y - camera.viewportHeight / 2 * camera.zoom;
-
- MapLayers mapLayers = map.getLayers();
- for (int i = 0; i < layers.length; i++) {
- renderLayer(camLeft, camTop, camRight, camBottom, mapLayers.getLayer(i));
+ public Rectangle getViewBounds() {
+ return viewBounds;
+ }
+
+ public CacheTiledMapRenderer(TiledMap map) {
+ this.map = map;
+ this.unitScale = 1;
+ this.spriteCache = new SpriteCache();
+ this.viewBounds = new Rectangle();
}
+
+ public CacheTiledMapRenderer(TiledMap map, float unitScale) {
+ this.map = map;
+ this.unitScale = unitScale;
+ this.viewBounds = new Rectangle();
+ this.spriteCache = new SpriteCache();
+ }
+
+ @Override
+ public void setViewBounds (float x, float y, float width, float height) {
+ viewBounds.set(x, y, width, height);
+ }
+
+ @Override
+ public void setProjectionMatrix (Matrix4 projection) {
+ spriteCache.setProjectionMatrix(projection);
+ }
+
+ @Override
+ public void begin () {
+ spriteCache.begin();
+ }
+
+ @Override
+ public void end () {
+ spriteCache.end();
+ }
+
+ @Override
+ public void render () {
+ for (MapLayer layer : map.getLayers()) {
+ if (layer.getVisible()) {
+ if (layer instanceof TiledMapTileLayer) {
+ renderTileLayer((TiledMapTileLayer) layer);
+ } else {
+ for (MapObject object : layer.getObjects()) {
+ renderObject(object);
+ }
+ }
+ }
+ }
+ }
+
+ @Override
+ public void renderObject (MapObject object) {
+ // Do nothing
+ }
+
+ @Override
+ public void renderTileLayer (TiledMapTileLayer layer) {
+ // Do nothing
+ }
+
}
-
- /**
- * Renders all the layers using the projection matrix and the given bounds for fustrum culling
- *
- * @param viewboundsX
- * @param viewboundsY
- * @param viewboundsWidth
- * @param viewboundsHeight
- */
- @Override
- public void render (float viewboundsX, float viewboundsY, float viewboundsWidth, float viewboundsHeight) {
- MapLayers mapLayers = map.getLayers();
- for (MapLayer layer : mapLayers) {
- renderLayer(viewboundsX, viewboundsY, viewboundsX + viewboundsHeight, viewboundsY + viewboundsHeight, layer);
+ public class IsometricTiledMapRenderer extends BatchTiledMapRenderer {
+
+ private TiledMap map;
+
+ private float[] vertices = new float[20];
+
+ public IsometricTiledMapRenderer(TiledMap map) {
+ super(map);
}
+
+ public IsometricTiledMapRenderer(TiledMap map, float unitScale) {
+ super(map, unitScale);
+ }
+
+ @Override
+ public void renderObject (MapObject object) {
+
+ }
+
+ @Override
+ public void renderTileLayer (TiledMapTileLayer layer) {
+
+ final float color = Color.toFloatBits(1, 1, 1, layer.getOpacity());
+
+ int col1 = 0;
+ int col2 = layer.getWidth() - 1;
+
+ int row1 = 0;
+ int row2 = layer.getHeight() - 1;
+
+ float tileWidth = layer.getTileWidth() * unitScale;
+ float tileHeight = layer.getTileHeight() * unitScale;
+ float halfTileWidth = tileWidth * 0.5f;
+ float halfTileHeight = tileHeight * 0.5f;
+
+ for (int row = row2; row >= row1; row--) {
+ for (int col = col1; col <= col2; col++) {
+ float x = (col * halfTileWidth) + (row * halfTileWidth);
+ float y = (row * halfTileHeight) - (col * halfTileHeight);
+
+ final TiledMapTileLayer.Cell cell = layer.getCell(col, row);
+ final TiledMapTile tile = cell.getTile();
+ if (tile != null) {
+
+ final boolean flipX = cell.getFlipHorizontally();
+ final boolean flipY = cell.getFlipVertically();
+ final int rotations = cell.getRotation();
+
+ TextureRegion region = tile.getTextureRegion();
+
+ float x1 = x;
+ float y1 = y;
+ float x2 = x1 + region.getRegionWidth() * unitScale;
+ float y2 = y1 + region.getRegionHeight() * unitScale;
+
+ float u1 = region.getU();
+ float v1 = region.getV2();
+ float u2 = region.getU2();
+ float v2 = region.getV();
+
+ vertices[X1] = x1;
+ vertices[Y1] = y1;
+ vertices[C1] = color;
+ vertices[U1] = u1;
+ vertices[V1] = v1;
+
+ vertices[X2] = x1;
+ vertices[Y2] = y2;
+ vertices[C2] = color;
+ vertices[U2] = u1;
+ vertices[V2] = v2;
+
+ vertices[X3] = x2;
+ vertices[Y3] = y2;
+ vertices[C3] = color;
+ vertices[U3] = u2;
+ vertices[V3] = v2;
+
+ vertices[X4] = x2;
+ vertices[Y4] = y1;
+ vertices[C4] = color;
+ vertices[U4] = u2;
+ vertices[V4] = v1;
+
+ if (flipX) {
+ float temp = vertices[U1];
+ vertices[U1] = vertices[U3];
+ vertices[U3] = temp;
+ temp = vertices[U2];
+ vertices[U2] = vertices[U4];
+ vertices[U4] = temp;
+ }
+ if (flipY) {
+ float temp = vertices[V1];
+ vertices[V1] = vertices[V3];
+ vertices[V3] = temp;
+ temp = vertices[V2];
+ vertices[V2] = vertices[V4];
+ vertices[V4] = temp;
+ }
+ if (rotations != 0) {
+ switch (rotations) {
+ case Cell.ROTATE_90: {
+ float tempV = vertices[V1];
+ vertices[V1] = vertices[V2];
+ vertices[V2] = vertices[V3];
+ vertices[V3] = vertices[V4];
+ vertices[V4] = tempV;
+
+ float tempU = vertices[U1];
+ vertices[U1] = vertices[U2];
+ vertices[U2] = vertices[U3];
+ vertices[U3] = vertices[U4];
+ vertices[U4] = tempU;
+ break;
+ }
+ case Cell.ROTATE_180: {
+ float tempU = vertices[U1];
+ vertices[U1] = vertices[U3];
+ vertices[U3] = tempU;
+ tempU = vertices[U2];
+ vertices[U2] = vertices[U4];
+ vertices[U4] = tempU;
+ float tempV = vertices[V1];
+ vertices[V1] = vertices[V3];
+ vertices[V3] = tempV;
+ tempV = vertices[V2];
+ vertices[V2] = vertices[V4];
+ vertices[V4] = tempV;
+ break;
+ }
+ case Cell.ROTATE_270: {
+ float tempV = vertices[V1];
+ vertices[V1] = vertices[V4];
+ vertices[V4] = vertices[V3];
+ vertices[V3] = vertices[V2];
+ vertices[V2] = tempV;
+
+ float tempU = vertices[U1];
+ vertices[U1] = vertices[U4];
+ vertices[U4] = vertices[U3];
+ vertices[U3] = vertices[U2];
+ vertices[U2] = tempU;
+ break;
+ }
+ }
+ }
+ spriteBatch.draw(region.getTexture(), vertices, 0, 20);
+ }
+ }
+ }
+ }
+
}
- /**
- * Renders the given layers using the projection matrix and the given bounds for fustrum culling
- *
- * @param viewboundsX
- * @param viewboundsY
- * @param viewboundsWidth
- * @param viewboundsHeight
- * @param layers
- */
- @Override
- public void render (float viewboundsX, float viewboundsY, float viewboundsWidth, float viewboundsHeight, int[] layers) {
- MapLayers mapLayers = map.getLayers();
- for (int i = 0; i < layers.length; i++) {
- renderLayer(viewboundsX, viewboundsY, viewboundsX + viewboundsHeight, viewboundsY + viewboundsHeight, mapLayers.getLayer(i));
+ public class OrthogonalTiledMapRenderer extends BatchTiledMapRenderer {
+
+ private float[] vertices = new float[20];
+
+ public OrthogonalTiledMapRenderer(TiledMap map) {
+ super(map);
}
- }
- float[] vertices = new float[20];
- protected void renderLayer(float camLeft, float camTop, float camRight, float camBottom, MapLayer layer) {
- if (layer.getVisible()) {
+ public OrthogonalTiledMapRenderer(TiledMap map, float unitScale) {
+ super(map, unitScale);
+ }
+
+ @Override
+ public void renderObject (MapObject object) {
- float color = Color.toFloatBits(1, 1, 1, layer.getOpacity());
+ }
+
+ @Override
+ public void renderTileLayer (TiledMapTileLayer layer) {
- if (layer instanceof TiledMapTileLayer) {
- final TiledMapTileLayer tileLayer = (TiledMapTileLayer) layer;
-
- final int layerWidth = tileLayer.getWidth();
- final int layerHeight = tileLayer.getHeight();
-
- final float layerTileWidth = tileLayer.getTileWidth() * unitScale;
- final float layerTileHeight = tileLayer.getTileHeight() * unitScale;
-
- final int col1 = Math.max(0, (int) (camLeft / layerTileWidth));
- final int col2 = Math.min(layerWidth, (int) ((camRight + layerTileWidth) / layerTileWidth));
-
- final int row1 = Math.max(0, (int) (camBottom / layerTileHeight));
- final int row2 = Math.min(layerHeight, (int) ((camTop + layerTileHeight) / layerTileHeight));
-
- for (int row = row1; row < row2; row++) {
- for (int col = col1; col < col2; col++) {
- final TiledMapTileLayer.Cell cell = tileLayer.getCell(col, row);
- final TiledMapTile tile = cell.getTile();
- if (tile != null) {
-
- final boolean flipX = cell.getFlipHorizontally();
- final boolean flipY = cell.getFlipVertically();
- final int rotations = cell.getRotation();
-
- TextureRegion region = tile.getTextureRegion();
-
- float x1 = col * layerTileWidth;
- float y1 = row * layerTileHeight;
- float x2 = x1 + region.getRegionWidth() * unitScale;
- float y2 = y1 + region.getRegionHeight() * unitScale;
-
- float u1 = region.getU();
- float v1 = region.getV2();
- float u2 = region.getU2();
- float v2 = region.getV();
-
- vertices[X1] = x1;
- vertices[Y1] = y1;
- vertices[C1] = color;
- vertices[U1] = u1;
- vertices[V1] = v1;
-
- vertices[X2] = x1;
- vertices[Y2] = y2;
- vertices[C2] = color;
- vertices[U2] = u1;
- vertices[V2] = v2;
-
- vertices[X3] = x2;
- vertices[Y3] = y2;
- vertices[C3] = color;
- vertices[U3] = u2;
- vertices[V3] = v2;
-
- vertices[X4] = x2;
- vertices[Y4] = y1;
- vertices[C4] = color;
- vertices[U4] = u2;
- vertices[V4] = v1;
-
- if (flipX) {
- float temp = vertices[U1];
- vertices[U1] = vertices[U3];
- vertices[U3] = temp;
- temp = vertices[U2];
- vertices[U2] = vertices[U4];
- vertices[U4] = temp;
- }
- if (flipY) {
- float temp = vertices[V1];
- vertices[V1] = vertices[V3];
- vertices[V3] = temp;
- temp = vertices[V2];
- vertices[V2] = vertices[V4];
- vertices[V4] = temp;
- }
- if (rotations != 0) {
- switch (rotations) {
- case Cell.ROTATE_90: {
- float tempV = vertices[V1];
- vertices[V1] = vertices[V2];
- vertices[V2] = vertices[V3];
- vertices[V3] = vertices[V4];
- vertices[V4] = tempV;
-
- float tempU = vertices[U1];
- vertices[U1] = vertices[U2];
- vertices[U2] = vertices[U3];
- vertices[U3] = vertices[U4];
- vertices[U4] = tempU;
- break;
- }
- case Cell.ROTATE_180: {
- float tempU = vertices[U1];
- vertices[U1] = vertices[U3];
- vertices[U3] = tempU;
- tempU = vertices[U2];
- vertices[U2] = vertices[U4];
- vertices[U4] = tempU;
- float tempV = vertices[V1];
- vertices[V1] = vertices[V3];
- vertices[V3] = tempV;
- tempV = vertices[V2];
- vertices[V2] = vertices[V4];
- vertices[V4] = tempV;
- break;
- }
- case Cell.ROTATE_270: {
- float tempV = vertices[V1];
- vertices[V1] = vertices[V4];
- vertices[V4] = vertices[V3];
- vertices[V3] = vertices[V2];
- vertices[V2] = tempV;
-
- float tempU = vertices[U1];
- vertices[U1] = vertices[U4];
- vertices[U4] = vertices[U3];
- vertices[U3] = vertices[U2];
- vertices[U2] = tempU;
- break;
- }
- }
- }
- spriteBatch.draw(region.getTexture(), vertices, 0, 20);
+ final float color = Color.toFloatBits(1, 1, 1, layer.getOpacity());
+
+ final int layerWidth = layer.getWidth();
+ final int layerHeight = layer.getHeight();
+
+ final float layerTileWidth = layer.getTileWidth() * unitScale;
+ final float layerTileHeight = layer.getTileHeight() * unitScale;
+
+ final int col1 = Math.max(0, (int) (viewBounds.x / layerTileWidth));
+ final int col2 = Math.min(layerWidth, (int) ((viewBounds.x + viewBounds.width + layerTileWidth) / layerTileWidth));
+
+ final int row1 = Math.max(0, (int) (viewBounds.y / layerTileHeight));
+ final int row2 = Math.min(layerHeight, (int) ((viewBounds.y + viewBounds.height + layerTileHeight) / layerTileHeight));
+
+ for (int row = row1; row < row2; row++) {
+ for (int col = col1; col < col2; col++) {
+ final TiledMapTileLayer.Cell cell = layer.getCell(col, row);
+ final TiledMapTile tile = cell.getTile();
+ if (tile != null) {
+
+ final boolean flipX = cell.getFlipHorizontally();
+ final boolean flipY = cell.getFlipVertically();
+ final int rotations = cell.getRotation();
+
+ TextureRegion region = tile.getTextureRegion();
+
+ float x1 = col * layerTileWidth;
+ float y1 = row * layerTileHeight;
+ float x2 = x1 + region.getRegionWidth() * unitScale;
+ float y2 = y1 + region.getRegionHeight() * unitScale;
+
+ float u1 = region.getU();
+ float v1 = region.getV2();
+ float u2 = region.getU2();
+ float v2 = region.getV();
+
+ vertices[X1] = x1;
+ vertices[Y1] = y1;
+ vertices[C1] = color;
+ vertices[U1] = u1;
+ vertices[V1] = v1;
+
+ vertices[X2] = x1;
+ vertices[Y2] = y2;
+ vertices[C2] = color;
+ vertices[U2] = u1;
+ vertices[V2] = v2;
+
+ vertices[X3] = x2;
+ vertices[Y3] = y2;
+ vertices[C3] = color;
+ vertices[U3] = u2;
+ vertices[V3] = v2;
+
+ vertices[X4] = x2;
+ vertices[Y4] = y1;
+ vertices[C4] = color;
+ vertices[U4] = u2;
+ vertices[V4] = v1;
+
+ if (flipX) {
+ float temp = vertices[U1];
+ vertices[U1] = vertices[U3];
+ vertices[U3] = temp;
+ temp = vertices[U2];
+ vertices[U2] = vertices[U4];
+ vertices[U4] = temp;
}
+ if (flipY) {
+ float temp = vertices[V1];
+ vertices[V1] = vertices[V3];
+ vertices[V3] = temp;
+ temp = vertices[V2];
+ vertices[V2] = vertices[V4];
+ vertices[V4] = temp;
+ }
+ if (rotations != 0) {
+ switch (rotations) {
+ case Cell.ROTATE_90: {
+ float tempV = vertices[V1];
+ vertices[V1] = vertices[V2];
+ vertices[V2] = vertices[V3];
+ vertices[V3] = vertices[V4];
+ vertices[V4] = tempV;
+
+ float tempU = vertices[U1];
+ vertices[U1] = vertices[U2];
+ vertices[U2] = vertices[U3];
+ vertices[U3] = vertices[U4];
+ vertices[U4] = tempU;
+ break;
+ }
+ case Cell.ROTATE_180: {
+ float tempU = vertices[U1];
+ vertices[U1] = vertices[U3];
+ vertices[U3] = tempU;
+ tempU = vertices[U2];
+ vertices[U2] = vertices[U4];
+ vertices[U4] = tempU;
+ float tempV = vertices[V1];
+ vertices[V1] = vertices[V3];
+ vertices[V3] = tempV;
+ tempV = vertices[V2];
+ vertices[V2] = vertices[V4];
+ vertices[V4] = tempV;
+ break;
+ }
+ case Cell.ROTATE_270: {
+ float tempV = vertices[V1];
+ vertices[V1] = vertices[V4];
+ vertices[V4] = vertices[V3];
+ vertices[V3] = vertices[V2];
+ vertices[V2] = tempV;
+
+ float tempU = vertices[U1];
+ vertices[U1] = vertices[U4];
+ vertices[U4] = vertices[U3];
+ vertices[U3] = vertices[U2];
+ vertices[U2] = tempU;
+ break;
+ }
+ }
+ }
+ spriteBatch.draw(region.getTexture(), vertices, 0, 20);
}
+ }
+ }
+ }
+
+ }
+
+ public class OrthogonalTiledMapRenderer2 implements TiledMapRenderer {
+
+ protected TiledMap map;
+
+ protected float unitScale;
+
+ protected SpriteCache spriteCache;
+
+ protected Rectangle viewBounds;
+
+ private float[] vertices = new float[20];
+
+ public boolean recache;
+
+ public OrthogonalTiledMapRenderer2(TiledMap map) {
+ this.map = map;
+ this.unitScale = 1;
+ this.spriteCache = new SpriteCache(4000, true);
+ this.viewBounds = new Rectangle();
+ }
+
+ public OrthogonalTiledMapRenderer2(TiledMap map, float unitScale) {
+ this.map = map;
+ this.unitScale = unitScale;
+ this.viewBounds = new Rectangle();
+ this.spriteCache = new SpriteCache(4000, true);
+ }
+
+ @Override
+ public void setViewBounds (float x, float y, float width, float height) {
+ viewBounds.set(x, y, width, height);
+ }
+
+ @Override
+ public void setProjectionMatrix (Matrix4 projection) {
+ spriteCache.setProjectionMatrix(projection);
+ }
+
+ @Override
+ public void begin () {
+ if (recache) {
+ cached = false;
+ recache = false;
+ spriteCache.clear();
+ }
+ if (!cached) {
+ spriteCache.beginCache();
+ } else {
+ Gdx.gl.glEnable(GL10.GL_BLEND);
+ Gdx.gl.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
+ spriteCache.begin();
+ }
+
+ }
+
+ @Override
+ public void end () {
+ if (!cached) {
+ spriteCache.endCache();
+ cached = true;
+ begin();
+ render();
+ end();
+ } else {
+ spriteCache.end();
+ Gdx.gl.glDisable(GL10.GL_BLEND);
+ }
+ }
+
+ @Override
+ public void render () {
+ if (cached) {
+ spriteCache.draw(0);
+ } else {
+ for (MapLayer layer : map.getLayers()) {
+ if (layer.getVisible()) {
+ if (layer instanceof TiledMapTileLayer) {
+ renderTileLayer((TiledMapTileLayer) layer);
+ } else {
+ for (MapObject object : layer.getObjects()) {
+ renderObject(object);
+ }
+ }
+ }
}
}
- }
- }
- @Override
- public void dispose() {
- if (ownsSpriteBatch) {
- spriteBatch.dispose();
}
+
+ @Override
+ public void renderObject (MapObject object) {
+ // TODO Auto-generated method stub
+
+ }
+
+ boolean cached = false;
+ int count = 0;
+ @Override
+ public void renderTileLayer (TiledMapTileLayer layer) {
+ final float color = Color.toFloatBits(1, 1, 1, layer.getOpacity());
+
+ final int layerWidth = layer.getWidth();
+ final int layerHeight = layer.getHeight();
+
+ final float layerTileWidth = layer.getTileWidth() * unitScale;
+ final float layerTileHeight = layer.getTileHeight() * unitScale;
+
+ final int col1 = Math.max(0, (int) (viewBounds.x / layerTileWidth));
+ final int col2 = Math.min(layerWidth, (int) ((viewBounds.x + viewBounds.width + layerTileWidth) / layerTileWidth));
+
+ final int row1 = Math.max(0, (int) (viewBounds.y / layerTileHeight));
+ final int row2 = Math.min(layerHeight, (int) ((viewBounds.y + viewBounds.height + layerTileHeight) / layerTileHeight));
+
+ for (int row = row1; row < row2; row++) {
+ for (int col = col1; col < col2; col++) {
+ final TiledMapTileLayer.Cell cell = layer.getCell(col, row);
+ final TiledMapTile tile = cell.getTile();
+ if (tile != null) {
+ count++;
+ final boolean flipX = cell.getFlipHorizontally();
+ final boolean flipY = cell.getFlipVertically();
+ final int rotations = cell.getRotation();
+
+ TextureRegion region = tile.getTextureRegion();
+
+ float x1 = col * layerTileWidth;
+ float y1 = row * layerTileHeight;
+ float x2 = x1 + region.getRegionWidth() * unitScale;
+ float y2 = y1 + region.getRegionHeight() * unitScale;
+
+ float u1 = region.getU();
+ float v1 = region.getV2();
+ float u2 = region.getU2();
+ float v2 = region.getV();
+
+ vertices[X1] = x1;
+ vertices[Y1] = y1;
+ vertices[C1] = color;
+ vertices[U1] = u1;
+ vertices[V1] = v1;
+
+ vertices[X2] = x1;
+ vertices[Y2] = y2;
+ vertices[C2] = color;
+ vertices[U2] = u1;
+ vertices[V2] = v2;
+
+ vertices[X3] = x2;
+ vertices[Y3] = y2;
+ vertices[C3] = color;
+ vertices[U3] = u2;
+ vertices[V3] = v2;
+
+ vertices[X4] = x2;
+ vertices[Y4] = y1;
+ vertices[C4] = color;
+ vertices[U4] = u2;
+ vertices[V4] = v1;
+
+ if (flipX) {
+ float temp = vertices[U1];
+ vertices[U1] = vertices[U3];
+ vertices[U3] = temp;
+ temp = vertices[U2];
+ vertices[U2] = vertices[U4];
+ vertices[U4] = temp;
+ }
+ if (flipY) {
+ float temp = vertices[V1];
+ vertices[V1] = vertices[V3];
+ vertices[V3] = temp;
+ temp = vertices[V2];
+ vertices[V2] = vertices[V4];
+ vertices[V4] = temp;
+ }
+ if (rotations != 0) {
+ switch (rotations) {
+ case Cell.ROTATE_90: {
+ float tempV = vertices[V1];
+ vertices[V1] = vertices[V2];
+ vertices[V2] = vertices[V3];
+ vertices[V3] = vertices[V4];
+ vertices[V4] = tempV;
+
+ float tempU = vertices[U1];
+ vertices[U1] = vertices[U2];
+ vertices[U2] = vertices[U3];
+ vertices[U3] = vertices[U4];
+ vertices[U4] = tempU;
+ break;
+ }
+ case Cell.ROTATE_180: {
+ float tempU = vertices[U1];
+ vertices[U1] = vertices[U3];
+ vertices[U3] = tempU;
+ tempU = vertices[U2];
+ vertices[U2] = vertices[U4];
+ vertices[U4] = tempU;
+ float tempV = vertices[V1];
+ vertices[V1] = vertices[V3];
+ vertices[V3] = tempV;
+ tempV = vertices[V2];
+ vertices[V2] = vertices[V4];
+ vertices[V4] = tempV;
+ break;
+ }
+ case Cell.ROTATE_270: {
+ float tempV = vertices[V1];
+ vertices[V1] = vertices[V4];
+ vertices[V4] = vertices[V3];
+ vertices[V3] = vertices[V2];
+ vertices[V2] = tempV;
+
+ float tempU = vertices[U1];
+ vertices[U1] = vertices[U4];
+ vertices[U4] = vertices[U3];
+ vertices[U3] = vertices[U2];
+ vertices[U2] = tempU;
+ break;
+ }
+ }
+ }
+ spriteCache.add(region.getTexture(), vertices, 0, 20);
+ }
+ }
+ }
+ }
+
}
-
}
+++ /dev/null
-package com.badlogic.gdx.maps.tiled;
-
-import static com.badlogic.gdx.graphics.g2d.SpriteBatch.*;
-
-import com.badlogic.gdx.Gdx;
-import com.badlogic.gdx.graphics.Color;
-import com.badlogic.gdx.graphics.GL10;
-import com.badlogic.gdx.graphics.GL11;
-import com.badlogic.gdx.graphics.g2d.SpriteBatch;
-import com.badlogic.gdx.graphics.g2d.SpriteCache;
-import com.badlogic.gdx.graphics.g2d.TextureRegion;
-import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
-import com.badlogic.gdx.maps.MapLayer;
-import com.badlogic.gdx.maps.MapObject;
-import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
-import com.badlogic.gdx.math.Matrix4;
-import com.badlogic.gdx.math.Polygon;
-import com.badlogic.gdx.math.Rectangle;
-
-public interface TiledMapRenderer2 {
-
- public void setViewBounds(float x, float y, float width, float height);
-
- public void setProjectionMatrix(Matrix4 projection);
-
- public void begin();
- public void end();
-
- public void render();
- public void renderObject(MapObject object);
- public void renderTileLayer(TiledMapTileLayer layer);
-
- public class BatchTiledMapRenderer implements TiledMapRenderer2 {
-
- 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 setViewBounds (float x, float y, float width, float height) {
- viewBounds.set(x, y, width, height);
- }
-
- @Override
- public void setProjectionMatrix (Matrix4 projection) {
- spriteBatch.setProjectionMatrix(projection);
- }
-
- @Override
- public void begin () {
- spriteBatch.begin();
- }
-
- @Override
- public void end () {
- spriteBatch.end();
- }
-
- @Override
- public void render () {
- for (MapLayer layer : map.getLayers()) {
- if (layer.getVisible()) {
- if (layer instanceof TiledMapTileLayer) {
- renderTileLayer((TiledMapTileLayer) layer);
- } else {
- for (MapObject object : layer.getObjects()) {
- renderObject(object);
- }
- }
- }
- }
- }
-
- @Override
- public void renderObject (MapObject object) {
- // Do nothing
- }
-
- @Override
- public void renderTileLayer (TiledMapTileLayer layer) {
- // Do nothing
- }
-
- }
-
- public class CacheTiledMapRenderer implements TiledMapRenderer2 {
- protected TiledMap map;
-
- protected float unitScale;
-
- protected SpriteCache spriteCache;
-
- protected Rectangle viewBounds;
-
- public TiledMap getMap() {
- return map;
- }
-
- public float getUnitScale() {
- return unitScale;
- }
-
- public SpriteCache getSpriteCache() {
- return spriteCache;
- }
-
- public Rectangle getViewBounds() {
- return viewBounds;
- }
-
- public CacheTiledMapRenderer(TiledMap map) {
- this.map = map;
- this.unitScale = 1;
- this.spriteCache = new SpriteCache();
- this.viewBounds = new Rectangle();
- }
-
- public CacheTiledMapRenderer(TiledMap map, float unitScale) {
- this.map = map;
- this.unitScale = unitScale;
- this.viewBounds = new Rectangle();
- this.spriteCache = new SpriteCache();
- }
-
- @Override
- public void setViewBounds (float x, float y, float width, float height) {
- viewBounds.set(x, y, width, height);
- }
-
- @Override
- public void setProjectionMatrix (Matrix4 projection) {
- spriteCache.setProjectionMatrix(projection);
- }
-
- @Override
- public void begin () {
- spriteCache.begin();
- }
-
- @Override
- public void end () {
- spriteCache.end();
- }
-
- @Override
- public void render () {
- for (MapLayer layer : map.getLayers()) {
- if (layer.getVisible()) {
- if (layer instanceof TiledMapTileLayer) {
- renderTileLayer((TiledMapTileLayer) layer);
- } else {
- for (MapObject object : layer.getObjects()) {
- renderObject(object);
- }
- }
- }
- }
- }
-
- @Override
- public void renderObject (MapObject object) {
- // Do nothing
- }
-
- @Override
- public void renderTileLayer (TiledMapTileLayer layer) {
- // Do nothing
- }
-
- }
- public class IsometricTiledMapRenderer extends BatchTiledMapRenderer {
-
- private TiledMap map;
-
- private float[] vertices = new float[20];
-
- public IsometricTiledMapRenderer(TiledMap map) {
- super(map);
- }
-
- public IsometricTiledMapRenderer(TiledMap map, float unitScale) {
- super(map, unitScale);
- }
-
- @Override
- public void renderObject (MapObject object) {
-
- }
-
- @Override
- public void renderTileLayer (TiledMapTileLayer layer) {
-
- final float color = Color.toFloatBits(1, 1, 1, layer.getOpacity());
-
- int col1 = 0;
- int col2 = layer.getWidth() - 1;
-
- int row1 = 0;
- int row2 = layer.getHeight() - 1;
-
- float tileWidth = layer.getTileWidth() * unitScale;
- float tileHeight = layer.getTileHeight() * unitScale;
- float halfTileWidth = tileWidth * 0.5f;
- float halfTileHeight = tileHeight * 0.5f;
-
- for (int row = row2; row >= row1; row--) {
- for (int col = col1; col <= col2; col++) {
- float x = (col * halfTileWidth) + (row * halfTileWidth);
- float y = (row * halfTileHeight) - (col * halfTileHeight);
-
- final TiledMapTileLayer.Cell cell = layer.getCell(col, row);
- final TiledMapTile tile = cell.getTile();
- if (tile != null) {
-
- final boolean flipX = cell.getFlipHorizontally();
- final boolean flipY = cell.getFlipVertically();
- final int rotations = cell.getRotation();
-
- TextureRegion region = tile.getTextureRegion();
-
- float x1 = x;
- float y1 = y;
- float x2 = x1 + region.getRegionWidth() * unitScale;
- float y2 = y1 + region.getRegionHeight() * unitScale;
-
- float u1 = region.getU();
- float v1 = region.getV2();
- float u2 = region.getU2();
- float v2 = region.getV();
-
- vertices[X1] = x1;
- vertices[Y1] = y1;
- vertices[C1] = color;
- vertices[U1] = u1;
- vertices[V1] = v1;
-
- vertices[X2] = x1;
- vertices[Y2] = y2;
- vertices[C2] = color;
- vertices[U2] = u1;
- vertices[V2] = v2;
-
- vertices[X3] = x2;
- vertices[Y3] = y2;
- vertices[C3] = color;
- vertices[U3] = u2;
- vertices[V3] = v2;
-
- vertices[X4] = x2;
- vertices[Y4] = y1;
- vertices[C4] = color;
- vertices[U4] = u2;
- vertices[V4] = v1;
-
- if (flipX) {
- float temp = vertices[U1];
- vertices[U1] = vertices[U3];
- vertices[U3] = temp;
- temp = vertices[U2];
- vertices[U2] = vertices[U4];
- vertices[U4] = temp;
- }
- if (flipY) {
- float temp = vertices[V1];
- vertices[V1] = vertices[V3];
- vertices[V3] = temp;
- temp = vertices[V2];
- vertices[V2] = vertices[V4];
- vertices[V4] = temp;
- }
- if (rotations != 0) {
- switch (rotations) {
- case Cell.ROTATE_90: {
- float tempV = vertices[V1];
- vertices[V1] = vertices[V2];
- vertices[V2] = vertices[V3];
- vertices[V3] = vertices[V4];
- vertices[V4] = tempV;
-
- float tempU = vertices[U1];
- vertices[U1] = vertices[U2];
- vertices[U2] = vertices[U3];
- vertices[U3] = vertices[U4];
- vertices[U4] = tempU;
- break;
- }
- case Cell.ROTATE_180: {
- float tempU = vertices[U1];
- vertices[U1] = vertices[U3];
- vertices[U3] = tempU;
- tempU = vertices[U2];
- vertices[U2] = vertices[U4];
- vertices[U4] = tempU;
- float tempV = vertices[V1];
- vertices[V1] = vertices[V3];
- vertices[V3] = tempV;
- tempV = vertices[V2];
- vertices[V2] = vertices[V4];
- vertices[V4] = tempV;
- break;
- }
- case Cell.ROTATE_270: {
- float tempV = vertices[V1];
- vertices[V1] = vertices[V4];
- vertices[V4] = vertices[V3];
- vertices[V3] = vertices[V2];
- vertices[V2] = tempV;
-
- float tempU = vertices[U1];
- vertices[U1] = vertices[U4];
- vertices[U4] = vertices[U3];
- vertices[U3] = vertices[U2];
- vertices[U2] = tempU;
- break;
- }
- }
- }
- spriteBatch.draw(region.getTexture(), vertices, 0, 20);
- }
- }
- }
- }
-
- }
-
- public class OrthogonalTiledMapRenderer extends BatchTiledMapRenderer {
-
- private float[] vertices = new float[20];
-
- public OrthogonalTiledMapRenderer(TiledMap map) {
- super(map);
- }
-
- public OrthogonalTiledMapRenderer(TiledMap map, float unitScale) {
- super(map, unitScale);
- }
-
- @Override
- public void renderObject (MapObject object) {
-
- }
-
- @Override
- public void renderTileLayer (TiledMapTileLayer layer) {
-
- final float color = Color.toFloatBits(1, 1, 1, layer.getOpacity());
-
- final int layerWidth = layer.getWidth();
- final int layerHeight = layer.getHeight();
-
- final float layerTileWidth = layer.getTileWidth() * unitScale;
- final float layerTileHeight = layer.getTileHeight() * unitScale;
-
- final int col1 = Math.max(0, (int) (viewBounds.x / layerTileWidth));
- final int col2 = Math.min(layerWidth, (int) ((viewBounds.x + viewBounds.width + layerTileWidth) / layerTileWidth));
-
- final int row1 = Math.max(0, (int) (viewBounds.y / layerTileHeight));
- final int row2 = Math.min(layerHeight, (int) ((viewBounds.y + viewBounds.height + layerTileHeight) / layerTileHeight));
-
- for (int row = row1; row < row2; row++) {
- for (int col = col1; col < col2; col++) {
- final TiledMapTileLayer.Cell cell = layer.getCell(col, row);
- final TiledMapTile tile = cell.getTile();
- if (tile != null) {
-
- final boolean flipX = cell.getFlipHorizontally();
- final boolean flipY = cell.getFlipVertically();
- final int rotations = cell.getRotation();
-
- TextureRegion region = tile.getTextureRegion();
-
- float x1 = col * layerTileWidth;
- float y1 = row * layerTileHeight;
- float x2 = x1 + region.getRegionWidth() * unitScale;
- float y2 = y1 + region.getRegionHeight() * unitScale;
-
- float u1 = region.getU();
- float v1 = region.getV2();
- float u2 = region.getU2();
- float v2 = region.getV();
-
- vertices[X1] = x1;
- vertices[Y1] = y1;
- vertices[C1] = color;
- vertices[U1] = u1;
- vertices[V1] = v1;
-
- vertices[X2] = x1;
- vertices[Y2] = y2;
- vertices[C2] = color;
- vertices[U2] = u1;
- vertices[V2] = v2;
-
- vertices[X3] = x2;
- vertices[Y3] = y2;
- vertices[C3] = color;
- vertices[U3] = u2;
- vertices[V3] = v2;
-
- vertices[X4] = x2;
- vertices[Y4] = y1;
- vertices[C4] = color;
- vertices[U4] = u2;
- vertices[V4] = v1;
-
- if (flipX) {
- float temp = vertices[U1];
- vertices[U1] = vertices[U3];
- vertices[U3] = temp;
- temp = vertices[U2];
- vertices[U2] = vertices[U4];
- vertices[U4] = temp;
- }
- if (flipY) {
- float temp = vertices[V1];
- vertices[V1] = vertices[V3];
- vertices[V3] = temp;
- temp = vertices[V2];
- vertices[V2] = vertices[V4];
- vertices[V4] = temp;
- }
- if (rotations != 0) {
- switch (rotations) {
- case Cell.ROTATE_90: {
- float tempV = vertices[V1];
- vertices[V1] = vertices[V2];
- vertices[V2] = vertices[V3];
- vertices[V3] = vertices[V4];
- vertices[V4] = tempV;
-
- float tempU = vertices[U1];
- vertices[U1] = vertices[U2];
- vertices[U2] = vertices[U3];
- vertices[U3] = vertices[U4];
- vertices[U4] = tempU;
- break;
- }
- case Cell.ROTATE_180: {
- float tempU = vertices[U1];
- vertices[U1] = vertices[U3];
- vertices[U3] = tempU;
- tempU = vertices[U2];
- vertices[U2] = vertices[U4];
- vertices[U4] = tempU;
- float tempV = vertices[V1];
- vertices[V1] = vertices[V3];
- vertices[V3] = tempV;
- tempV = vertices[V2];
- vertices[V2] = vertices[V4];
- vertices[V4] = tempV;
- break;
- }
- case Cell.ROTATE_270: {
- float tempV = vertices[V1];
- vertices[V1] = vertices[V4];
- vertices[V4] = vertices[V3];
- vertices[V3] = vertices[V2];
- vertices[V2] = tempV;
-
- float tempU = vertices[U1];
- vertices[U1] = vertices[U4];
- vertices[U4] = vertices[U3];
- vertices[U3] = vertices[U2];
- vertices[U2] = tempU;
- break;
- }
- }
- }
- spriteBatch.draw(region.getTexture(), vertices, 0, 20);
- }
- }
- }
- }
-
- }
-
- public class OrthogonalTiledMapRenderer2 implements TiledMapRenderer2 {
-
- protected TiledMap map;
-
- protected float unitScale;
-
- protected SpriteCache spriteCache;
-
- protected Rectangle viewBounds;
-
- private float[] vertices = new float[20];
-
- public boolean recache;
-
- public OrthogonalTiledMapRenderer2(TiledMap map) {
- this.map = map;
- this.unitScale = 1;
- this.spriteCache = new SpriteCache(4000, true);
- this.viewBounds = new Rectangle();
- }
-
- public OrthogonalTiledMapRenderer2(TiledMap map, float unitScale) {
- this.map = map;
- this.unitScale = unitScale;
- this.viewBounds = new Rectangle();
- this.spriteCache = new SpriteCache(4000, true);
- }
-
- @Override
- public void setViewBounds (float x, float y, float width, float height) {
- viewBounds.set(x, y, width, height);
- }
-
- @Override
- public void setProjectionMatrix (Matrix4 projection) {
- spriteCache.setProjectionMatrix(projection);
- }
-
- @Override
- public void begin () {
- if (recache) {
- cached = false;
- recache = false;
- spriteCache.clear();
- }
- if (!cached) {
- spriteCache.beginCache();
- } else {
- Gdx.gl.glEnable(GL10.GL_BLEND);
- Gdx.gl.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
- spriteCache.begin();
- }
-
- }
-
- @Override
- public void end () {
- if (!cached) {
- spriteCache.endCache();
- cached = true;
- begin();
- render();
- end();
- } else {
- spriteCache.end();
- Gdx.gl.glDisable(GL10.GL_BLEND);
- }
- }
-
- @Override
- public void render () {
- if (cached) {
- spriteCache.draw(0);
- } else {
- for (MapLayer layer : map.getLayers()) {
- if (layer.getVisible()) {
- if (layer instanceof TiledMapTileLayer) {
- renderTileLayer((TiledMapTileLayer) layer);
- } else {
- for (MapObject object : layer.getObjects()) {
- renderObject(object);
- }
- }
- }
- }
- }
-
- }
-
- @Override
- public void renderObject (MapObject object) {
- // TODO Auto-generated method stub
-
- }
-
- boolean cached = false;
- int count = 0;
- @Override
- public void renderTileLayer (TiledMapTileLayer layer) {
- final float color = Color.toFloatBits(1, 1, 1, layer.getOpacity());
-
- final int layerWidth = layer.getWidth();
- final int layerHeight = layer.getHeight();
-
- final float layerTileWidth = layer.getTileWidth() * unitScale;
- final float layerTileHeight = layer.getTileHeight() * unitScale;
-
- final int col1 = Math.max(0, (int) (viewBounds.x / layerTileWidth));
- final int col2 = Math.min(layerWidth, (int) ((viewBounds.x + viewBounds.width + layerTileWidth) / layerTileWidth));
-
- final int row1 = Math.max(0, (int) (viewBounds.y / layerTileHeight));
- final int row2 = Math.min(layerHeight, (int) ((viewBounds.y + viewBounds.height + layerTileHeight) / layerTileHeight));
-
- for (int row = row1; row < row2; row++) {
- for (int col = col1; col < col2; col++) {
- final TiledMapTileLayer.Cell cell = layer.getCell(col, row);
- final TiledMapTile tile = cell.getTile();
- if (tile != null) {
- count++;
- final boolean flipX = cell.getFlipHorizontally();
- final boolean flipY = cell.getFlipVertically();
- final int rotations = cell.getRotation();
-
- TextureRegion region = tile.getTextureRegion();
-
- float x1 = col * layerTileWidth;
- float y1 = row * layerTileHeight;
- float x2 = x1 + region.getRegionWidth() * unitScale;
- float y2 = y1 + region.getRegionHeight() * unitScale;
-
- float u1 = region.getU();
- float v1 = region.getV2();
- float u2 = region.getU2();
- float v2 = region.getV();
-
- vertices[X1] = x1;
- vertices[Y1] = y1;
- vertices[C1] = color;
- vertices[U1] = u1;
- vertices[V1] = v1;
-
- vertices[X2] = x1;
- vertices[Y2] = y2;
- vertices[C2] = color;
- vertices[U2] = u1;
- vertices[V2] = v2;
-
- vertices[X3] = x2;
- vertices[Y3] = y2;
- vertices[C3] = color;
- vertices[U3] = u2;
- vertices[V3] = v2;
-
- vertices[X4] = x2;
- vertices[Y4] = y1;
- vertices[C4] = color;
- vertices[U4] = u2;
- vertices[V4] = v1;
-
- if (flipX) {
- float temp = vertices[U1];
- vertices[U1] = vertices[U3];
- vertices[U3] = temp;
- temp = vertices[U2];
- vertices[U2] = vertices[U4];
- vertices[U4] = temp;
- }
- if (flipY) {
- float temp = vertices[V1];
- vertices[V1] = vertices[V3];
- vertices[V3] = temp;
- temp = vertices[V2];
- vertices[V2] = vertices[V4];
- vertices[V4] = temp;
- }
- if (rotations != 0) {
- switch (rotations) {
- case Cell.ROTATE_90: {
- float tempV = vertices[V1];
- vertices[V1] = vertices[V2];
- vertices[V2] = vertices[V3];
- vertices[V3] = vertices[V4];
- vertices[V4] = tempV;
-
- float tempU = vertices[U1];
- vertices[U1] = vertices[U2];
- vertices[U2] = vertices[U3];
- vertices[U3] = vertices[U4];
- vertices[U4] = tempU;
- break;
- }
- case Cell.ROTATE_180: {
- float tempU = vertices[U1];
- vertices[U1] = vertices[U3];
- vertices[U3] = tempU;
- tempU = vertices[U2];
- vertices[U2] = vertices[U4];
- vertices[U4] = tempU;
- float tempV = vertices[V1];
- vertices[V1] = vertices[V3];
- vertices[V3] = tempV;
- tempV = vertices[V2];
- vertices[V2] = vertices[V4];
- vertices[V4] = tempV;
- break;
- }
- case Cell.ROTATE_270: {
- float tempV = vertices[V1];
- vertices[V1] = vertices[V4];
- vertices[V4] = vertices[V3];
- vertices[V3] = vertices[V2];
- vertices[V2] = tempV;
-
- float tempU = vertices[U1];
- vertices[U1] = vertices[U4];
- vertices[U4] = vertices[U3];
- vertices[U3] = vertices[U2];
- vertices[U2] = tempU;
- break;
- }
- }
- }
- spriteCache.add(region.getTexture(), vertices, 0, 20);
- }
- }
- }
- }
-
- }
-}