use of com.badlogic.gdx.maps.tiled.TiledMapTileLayer in project libgdx by libgdx.
the class HexagonalTiledMapRenderer method init.
private void init(TiledMap map) {
String axis = map.getProperties().get("staggeraxis", String.class);
if (axis != null) {
if (axis.equals("x")) {
staggerAxisX = true;
} else {
staggerAxisX = false;
}
}
String index = map.getProperties().get("staggerindex", String.class);
if (index != null) {
if (index.equals("even")) {
staggerIndexEven = true;
} else {
staggerIndexEven = false;
}
}
Integer length = map.getProperties().get("hexsidelength", Integer.class);
if (length != null) {
hexSideLength = length.intValue();
} else {
if (staggerAxisX) {
length = map.getProperties().get("tilewidth", Integer.class);
if (length != null) {
hexSideLength = 0.5f * length.intValue();
} else {
TiledMapTileLayer tmtl = (TiledMapTileLayer) map.getLayers().get(0);
hexSideLength = 0.5f * tmtl.getTileWidth();
}
} else {
length = map.getProperties().get("tileheight", Integer.class);
if (length != null) {
hexSideLength = 0.5f * length.intValue();
} else {
TiledMapTileLayer tmtl = (TiledMapTileLayer) map.getLayers().get(0);
hexSideLength = 0.5f * tmtl.getTileHeight();
}
}
}
}
use of com.badlogic.gdx.maps.tiled.TiledMapTileLayer in project libgdx by libgdx.
the class IsometricTiledMapRenderer method renderTileLayer.
@Override
public void renderTileLayer(TiledMapTileLayer layer) {
final Color batchColor = batch.getColor();
final float color = Color.toFloatBits(batchColor.r, batchColor.g, batchColor.b, batchColor.a * layer.getOpacity());
float tileWidth = layer.getTileWidth() * unitScale;
float tileHeight = layer.getTileHeight() * unitScale;
final float layerOffsetX = layer.getOffsetX() * unitScale;
// offset in tiled is y down, so we flip it
final float layerOffsetY = -layer.getOffsetY() * unitScale;
float halfTileWidth = tileWidth * 0.5f;
float halfTileHeight = tileHeight * 0.5f;
// setting up the screen points
// COL1
topRight.set(viewBounds.x + viewBounds.width - layerOffsetX, viewBounds.y - layerOffsetY);
// COL2
bottomLeft.set(viewBounds.x - layerOffsetX, viewBounds.y + viewBounds.height - layerOffsetY);
// ROW1
topLeft.set(viewBounds.x - layerOffsetX, viewBounds.y - layerOffsetY);
// ROW2
bottomRight.set(viewBounds.x + viewBounds.width - layerOffsetX, viewBounds.y + viewBounds.height - layerOffsetY);
// transforming screen coordinates to iso coordinates
int row1 = (int) (translateScreenToIso(topLeft).y / tileWidth) - 2;
int row2 = (int) (translateScreenToIso(bottomRight).y / tileWidth) + 2;
int col1 = (int) (translateScreenToIso(bottomLeft).x / tileWidth) - 2;
int col2 = (int) (translateScreenToIso(topRight).x / tileWidth) + 2;
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);
if (cell == null)
continue;
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 + tile.getOffsetX() * unitScale + layerOffsetX;
float y1 = y + tile.getOffsetY() * unitScale + layerOffsetY;
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;
}
}
}
batch.draw(region.getTexture(), vertices, 0, NUM_VERTICES);
}
}
}
}
use of com.badlogic.gdx.maps.tiled.TiledMapTileLayer in project libgdx by libgdx.
the class OrthoCachedTiledMapRenderer method renderTileLayer.
@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 float layerOffsetX = layer.getOffsetX() * unitScale;
// offset in tiled is y down, so we flip it
final float layerOffsetY = -layer.getOffsetY() * unitScale;
final int col1 = Math.max(0, (int) ((cacheBounds.x - layerOffsetX) / layerTileWidth));
final int col2 = Math.min(layerWidth, (int) ((cacheBounds.x + cacheBounds.width + layerTileWidth - layerOffsetX) / layerTileWidth));
final int row1 = Math.max(0, (int) ((cacheBounds.y - layerOffsetY) / layerTileHeight));
final int row2 = Math.min(layerHeight, (int) ((cacheBounds.y + cacheBounds.height + layerTileHeight - layerOffsetY) / layerTileHeight));
canCacheMoreN = row2 < layerHeight;
canCacheMoreE = col2 < layerWidth;
canCacheMoreW = col1 > 0;
canCacheMoreS = row1 > 0;
float[] vertices = this.vertices;
for (int row = row2; row >= row1; row--) {
for (int col = col1; col < col2; col++) {
final TiledMapTileLayer.Cell cell = layer.getCell(col, row);
if (cell == null)
continue;
final TiledMapTile tile = cell.getTile();
if (tile == null)
continue;
count++;
final boolean flipX = cell.getFlipHorizontally();
final boolean flipY = cell.getFlipVertically();
final int rotations = cell.getRotation();
final TextureRegion region = tile.getTextureRegion();
final Texture texture = region.getTexture();
final float x1 = col * layerTileWidth + tile.getOffsetX() * unitScale + layerOffsetX;
final float y1 = row * layerTileHeight + tile.getOffsetY() * unitScale + layerOffsetY;
final float x2 = x1 + region.getRegionWidth() * unitScale;
final float y2 = y1 + region.getRegionHeight() * unitScale;
final float adjustX = 0.5f / texture.getWidth();
final float adjustY = 0.5f / texture.getHeight();
final float u1 = region.getU() + adjustX;
final float v1 = region.getV2() - adjustY;
final float u2 = region.getU2() - adjustX;
final float v2 = region.getV() + adjustY;
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(texture, vertices, 0, NUM_VERTICES);
}
}
}
use of com.badlogic.gdx.maps.tiled.TiledMapTileLayer in project libgdx by libgdx.
the class OrthoCachedTiledMapRenderer method render.
@Override
public void render(int[] layers) {
if (!cached) {
cached = true;
count = 0;
spriteCache.clear();
final float extraWidth = viewBounds.width * overCache;
final float extraHeight = viewBounds.height * overCache;
cacheBounds.x = viewBounds.x - extraWidth;
cacheBounds.y = viewBounds.y - extraHeight;
cacheBounds.width = viewBounds.width + extraWidth * 2;
cacheBounds.height = viewBounds.height + extraHeight * 2;
for (MapLayer layer : map.getLayers()) {
spriteCache.beginCache();
if (layer instanceof TiledMapTileLayer) {
renderTileLayer((TiledMapTileLayer) layer);
} else if (layer instanceof TiledMapImageLayer) {
renderImageLayer((TiledMapImageLayer) layer);
}
spriteCache.endCache();
}
}
if (blending) {
Gdx.gl.glEnable(GL20.GL_BLEND);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
}
spriteCache.begin();
MapLayers mapLayers = map.getLayers();
for (int i : layers) {
MapLayer layer = mapLayers.get(i);
if (layer.isVisible()) {
spriteCache.draw(i);
renderObjects(layer);
}
}
spriteCache.end();
if (blending)
Gdx.gl.glDisable(GL20.GL_BLEND);
}
use of com.badlogic.gdx.maps.tiled.TiledMapTileLayer in project libgdx by libgdx.
the class OrthogonalTiledMapRenderer method renderTileLayer.
@Override
public void renderTileLayer(TiledMapTileLayer layer) {
final Color batchColor = batch.getColor();
final float color = Color.toFloatBits(batchColor.r, batchColor.g, batchColor.b, batchColor.a * 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 float layerOffsetX = layer.getOffsetX() * unitScale;
// offset in tiled is y down, so we flip it
final float layerOffsetY = -layer.getOffsetY() * unitScale;
final int col1 = Math.max(0, (int) ((viewBounds.x - layerOffsetX) / layerTileWidth));
final int col2 = Math.min(layerWidth, (int) ((viewBounds.x + viewBounds.width + layerTileWidth - layerOffsetX) / layerTileWidth));
final int row1 = Math.max(0, (int) ((viewBounds.y - layerOffsetY) / layerTileHeight));
final int row2 = Math.min(layerHeight, (int) ((viewBounds.y + viewBounds.height + layerTileHeight - layerOffsetY) / layerTileHeight));
float y = row2 * layerTileHeight + layerOffsetY;
float xStart = col1 * layerTileWidth + layerOffsetX;
final float[] vertices = this.vertices;
for (int row = row2; row >= row1; row--) {
float x = xStart;
for (int col = col1; col < col2; col++) {
final TiledMapTileLayer.Cell cell = layer.getCell(col, row);
if (cell == null) {
x += layerTileWidth;
continue;
}
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 + tile.getOffsetX() * unitScale;
float y1 = y + tile.getOffsetY() * unitScale;
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;
}
}
}
batch.draw(region.getTexture(), vertices, 0, NUM_VERTICES);
}
x += layerTileWidth;
}
y -= layerTileHeight;
}
}
Aggregations