Search in sources :

Example 16 with FileHandle

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

the class TideMapLoader method loadTileSheets.

/** Loads the tilesets
	 * @param root the root XML element
	 * @return a list of filenames for images containing tiles
	 * @throws IOException */
private Array<FileHandle> loadTileSheets(Element root, FileHandle tideFile) throws IOException {
    Array<FileHandle> images = new Array<FileHandle>();
    Element tilesheets = root.getChildByName("TileSheets");
    for (Element tileset : tilesheets.getChildrenByName("TileSheet")) {
        Element imageSource = tileset.getChildByName("ImageSource");
        FileHandle image = getRelativeFileHandle(tideFile, imageSource.getText());
        images.add(image);
    }
    return images;
}
Also used : Array(com.badlogic.gdx.utils.Array) FileHandle(com.badlogic.gdx.files.FileHandle) Element(com.badlogic.gdx.utils.XmlReader.Element)

Example 17 with FileHandle

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

the class TideMapLoader method load.

public TiledMap load(String fileName) {
    try {
        FileHandle tideFile = resolve(fileName);
        root = xml.parse(tideFile);
        ObjectMap<String, Texture> textures = new ObjectMap<String, Texture>();
        for (FileHandle textureFile : loadTileSheets(root, tideFile)) {
            textures.put(textureFile.path(), new Texture(textureFile));
        }
        DirectImageResolver imageResolver = new DirectImageResolver(textures);
        TiledMap map = loadMap(root, tideFile, imageResolver);
        map.setOwnedResources(textures.values().toArray());
        return map;
    } catch (IOException e) {
        throw new GdxRuntimeException("Couldn't load tilemap '" + fileName + "'", e);
    }
}
Also used : DirectImageResolver(com.badlogic.gdx.maps.ImageResolver.DirectImageResolver) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) ObjectMap(com.badlogic.gdx.utils.ObjectMap) FileHandle(com.badlogic.gdx.files.FileHandle) IOException(java.io.IOException) Texture(com.badlogic.gdx.graphics.Texture)

Example 18 with FileHandle

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

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

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

the class TmxMapLoader method load.

/** Loads the {@link TiledMap} from the given file. The file is resolved via the {@link FileHandleResolver} set in the
	 * constructor of this class. By default it will resolve to an internal file.
	 * @param fileName the filename
	 * @param parameters specifies whether to use y-up, generate mip maps etc.
	 * @return the TiledMap */
public TiledMap load(String fileName, TmxMapLoader.Parameters parameters) {
    try {
        this.convertObjectToTileSpace = parameters.convertObjectToTileSpace;
        this.flipY = parameters.flipY;
        FileHandle tmxFile = resolve(fileName);
        root = xml.parse(tmxFile);
        ObjectMap<String, Texture> textures = new ObjectMap<String, Texture>();
        Array<FileHandle> textureFiles = loadTilesets(root, tmxFile);
        textureFiles.addAll(loadImages(root, tmxFile));
        for (FileHandle textureFile : textureFiles) {
            Texture texture = new Texture(textureFile, parameters.generateMipMaps);
            texture.setFilter(parameters.textureMinFilter, parameters.textureMagFilter);
            textures.put(textureFile.path(), texture);
        }
        DirectImageResolver imageResolver = new DirectImageResolver(textures);
        TiledMap map = loadTilemap(root, tmxFile, imageResolver);
        map.setOwnedResources(textures.values().toArray());
        return map;
    } catch (IOException e) {
        throw new GdxRuntimeException("Couldn't load tilemap '" + fileName + "'", e);
    }
}
Also used : DirectImageResolver(com.badlogic.gdx.maps.ImageResolver.DirectImageResolver) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) ObjectMap(com.badlogic.gdx.utils.ObjectMap) FileHandle(com.badlogic.gdx.files.FileHandle) IOException(java.io.IOException) Texture(com.badlogic.gdx.graphics.Texture)

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