Search in sources :

Example 21 with FileHandle

use of com.badlogic.gdx.files.FileHandle 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 22 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project libgdx by libgdx.

the class AtlasTmxMapLoader method loadAtlas.

/** May return null. */
protected FileHandle loadAtlas(Element root, FileHandle tmxFile) throws IOException {
    Element e = root.getChildByName("properties");
    if (e != null) {
        for (Element property : e.getChildrenByName("property")) {
            String name = property.getAttribute("name", null);
            String value = property.getAttribute("value", null);
            if (name.equals("atlas")) {
                if (value == null) {
                    value = property.getText();
                }
                if (value == null || value.length() == 0) {
                    // keep trying until there are no more atlas properties
                    continue;
                }
                return getRelativeFileHandle(tmxFile, value);
            }
        }
    }
    FileHandle atlasFile = tmxFile.sibling(tmxFile.nameWithoutExtension() + ".atlas");
    return atlasFile.exists() ? atlasFile : null;
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) Element(com.badlogic.gdx.utils.XmlReader.Element)

Example 23 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project libgdx by libgdx.

the class ExternalMusicTest method create.

@Override
public void create() {
    // copy an internal mp3 to the external storage
    FileHandle src = Gdx.files.internal("data/8.12.mp3");
    FileHandle dst = Gdx.files.external("8.12.mp3");
    src.copyTo(dst);
    // create a music instance and start playback
    Music music = Gdx.audio.newMusic(dst);
    music.play();
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) Music(com.badlogic.gdx.audio.Music)

Example 24 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project libgdx by libgdx.

the class FilesTest method testInternal.

private void testInternal() throws IOException {
    FileHandle handle = Gdx.files.internal("data/badlogic.jpg");
    if (!handle.exists())
        fail("Couldn't find internal file");
    if (handle.isDirectory())
        fail("Internal file shouldn't be a directory");
    try {
        handle.delete();
        fail("Shouldn't be able to delete internal file");
    } catch (Exception expected) {
    }
    if (handle.list().length != 0)
        fail("File length shouldn't be 0");
    if (Gdx.app.getType() != ApplicationType.Android) {
        if (!handle.parent().exists())
            fail("Parent doesn't exist");
    }
    try {
        handle.read().close();
        fail();
    } catch (Exception ignored) {
    }
    FileHandle dir;
    if (Gdx.app.getType() == ApplicationType.Android || Gdx.app.getType() == ApplicationType.iOS)
        dir = Gdx.files.internal("data");
    else
        dir = Gdx.files.internal("../gdx-tests-android/assets/data");
    if (Gdx.app.getType() != ApplicationType.Android) {
        if (!dir.exists())
            fail();
    }
    if (!dir.isDirectory())
        fail();
    if (dir.list().length == 0)
        fail();
    FileHandle child = dir.child("badlogic.jpg");
    if (!child.name().equals("badlogic.jpg"))
        fail();
    if (!child.nameWithoutExtension().equals("badlogic"))
        fail();
    if (!child.extension().equals("jpg"))
        fail();
    if (Gdx.app.getType() != ApplicationType.Android) {
        if (!child.parent().exists())
            fail();
    }
    FileHandle copy = Gdx.files.external("badlogic.jpg-copy");
    copy.delete();
    if (copy.exists())
        fail();
    handle.copyTo(copy);
    if (!copy.exists())
        fail();
    if (copy.length() != 68465)
        fail();
    copy.delete();
    if (copy.exists())
        fail();
    handle.read().close();
    if (handle.readBytes().length != handle.length())
        fail();
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) IOException(java.io.IOException) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException)

Example 25 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project libgdx by libgdx.

the class FilesTest method testClasspath.

private void testClasspath() throws IOException {
    // no classpath support on ios
    if (Gdx.app.getType() == ApplicationType.iOS)
        return;
    FileHandle handle = Gdx.files.classpath("com/badlogic/gdx/utils/arial-15.png");
    if (!handle.exists())
        fail();
    if (handle.isDirectory())
        fail();
    try {
        handle.delete();
        fail();
    } catch (Exception expected) {
    }
    try {
        handle.list();
        fail();
    } catch (Exception expected) {
    }
    try {
        handle.read().close();
        fail();
    } catch (Exception ignored) {
    }
    FileHandle dir = Gdx.files.classpath("com/badlogic/gdx/utils");
    if (dir.isDirectory())
        fail();
    FileHandle child = dir.child("arial-15.fnt");
    if (!child.name().equals("arial-15.fnt"))
        fail();
    if (!child.nameWithoutExtension().equals("arial-15"))
        fail();
    if (!child.extension().equals("fnt"))
        fail();
    handle.read().close();
    if (handle.readBytes().length != handle.length())
        fail();
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) IOException(java.io.IOException) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException)

Aggregations

FileHandle (com.badlogic.gdx.files.FileHandle)79 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)23 IOException (java.io.IOException)22 Array (com.badlogic.gdx.utils.Array)18 File (java.io.File)18 Element (com.badlogic.gdx.utils.XmlReader.Element)9 Texture (com.badlogic.gdx.graphics.Texture)8 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)7 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)7 IntArray (com.badlogic.gdx.utils.IntArray)7 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)6 AssetDescriptor (com.badlogic.gdx.assets.AssetDescriptor)5 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)4 TextureAtlasData (com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData)4 Json (com.badlogic.gdx.utils.Json)4 GwtFileHandle (com.badlogic.gdx.backends.gwt.GwtFileHandle)3 Color (com.badlogic.gdx.graphics.Color)3 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 Writer (java.io.Writer)3