Example usage for com.badlogic.gdx.utils Array add

List of usage examples for com.badlogic.gdx.utils Array add

Introduction

In this page you can find the example usage for com.badlogic.gdx.utils Array add.

Prototype

public void add(T value) 

Source Link

Usage

From source file:MyGdxGame.java

License:Apache License

private void getTiles(int startX, int startY, int endX, int endY, Array<Rectangle> tiles) {
    TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get("walls");
    rectPool.freeAll(tiles);/*from   w w  w .  j a v  a  2s.co  m*/
    tiles.clear();
    for (int y = startY; y <= endY; y++) {
        for (int x = startX; x <= endX; x++) {
            Cell cell = layer.getCell(x, y);
            if (cell != null) {
                Rectangle rect = rectPool.obtain();
                rect.set(x, y, 1, 1);
                tiles.add(rect);
            }
        }
    }
}

From source file:by.aleks.christmasboard.data.BitmapFontWriter.java

License:Apache License

/** Writes the given BitmapFontData to a file, using the specified <tt>pageRefs</tt> strings as the image paths for each texture
 * page. The glyphs in BitmapFontData have a "page" id, which references the index of the pageRef you specify here.
 * /*from www.j av a2 s  .c o m*/
 * The FontInfo parameter is useful for cleaner output; such as including a size and font face name hint. However, it can be
 * null to use default values. Ultimately, LibGDX ignores the "info" line when reading back fonts.
 * 
 * Likewise, the scaleW and scaleH are only for cleaner output. They are currently ignored by LibGDX's reader. For maximum
 * compatibility with other BMFont tools, you should use the width and height of your texture pages (each page should be the
 * same size).
 * 
 * @param fontData the bitmap font
 * @param pageRefs the references to each texture page image file, generally in the same folder as outFntFile
 * @param outFntFile the font file to save to (typically ends with '.fnt')
 * @param info the optional info for the file header; can be null
 * @param scaleW the width of your texture pages
 * @param scaleH the height of your texture pages */
public static void writeFont(BitmapFontData fontData, String[] pageRefs, FileHandle outFntFile, FontInfo info,
        int scaleW, int scaleH) {
    if (info == null) {
        info = new FontInfo();
        info.face = outFntFile.nameWithoutExtension();
    }

    int lineHeight = (int) fontData.lineHeight;
    int pages = pageRefs.length;
    int packed = 0;
    int base = (int) ((fontData.capHeight) + (fontData.flipped ? -fontData.ascent : fontData.ascent));
    OutputFormat fmt = BitmapFontWriter.getOutputFormat();
    boolean xml = fmt == OutputFormat.XML;

    StringBuilder buf = new StringBuilder();

    if (xml) {
        buf.append("<font>\n");
    }
    String xmlOpen = xml ? "\t<" : "";
    String xmlCloseSelf = xml ? "/>" : "";
    String xmlTab = xml ? "\t" : "";
    String xmlClose = xml ? ">" : "";

    String xmlQuote = xml ? "\"" : "";
    String alphaChnlParams = xml ? " alphaChnl=\"0\" redChnl=\"0\" greenChnl=\"0\" blueChnl=\"0\""
            : " alphaChnl=0 redChnl=0 greenChnl=0 blueChnl=0";
    //INFO LINE

    buf.append(xmlOpen).append("info face=\"").append(info.face == null ? "" : info.face.replaceAll("\"", "'"))
            .append("\" size=").append(quote(info.size)).append(" bold=").append(quote(info.bold ? 1 : 0))
            .append(" italic=").append(quote(info.italic ? 1 : 0)).append(" charset=\"")
            .append(info.charset == null ? "" : info.charset).append("\" unicode=")
            .append(quote(info.unicode ? 1 : 0)).append(" stretchH=").append(quote(info.stretchH))
            .append(" smooth=").append(quote(info.smooth ? 1 : 0)).append(" aa=").append(quote(info.aa))
            .append(" padding=").append(xmlQuote).append(info.padding.up).append(",").append(info.padding.down)
            .append(",").append(info.padding.left).append(",").append(info.padding.right).append(xmlQuote)
            .append(" spacing=").append(xmlQuote).append(info.spacing.horizontal).append(",")
            .append(info.spacing.vertical).append(xmlQuote).append(xmlCloseSelf).append("\n");

    //COMMON line
    buf.append(xmlOpen).append("common lineHeight=").append(quote(lineHeight)).append(" base=")
            .append(quote(base)).append(" scaleW=").append(quote(scaleW)).append(" scaleH=")
            .append(quote(scaleH)).append(" pages=").append(quote(pages)).append(" packed=")
            .append(quote(packed)).append(alphaChnlParams).append(xmlCloseSelf).append("\n");

    if (xml)
        buf.append("\t<pages>\n");

    //PAGES
    for (int i = 0; i < pageRefs.length; i++) {
        buf.append(xmlTab).append(xmlOpen).append("page id=").append(quote(i)).append(" file=\"")
                .append(pageRefs[i]).append("\"").append(xmlCloseSelf).append("\n");
    }

    if (xml)
        buf.append("\t</pages>\n");

    //CHARS
    Array<Glyph> glyphs = new Array<Glyph>(256);
    for (int i = 0; i < fontData.glyphs.length; i++) {
        if (fontData.glyphs[i] == null)
            continue;

        for (int j = 0; j < fontData.glyphs[i].length; j++) {
            if (fontData.glyphs[i][j] != null) {
                glyphs.add(fontData.glyphs[i][j]);
            }
        }
    }

    buf.append(xmlOpen).append("chars count=").append(quote(glyphs.size)).append(xmlClose).append("\n");

    //CHAR definitions
    for (int i = 0; i < glyphs.size; i++) {
        Glyph g = glyphs.get(i);
        buf.append(xmlTab).append(xmlOpen).append("char id=").append(quote(String.format("%-5s", g.id), true))
                .append("x=").append(quote(String.format("%-5s", g.srcX), true)).append("y=")
                .append(quote(String.format("%-5s", g.srcY), true)).append("width=")
                .append(quote(String.format("%-5s", g.width), true)).append("height=")
                .append(quote(String.format("%-5s", g.height), true)).append("xoffset=")
                .append(quote(String.format("%-5s", g.xoffset), true)).append("yoffset=")
                .append(quote(String.format("%-5s", fontData.flipped ? g.yoffset : -(g.height + g.yoffset)),
                        true))
                .append("xadvance=").append(quote(String.format("%-5s", g.xadvance), true)).append("page=")
                .append(quote(String.format("%-5s", g.page), true)).append("chnl=").append(quote(0, true))
                .append(xmlCloseSelf).append("\n");
    }

    if (xml)
        buf.append("\t</chars>\n");

    //KERNINGS
    int kernCount = 0;
    StringBuilder kernBuf = new StringBuilder();
    for (int i = 0; i < glyphs.size; i++) {
        for (int j = 0; j < glyphs.size; j++) {
            Glyph first = glyphs.get(i);
            Glyph second = glyphs.get(j);
            int kern = first.getKerning((char) second.id);
            if (kern != 0) {
                kernCount++;
                kernBuf.append(xmlTab).append(xmlOpen).append("kerning first=").append(quote(first.id))
                        .append(" second=").append(quote(second.id)).append(" amount=")
                        .append(quote(kern, true)).append(xmlCloseSelf).append("\n");
            }
        }
    }

    //KERN info
    buf.append(xmlOpen).append("kernings count=").append(quote(kernCount)).append(xmlClose).append("\n");
    buf.append(kernBuf);

    if (xml) {
        buf.append("\t</kernings>\n");
        buf.append("</font>");
    }

    String charset = info.charset;
    if (charset != null && charset.length() == 0)
        charset = null;

    outFntFile.writeString(buf.toString(), false, charset);
}

From source file:CB_UI_Base.CB_Texturepacker.MaxRectsPacker.java

License:Apache License

public Array<Page> pack(Array<Rect_Base> inputRects) {
    for (int i = 0, nn = inputRects.size; i < nn; i++) {
        Rect_Base rect = inputRects.get(i);
        rect.width += settings.paddingX;
        rect.height += settings.paddingY;
    }//from  w  w w .java 2s. co  m

    if (settings.fast) {
        if (settings.rotation) {
            // Sort by longest side if rotation is enabled.
            inputRects.sort(new Comparator<Rect_Base>() {
                public int compare(Rect_Base o1, Rect_Base o2) {
                    int n1 = o1.width > o1.height ? o1.width : o1.height;
                    int n2 = o2.width > o2.height ? o2.width : o2.height;
                    return n2 - n1;
                }
            });
        } else {
            // Sort only by width (largest to smallest) if rotation is disabled.
            inputRects.sort(new Comparator<Rect_Base>() {
                public int compare(Rect_Base o1, Rect_Base o2) {
                    return o2.width - o1.width;
                }
            });
        }
    }

    Array<Page> pages = new Array<Page>();
    while (inputRects.size > 0) {
        Page result = packPage(inputRects);
        pages.add(result);
        inputRects = result.remainingRects;
    }
    return pages;
}

From source file:CB_UI_Base.CB_Texturepacker.MaxRectsPacker.java

License:Apache License

/**
 * @param fully//from ww w . jav  a  2 s.co m
 *            If true, the only results that pack all rects will be considered. If false, all results are considered, not all rects may
 *            be packed.
 */
private Page packAtSize(boolean fully, int width, int height, Array<Rect_Base> inputRects) {
    Page bestResult = null;
    for (int i = 0, n = methods.length; i < n; i++) {
        maxRects.init(width, height);
        Page result;
        if (!settings.fast) {
            result = maxRects.pack(inputRects, methods[i]);
        } else {
            Array<Rect_Base> remaining = new Array<Rect_Base>();
            for (int ii = 0, nn = inputRects.size; ii < nn; ii++) {
                Rect_Base rect = inputRects.get(ii);
                if (maxRects.insert(rect, methods[i]) == null) {
                    while (ii < nn)
                        remaining.add(inputRects.get(ii++));
                }
            }
            result = maxRects.getResult();
            result.remainingRects = remaining;
        }
        if (fully && result.remainingRects.size > 0)
            continue;
        if (result.outputRects.size == 0)
            continue;
        bestResult = getBest(bestResult, result);
    }
    return bestResult;
}

From source file:ch.coldpixel.mario.Sprites.Mario.java

public Mario(World world, PlayScreen screen) {
    super(screen.getAtlas().findRegion("little_mario"));
    this.world = world;
    currentState = State.STANDING;
    previousState = State.STANDING;
    stateTimer = 0;/*from w  w  w  .  jav a  2 s  .c o m*/
    runningRight = true;

    Array<TextureRegion> frames = new Array<TextureRegion>();
    for (int i = 1; i < 4; i++) {
        frames.add(new TextureRegion(getTexture(), i * 16, 0, 16, 16));
    }
    marioRun = new Animation(0.1f, frames);
    frames.clear();

    for (int i = 4; i < 6; i++) {
        frames.add(new TextureRegion(getTexture(), i * 16, 0, 16, 16));
    }
    marioJump = new Animation(0.1f, frames);

    marioStand = new TextureRegion(getTexture(), 0, 0, 16, 16);

    defineMario();
    setBounds(0, 0, 16 / MarioBros.PPM, 16 / MarioBros.PPM);
    setRegion(marioStand);

}

From source file:com.adidesi95.nodechess.model.pieces.Piece.java

License:Apache License

public Array<Tile> getValidMoveTiles(Board board) {
    Array<Tile> tiles = new Array<Tile>();
    int x = (int) this.getX(), y = (int) this.getY();
    boolean isLooping;

    for (Move move : this.validMoves) {
        isLooping = true;//from ww w  .ja  va  2s .c o m
        for (int i = 1; isLooping; i++) {
            int tx = x + (move.xOffset * i);
            //TODO the player have opposite sittings. Think bout it!
            int ty = y + ((this.isWhite ? move.yOffset : -move.yOffset) * i);
            if ((tx > -1) && (tx < 8) && (ty > -1) && (ty < 8)) {
                Tile tile = board.getTileAt(tx, ty);
                Piece otherPiece = board.getPieceAt(tx, ty);
                if (otherPiece != null) {
                    if (otherPiece.isWhite != this.isWhite && this.canCaptureWithMove) {
                        tiles.add(tile);
                    }
                    isLooping = false;
                } else {
                    tiles.add(tile);
                }
            } else {
                isLooping = false;
            }
            if (!move.isVector) {
                isLooping = false;
            }
        }
    }
    return tiles;
}

From source file:com.adidesi95.nodechess.model.pieces.Piece.java

License:Apache License

public Array<Tile> getCaptureOnlyTiles(Board board, boolean check) {
    Array<Tile> tiles = new Array<Tile>();
    int x = (int) this.getX();
    int y = (int) this.getY();

    for (Move move : this.captureOnlyMoves) {
        int tx = x + move.xOffset; // Tile x.
        int ty = y + (this.isWhite ? move.yOffset : -move.yOffset); // Tile
        // y.//from w  w  w  .ja  va  2s  . c  om
        if ((tx > -1) && (tx < 8) && (ty > -1) && (ty < 8)) {
            Tile tile = board.getTileAt(tx, ty);
            Piece otherPiece = board.getPieceAt(tx, ty);

            if (!check || ((otherPiece != null) && (otherPiece.isWhite != this.isWhite))) {
                tiles.add(tile);
            }
        }
    }
    return tiles;
}

From source file:com.agateau.pixelwheels.gamesetup.ChampionshipMaestro.java

License:Open Source License

private Screen createOnePlayerVehicleScreen() {
    SelectVehicleScreen.Listener listener = new SelectVehicleScreen.Listener() {
        @Override// w  w w .  j ava 2  s .  c  o  m
        public void onBackPressed() {
            getGame().replaceScreen(createChampionshipScreen());
        }

        @Override
        public void onPlayerSelected(GameInfo.Player player) {
            Array<GameInfo.Player> players = new Array<GameInfo.Player>();
            players.add(player);
            mGameInfoBuilder.setPlayers(players);
            startChampionship();
        }
    };
    return new SelectVehicleScreen(getGame(), listener);
}

From source file:com.agateau.pixelwheels.gamesetup.QuickRaceMaestro.java

License:Open Source License

private Screen createOnePlayerVehicleScreen() {
    SelectVehicleScreen.Listener listener = new SelectVehicleScreen.Listener() {
        @Override/*from w ww .  j a  v  a  2 s.co  m*/
        public void onBackPressed() {
            getGame().replaceScreen(createSelectTrackScreen());
        }

        @Override
        public void onPlayerSelected(GameInfo.Player player) {
            Array<GameInfo.Player> players = new Array<GameInfo.Player>();
            players.add(player);
            mGameInfoBuilder.setPlayers(players);
            getGame().replaceScreen(createRaceScreen());
        }
    };
    return new SelectVehicleScreen(getGame(), listener);
}

From source file:com.agateau.pixelwheels.map.LapPositionTableIO.java

License:Open Source License

public static LapPositionTable load(TiledMap map) {
    MapLayer layer = map.getLayers().get("Sections");
    Assert.check(layer != null, "No 'Sections' layer found");
    MapObjects objects = layer.getObjects();

    Array<Line> lines = new Array<Line>();
    lines.ensureCapacity(objects.getCount());
    Set<String> names = new HashSet<String>();
    for (MapObject obj : objects) {
        String name = obj.getName();
        Assert.check(!name.isEmpty(), "Section line is missing a name");
        Assert.check(!names.contains(name), "Duplicate section line " + name);
        names.add(name);/*ww  w . j  a  v a 2s  .  c o  m*/

        float order;
        try {
            order = Float.parseFloat(name);
        } catch (NumberFormatException e) {
            throw new RuntimeException("Invalid section name " + name);
        }
        Assert.check(obj instanceof PolylineMapObject,
                "'Sections' layer should only contain PolylineMapObjects");
        Polyline polyline = ((PolylineMapObject) obj).getPolyline();
        float[] vertices = polyline.getTransformedVertices();
        Assert.check(vertices.length == 4,
                "Polyline with name " + order + "should have 2 points, not " + (vertices.length / 2));
        Line line = new Line();
        line.x1 = vertices[0];
        line.y1 = vertices[1];
        line.x2 = vertices[2];
        line.y2 = vertices[3];
        line.order = order;
        lines.add(line);
    }
    lines.sort();

    LapPositionTable table = new LapPositionTable();
    for (int idx = 0; idx < lines.size; ++idx) {
        Line line1 = lines.get(idx);
        Line line2 = lines.get((idx + 1) % lines.size);
        float[] vertices = { line1.x1, line1.y1, line2.x1, line2.y1, line2.x2, line2.y2, line1.x2, line1.y2 };
        Polygon polygon = new Polygon(vertices);
        table.addSection(idx, polygon);
    }
    return table;
}