Search in sources :

Example 21 with ImageBuffer

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

the class SpriteTiledTest method testHashCode.

/**
 * Test hash code.
 */
@Test
public void testHashCode() {
    final ImageBuffer surface = Graphics.createImageBuffer(64, 32);
    final int sprite = new SpriteTiledImpl(surface, 16, 8).hashCode();
    final SpriteTiled spriteMedia = new SpriteTiledImpl(media, 16, 8);
    spriteMedia.load();
    Assert.assertEquals(sprite, new SpriteTiledImpl(surface, 16, 8).hashCode());
    Assert.assertNotEquals(sprite, new Object().hashCode());
    Assert.assertNotEquals(spriteMedia.hashCode(), new SpriteTiledImpl(media, 16, 8).hashCode());
    Assert.assertNotEquals(sprite, new SpriteTiledImpl(Graphics.createImageBuffer(64, 32), 16, 8).hashCode());
    Assert.assertNotEquals(sprite, new SpriteTiledImpl(Graphics.createImageBuffer(32, 32), 16, 8).hashCode());
    Assert.assertNotEquals(sprite, new SpriteTiledImpl(Graphics.createImageBuffer(64, 64), 16, 8).hashCode());
    Assert.assertNotEquals(sprite, new SpriteTiledImpl(Graphics.createImageBuffer(32, 64), 16, 8).hashCode());
    Assert.assertNotEquals(sprite, new SpriteTiledImpl(Graphics.createImageBuffer(64, 32), 8, 8).hashCode());
    Assert.assertNotEquals(sprite, new SpriteTiledImpl(Graphics.createImageBuffer(64, 32), 16, 16).hashCode());
    Assert.assertNotEquals(sprite, new SpriteTiledImpl(Graphics.createImageBuffer(32, 32), 8, 8).hashCode());
    Assert.assertNotEquals(sprite, new SpriteTiledImpl(Graphics.createImageBuffer(64, 64), 16, 16).hashCode());
}
Also used : ImageBuffer(com.b3dgs.lionengine.graphic.ImageBuffer) SpriteTiled(com.b3dgs.lionengine.graphic.SpriteTiled) Test(org.junit.Test)

Example 22 with ImageBuffer

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

the class TilesExtractor method isExtracted.

/**
 * Check if tile has already been extracted regarding the current tile on level rip.
 *
 * @param level The level rip reference.
 * @param x The current level rip horizontal location.
 * @param y The current level rip vertical location.
 * @param tiles The current extracted tiles.
 * @return <code>true</code> if already extracted, <code>false</code> else.
 */
private static boolean isExtracted(SpriteTiled level, int x, int y, Collection<ImageBuffer> tiles) {
    final int tw = level.getTileWidth();
    final int th = level.getTileHeight();
    final ImageBuffer surface = level.getSurface();
    for (final ImageBuffer tile : tiles) {
        if (compareTile(tw, th, surface, x, y, tile, 0, 0)) {
            return true;
        }
    }
    return false;
}
Also used : ImageBuffer(com.b3dgs.lionengine.graphic.ImageBuffer)

Example 23 with ImageBuffer

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

the class TilesExtractor method extract.

/**
 * Start using specified output file. Listeners are cleared once ended.
 *
 * @param canceler The canceler reference (can be <code>null</code>).
 * @param tw The tile width.
 * @param th The tile height.
 * @param levelRips The levels rip used.
 * @return The extracted tile sheets.
 * @throws LionEngineException If an error occurred when computing sheet.
 */
public Collection<ImageBuffer> extract(Canceler canceler, int tw, int th, Collection<Media> levelRips) {
    final int tilesNumber = getTilesNumber(tw, th, levelRips);
    final Collection<ImageBuffer> tiles = new ArrayList<>();
    int checkedTiles = 0;
    for (final Media levelRip : levelRips) {
        final SpriteTiled level = Drawable.loadSpriteTiled(levelRip, tw, th);
        level.load();
        level.prepare();
        checkedTiles = extract(canceler, level, tilesNumber, tiles, checkedTiles);
        level.getSurface().dispose();
        if (checkedTiles < 0) {
            break;
        }
    }
    listeners.clear();
    return tiles;
}
Also used : ImageBuffer(com.b3dgs.lionengine.graphic.ImageBuffer) ArrayList(java.util.ArrayList) Media(com.b3dgs.lionengine.Media) SpriteTiled(com.b3dgs.lionengine.graphic.drawable.SpriteTiled)

Example 24 with ImageBuffer

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

the class TilesExtractor method extract.

/**
 * Extract the tile from level.
 *
 * @param level The level image reference.
 * @param number The tile number on level.
 * @return The extracted tile image from level.
 */
private static ImageBuffer extract(SpriteTiled level, int number) {
    final ColorRgba transparency = level.getSurface().getTransparentColor();
    final ImageBuffer tile = Graphics.createImageBuffer(level.getTileWidth(), level.getTileHeight(), transparency);
    final Graphic g = tile.createGraphic();
    level.setTile(number);
    level.render(g);
    g.dispose();
    final ImageBuffer copy = Graphics.getImageBuffer(tile);
    tile.dispose();
    return copy;
}
Also used : ImageBuffer(com.b3dgs.lionengine.graphic.ImageBuffer) ColorRgba(com.b3dgs.lionengine.graphic.ColorRgba) Graphic(com.b3dgs.lionengine.graphic.Graphic)

Example 25 with ImageBuffer

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

the class LevelRipConverter method start.

/**
 * Run the converter.
 *
 * @param levelrip The file containing the levelrip as an image.
 * @param map The destination map reference.
 * @param listener The progress listener.
 * @param canceler The canceler reference.
 * @return The total number of not found tiles.
 * @throws LionEngineException If media is <code>null</code> or image cannot be read.
 */
public static int start(Media levelrip, MapTile map, ProgressListener listener, Canceler canceler) {
    final Sprite imageMap = Drawable.loadSprite(levelrip);
    imageMap.load();
    imageMap.prepare();
    final int imageTilesInX = imageMap.getWidth() / map.getTileWidth();
    final int imageTilesInY = imageMap.getHeight() / map.getTileHeight();
    map.create(map.getTileWidth(), map.getTileHeight(), imageTilesInX, imageTilesInY);
    final double progressMax = imageTilesInX * (double) imageTilesInY;
    long progress = 0L;
    int lastPercent = 0;
    int errors = 0;
    final ImageBuffer tileRef = imageMap.getSurface();
    for (int progressTileY = 0; progressTileY < imageTilesInY; progressTileY++) {
        for (int progressTileX = 0; progressTileX < imageTilesInX; progressTileX++) {
            if (!checkPixel(map, tileRef, progressTileX, progressTileY)) {
                Verbose.warning("Tile missing at: " + progressTileX + " " + progressTileY);
                errors++;
            }
            final int percent = (int) Math.round(progress / progressMax * 100);
            if (listener != null && percent != lastPercent) {
                listener.notifyProgress(percent, progressTileX, progressTileY);
            }
            lastPercent = percent;
            progress++;
            if (canceler != null && canceler.isCanceled()) {
                tileRef.dispose();
                return errors;
            }
        }
    }
    tileRef.dispose();
    return errors;
}
Also used : ImageBuffer(com.b3dgs.lionengine.graphic.ImageBuffer) Sprite(com.b3dgs.lionengine.graphic.drawable.Sprite)

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