Search in sources :

Example 11 with Array

use of com.badlogic.gdx.utils.Array in project libgdx by libgdx.

the class TmxMapLoader method loadTilesets.

/** Loads the tilesets
	 * @param root the root XML element
	 * @return a list of filenames for images containing tiles
	 * @throws IOException */
protected Array<FileHandle> loadTilesets(Element root, FileHandle tmxFile) throws IOException {
    Array<FileHandle> images = new Array<FileHandle>();
    for (Element tileset : root.getChildrenByName("tileset")) {
        String source = tileset.getAttribute("source", null);
        if (source != null) {
            FileHandle tsxFile = getRelativeFileHandle(tmxFile, source);
            tileset = xml.parse(tsxFile);
            Element imageElement = tileset.getChildByName("image");
            if (imageElement != null) {
                String imageSource = tileset.getChildByName("image").getAttribute("source");
                FileHandle image = getRelativeFileHandle(tsxFile, imageSource);
                images.add(image);
            } else {
                for (Element tile : tileset.getChildrenByName("tile")) {
                    String imageSource = tile.getChildByName("image").getAttribute("source");
                    FileHandle image = getRelativeFileHandle(tsxFile, imageSource);
                    images.add(image);
                }
            }
        } else {
            Element imageElement = tileset.getChildByName("image");
            if (imageElement != null) {
                String imageSource = tileset.getChildByName("image").getAttribute("source");
                FileHandle image = getRelativeFileHandle(tmxFile, imageSource);
                images.add(image);
            } else {
                for (Element tile : tileset.getChildrenByName("tile")) {
                    String imageSource = tile.getChildByName("image").getAttribute("source");
                    FileHandle image = getRelativeFileHandle(tmxFile, imageSource);
                    images.add(image);
                }
            }
        }
    }
    return images;
}
Also used : Array(com.badlogic.gdx.utils.Array) IntArray(com.badlogic.gdx.utils.IntArray) FileHandle(com.badlogic.gdx.files.FileHandle) Element(com.badlogic.gdx.utils.XmlReader.Element)

Example 12 with Array

use of com.badlogic.gdx.utils.Array in project libgdx by libgdx.

the class TmxMapLoader method loadImages.

/** Loads the images in image layers
	 * @param root the root XML element
	 * @return a list of filenames for images inside image layers
	 * @throws IOException */
protected Array<FileHandle> loadImages(Element root, FileHandle tmxFile) throws IOException {
    Array<FileHandle> images = new Array<FileHandle>();
    for (Element imageLayer : root.getChildrenByName("imagelayer")) {
        Element image = imageLayer.getChildByName("image");
        String source = image.getAttribute("source", null);
        if (source != null) {
            FileHandle handle = getRelativeFileHandle(tmxFile, source);
            if (!images.contains(handle, false)) {
                images.add(handle);
            }
        }
    }
    return images;
}
Also used : Array(com.badlogic.gdx.utils.Array) IntArray(com.badlogic.gdx.utils.IntArray) FileHandle(com.badlogic.gdx.files.FileHandle) Element(com.badlogic.gdx.utils.XmlReader.Element)

Example 13 with Array

use of com.badlogic.gdx.utils.Array in project libgdx by libgdx.

the class TmxMapLoader method loadTileSet.

/** Loads the specified tileset data, adding it to the collection of the specified map, given the XML element, the tmxFile and
	 * an {@link ImageResolver} used to retrieve the tileset Textures.
	 * 
	 * <p>
	 * Default tileset's property keys that are loaded by default are:
	 * </p>
	 * 
	 * <ul>
	 * <li><em>firstgid</em>, (int, defaults to 1) the first valid global id used for tile numbering</li>
	 * <li><em>imagesource</em>, (String, defaults to empty string) the tileset source image filename</li>
	 * <li><em>imagewidth</em>, (int, defaults to 0) the tileset source image width</li>
	 * <li><em>imageheight</em>, (int, defaults to 0) the tileset source image height</li>
	 * <li><em>tilewidth</em>, (int, defaults to 0) the tile width</li>
	 * <li><em>tileheight</em>, (int, defaults to 0) the tile height</li>
	 * <li><em>margin</em>, (int, defaults to 0) the tileset margin</li>
	 * <li><em>spacing</em>, (int, defaults to 0) the tileset spacing</li>
	 * </ul>
	 * 
	 * <p>
	 * The values are extracted from the specified Tmx file, if a value can't be found then the default is used.
	 * </p>
	 * @param map the Map whose tilesets collection will be populated
	 * @param element the XML element identifying the tileset to load
	 * @param tmxFile the Filehandle of the tmx file
	 * @param imageResolver the {@link ImageResolver} */
protected void loadTileSet(TiledMap map, Element element, FileHandle tmxFile, ImageResolver imageResolver) {
    if (element.getName().equals("tileset")) {
        String name = element.get("name", null);
        int firstgid = element.getIntAttribute("firstgid", 1);
        int tilewidth = element.getIntAttribute("tilewidth", 0);
        int tileheight = element.getIntAttribute("tileheight", 0);
        int spacing = element.getIntAttribute("spacing", 0);
        int margin = element.getIntAttribute("margin", 0);
        String source = element.getAttribute("source", null);
        int offsetX = 0;
        int offsetY = 0;
        String imageSource = "";
        int imageWidth = 0, imageHeight = 0;
        FileHandle image = null;
        if (source != null) {
            FileHandle tsx = getRelativeFileHandle(tmxFile, source);
            try {
                element = xml.parse(tsx);
                name = element.get("name", null);
                tilewidth = element.getIntAttribute("tilewidth", 0);
                tileheight = element.getIntAttribute("tileheight", 0);
                spacing = element.getIntAttribute("spacing", 0);
                margin = element.getIntAttribute("margin", 0);
                Element offset = element.getChildByName("tileoffset");
                if (offset != null) {
                    offsetX = offset.getIntAttribute("x", 0);
                    offsetY = offset.getIntAttribute("y", 0);
                }
                Element imageElement = element.getChildByName("image");
                if (imageElement != null) {
                    imageSource = imageElement.getAttribute("source");
                    imageWidth = imageElement.getIntAttribute("width", 0);
                    imageHeight = imageElement.getIntAttribute("height", 0);
                    image = getRelativeFileHandle(tsx, imageSource);
                }
            } catch (IOException e) {
                throw new GdxRuntimeException("Error parsing external tileset.");
            }
        } else {
            Element offset = element.getChildByName("tileoffset");
            if (offset != null) {
                offsetX = offset.getIntAttribute("x", 0);
                offsetY = offset.getIntAttribute("y", 0);
            }
            Element imageElement = element.getChildByName("image");
            if (imageElement != null) {
                imageSource = imageElement.getAttribute("source");
                imageWidth = imageElement.getIntAttribute("width", 0);
                imageHeight = imageElement.getIntAttribute("height", 0);
                image = getRelativeFileHandle(tmxFile, imageSource);
            }
        }
        TiledMapTileSet tileset = new TiledMapTileSet();
        tileset.setName(name);
        tileset.getProperties().put("firstgid", firstgid);
        if (image != null) {
            TextureRegion texture = imageResolver.getImage(image.path());
            MapProperties props = tileset.getProperties();
            props.put("imagesource", imageSource);
            props.put("imagewidth", imageWidth);
            props.put("imageheight", imageHeight);
            props.put("tilewidth", tilewidth);
            props.put("tileheight", tileheight);
            props.put("margin", margin);
            props.put("spacing", spacing);
            int stopWidth = texture.getRegionWidth() - tilewidth;
            int stopHeight = texture.getRegionHeight() - tileheight;
            int id = firstgid;
            for (int y = margin; y <= stopHeight; y += tileheight + spacing) {
                for (int x = margin; x <= stopWidth; x += tilewidth + spacing) {
                    TextureRegion tileRegion = new TextureRegion(texture, x, y, tilewidth, tileheight);
                    TiledMapTile tile = new StaticTiledMapTile(tileRegion);
                    tile.setId(id);
                    tile.setOffsetX(offsetX);
                    tile.setOffsetY(flipY ? -offsetY : offsetY);
                    tileset.putTile(id++, tile);
                }
            }
        } else {
            Array<Element> tileElements = element.getChildrenByName("tile");
            for (Element tileElement : tileElements) {
                Element imageElement = tileElement.getChildByName("image");
                if (imageElement != null) {
                    imageSource = imageElement.getAttribute("source");
                    imageWidth = imageElement.getIntAttribute("width", 0);
                    imageHeight = imageElement.getIntAttribute("height", 0);
                    if (source != null) {
                        image = getRelativeFileHandle(getRelativeFileHandle(tmxFile, source), imageSource);
                    } else {
                        image = getRelativeFileHandle(tmxFile, imageSource);
                    }
                }
                TextureRegion texture = imageResolver.getImage(image.path());
                TiledMapTile tile = new StaticTiledMapTile(texture);
                tile.setId(firstgid + tileElement.getIntAttribute("id"));
                tile.setOffsetX(offsetX);
                tile.setOffsetY(flipY ? -offsetY : offsetY);
                tileset.putTile(tile.getId(), tile);
            }
        }
        Array<Element> tileElements = element.getChildrenByName("tile");
        Array<AnimatedTiledMapTile> animatedTiles = new Array<AnimatedTiledMapTile>();
        for (Element tileElement : tileElements) {
            int localtid = tileElement.getIntAttribute("id", 0);
            TiledMapTile tile = tileset.getTile(firstgid + localtid);
            if (tile != null) {
                Element animationElement = tileElement.getChildByName("animation");
                if (animationElement != null) {
                    Array<StaticTiledMapTile> staticTiles = new Array<StaticTiledMapTile>();
                    IntArray intervals = new IntArray();
                    for (Element frameElement : animationElement.getChildrenByName("frame")) {
                        staticTiles.add((StaticTiledMapTile) tileset.getTile(firstgid + frameElement.getIntAttribute("tileid")));
                        intervals.add(frameElement.getIntAttribute("duration"));
                    }
                    AnimatedTiledMapTile animatedTile = new AnimatedTiledMapTile(intervals, staticTiles);
                    animatedTile.setId(tile.getId());
                    animatedTiles.add(animatedTile);
                    tile = animatedTile;
                }
                String terrain = tileElement.getAttribute("terrain", null);
                if (terrain != null) {
                    tile.getProperties().put("terrain", terrain);
                }
                String probability = tileElement.getAttribute("probability", null);
                if (probability != null) {
                    tile.getProperties().put("probability", probability);
                }
                Element properties = tileElement.getChildByName("properties");
                if (properties != null) {
                    loadProperties(tile.getProperties(), properties);
                }
            }
        }
        for (AnimatedTiledMapTile tile : animatedTiles) {
            tileset.putTile(tile.getId(), tile);
        }
        Element properties = element.getChildByName("properties");
        if (properties != null) {
            loadProperties(tileset.getProperties(), properties);
        }
        map.getTileSets().addTileSet(tileset);
    }
}
Also used : AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) FileHandle(com.badlogic.gdx.files.FileHandle) Element(com.badlogic.gdx.utils.XmlReader.Element) MapProperties(com.badlogic.gdx.maps.MapProperties) IOException(java.io.IOException) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Array(com.badlogic.gdx.utils.Array) IntArray(com.badlogic.gdx.utils.IntArray) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) IntArray(com.badlogic.gdx.utils.IntArray) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile)

Example 14 with Array

use of com.badlogic.gdx.utils.Array in project libgdx by libgdx.

the class ViewportTest1 method getViewports.

public static Array<Viewport> getViewports(Camera camera) {
    int minWorldWidth = 640;
    int minWorldHeight = 480;
    int maxWorldWidth = 800;
    int maxWorldHeight = 480;
    Array<Viewport> viewports = new Array();
    viewports.add(new StretchViewport(minWorldWidth, minWorldHeight, camera));
    viewports.add(new FillViewport(minWorldWidth, minWorldHeight, camera));
    viewports.add(new FitViewport(minWorldWidth, minWorldHeight, camera));
    viewports.add(new ExtendViewport(minWorldWidth, minWorldHeight, camera));
    viewports.add(new ExtendViewport(minWorldWidth, minWorldHeight, maxWorldWidth, maxWorldHeight, camera));
    viewports.add(new ScreenViewport(camera));
    ScreenViewport screenViewport = new ScreenViewport(camera);
    screenViewport.setUnitsPerPixel(0.75f);
    viewports.add(screenViewport);
    viewports.add(new ScalingViewport(Scaling.none, minWorldWidth, minWorldHeight, camera));
    return viewports;
}
Also used : Array(com.badlogic.gdx.utils.Array) FillViewport(com.badlogic.gdx.utils.viewport.FillViewport) ExtendViewport(com.badlogic.gdx.utils.viewport.ExtendViewport) StretchViewport(com.badlogic.gdx.utils.viewport.StretchViewport) ExtendViewport(com.badlogic.gdx.utils.viewport.ExtendViewport) ScalingViewport(com.badlogic.gdx.utils.viewport.ScalingViewport) FitViewport(com.badlogic.gdx.utils.viewport.FitViewport) Viewport(com.badlogic.gdx.utils.viewport.Viewport) FillViewport(com.badlogic.gdx.utils.viewport.FillViewport) ScreenViewport(com.badlogic.gdx.utils.viewport.ScreenViewport) ScreenViewport(com.badlogic.gdx.utils.viewport.ScreenViewport) StretchViewport(com.badlogic.gdx.utils.viewport.StretchViewport) FitViewport(com.badlogic.gdx.utils.viewport.FitViewport) ScalingViewport(com.badlogic.gdx.utils.viewport.ScalingViewport)

Example 15 with Array

use of com.badlogic.gdx.utils.Array in project libgdx by libgdx.

the class FreeTypeIncrementalTest method create.

public void create() {
    batch = new SpriteBatch();
    shapes = new ShapeRenderer();
    shapes.setColor(Color.RED);
    FreeTypeFontGenerator.setMaxTextureSize(128);
    generator = new FreeTypeFontGenerator(Gdx.files.internal("data/arial.ttf"));
    FreeTypeFontParameter param = new FreeTypeFontParameter();
    param.incremental = true;
    param.size = 24;
    param.characters = "howdY";
    FreeTypeBitmapFontData data = new FreeTypeBitmapFontData() {

        public int getWrapIndex(Array<Glyph> glyphs, int start) {
            return SimplifiedChinese.getWrapIndex(glyphs, start);
        }
    };
    // By default latin chars are used for x and cap height, causing some fonts to display non-latin chars out of bounds.
    data.xChars = new char[] { '动' };
    data.capChars = new char[] { '动' };
    font = generator.generateFont(param, data);
}
Also used : Array(com.badlogic.gdx.utils.Array) FreeTypeFontGenerator(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator) FreeTypeFontParameter(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter) FreeTypeBitmapFontData(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeBitmapFontData) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) ShapeRenderer(com.badlogic.gdx.graphics.glutils.ShapeRenderer)

Aggregations

Array (com.badlogic.gdx.utils.Array)67 FileHandle (com.badlogic.gdx.files.FileHandle)18 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)16 AssetDescriptor (com.badlogic.gdx.assets.AssetDescriptor)9 Texture (com.badlogic.gdx.graphics.Texture)8 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)7 IntArray (com.badlogic.gdx.utils.IntArray)7 Element (com.badlogic.gdx.utils.XmlReader.Element)7 IOException (java.io.IOException)7 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)5 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)5 JsonValue (com.badlogic.gdx.utils.JsonValue)5 ObjectMap (com.badlogic.gdx.utils.ObjectMap)5 Color (com.badlogic.gdx.graphics.Color)4 ModelMaterial (com.badlogic.gdx.graphics.g3d.model.data.ModelMaterial)4 Page (com.badlogic.gdx.tools.texturepacker.TexturePacker.Page)4 Rect (com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect)4 Json (com.badlogic.gdx.utils.Json)4 TextureParameter (com.badlogic.gdx.assets.loaders.TextureLoader.TextureParameter)3 GwtFileHandle (com.badlogic.gdx.backends.gwt.GwtFileHandle)3