Search in sources :

Example 6 with Region

use of com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region in project libgdx by libgdx.

the class TextureUnpacker method splitAtlas.

/** Splits an atlas into seperate image and ninepatch files. */
public void splitAtlas(TextureAtlasData atlas, String outputDir) {
    // create the output directory if it did not exist yet
    File outputDirFile = new File(outputDir);
    if (!outputDirFile.exists()) {
        outputDirFile.mkdirs();
        System.out.println(String.format("Creating directory: %s", outputDirFile.getPath()));
    }
    for (Page page : atlas.getPages()) {
        // load the image file belonging to this page as a Buffered Image
        BufferedImage img = null;
        try {
            img = ImageIO.read(page.textureFile.file());
        } catch (IOException e) {
            printExceptionAndExit(e);
        }
        for (Region region : atlas.getRegions()) {
            System.out.println(String.format("Processing image for %s: x[%s] y[%s] w[%s] h[%s], rotate[%s]", region.name, region.left, region.top, region.width, region.height, region.rotate));
            // check if the page this region is in is currently loaded in a Buffered Image
            if (region.page == page) {
                BufferedImage splitImage = null;
                String extension = null;
                // check if the region is a ninepatch or a normal image and delegate accordingly
                if (region.splits == null) {
                    splitImage = extractImage(img, region, outputDirFile, 0);
                    extension = OUTPUT_TYPE;
                } else {
                    splitImage = extractNinePatch(img, region, outputDirFile);
                    extension = String.format("9.%s", OUTPUT_TYPE);
                }
                // check if the parent directories of this image file exist and create them if not
                File imgOutput = new File(outputDirFile, String.format("%s.%s", region.index == -1 ? region.name : region.name + "_" + region.index, extension));
                File imgDir = imgOutput.getParentFile();
                if (!imgDir.exists()) {
                    System.out.println(String.format("Creating directory: %s", imgDir.getPath()));
                    imgDir.mkdirs();
                }
                // save the image
                try {
                    ImageIO.write(splitImage, OUTPUT_TYPE, imgOutput);
                } catch (IOException e) {
                    printExceptionAndExit(e);
                }
            }
        }
    }
}
Also used : Region(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region) Page(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Page) IOException(java.io.IOException) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 7 with Region

use of com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region in project libgdx by libgdx.

the class TextureAtlas method load.

private void load(TextureAtlasData data) {
    ObjectMap<Page, Texture> pageToTexture = new ObjectMap<Page, Texture>();
    for (Page page : data.pages) {
        Texture texture = null;
        if (page.texture == null) {
            texture = new Texture(page.textureFile, page.format, page.useMipMaps);
            texture.setFilter(page.minFilter, page.magFilter);
            texture.setWrap(page.uWrap, page.vWrap);
        } else {
            texture = page.texture;
            texture.setFilter(page.minFilter, page.magFilter);
            texture.setWrap(page.uWrap, page.vWrap);
        }
        textures.add(texture);
        pageToTexture.put(page, texture);
    }
    for (Region region : data.regions) {
        int width = region.width;
        int height = region.height;
        AtlasRegion atlasRegion = new AtlasRegion(pageToTexture.get(region.page), region.left, region.top, region.rotate ? height : width, region.rotate ? width : height);
        atlasRegion.index = region.index;
        atlasRegion.name = region.name;
        atlasRegion.offsetX = region.offsetX;
        atlasRegion.offsetY = region.offsetY;
        atlasRegion.originalHeight = region.originalHeight;
        atlasRegion.originalWidth = region.originalWidth;
        atlasRegion.rotate = region.rotate;
        atlasRegion.splits = region.splits;
        atlasRegion.pads = region.pads;
        if (region.flip)
            atlasRegion.flip(false, true);
        regions.add(atlasRegion);
    }
}
Also used : ObjectMap(com.badlogic.gdx.utils.ObjectMap) Region(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region) Page(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Page) Texture(com.badlogic.gdx.graphics.Texture)

Example 8 with Region

use of com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region in project skin-composer by raeleus.

the class TexturePacker method writePackFile.

private void writePackFile(File outputDir, String scaledPackFileName, Array<Page> pages) throws IOException {
    File packFile = new File(outputDir, scaledPackFileName + settings.atlasExtension);
    File packDir = packFile.getParentFile();
    packDir.mkdirs();
    if (packFile.exists()) {
        // Make sure there aren't duplicate names.
        TextureAtlasData textureAtlasData = new TextureAtlasData(new FileHandle(packFile), new FileHandle(packFile), false);
        for (Page page : pages) {
            for (Rect rect : page.outputRects) {
                String rectName = Rect.getAtlasName(rect.name, settings.flattenPaths);
                for (Region region : textureAtlasData.getRegions()) {
                    if (region.name.equals(rectName)) {
                        throw new GdxRuntimeException("A region with the name \"" + rectName + "\" has already been packed: " + rect.name);
                    }
                }
            }
        }
    }
    Writer writer = new OutputStreamWriter(new FileOutputStream(packFile, true), "UTF-8");
    for (Page page : pages) {
        writer.write("\n" + page.imageName + "\n");
        writer.write("size: " + page.imageWidth + "," + page.imageHeight + "\n");
        writer.write("format: " + settings.format + "\n");
        writer.write("filter: " + settings.filterMin + "," + settings.filterMag + "\n");
        writer.write("repeat: " + getRepeatValue() + "\n");
        page.outputRects.sort();
        for (Rect rect : page.outputRects) {
            writeRect(writer, page, rect, rect.name);
            Array<Alias> aliases = new Array(rect.aliases.toArray());
            aliases.sort();
            for (Alias alias : aliases) {
                Rect aliasRect = new Rect();
                aliasRect.set(rect);
                alias.apply(aliasRect);
                writeRect(writer, page, aliasRect, alias.name);
            }
        }
    }
    writer.close();
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Array(com.badlogic.gdx.utils.Array) TextureAtlasData(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData) FileOutputStream(java.io.FileOutputStream) Region(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) ImageWriter(javax.imageio.ImageWriter) Writer(java.io.Writer)

Example 9 with Region

use of com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region in project bladecoder-adventure-engine by bladecoder.

the class CustomTextureUnpacker method splitAtlas.

/**
 * Splits an atlas into seperate image and ninepatch files.
 */
public void splitAtlas(TextureAtlasData atlas, String outputDir) {
    // create the output directory if it did not exist yet
    File outputDirFile = new File(outputDir);
    if (!outputDirFile.exists()) {
        outputDirFile.mkdirs();
        EditorLogger.debug(String.format("Creating directory: %s", outputDirFile.getPath()));
    }
    for (Page page : atlas.getPages()) {
        // load the image file belonging to this page as a Buffered Image
        BufferedImage img = null;
        try {
            img = ImageIO.read(page.textureFile.file());
        } catch (IOException e) {
            printExceptionAndExit(e);
        }
        for (Region region : atlas.getRegions()) {
            EditorLogger.debug(String.format("Processing image for %s(%s): x[%s] y[%s] w[%s] h[%s], rotate[%s]", region.name, region.index, region.left, region.top, region.width, region.height, region.rotate));
            // Buffered Image
            if (region.page == page) {
                BufferedImage splitImage = null;
                String extension = null;
                // delegate accordingly
                if (region.splits == null) {
                    splitImage = extractImage(img, region, outputDirFile, 0);
                    extension = OUTPUT_TYPE;
                } else {
                    splitImage = extractNinePatch(img, region, outputDirFile);
                    extension = String.format("9.%s", OUTPUT_TYPE);
                }
                // check if the parent directories of this image file exist
                // and create them if not
                File imgOutput = new File(outputDirFile, String.format("%s.%s", region.index == -1 ? region.name : region.name + "_" + region.index, extension));
                File imgDir = imgOutput.getParentFile();
                if (!imgDir.exists()) {
                    System.out.println(String.format("Creating directory: %s", imgDir.getPath()));
                    imgDir.mkdirs();
                }
                // save the image
                try {
                    ImageIO.write(splitImage, OUTPUT_TYPE, imgOutput);
                } catch (IOException e) {
                    printExceptionAndExit(e);
                }
            }
        }
    }
}
Also used : Region(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region) Page(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Page) IOException(java.io.IOException) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 10 with Region

use of com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region in project ultimate-java by pantinor.

the class GeneratedMapTmxConvert method main.

public static void main(String[] args) throws Exception {
    int TILE_SIZE = 16;
    FileHandle f = new FileHandle("assets/tilemaps/tiles-vga-atlas.txt");
    TextureAtlasData atlas = new TextureAtlasData(f, f.parent(), false);
    String[] mapTileIds = new String[atlas.getRegions().size + 1];
    for (Region r : atlas.getRegions()) {
        int x = r.left / r.width;
        int y = r.top / r.height;
        int i = y * TILE_SIZE + x + 1;
        mapTileIds[i] = r.name;
    }
    List<StaticGeneratedDungeon> dungeons = new ArrayList<StaticGeneratedDungeon>();
    dungeons.add(new StaticGeneratedDungeon("The Dark Pit of Emes the Fallen 01 (tsv).txt"));
    dungeons.add(new StaticGeneratedDungeon("The Dark Pit of Emes the Fallen 10 (tsv).txt"));
    dungeons.add(new StaticGeneratedDungeon("The Dark Pit of Emes the Fallen 20 (tsv).txt"));
    List<String> layers = new ArrayList<String>();
    for (StaticGeneratedDungeon sd : dungeons) {
        StringBuffer data = new StringBuffer();
        for (int y = 0; y < StaticGeneratedDungeon.DIM; y++) {
            for (int x = 0; x < StaticGeneratedDungeon.DIM; x++) {
                Key val = sd.getCell(x, y);
                if (val == null) {
                    val = StaticGeneratedDungeon.Key.NULL;
                }
                data.append(findTileId(mapTileIds, val.getName()) + ",");
            }
            data.append("\n");
        }
        String dl = data.toString();
        dl = dl.substring(0, dl.length() - 2);
        layers.add(dl);
    }
    GeneratedMapTmxConvert c = new GeneratedMapTmxConvert("delve", "tiles-vga.png", StaticGeneratedDungeon.DIM, StaticGeneratedDungeon.DIM, TILE_SIZE, TILE_SIZE, layers.get(0), layers.get(1), layers.get(2), "", "", "");
    FileUtils.writeStringToFile(new File("assets/tilemaps/generatedDungeon.tmx"), c.toString());
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) ArrayList(java.util.ArrayList) TextureAtlasData(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData) Region(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region) File(java.io.File) Key(test.StaticGeneratedDungeon.Key)

Aggregations

Region (com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region)15 File (java.io.File)12 FileHandle (com.badlogic.gdx.files.FileHandle)11 TextureAtlasData (com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData)10 Tile (objects.Tile)6 JAXBContext (javax.xml.bind.JAXBContext)5 Unmarshaller (javax.xml.bind.Unmarshaller)5 BaseMap (objects.BaseMap)5 MapSet (objects.MapSet)5 TileSet (objects.TileSet)5 Page (com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Page)4 BufferedImage (java.awt.image.BufferedImage)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Texture (com.badlogic.gdx.graphics.Texture)2 MapLayer (com.badlogic.gdx.maps.MapLayer)2 TiledMap (com.badlogic.gdx.maps.tiled.TiledMap)2 TmxMapLoader (com.badlogic.gdx.maps.tiled.TmxMapLoader)2 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)2 FileInputStream (java.io.FileInputStream)2