Search in sources :

Example 1 with IntArray

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

the class TiledMapPacker method packTilesets.

/** Traverse the specified tilesets, optionally lookup the used ids and pass every tile image to the {@link TexturePacker},
	 * optionally ignoring unused tile ids */
private void packTilesets(FileHandle inputDirHandle, Settings texturePackerSettings) throws IOException {
    BufferedImage tile;
    Vector2 tileLocation;
    Graphics g;
    packer = new TexturePacker(texturePackerSettings);
    for (TiledMapTileSet set : tilesetsToPack.values()) {
        String tilesetName = set.getName();
        System.out.println("Processing tileset " + tilesetName);
        IntArray usedIds = this.settings.stripUnusedTiles ? getUsedIdsBucket(tilesetName, -1) : null;
        int tileWidth = set.getProperties().get("tilewidth", Integer.class);
        int tileHeight = set.getProperties().get("tileheight", Integer.class);
        int firstgid = set.getProperties().get("firstgid", Integer.class);
        String imageName = set.getProperties().get("imagesource", String.class);
        TileSetLayout layout = new TileSetLayout(firstgid, set, inputDirHandle);
        for (int gid = layout.firstgid, i = 0; i < layout.numTiles; gid++, i++) {
            boolean verbose = this.settings.verbose;
            if (usedIds != null && !usedIds.contains(gid)) {
                if (verbose) {
                    System.out.println("Stripped id #" + gid + " from tileset \"" + tilesetName + "\"");
                }
                continue;
            }
            tileLocation = layout.getLocation(gid);
            tile = new BufferedImage(tileWidth, tileHeight, BufferedImage.TYPE_4BYTE_ABGR);
            g = tile.createGraphics();
            g.drawImage(layout.image, 0, 0, tileWidth, tileHeight, (int) tileLocation.x, (int) tileLocation.y, (int) tileLocation.x + tileWidth, (int) tileLocation.y + tileHeight, null);
            if (verbose) {
                System.out.println("Adding " + tileWidth + "x" + tileHeight + " (" + (int) tileLocation.x + ", " + (int) tileLocation.y + ")");
            }
            // AtlasTmxMapLoader expects every tileset's index to begin at zero for the first tile in every tileset.
            // so the region's adjusted gid is (gid - layout.firstgid). firstgid will be added back in AtlasTmxMapLoader on load
            int adjustedGid = gid - layout.firstgid;
            final String separator = "_";
            String regionName = tilesetName + separator + adjustedGid;
            packer.addImage(tile, regionName);
        }
    }
    String tilesetOutputDir = outputDir.toString() + "/" + this.settings.tilesetOutputDirectory;
    File relativeTilesetOutputDir = new File(tilesetOutputDir);
    File outputDirTilesets = new File(relativeTilesetOutputDir.getCanonicalPath());
    outputDirTilesets.mkdirs();
    packer.pack(outputDirTilesets, this.settings.atlasOutputName + ".atlas");
}
Also used : Graphics(java.awt.Graphics) IntArray(com.badlogic.gdx.utils.IntArray) Vector2(com.badlogic.gdx.math.Vector2) TiledMapTileSet(com.badlogic.gdx.maps.tiled.TiledMapTileSet) TexturePacker(com.badlogic.gdx.tools.texturepacker.TexturePacker) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 2 with IntArray

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

the class Kerning method readCoverageTable.

private int[] readCoverageTable() throws IOException {
    int format = input.readUnsignedShort();
    if (format == 1) {
        int glyphCount = input.readUnsignedShort();
        int[] glyphArray = input.readUnsignedShortArray(glyphCount);
        return glyphArray;
    } else if (format == 2) {
        int rangeCount = input.readUnsignedShort();
        IntArray glyphArray = new IntArray();
        for (int i = 0; i < rangeCount; i++) {
            int start = input.readUnsignedShort();
            int end = input.readUnsignedShort();
            input.skip(2);
            for (int glyph = start; glyph <= end; glyph++) {
                glyphArray.add(glyph);
            }
        }
        return glyphArray.shrink();
    }
    throw new IOException("Unknown coverage table format " + format);
}
Also used : IntArray(com.badlogic.gdx.utils.IntArray) IOException(java.io.IOException)

Example 3 with IntArray

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

the class BitmapFontCache method setColors.

/** Sets the color of the specified characters. This may only be called after {@link #setText(CharSequence, float, float)} and
	 * is reset every time setText is called. */
public void setColors(float color, int start, int end) {
    if (pageVertices.length == 1) {
        // One page.
        float[] vertices = pageVertices[0];
        for (int i = start * 20 + 2, n = end * 20; i < n; i += 5) vertices[i] = color;
        return;
    }
    int pageCount = pageVertices.length;
    for (int i = 0; i < pageCount; i++) {
        float[] vertices = pageVertices[i];
        IntArray glyphIndices = pageGlyphIndices[i];
        // Loop through the indices and determine whether the glyph is inside begin/end.
        for (int j = 0, n = glyphIndices.size; j < n; j++) {
            int glyphIndex = glyphIndices.items[j];
            // Break early if the glyph is out of bounds.
            if (glyphIndex >= end)
                break;
            // If inside start and end, change its colour.
            if (glyphIndex >= start) {
                // && glyphIndex < end
                for (int off = 0; off < 20; off += 5) vertices[off + (j * 20 + 2)] = color;
            }
        }
    }
}
Also used : IntArray(com.badlogic.gdx.utils.IntArray)

Example 4 with IntArray

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

the class BitmapFontCache method addToCache.

private void addToCache(GlyphLayout layout, float x, float y) {
    // Check if the number of font pages has changed.
    int pageCount = font.regions.size;
    if (pageVertices.length < pageCount) {
        float[][] newPageVertices = new float[pageCount][];
        System.arraycopy(pageVertices, 0, newPageVertices, 0, pageVertices.length);
        pageVertices = newPageVertices;
        int[] newIdx = new int[pageCount];
        System.arraycopy(idx, 0, newIdx, 0, idx.length);
        idx = newIdx;
        IntArray[] newPageGlyphIndices = new IntArray[pageCount];
        int pageGlyphIndicesLength = 0;
        if (pageGlyphIndices != null) {
            pageGlyphIndicesLength = pageGlyphIndices.length;
            System.arraycopy(pageGlyphIndices, 0, newPageGlyphIndices, 0, pageGlyphIndices.length);
        }
        for (int i = pageGlyphIndicesLength; i < pageCount; i++) newPageGlyphIndices[i] = new IntArray();
        pageGlyphIndices = newPageGlyphIndices;
        tempGlyphCount = new int[pageCount];
    }
    layouts.add(layout);
    requireGlyphs(layout);
    for (int i = 0, n = layout.runs.size; i < n; i++) {
        GlyphRun run = layout.runs.get(i);
        Array<Glyph> glyphs = run.glyphs;
        FloatArray xAdvances = run.xAdvances;
        float color = run.color.toFloatBits();
        float gx = x + run.x, gy = y + run.y;
        for (int ii = 0, nn = glyphs.size; ii < nn; ii++) {
            Glyph glyph = glyphs.get(ii);
            gx += xAdvances.get(ii);
            addGlyph(glyph, gx, gy, color);
        }
    }
    // Cached glyphs have changed, reset the current tint.
    currentTint = whiteTint;
}
Also used : FloatArray(com.badlogic.gdx.utils.FloatArray) IntArray(com.badlogic.gdx.utils.IntArray) GlyphRun(com.badlogic.gdx.graphics.g2d.GlyphLayout.GlyphRun) Glyph(com.badlogic.gdx.graphics.g2d.BitmapFont.Glyph)

Example 5 with IntArray

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

Aggregations

IntArray (com.badlogic.gdx.utils.IntArray)22 IOException (java.io.IOException)4 FileHandle (com.badlogic.gdx.files.FileHandle)3 Array (com.badlogic.gdx.utils.Array)3 ParticleController (com.badlogic.gdx.graphics.g3d.particles.ParticleController)2 ParticleEffect (com.badlogic.gdx.graphics.g3d.particles.ParticleEffect)2 SaveData (com.badlogic.gdx.graphics.g3d.particles.ResourceData.SaveData)2 MapProperties (com.badlogic.gdx.maps.MapProperties)2 TiledMapTileSet (com.badlogic.gdx.maps.tiled.TiledMapTileSet)2 AnimatedTiledMapTile (com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile)2 StaticTiledMapTile (com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile)2 FloatArray (com.badlogic.gdx.utils.FloatArray)2 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)2 ShortArray (com.badlogic.gdx.utils.ShortArray)2 Element (com.badlogic.gdx.utils.XmlReader.Element)2 AssetDescriptor (com.badlogic.gdx.assets.AssetDescriptor)1 Texture (com.badlogic.gdx.graphics.Texture)1 Glyph (com.badlogic.gdx.graphics.g2d.BitmapFont.Glyph)1 GlyphRun (com.badlogic.gdx.graphics.g2d.GlyphLayout.GlyphRun)1 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)1