Search in sources :

Example 96 with TextureRegion

use of com.badlogic.gdx.graphics.g2d.TextureRegion in project libgdx by libgdx.

the class BaseTmxMapLoader method loadObject.

protected void loadObject(TiledMap map, MapLayer layer, Element element) {
    if (element.getName().equals("object")) {
        MapObject object = null;
        float scaleX = convertObjectToTileSpace ? 1.0f / mapTileWidth : 1.0f;
        float scaleY = convertObjectToTileSpace ? 1.0f / mapTileHeight : 1.0f;
        float x = element.getFloatAttribute("x", 0) * scaleX;
        float y = (flipY ? (mapHeightInPixels - element.getFloatAttribute("y", 0)) : element.getFloatAttribute("y", 0)) * scaleY;
        float width = element.getFloatAttribute("width", 0) * scaleX;
        float height = element.getFloatAttribute("height", 0) * scaleY;
        if (element.getChildCount() > 0) {
            Element child = null;
            if ((child = element.getChildByName("polygon")) != null) {
                String[] points = child.getAttribute("points").split(" ");
                float[] vertices = new float[points.length * 2];
                for (int i = 0; i < points.length; i++) {
                    String[] point = points[i].split(",");
                    vertices[i * 2] = Float.parseFloat(point[0]) * scaleX;
                    vertices[i * 2 + 1] = Float.parseFloat(point[1]) * scaleY * (flipY ? -1 : 1);
                }
                Polygon polygon = new Polygon(vertices);
                polygon.setPosition(x, y);
                object = new PolygonMapObject(polygon);
            } else if ((child = element.getChildByName("polyline")) != null) {
                String[] points = child.getAttribute("points").split(" ");
                float[] vertices = new float[points.length * 2];
                for (int i = 0; i < points.length; i++) {
                    String[] point = points[i].split(",");
                    vertices[i * 2] = Float.parseFloat(point[0]) * scaleX;
                    vertices[i * 2 + 1] = Float.parseFloat(point[1]) * scaleY * (flipY ? -1 : 1);
                }
                Polyline polyline = new Polyline(vertices);
                polyline.setPosition(x, y);
                object = new PolylineMapObject(polyline);
            } else if ((child = element.getChildByName("ellipse")) != null) {
                object = new EllipseMapObject(x, flipY ? y - height : y, width, height);
            }
        }
        if (object == null) {
            String gid = null;
            if ((gid = element.getAttribute("gid", null)) != null) {
                int id = (int) Long.parseLong(gid);
                boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0);
                boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0);
                TiledMapTile tile = map.getTileSets().getTile(id & ~MASK_CLEAR);
                TiledMapTileMapObject tiledMapTileMapObject = new TiledMapTileMapObject(tile, flipHorizontally, flipVertically);
                TextureRegion textureRegion = tiledMapTileMapObject.getTextureRegion();
                tiledMapTileMapObject.getProperties().put("gid", id);
                tiledMapTileMapObject.setX(x);
                tiledMapTileMapObject.setY(flipY ? y : y - height);
                float objectWidth = element.getFloatAttribute("width", textureRegion.getRegionWidth());
                float objectHeight = element.getFloatAttribute("height", textureRegion.getRegionHeight());
                tiledMapTileMapObject.setScaleX(scaleX * (objectWidth / textureRegion.getRegionWidth()));
                tiledMapTileMapObject.setScaleY(scaleY * (objectHeight / textureRegion.getRegionHeight()));
                tiledMapTileMapObject.setRotation(element.getFloatAttribute("rotation", 0));
                object = tiledMapTileMapObject;
            } else {
                object = new RectangleMapObject(x, flipY ? y - height : y, width, height);
            }
        }
        object.setName(element.getAttribute("name", null));
        String rotation = element.getAttribute("rotation", null);
        if (rotation != null) {
            object.getProperties().put("rotation", Float.parseFloat(rotation));
        }
        String type = element.getAttribute("type", null);
        if (type != null) {
            object.getProperties().put("type", type);
        }
        int id = element.getIntAttribute("id", 0);
        if (id != 0) {
            object.getProperties().put("id", id);
        }
        object.getProperties().put("x", x);
        if (object instanceof TiledMapTileMapObject) {
            object.getProperties().put("y", y);
        } else {
            object.getProperties().put("y", (flipY ? y - height : y));
        }
        object.getProperties().put("width", width);
        object.getProperties().put("height", height);
        object.setVisible(element.getIntAttribute("visible", 1) == 1);
        Element properties = element.getChildByName("properties");
        if (properties != null) {
            loadProperties(object.getProperties(), properties);
        }
        layer.getObjects().add(object);
    }
}
Also used : Element(com.badlogic.gdx.utils.XmlReader.Element) PolygonMapObject(com.badlogic.gdx.maps.objects.PolygonMapObject) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) RectangleMapObject(com.badlogic.gdx.maps.objects.RectangleMapObject) Polyline(com.badlogic.gdx.math.Polyline) EllipseMapObject(com.badlogic.gdx.maps.objects.EllipseMapObject) PolylineMapObject(com.badlogic.gdx.maps.objects.PolylineMapObject) TiledMapTileMapObject(com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject) Polygon(com.badlogic.gdx.math.Polygon) MapObject(com.badlogic.gdx.maps.MapObject) EllipseMapObject(com.badlogic.gdx.maps.objects.EllipseMapObject) PolygonMapObject(com.badlogic.gdx.maps.objects.PolygonMapObject) PolylineMapObject(com.badlogic.gdx.maps.objects.PolylineMapObject) TiledMapTileMapObject(com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject) RectangleMapObject(com.badlogic.gdx.maps.objects.RectangleMapObject)

Example 97 with TextureRegion

use of com.badlogic.gdx.graphics.g2d.TextureRegion in project libgdx by libgdx.

the class TideMapLoader method loadTileSheet.

private void loadTileSheet(TiledMap map, Element element, FileHandle tideFile, ImageResolver imageResolver) {
    if (element.getName().equals("TileSheet")) {
        String id = element.getAttribute("Id");
        String description = element.getChildByName("Description").getText();
        String imageSource = element.getChildByName("ImageSource").getText();
        Element alignment = element.getChildByName("Alignment");
        String sheetSize = alignment.getAttribute("SheetSize");
        String tileSize = alignment.getAttribute("TileSize");
        String margin = alignment.getAttribute("Margin");
        String spacing = alignment.getAttribute("Spacing");
        String[] sheetSizeParts = sheetSize.split(" x ");
        int sheetSizeX = Integer.parseInt(sheetSizeParts[0]);
        int sheetSizeY = Integer.parseInt(sheetSizeParts[1]);
        String[] tileSizeParts = tileSize.split(" x ");
        int tileSizeX = Integer.parseInt(tileSizeParts[0]);
        int tileSizeY = Integer.parseInt(tileSizeParts[1]);
        String[] marginParts = margin.split(" x ");
        int marginX = Integer.parseInt(marginParts[0]);
        int marginY = Integer.parseInt(marginParts[1]);
        String[] spacingParts = margin.split(" x ");
        int spacingX = Integer.parseInt(spacingParts[0]);
        int spacingY = Integer.parseInt(spacingParts[1]);
        FileHandle image = getRelativeFileHandle(tideFile, imageSource);
        TextureRegion texture = imageResolver.getImage(image.path());
        TiledMapTileSets tilesets = map.getTileSets();
        int firstgid = 1;
        for (TiledMapTileSet tileset : tilesets) {
            firstgid += tileset.size();
        }
        TiledMapTileSet tileset = new TiledMapTileSet();
        tileset.setName(id);
        tileset.getProperties().put("firstgid", firstgid);
        int gid = firstgid;
        int stopWidth = texture.getRegionWidth() - tileSizeX;
        int stopHeight = texture.getRegionHeight() - tileSizeY;
        for (int y = marginY; y <= stopHeight; y += tileSizeY + spacingY) {
            for (int x = marginX; x <= stopWidth; x += tileSizeX + spacingX) {
                TiledMapTile tile = new StaticTiledMapTile(new TextureRegion(texture, x, y, tileSizeX, tileSizeY));
                tile.setId(gid);
                tileset.putTile(gid++, tile);
            }
        }
        Element properties = element.getChildByName("Properties");
        if (properties != null) {
            loadProperties(tileset.getProperties(), properties);
        }
        tilesets.addTileSet(tileset);
    }
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) FileHandle(com.badlogic.gdx.files.FileHandle) Element(com.badlogic.gdx.utils.XmlReader.Element) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile)

Example 98 with TextureRegion

use of com.badlogic.gdx.graphics.g2d.TextureRegion in project libgdx by libgdx.

the class BatchTiledMapRenderer method renderImageLayer.

@Override
public void renderImageLayer(TiledMapImageLayer layer) {
    final Color batchColor = batch.getColor();
    final float color = Color.toFloatBits(batchColor.r, batchColor.g, batchColor.b, batchColor.a * layer.getOpacity());
    final float[] vertices = this.vertices;
    TextureRegion region = layer.getTextureRegion();
    if (region == null) {
        return;
    }
    final float x = layer.getX();
    final float y = layer.getY();
    final float x1 = x * unitScale;
    final float y1 = y * unitScale;
    final float x2 = x1 + region.getRegionWidth() * unitScale;
    final float y2 = y1 + region.getRegionHeight() * unitScale;
    imageBounds.set(x1, y1, x2 - x1, y2 - y1);
    if (viewBounds.contains(imageBounds) || viewBounds.overlaps(imageBounds)) {
        final float u1 = region.getU();
        final float v1 = region.getV2();
        final float u2 = region.getU2();
        final 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;
        batch.draw(region.getTexture(), vertices, 0, NUM_VERTICES);
    }
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) Color(com.badlogic.gdx.graphics.Color)

Example 99 with TextureRegion

use of com.badlogic.gdx.graphics.g2d.TextureRegion in project libgdx by libgdx.

the class HexagonalTiledMapRenderer method renderCell.

/** render a single cell */
private void renderCell(final TiledMapTileLayer.Cell cell, final float x, final float y, final float color) {
    if (cell != null) {
        final TiledMapTile tile = cell.getTile();
        if (tile != null) {
            if (tile instanceof AnimatedTiledMapTile)
                return;
            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 == 2) {
                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;
            }
            batch.draw(region.getTexture(), vertices, 0, NUM_VERTICES);
        }
    }
}
Also used : AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) TiledMapTile(com.badlogic.gdx.maps.tiled.TiledMapTile)

Example 100 with TextureRegion

use of com.badlogic.gdx.graphics.g2d.TextureRegion in project libgdx by libgdx.

the class IsometricStaggeredTiledMapRenderer 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 layerOffsetX = layer.getOffsetX() * unitScale;
    // offset in tiled is y down, so we flip it
    final float layerOffsetY = -layer.getOffsetY() * unitScale;
    final float layerTileWidth = layer.getTileWidth() * unitScale;
    final float layerTileHeight = layer.getTileHeight() * unitScale;
    final float layerTileWidth50 = layerTileWidth * 0.50f;
    final float layerTileHeight50 = layerTileHeight * 0.50f;
    final int minX = Math.max(0, (int) (((viewBounds.x - layerTileWidth50 - layerOffsetX) / layerTileWidth)));
    final int maxX = Math.min(layerWidth, (int) ((viewBounds.x + viewBounds.width + layerTileWidth + layerTileWidth50 - layerOffsetX) / layerTileWidth));
    final int minY = Math.max(0, (int) (((viewBounds.y - layerTileHeight - layerOffsetY) / layerTileHeight)));
    final int maxY = Math.min(layerHeight, (int) ((viewBounds.y + viewBounds.height + layerTileHeight - layerOffsetY) / layerTileHeight50));
    for (int y = maxY - 1; y >= minY; y--) {
        float offsetX = (y % 2 == 1) ? layerTileWidth50 : 0;
        for (int x = maxX - 1; x >= minX; x--) {
            final TiledMapTileLayer.Cell cell = layer.getCell(x, y);
            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 * layerTileWidth - offsetX + tile.getOffsetX() * unitScale + layerOffsetX;
                float y1 = y * layerTileHeight50 + 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);
            }
        }
    }
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) TiledMapTileLayer(com.badlogic.gdx.maps.tiled.TiledMapTileLayer) TiledMapTile(com.badlogic.gdx.maps.tiled.TiledMapTile) Color(com.badlogic.gdx.graphics.Color) Cell(com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell)

Aggregations

TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)115 Texture (com.badlogic.gdx.graphics.Texture)57 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)22 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)18 Stage (com.badlogic.gdx.scenes.scene2d.Stage)18 Sprite (com.badlogic.gdx.graphics.g2d.Sprite)13 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)13 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)11 NinePatch (com.badlogic.gdx.graphics.g2d.NinePatch)11 AtlasSprite (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite)11 AtlasRegion (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)10 Image (com.badlogic.gdx.scenes.scene2d.ui.Image)10 Skin (com.badlogic.gdx.scenes.scene2d.ui.Skin)10 Pixmap (com.badlogic.gdx.graphics.Pixmap)8 Color (com.badlogic.gdx.graphics.Color)7 ShapeRenderer (com.badlogic.gdx.graphics.glutils.ShapeRenderer)7 TextureRegionDrawable (com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable)7 Test (org.junit.Test)7 FileHandle (com.badlogic.gdx.files.FileHandle)6 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)6