Example usage for com.badlogic.gdx.maps.tiled TiledMapTileLayer getOpacity

List of usage examples for com.badlogic.gdx.maps.tiled TiledMapTileLayer getOpacity

Introduction

In this page you can find the example usage for com.badlogic.gdx.maps.tiled TiledMapTileLayer getOpacity.

Prototype

public float getOpacity() 

Source Link

Usage

From source file:br.com.animvs.koalory.entities.engine.graphics.tiles.TileRenderer.java

License:Apache License

@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) (cacheBounds.x / layerTileWidth));
    final int col2 = Math.min(layerWidth,
            (int) ((cacheBounds.x + cacheBounds.width + layerTileWidth) / layerTileWidth));

    final int row1 = Math.max(0, (int) (cacheBounds.y / layerTileHeight));
    final int row2 = Math.min(layerHeight,
            (int) ((cacheBounds.y + cacheBounds.height + layerTileHeight) / layerTileHeight));

    canCacheMoreN = row2 < layerHeight;
    canCacheMoreE = col2 < layerWidth;
    canCacheMoreW = col1 > 0;/*from   www . j  a  v  a  2 s  . c  o  m*/
    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;
            final float y1 = row * layerTileHeight + tile.getOffsetY() * unitScale;
            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, 20);
        }
    }
}

From source file:com.khoa.demo1.OrthogonalTiledMapRenderer2.java

License:Apache License

@Override
public void renderTileLayer(TiledMapTileLayer layer) {
    final Color batchColor = spriteBatch.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 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));

    float y = row1 * layerTileHeight;
    float xStart = col1 * layerTileWidth + displacement;
    final float[] vertices = this.vertices;

    for (int row = row1; row < row2; row++) {
        float x = xStart;
        for (int col = col1; col < col2; col++) {
            final Cell cell = layer.getCell(col, row);
            if (cell == null) {
                x += layerTileWidth;/*from  w  w w .jav a 2s  .  c  o  m*/
                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;
                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);
                x += layerTileWidth;
            }
        }
        y += layerTileHeight;
    }
}

From source file:com.khoa.demo1.OrthogonalTiledMapRenderer2.java

License:Apache License

public void renderTileLayer(TiledMapTileLayer layer, int xDisplacement) {
    final Color batchColor = spriteBatch.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 int col1 = Math.max(xDisplacement, (int) (viewBounds.x / layerTileWidth));
    final int col2 = Math.min(layerWidth + xDisplacement,
            (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));

    float y = row1 * layerTileHeight;
    float xStart = col1 * layerTileWidth + displacement;

    //        System.out.println(col1);
    //        System.out.println(col2);
    //        System.out.println(xStart);
    //        System.out.println(xDisplacement);

    final float[] vertices = this.vertices;

    for (int row = row1; row < row2; row++) {
        float x = xStart;
        for (int col = col1; col < col2; col++) {
            final Cell cell = layer.getCell(col, row);
            if (cell == null) {
                x += layerTileWidth;//  w  w  w .  java2 s.  c om
                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;
                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);
                x += layerTileWidth;
            }
        }
        y += layerTileHeight;
    }
}

From source file:com.letsrace.game.map.OrthogonalTiledMapRenderer.java

License:Apache License

@Override
public void renderTileLayer(TiledMapTileLayer layer) {
    final Color batchColor = spriteBatch.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 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));

    float y = row2 * layerTileHeight;
    float xStart = col1 * layerTileWidth;
    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;/*from   www.  j  a  v  a 2 s .c o  m*/
                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;
                    }
                    }
                }
                spriteBatch.draw(region.getTexture(), vertices, 0, 20);
            }
            x += layerTileWidth;
        }
        y -= layerTileHeight;
    }
}

From source file:com.mygdx.game.MyOrthogonalTiledMapRenderer.java

License:Apache License

@Override
public void renderTileLayer(TiledMapTileLayer layer) {
    final Color batchColor = spriteBatch.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 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));

    float y = row2 * layerTileHeight;
    float xStart = col1 * layerTileWidth;
    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;/*www  . ja  va2  s .  co m*/
                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();
                fixBleeding(region);

                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;
                    }
                    }
                }
                spriteBatch.draw(region.getTexture(), vertices, 0, 20);
            }
            x += layerTileWidth;
        }
        y -= layerTileHeight;
    }
}

From source file:com.stercore.code.net.dermetfan.utils.libgdx.maps.TmxMapWriter.java

License:Apache License

/** @param layer the {@link TiledMapTileLayer} to write in TMX format
 *  @param format the {@link Format} to use
 *  @return this {@link TmxMapWriter} */
public TmxMapWriter tmx(TiledMapTileLayer layer, Format format) throws IOException {
    element("layer");
    attribute("name", layer.getName());
    attribute("width", layer.getWidth());
    attribute("height", layer.getHeight());
    attribute("visible", layer.isVisible() ? 1 : 0);
    attribute("opacity", layer.getOpacity());

    tmx(layer.getProperties());//from   ww w  .ja  v  a  2s  .  c om

    element("data");
    if (format == XML) {
        attribute("encoding", "xml");
        for (int y = layer.getHeight() - 1; y > -1; y--)
            for (int x = 0; x < layer.getWidth(); x++) {
                Cell cell = layer.getCell(x, y);
                if (cell != null) {
                    TiledMapTile tile = cell.getTile();
                    if (tile == null)
                        continue;
                    element("tile");
                    attribute("gid", tile.getId());
                    pop();
                }
            }
    } else if (format == CSV) {
        attribute("encoding", "csv");
        StringBuilder csv = new StringBuilder();
        for (int y = layer.getHeight() - 1; y > -1; y--) {
            for (int x = 0; x < layer.getWidth(); x++) {
                Cell cell = layer.getCell(x, y);
                TiledMapTile tile = cell != null ? cell.getTile() : null;
                csv.append(tile != null ? tile.getId() : 0);
                if (x + 1 < layer.getWidth() || y - 1 > -1)
                    csv.append(',');
            }
            csv.append('\n');
        }
        append('\n').append(csv);
    } else if (format == Base64 || format == Base64Zlib || format == Base64Gzip) {
        attribute("encoding", "base64");
        if (format == Base64Zlib)
            attribute("compression", "zlib");
        else if (format == Base64Gzip)
            attribute("compression", "gzip");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStream out = format == Base64Zlib ? new DeflaterOutputStream(baos)
                : format == Base64Gzip ? new GZIPOutputStream(baos) : baos;
        final short LAST_BYTE = 0xFF;
        for (int y = layer.getHeight() - 1; y > -1; y--)
            for (int x = 0; x < layer.getWidth(); x++) {
                Cell cell = layer.getCell(x, y);
                TiledMapTile tile = cell != null ? cell.getTile() : null;
                int gid = tile != null ? tile.getId() : 0;
                out.write(gid & LAST_BYTE);
                out.write(gid >> 8 & LAST_BYTE);
                out.write(gid >> 16 & LAST_BYTE);
                out.write(gid >> 24 & LAST_BYTE);
            }
        if (out instanceof DeflaterOutputStream)
            ((DeflaterOutputStream) out).finish();
        out.close();
        baos.close();
        append('\n').append(String.valueOf(Base64Coder.encode(baos.toByteArray()))).append('\n');
    }
    pop();

    pop();
    return this;
}

From source file:de.dogedevs.photoria.rendering.map.CustomTiledMapRenderer.java

License:Apache License

@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 int col1 = Math.max(0, (int) (viewBounds.x / layerTileWidth));
    //      final int col1 = (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 row1 = (int)(viewBounds.y / layerTileHeight);
    final int row2 = Math.min(layerHeight,
            (int) ((viewBounds.y + viewBounds.height + layerTileHeight) / layerTileHeight));

    float y = row2 * layerTileHeight;
    float xStart = col1 * layerTileWidth;
    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;/*from   www  .ja v a2s  . c om*/
                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;
    }
}

From source file:de.fhkoeln.game.utils.libgdx.maps.TmxMapWriter.java

License:Apache License

/** @param layer the {@link com.badlogic.gdx.maps.tiled.TiledMapTileLayer} to write in TMX format
 *  @param format the {@link de.fhkoeln.game.utils.libgdx.maps.TmxMapWriter.Format} to use
 *  @return this {@link de.fhkoeln.game.utils.libgdx.maps.TmxMapWriter} */
public TmxMapWriter tmx(TiledMapTileLayer layer, Format format) throws IOException {
    element("layer");
    attribute("name", layer.getName());
    attribute("width", layer.getWidth());
    attribute("height", layer.getHeight());
    attribute("visible", layer.isVisible() ? 1 : 0);
    attribute("opacity", layer.getOpacity());

    tmx(layer.getProperties());/*from  w w  w  .ja  v a  2  s.  c  om*/

    element("data");
    if (format == Format.XML) {
        attribute("encoding", "xml");
        for (int y = layer.getHeight() - 1; y > -1; y--)
            for (int x = 0; x < layer.getWidth(); x++) {
                Cell cell = layer.getCell(x, y);
                if (cell != null) {
                    TiledMapTile tile = cell.getTile();
                    if (tile == null)
                        continue;
                    element("tile");
                    attribute("gid", tile.getId());
                    pop();
                }
            }
    } else if (format == Format.CSV) {
        attribute("encoding", "csv");
        StringBuilder csv = new StringBuilder();
        for (int y = layer.getHeight() - 1; y > -1; y--) {
            for (int x = 0; x < layer.getWidth(); x++) {
                Cell cell = layer.getCell(x, y);
                TiledMapTile tile = cell != null ? cell.getTile() : null;
                csv.append(tile != null ? tile.getId() : 0);
                if (x + 1 < layer.getWidth() || y - 1 > -1)
                    csv.append(',');
            }
            csv.append('\n');
        }
        append('\n').append(csv);
    } else if (format == Format.Base64 || format == Format.Base64Zlib || format == Format.Base64Gzip) {
        attribute("encoding", "base64");
        if (format == Format.Base64Zlib)
            attribute("compression", "zlib");
        else if (format == Format.Base64Gzip)
            attribute("compression", "gzip");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStream out = format == Format.Base64Zlib ? new DeflaterOutputStream(baos)
                : format == Format.Base64Gzip ? new GZIPOutputStream(baos) : baos;
        final short LAST_BYTE = 0xFF;
        for (int y = layer.getHeight() - 1; y > -1; y--)
            for (int x = 0; x < layer.getWidth(); x++) {
                Cell cell = layer.getCell(x, y);
                TiledMapTile tile = cell != null ? cell.getTile() : null;
                int gid = tile != null ? tile.getId() : 0;
                out.write(gid & LAST_BYTE);
                out.write(gid >> 8 & LAST_BYTE);
                out.write(gid >> 16 & LAST_BYTE);
                out.write(gid >> 24 & LAST_BYTE);
            }
        if (out instanceof DeflaterOutputStream)
            ((DeflaterOutputStream) out).finish();
        out.close();
        baos.close();
        append('\n').append(String.valueOf(Base64Coder.encode(baos.toByteArray()))).append('\n');
    }
    pop();

    pop();
    return this;
}

From source file:net.sleepystudios.bankvault.FixedTiledMapRenderer.java

License:Apache License

@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 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));

    float y = row2 * layerTileHeight;
    float xStart = col1 * layerTileWidth;
    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;/* w w  w.  j  a va 2 s . com*/
                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();
                fixBleeding(region);

                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;
    }
}