Search in sources :

Example 91 with ImageBuffer

use of com.b3dgs.lionengine.graphic.ImageBuffer in project lionengine by b3dgs.

the class Minimap method computeSheet.

/**
 * Compute the current sheet.
 *
 * @param colors The colors data.
 * @param sheetId The sheet id.
 */
private void computeSheet(Map<Integer, ColorRgba> colors, int sheetId) {
    final SpriteTiled tiles = map.getSheet(sheetId);
    final ImageBuffer tilesSurface = tiles.getSurface();
    final int tw = map.getTileWidth();
    final int th = map.getTileHeight();
    int number = 0;
    for (int i = 0; i < tilesSurface.getWidth(); i += tw) {
        for (int j = 0; j < tilesSurface.getHeight(); j += th) {
            final int h = number * tw % tiles.getWidth();
            final int v = number / tiles.getTilesHorizontal() * th;
            final ColorRgba color = UtilColor.getWeightedColor(tilesSurface, h, v, tw, th);
            if (!(NO_TILE.equals(color) || color.getAlpha() == 0)) {
                colors.put(Integer.valueOf(number), color);
            }
            number++;
        }
    }
}
Also used : ImageBuffer(com.b3dgs.lionengine.graphic.ImageBuffer) ColorRgba(com.b3dgs.lionengine.graphic.ColorRgba) SpriteTiled(com.b3dgs.lionengine.graphic.drawable.SpriteTiled)

Example 92 with ImageBuffer

use of com.b3dgs.lionengine.graphic.ImageBuffer 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)

Example 93 with ImageBuffer

use of com.b3dgs.lionengine.graphic.ImageBuffer in project lionengine by b3dgs.

the class LevelRipConverter method checkTile.

/**
 * Check tile of sheet.
 *
 * @param map The destination map reference.
 * @param tileSprite The tiled sprite
 * @param sheetId The sheet number.
 * @param x The location x.
 * @param y The location y.
 * @return <code>true</code> if tile found, <code>false</code> else.
 */
private static boolean checkTile(MapTile map, ImageBuffer tileSprite, int sheetId, int x, int y) {
    final int tw = map.getTileWidth();
    final int th = map.getTileHeight();
    final SpriteTiled tileSheet = map.getSheet(sheetId);
    final ImageBuffer sheetImage = tileSheet.getSurface();
    final int tilesInX = tileSheet.getWidth() / tw;
    final int tilesInY = tileSheet.getHeight() / th;
    for (int surfaceCurrentTileY = 0; surfaceCurrentTileY < tilesInY; surfaceCurrentTileY++) {
        for (int surfaceCurrentTileX = 0; surfaceCurrentTileX < tilesInX; surfaceCurrentTileX++) {
            // Tile number on tile sheet
            final int number = surfaceCurrentTileX + surfaceCurrentTileY * tilesInX;
            // Compare tiles between sheet and image map
            final int xa = x * tw;
            final int ya = y * th;
            final int xb = surfaceCurrentTileX * tw;
            final int yb = surfaceCurrentTileY * th;
            if (TilesExtractor.compareTile(tw, th, tileSprite, xa, ya, sheetImage, xb, yb)) {
                map.setTile(x, map.getInTileHeight() - 1 - y, number);
                return true;
            }
        }
    }
    return false;
}
Also used : ImageBuffer(com.b3dgs.lionengine.graphic.ImageBuffer) SpriteTiled(com.b3dgs.lionengine.graphic.drawable.SpriteTiled)

Example 94 with ImageBuffer

use of com.b3dgs.lionengine.graphic.ImageBuffer in project lionengine by b3dgs.

the class TilesExtractor method extract.

/**
 * Proceed the specified level rip.
 *
 * @param canceler The canceler reference (can be <code>null</code>).
 * @param level The level rip.
 * @param tilesNumber The total tiles number to extract.
 * @param tiles The current extracted tiles.
 * @param checkedTiles The last number of checked tiles.
 * @return The current number of checked tiles, or -1 if canceled.
 * @throws LionEngineException If an error occurred when proceeding the image.
 */
private int extract(Canceler canceler, SpriteTiled level, int tilesNumber, Collection<ImageBuffer> tiles, int checkedTiles) {
    final int horizontalTiles = level.getTilesHorizontal();
    final int verticalTiles = level.getTilesVertical();
    final ImageBuffer surface = level.getSurface();
    final int tw = level.getTileWidth();
    final int th = level.getTileHeight();
    int checked = checkedTiles;
    int oldPercent = 0;
    for (int v = 0; v < verticalTiles; v++) {
        for (int h = 0; h < horizontalTiles; h++) {
            final int x = h * tw;
            final int y = v * th;
            if (IGNORED_COLOR_VALUE != surface.getRgb(x, y) && !isExtracted(level, x, y, tiles)) {
                final ImageBuffer tile = extract(level, h + v * horizontalTiles);
                tiles.add(tile);
            }
            checked++;
            oldPercent = updateProgress(checked, tilesNumber, oldPercent, tiles);
            if (canceler != null && canceler.isCanceled()) {
                return -1;
            }
        }
    }
    return checked;
}
Also used : ImageBuffer(com.b3dgs.lionengine.graphic.ImageBuffer)

Example 95 with ImageBuffer

use of com.b3dgs.lionengine.graphic.ImageBuffer in project lionengine by b3dgs.

the class FactoryGraphicAwt method generateTileset.

@Override
public void generateTileset(ImageBuffer[] images, Media media) {
    Check.notNull(images);
    Check.notNull(media);
    if (images.length == 0) {
        throw new LionEngineException("No images found !");
    }
    final int width = images[0].getWidth();
    final int height = images[0].getHeight();
    final Transparency transparency = images[0].getTransparency();
    final int multDistance = (int) Math.ceil(width * images.length / (double) height) / 4;
    final int[] mult = UtilMath.getClosestSquareMult(images.length, multDistance);
    final ImageBuffer tile = new ImageBufferAwt(ToolsAwt.createImage(width * mult[1], height * mult[0], ToolsAwt.getTransparency(transparency)));
    int x = 0;
    int y = 0;
    int line = 0;
    final Graphic g = tile.createGraphic();
    for (final ImageBuffer b : images) {
        g.drawImage(b, x, y);
        x += b.getWidth();
        line++;
        if (line == mult[1]) {
            x = 0;
            y += b.getHeight();
            line = 0;
        }
    }
    g.dispose();
    saveImage(tile, media);
}
Also used : Transparency(com.b3dgs.lionengine.graphic.Transparency) ImageBuffer(com.b3dgs.lionengine.graphic.ImageBuffer) LionEngineException(com.b3dgs.lionengine.LionEngineException) Graphic(com.b3dgs.lionengine.graphic.Graphic) FactoryGraphic(com.b3dgs.lionengine.graphic.FactoryGraphic)

Aggregations

ImageBuffer (com.b3dgs.lionengine.graphic.ImageBuffer)110 Test (org.junit.Test)34 Test (org.junit.jupiter.api.Test)28 Media (com.b3dgs.lionengine.Media)16 Graphic (com.b3dgs.lionengine.graphic.Graphic)12 GraphicTest (com.b3dgs.lionengine.graphic.GraphicTest)12 FactoryGraphic (com.b3dgs.lionengine.graphic.FactoryGraphic)9 BufferedImage (java.awt.image.BufferedImage)9 SpriteTiled (com.b3dgs.lionengine.graphic.SpriteTiled)6 SpriteTiled (com.b3dgs.lionengine.graphic.drawable.SpriteTiled)5 ColorRgba (com.b3dgs.lionengine.graphic.ColorRgba)3 FactoryGraphicTest (com.b3dgs.lionengine.graphic.FactoryGraphicTest)3 Image (com.b3dgs.lionengine.graphic.Image)3 Sprite (com.b3dgs.lionengine.graphic.Sprite)3 SpriteAnimated (com.b3dgs.lionengine.graphic.SpriteAnimated)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 TilesExtractor (com.b3dgs.lionengine.game.feature.tile.TilesExtractor)2 ArrayList (java.util.ArrayList)2 BeforeAll (org.junit.jupiter.api.BeforeAll)2 Config (com.b3dgs.lionengine.Config)1