Search in sources :

Example 1 with Surface

use of com.b3dgs.lionengine.Surface in project lionengine by b3dgs.

the class SheetsExtractor method extract.

/**
 * Convert a set of tile to a set of tile sheets.
 *
 * @param tiles The list of tiles.
 * @param horizontalTiles The number of horizontal tiles on sheet (inferior or equal to 0 to use automatic).
 * @return The list of tile sheets.
 */
public static List<SpriteTiled> extract(Collection<ImageBuffer> tiles, int horizontalTiles) {
    final Surface surface = getSheetSize(tiles, horizontalTiles);
    final int horizontals = surface.getWidth();
    final int verticals = surface.getHeight();
    final int tilesPerSheet = Math.min(tiles.size(), horizontals * verticals);
    final List<SpriteTiled> sheets = new ArrayList<>();
    ImageBuffer sheet = null;
    Graphic g = null;
    int number = 0;
    int tw = 0;
    int th = 0;
    for (final ImageBuffer tile : tiles) {
        if (g == null) {
            tw = tile.getWidth();
            th = tile.getHeight();
            final int width = Math.max(tw, horizontals * tw);
            final int height = Math.max(th, verticals * th);
            sheet = Graphics.createImageBuffer(width, height, tile.getTransparentColor());
            g = sheet.createGraphic();
        }
        final int x = number % horizontals;
        final int y = (int) Math.floor(number / (double) horizontals);
        g.drawImage(tile, x * tw, y * th);
        number++;
        if (number >= tilesPerSheet) {
            g.dispose();
            sheets.add(Drawable.loadSpriteTiled(Graphics.getImageBuffer(sheet), tw, th));
            sheet = null;
            g = null;
            number = 0;
        }
    }
    return sheets;
}
Also used : ImageBuffer(com.b3dgs.lionengine.graphic.ImageBuffer) Graphic(com.b3dgs.lionengine.graphic.Graphic) SpriteTiled(com.b3dgs.lionengine.graphic.drawable.SpriteTiled) ArrayList(java.util.ArrayList) Surface(com.b3dgs.lionengine.Surface)

Aggregations

Surface (com.b3dgs.lionengine.Surface)1 Graphic (com.b3dgs.lionengine.graphic.Graphic)1 ImageBuffer (com.b3dgs.lionengine.graphic.ImageBuffer)1 SpriteTiled (com.b3dgs.lionengine.graphic.drawable.SpriteTiled)1 ArrayList (java.util.ArrayList)1