Search in sources :

Example 31 with Graphic

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

the class FactoryGraphicHeadless method createImageBuffer.

@Override
public ImageBuffer createImageBuffer(int width, int height) {
    final ImageBuffer buffer = new ImageBufferHeadless(width, height, Transparency.OPAQUE);
    final Graphic g = buffer.createGraphic();
    g.setColor(ColorRgba.BLACK);
    g.drawRect(0, 0, width, height, true);
    g.dispose();
    return buffer;
}
Also used : ImageBuffer(com.b3dgs.lionengine.graphic.ImageBuffer) Graphic(com.b3dgs.lionengine.graphic.Graphic) FactoryGraphic(com.b3dgs.lionengine.graphic.FactoryGraphic)

Example 32 with Graphic

use of com.b3dgs.lionengine.graphic.Graphic 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 33 with Graphic

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

the class CollidableModelTest method testRender.

/**
 * Test collidable rendering.
 */
@Test
void testRender() {
    transformable1.moveLocation(1.0, 1.0, 1.0);
    final Graphic g = Graphics.createGraphic();
    collidable1.setOrigin(Origin.MIDDLE);
    collidable1.render(g);
    final Collision collision = new Collision("test", 0, 0, 3, 3, false);
    collidable1.addCollision(collision);
    collidable1.addCollision(Collision.AUTOMATIC);
    collidable1.setCollisionVisibility(true);
    transformable1.teleport(1.0, 1.0);
    transformable1.moveLocation(1.0, 1.0, 1.0);
    collidable1.setEnabled(false);
    collidable1.render(g);
    collidable1.setEnabled(true);
    collidable1.render(g);
}
Also used : Graphic(com.b3dgs.lionengine.graphic.Graphic) Test(org.junit.jupiter.api.Test)

Example 34 with Graphic

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

Example 35 with Graphic

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

the class FactoryGraphicAwt method createImageBuffer.

@Override
public ImageBuffer createImageBuffer(int width, int height, ColorRgba transparency) {
    Check.notNull(transparency);
    final BufferedImage image = ToolsAwt.createImage(width, height, java.awt.Transparency.BITMASK);
    final ImageBuffer buffer = new ImageBufferAwt(image);
    final Graphic g = buffer.createGraphic();
    g.setColor(transparency);
    g.drawRect(0, 0, width, height, true);
    g.dispose();
    return buffer;
}
Also used : ImageBuffer(com.b3dgs.lionengine.graphic.ImageBuffer) Graphic(com.b3dgs.lionengine.graphic.Graphic) FactoryGraphic(com.b3dgs.lionengine.graphic.FactoryGraphic) BufferedImage(java.awt.image.BufferedImage)

Aggregations

Graphic (com.b3dgs.lionengine.graphic.Graphic)45 Test (org.junit.jupiter.api.Test)18 ImageBuffer (com.b3dgs.lionengine.graphic.ImageBuffer)12 FactoryGraphic (com.b3dgs.lionengine.graphic.FactoryGraphic)9 Test (org.junit.Test)8 BufferedImage (java.awt.image.BufferedImage)5 GraphicMock (com.b3dgs.lionengine.graphic.GraphicMock)4 LionEngineException (com.b3dgs.lionengine.LionEngineException)3 Resolution (com.b3dgs.lionengine.Resolution)3 FactoryGraphicMock (com.b3dgs.lionengine.graphic.FactoryGraphicMock)3 Media (com.b3dgs.lionengine.Media)2 ColorRgba (com.b3dgs.lionengine.graphic.ColorRgba)2 SpriteFont (com.b3dgs.lionengine.graphic.SpriteFont)2 Text (com.b3dgs.lionengine.graphic.Text)2 Align (com.b3dgs.lionengine.Align)1 Surface (com.b3dgs.lionengine.Surface)1 Tile (com.b3dgs.lionengine.game.feature.tile.Tile)1 Image (com.b3dgs.lionengine.graphic.Image)1 Sprite (com.b3dgs.lionengine.graphic.Sprite)1 SpriteAnimated (com.b3dgs.lionengine.graphic.SpriteAnimated)1