Search in sources :

Example 71 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException 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 72 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException 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)

Example 73 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException 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 74 with GdxRuntimeException

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

the class TmxMapLoader method loadAsync.

@Override
public void loadAsync(AssetManager manager, String fileName, FileHandle tmxFile, TmxMapLoader.Parameters parameter) {
    map = null;
    if (parameter != null) {
        convertObjectToTileSpace = parameter.convertObjectToTileSpace;
        flipY = parameter.flipY;
    } else {
        convertObjectToTileSpace = false;
        flipY = true;
    }
    try {
        map = loadTilemap(root, tmxFile, new AssetManagerImageResolver(manager));
    } catch (Exception e) {
        throw new GdxRuntimeException("Couldn't load tilemap '" + fileName + "'", e);
    }
}
Also used : AssetManagerImageResolver(com.badlogic.gdx.maps.ImageResolver.AssetManagerImageResolver) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) IOException(java.io.IOException) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException)

Example 75 with GdxRuntimeException

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

the class Bezier method set.

public Bezier set(final Array<T> points, final int offset, final int length) {
    if (length < 2 || length > 4)
        throw new GdxRuntimeException("Only first, second and third degree Bezier curves are supported.");
    if (tmp == null)
        tmp = points.get(0).cpy();
    this.points.clear();
    this.points.addAll(points, offset, length);
    return this;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException)

Aggregations

GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)202 IOException (java.io.IOException)40 FileHandle (com.badlogic.gdx.files.FileHandle)14 Array (com.badlogic.gdx.utils.Array)13 Texture (com.badlogic.gdx.graphics.Texture)12 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)11 AtlasRegion (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)9 InputStream (java.io.InputStream)9 Pixmap (com.badlogic.gdx.graphics.Pixmap)8 Sprite (com.badlogic.gdx.graphics.g2d.Sprite)8 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)7 AtlasSprite (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite)7 BufferedInputStream (java.io.BufferedInputStream)7 File (java.io.File)7 OutputStream (java.io.OutputStream)7 VertexAttribute (com.badlogic.gdx.graphics.VertexAttribute)6 LifecycleListener (com.badlogic.gdx.LifecycleListener)5 ByteBuffer (java.nio.ByteBuffer)5 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)4 NinePatch (com.badlogic.gdx.graphics.g2d.NinePatch)4