Search in sources :

Example 16 with IntArray

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

the class BitmapFontCache method draw.

public void draw(Batch spriteBatch, int start, int end) {
    if (pageVertices.length == 1) {
        // 1 page.
        spriteBatch.draw(font.getRegion().getTexture(), pageVertices[0], start * 20, (end - start) * 20);
        return;
    }
    // Determine vertex offset and count to render for each page. Some pages might not need to be rendered at all.
    Array<TextureRegion> regions = font.getRegions();
    for (int i = 0, pageCount = pageVertices.length; i < pageCount; i++) {
        int offset = -1, count = 0;
        // For each set of glyph indices, determine where to begin within the start/end bounds.
        IntArray glyphIndices = pageGlyphIndices[i];
        for (int ii = 0, n = glyphIndices.size; ii < n; ii++) {
            int glyphIndex = glyphIndices.get(ii);
            // Break early if the glyph is out of bounds.
            if (glyphIndex >= end)
                break;
            // Determine if this glyph is within bounds. Use the first match of that for the offset.
            if (offset == -1 && glyphIndex >= start)
                offset = ii;
            // Determine the vertex count by counting glyphs within bounds.
            if (// && gInd < end
            glyphIndex >= start)
                count++;
        }
        // Page doesn't need to be rendered.
        if (offset == -1 || count == 0)
            continue;
        // Render the page vertex data with the offset and count.
        spriteBatch.draw(regions.get(i).getTexture(), pageVertices[i], offset * 20, count * 20);
    }
}
Also used : IntArray(com.badlogic.gdx.utils.IntArray)

Example 17 with IntArray

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

the class TextArea method initialize.

@Override
protected void initialize() {
    super.initialize();
    writeEnters = true;
    linesBreak = new IntArray();
    cursorLine = 0;
    firstLineShowing = 0;
    moveOffset = -1;
    linesShowing = 0;
}
Also used : IntArray(com.badlogic.gdx.utils.IntArray)

Example 18 with IntArray

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

the class AtlasTmxMapLoader method loadTileset.

protected void loadTileset(TiledMap map, Element element, FileHandle tmxFile, AtlasResolver resolver) {
    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);
            }
        }
        String atlasFilePath = map.getProperties().get("atlas", String.class);
        if (atlasFilePath == null) {
            FileHandle atlasFile = tmxFile.sibling(tmxFile.nameWithoutExtension() + ".atlas");
            if (atlasFile.exists())
                atlasFilePath = atlasFile.name();
        }
        if (atlasFilePath == null) {
            throw new GdxRuntimeException("The map is missing the 'atlas' property");
        }
        // get the TextureAtlas for this tileset
        FileHandle atlasHandle = getRelativeFileHandle(tmxFile, atlasFilePath);
        atlasHandle = resolve(atlasHandle.path());
        TextureAtlas atlas = resolver.getAtlas(atlasHandle.path());
        String regionsName = name;
        for (Texture texture : atlas.getTextures()) {
            trackedTextures.add(texture);
        }
        TiledMapTileSet tileset = new TiledMapTileSet();
        MapProperties props = tileset.getProperties();
        tileset.setName(name);
        props.put("firstgid", firstgid);
        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);
        if (imageSource != null && imageSource.length() > 0) {
            int lastgid = firstgid + ((imageWidth / tilewidth) * (imageHeight / tileheight)) - 1;
            for (AtlasRegion region : atlas.findRegions(regionsName)) {
                // handle unused tile ids
                if (region != null) {
                    int tileid = region.index + 1;
                    if (tileid >= firstgid && tileid <= lastgid) {
                        StaticTiledMapTile tile = new StaticTiledMapTile(region);
                        tile.setId(tileid);
                        tile.setOffsetX(offsetX);
                        tile.setOffsetY(flipY ? -offsetY : offsetY);
                        tileset.putTile(tileid, tile);
                    }
                }
            }
        }
        for (Element tileElement : element.getChildrenByName("tile")) {
            int tileid = firstgid + tileElement.getIntAttribute("id", 0);
            TiledMapTile tile = tileset.getTile(tileid);
            if (tile == null) {
                Element imageElement = tileElement.getChildByName("image");
                if (imageElement != null) {
                    // Is a tilemap with individual images.
                    String regionName = imageElement.getAttribute("source");
                    regionName = regionName.substring(0, regionName.lastIndexOf('.'));
                    AtlasRegion region = atlas.findRegion(regionName);
                    if (region == null)
                        throw new GdxRuntimeException("Tileset region not found: " + regionName);
                    tile = new StaticTiledMapTile(region);
                    tile.setId(tileid);
                    tile.setOffsetX(offsetX);
                    tile.setOffsetY(flipY ? -offsetY : offsetY);
                    tileset.putTile(tileid, tile);
                }
            }
            if (tile != null) {
                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);
                }
            }
        }
        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) Texture(com.badlogic.gdx.graphics.Texture) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Array(com.badlogic.gdx.utils.Array) IntArray(com.badlogic.gdx.utils.IntArray) LongArray(com.badlogic.gdx.utils.LongArray) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) IntArray(com.badlogic.gdx.utils.IntArray) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) AtlasRegion(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)

Example 19 with IntArray

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

the class ConvexHull method sort.

/** Sorts x,y pairs of values by the x value, then the y value.
	 * @param count Number of indices, must be even. */
private void sort(float[] values, int count) {
    int lower = 0;
    int upper = count - 1;
    IntArray stack = quicksortStack;
    stack.add(lower);
    stack.add(upper - 1);
    while (stack.size > 0) {
        upper = stack.pop();
        lower = stack.pop();
        if (upper <= lower)
            continue;
        int i = quicksortPartition(values, lower, upper);
        if (i - lower > upper - i) {
            stack.add(lower);
            stack.add(i - 2);
        }
        stack.add(i + 2);
        stack.add(upper);
        if (upper - i >= i - lower) {
            stack.add(lower);
            stack.add(i - 2);
        }
    }
}
Also used : IntArray(com.badlogic.gdx.utils.IntArray)

Example 20 with IntArray

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

the class DelaunayTriangulator method computeTriangles.

/** Triangulates the given point cloud to a list of triangle indices that make up the Delaunay triangulation.
	 * @param points x,y pairs describing points. Duplicate points will result in undefined behavior.
	 * @param sorted If false, the points will be sorted by the x coordinate, which is required by the triangulation algorithm. If
	 *           sorting is done the input array is not modified, the returned indices are for the input array, and count*2
	 *           additional working memory is needed.
	 * @return triples of indices into the points that describe the triangles in clockwise order. Note the returned array is reused
	 *         for later calls to the same method. */
public ShortArray computeTriangles(float[] points, int offset, int count, boolean sorted) {
    ShortArray triangles = this.triangles;
    triangles.clear();
    if (count < 6)
        return triangles;
    triangles.ensureCapacity(count);
    if (!sorted) {
        if (sortedPoints == null || sortedPoints.length < count)
            sortedPoints = new float[count];
        System.arraycopy(points, offset, sortedPoints, 0, count);
        points = sortedPoints;
        offset = 0;
        sort(points, count);
    }
    int end = offset + count;
    // Determine bounds for super triangle.
    float xmin = points[0], ymin = points[1];
    float xmax = xmin, ymax = ymin;
    for (int i = offset + 2; i < end; i++) {
        float value = points[i];
        if (value < xmin)
            xmin = value;
        if (value > xmax)
            xmax = value;
        i++;
        value = points[i];
        if (value < ymin)
            ymin = value;
        if (value > ymax)
            ymax = value;
    }
    float dx = xmax - xmin, dy = ymax - ymin;
    float dmax = (dx > dy ? dx : dy) * 20f;
    float xmid = (xmax + xmin) / 2f, ymid = (ymax + ymin) / 2f;
    // Setup the super triangle, which contains all points.
    float[] superTriangle = this.superTriangle;
    superTriangle[0] = xmid - dmax;
    superTriangle[1] = ymid - dmax;
    superTriangle[2] = xmid;
    superTriangle[3] = ymid + dmax;
    superTriangle[4] = xmid + dmax;
    superTriangle[5] = ymid - dmax;
    IntArray edges = this.edges;
    edges.ensureCapacity(count / 2);
    BooleanArray complete = this.complete;
    complete.clear();
    complete.ensureCapacity(count);
    // Add super triangle.
    triangles.add(end);
    triangles.add(end + 2);
    triangles.add(end + 4);
    complete.add(false);
    // Include each point one at a time into the existing mesh.
    for (int pointIndex = offset; pointIndex < end; pointIndex += 2) {
        float x = points[pointIndex], y = points[pointIndex + 1];
        // If x,y lies inside the circumcircle of a triangle, the edges are stored and the triangle removed.
        short[] trianglesArray = triangles.items;
        boolean[] completeArray = complete.items;
        for (int triangleIndex = triangles.size - 1; triangleIndex >= 0; triangleIndex -= 3) {
            int completeIndex = triangleIndex / 3;
            if (completeArray[completeIndex])
                continue;
            int p1 = trianglesArray[triangleIndex - 2];
            int p2 = trianglesArray[triangleIndex - 1];
            int p3 = trianglesArray[triangleIndex];
            float x1, y1, x2, y2, x3, y3;
            if (p1 >= end) {
                int i = p1 - end;
                x1 = superTriangle[i];
                y1 = superTriangle[i + 1];
            } else {
                x1 = points[p1];
                y1 = points[p1 + 1];
            }
            if (p2 >= end) {
                int i = p2 - end;
                x2 = superTriangle[i];
                y2 = superTriangle[i + 1];
            } else {
                x2 = points[p2];
                y2 = points[p2 + 1];
            }
            if (p3 >= end) {
                int i = p3 - end;
                x3 = superTriangle[i];
                y3 = superTriangle[i + 1];
            } else {
                x3 = points[p3];
                y3 = points[p3 + 1];
            }
            switch(circumCircle(x, y, x1, y1, x2, y2, x3, y3)) {
                case COMPLETE:
                    completeArray[completeIndex] = true;
                    break;
                case INSIDE:
                    edges.add(p1);
                    edges.add(p2);
                    edges.add(p2);
                    edges.add(p3);
                    edges.add(p3);
                    edges.add(p1);
                    triangles.removeIndex(triangleIndex);
                    triangles.removeIndex(triangleIndex - 1);
                    triangles.removeIndex(triangleIndex - 2);
                    complete.removeIndex(completeIndex);
                    break;
            }
        }
        int[] edgesArray = edges.items;
        for (int i = 0, n = edges.size; i < n; i += 2) {
            // Skip multiple edges. If all triangles are anticlockwise then all interior edges are opposite pointing in direction.
            int p1 = edgesArray[i];
            if (p1 == -1)
                continue;
            int p2 = edgesArray[i + 1];
            boolean skip = false;
            for (int ii = i + 2; ii < n; ii += 2) {
                if (p1 == edgesArray[ii + 1] && p2 == edgesArray[ii]) {
                    skip = true;
                    edgesArray[ii] = -1;
                }
            }
            if (skip)
                continue;
            // Form new triangles for the current point. Edges are arranged in clockwise order.
            triangles.add(p1);
            triangles.add(edgesArray[i + 1]);
            triangles.add(pointIndex);
            complete.add(false);
        }
        edges.clear();
    }
    // Remove triangles with super triangle vertices.
    short[] trianglesArray = triangles.items;
    for (int i = triangles.size - 1; i >= 0; i -= 3) {
        if (trianglesArray[i] >= end || trianglesArray[i - 1] >= end || trianglesArray[i - 2] >= end) {
            triangles.removeIndex(i);
            triangles.removeIndex(i - 1);
            triangles.removeIndex(i - 2);
        }
    }
    // Convert sorted to unsorted indices.
    if (!sorted) {
        short[] originalIndicesArray = originalIndices.items;
        for (int i = 0, n = triangles.size; i < n; i++) trianglesArray[i] = (short) (originalIndicesArray[trianglesArray[i] / 2] * 2);
    }
    // Adjust triangles to start from zero and count by 1, not by vertex x,y coordinate pairs.
    if (offset == 0) {
        for (int i = 0, n = triangles.size; i < n; i++) trianglesArray[i] = (short) (trianglesArray[i] / 2);
    } else {
        for (int i = 0, n = triangles.size; i < n; i++) trianglesArray[i] = (short) ((trianglesArray[i] - offset) / 2);
    }
    return triangles;
}
Also used : IntArray(com.badlogic.gdx.utils.IntArray) ShortArray(com.badlogic.gdx.utils.ShortArray) BooleanArray(com.badlogic.gdx.utils.BooleanArray)

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