Search in sources :

Example 1 with Region

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

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 2 with Region

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

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);
                    }
                }
            }
        }
    }
    FileWriter writer = new FileWriter(packFile, true);
    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");
        for (Rect rect : page.outputRects) {
            writeRect(writer, page, rect, rect.name);
            for (Alias alias : rect.aliases) {
                Rect aliasRect = new Rect();
                aliasRect.set(rect);
                alias.apply(aliasRect);
                writeRect(writer, page, aliasRect, alias.name);
            }
        }
    }
    writer.close();
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) TextureAtlasData(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData) FileHandle(com.badlogic.gdx.files.FileHandle) FileWriter(java.io.FileWriter) Region(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region) File(java.io.File)

Example 3 with Region

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

the class CombatMapTmxConvert method main.

public static void main(String[] args) throws Exception {
    File file2 = new File("target/classes/xml/tileset-base.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(TileSet.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    TileSet ts = (TileSet) jaxbUnmarshaller.unmarshal(file2);
    ts.setMaps();
    File file3 = new File("target/classes/xml/maps.xml");
    jaxbContext = JAXBContext.newInstance(MapSet.class);
    jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    MapSet ms = (MapSet) jaxbUnmarshaller.unmarshal(file3);
    ms.init(ts);
    int TILE_SIZE = 16;
    boolean foundShrine = false;
    for (BaseMap map : ms.getMaps()) {
        String tmxmapname = null;
        boolean isShrine = false;
        if (map.getType() == MapType.combat || (map.getType() == MapType.shrine && !foundShrine)) {
            if (map.getType() == MapType.shrine && !foundShrine) {
                foundShrine = true;
                tmxmapname = "shrine.tmx";
                isShrine = true;
            } else if (map.getType() == MapType.combat) {
                tmxmapname = "combat_" + map.getId() + ".tmx";
            }
        } else {
            continue;
        }
        InputStream is = TestMain.class.getResourceAsStream("/data/" + map.getFname());
        byte[] bytes = IOUtils.toByteArray(is);
        Tile[] tiles = new Tile[map.getWidth() * map.getHeight()];
        int pos = isShrine ? 0 : 64;
        for (int y = 0; y < map.getHeight(); ++y) {
            for (int x = 0; x < map.getWidth(); ++x) {
                // convert a byte to an unsigned int value
                int index = bytes[pos] & 0xff;
                pos++;
                Tile tile = ts.getTileByIndex(index);
                if (tile == null) {
                    System.out.println("Tile index cannot be found: " + index + " using index 129 for black space.");
                    tile = ts.getTileByIndex(129);
                }
                tiles[x + y * map.getWidth()] = tile;
            }
        }
        // load the atlas and determine the tile indexes per tilemap position
        FileHandle f = new FileHandle("target/classes/tilemaps/tile-atlas.txt");
        TextureAtlasData atlas = new TextureAtlasData(f, f.parent(), false);
        Tile[] mapTileIds = new Tile[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] = ts.getTileByName(r.name);
        }
        // map layer
        StringBuffer data = new StringBuffer();
        int count = 1;
        int total = 1;
        for (int i = 0; i < tiles.length; i++) {
            Tile t = tiles[i];
            data.append(findTileId(mapTileIds, t.getName()) + ",");
            count++;
            total++;
            if (count > map.getWidth()) {
                data.append("\n");
                count = 1;
            }
            if (total > map.getWidth() * map.getHeight()) {
                break;
            }
        }
        String d = data.toString();
        d = d.substring(0, d.length() - 2);
        Position[] monPos = new Position[16];
        for (int i = 0; i < 16; i++) {
            monPos[i] = new Position(i, (int) bytes[i], 0);
        }
        for (int i = 0; i < 16; i++) {
            monPos[i].startY = (int) bytes[i + 16];
        }
        Position[] playerPos = new Position[8];
        for (int i = 0; i < 8; i++) {
            playerPos[i] = new Position(i, (int) bytes[i + 32], 0);
        }
        for (int i = 0; i < 8; i++) {
            playerPos[i].startY = (int) bytes[i + 40];
        }
        CombatMapTmxConvert c = new CombatMapTmxConvert(map.getFname(), "tiles.png", map.getWidth(), map.getHeight(), TILE_SIZE, TILE_SIZE, TILE_SIZE ^ 2, TILE_SIZE ^ 2, d, monPos, playerPos);
        FileUtils.writeStringToFile(new File("src/main/resources/tilemaps/" + tmxmapname), c.toString());
    }
}
Also used : InputStream(java.io.InputStream) FileHandle(com.badlogic.gdx.files.FileHandle) Tile(objects.Tile) JAXBContext(javax.xml.bind.JAXBContext) TileSet(objects.TileSet) BaseMap(objects.BaseMap) TextureAtlasData(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData) MapSet(objects.MapSet) Region(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File)

Example 4 with Region

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

the class DungeonMapTmxConvert method main.

public static void main(String[] args) throws Exception {
    File file2 = new File("assets/xml/tileset-base.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(TileSet.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    TileSet ts = (TileSet) jaxbUnmarshaller.unmarshal(file2);
    ts.setMaps();
    File file3 = new File("assets/xml/maps.xml");
    jaxbContext = JAXBContext.newInstance(MapSet.class);
    jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    MapSet ms = (MapSet) jaxbUnmarshaller.unmarshal(file3);
    ms.init(ts);
    int TILE_SIZE = 16;
    for (BaseMap map : ms.getMaps()) {
        String tmxmapname = null;
        boolean isAbyss = false;
        if (map.getType() == MapType.dungeon) {
            if (map.getFname().equals("abyss.dng")) {
                tmxmapname = "abyss.tmx";
                isAbyss = true;
            } else {
                tmxmapname = "dungeon_" + map.getId() + ".tmx";
            }
        } else {
            continue;
        }
        InputStream is = new FileInputStream("assets/data/" + map.getFname());
        byte[] bytes = IOUtils.toByteArray(is);
        List<DungeonTile[]> dungeonTiles = new ArrayList<>();
        int pos = 0;
        for (int i = 0; i < 8; i++) {
            DungeonTile[] tiles = new DungeonTile[64];
            for (int y = 0; y < map.getHeight(); ++y) {
                for (int x = 0; x < map.getWidth(); ++x) {
                    int index = bytes[pos] & 0xff;
                    pos++;
                    DungeonTile tile = DungeonTile.getTileByValue(index);
                    tiles[x + y * map.getWidth()] = tile;
                }
            }
            dungeonTiles.add(tiles);
        }
        // load the atlas and determine the tile indexes per tilemap position
        FileHandle f = new FileHandle("assets/tilemaps/tiles-vga-atlas.txt");
        TextureAtlasData atlas = new TextureAtlasData(f, f.parent(), false);
        Tile[] mapTileIds = new Tile[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] = ts.getTileByName(r.name);
        }
        // map layers
        List<String> layers = new ArrayList<>();
        for (int j = 0; j < 8; j++) {
            DungeonTile[] tiles = dungeonTiles.get(j);
            StringBuilder data = new StringBuilder();
            int count = 1;
            int total = 1;
            for (int i = 0; i < tiles.length; i++) {
                DungeonTile t = tiles[i];
                data.append(findTileId(mapTileIds, t.getTileName()) + ",");
                count++;
                total++;
                if (count > map.getWidth()) {
                    data.append("\n");
                    count = 1;
                }
                if (total > map.getWidth() * map.getHeight()) {
                    break;
                }
            }
            String d = data.toString();
            d = d.substring(0, d.length() - 2);
            layers.add(d);
        }
        System.out.println("writing");
        DungeonMapTmxConvert c = new DungeonMapTmxConvert(map.getFname(), "tiles-vga.png", map.getWidth(), map.getHeight(), TILE_SIZE, TILE_SIZE, TILE_SIZE ^ 2, TILE_SIZE ^ 2, layers);
        FileUtils.writeStringToFile(new File(tmxmapname), c.toString());
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileHandle(com.badlogic.gdx.files.FileHandle) ArrayList(java.util.ArrayList) Tile(objects.Tile) JAXBContext(javax.xml.bind.JAXBContext) TileSet(objects.TileSet) BaseMap(objects.BaseMap) FileInputStream(java.io.FileInputStream) TextureAtlasData(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData) MapSet(objects.MapSet) Region(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File)

Example 5 with Region

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

the class UltMapTmxConvert method create.

@Override
public void create() {
    try {
        File file2 = new File("assets/xml/tileset-base.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(TileSet.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        TileSet ts = (TileSet) jaxbUnmarshaller.unmarshal(file2);
        ts.setMaps();
        File file3 = new File("assets/xml/maps.xml");
        jaxbContext = JAXBContext.newInstance(MapSet.class);
        jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        MapSet ms = (MapSet) jaxbUnmarshaller.unmarshal(file3);
        ms.init(ts);
        // load the atlas and determine the tile indexes per tilemap position
        FileHandle f = new FileHandle("assets/tilemaps/latest-atlas.txt");
        TextureAtlasData atlas = new TextureAtlasData(f, f.parent(), false);
        int png_grid_width = 24;
        Tile[] mapTileIds = new Tile[png_grid_width * Constants.tilePixelWidth + 1];
        for (Region r : atlas.getRegions()) {
            int x = r.left / r.width;
            int y = r.top / r.height;
            int i = x + (y * png_grid_width) + 1;
            mapTileIds[i] = ts.getTileByName(r.name);
        }
        for (BaseMap map : ms.getMaps()) {
            if (!map.getFname().endsWith("ult")) {
                continue;
            }
            FileInputStream is = new FileInputStream("assets/data/" + map.getFname());
            byte[] bytes = IOUtils.toByteArray(is);
            Tile[] tiles = new Tile[map.getWidth() * map.getHeight()];
            int pos = 0;
            for (int y = 0; y < map.getHeight(); y++) {
                for (int x = 0; x < map.getWidth(); x++) {
                    // convert a byte to an unsigned int value
                    int index = bytes[pos] & 0xff;
                    pos++;
                    Tile tile = ts.getTileByIndex(index);
                    if (tile == null) {
                        System.out.println("Tile index cannot be found: " + index + " using index 129 for black space.");
                        tile = ts.getTileByIndex(129);
                    }
                    tiles[x + y * map.getWidth()] = tile;
                }
            }
            // map layer
            StringBuilder data = new StringBuilder();
            int count = 1;
            int total = 1;
            for (int i = 0; i < tiles.length; i++) {
                Tile t = tiles[i];
                data.append(findTileMapId(mapTileIds, t.getName())).append(",");
                count++;
                total++;
                if (count > 32) {
                    data.append("\n");
                    count = 1;
                }
            }
            String d = data.toString();
            d = d.substring(0, d.length() - 2);
            List<Person> people = map.getCity().getPeople();
            StringBuilder peopleBuffer = new StringBuilder();
            if (people != null) {
                for (int y = 0; y < map.getHeight(); y++) {
                    for (int x = 0; x < map.getWidth(); x++) {
                        Person p = findPersonAtCoords(people, x, y);
                        if (p == null) {
                            peopleBuffer.append("0,");
                        } else {
                            peopleBuffer.append(findTileMapId(mapTileIds, p.getTile().getName())).append(",");
                        }
                    }
                    peopleBuffer.append("\n");
                }
            }
            String p = peopleBuffer.toString();
            if (p == null || p.length() < 1) {
                count = 1;
                // make empty
                for (int i = 0; i < map.getWidth() * map.getHeight(); i++) {
                    peopleBuffer.append("0,");
                    count++;
                    if (count > map.getWidth()) {
                        peopleBuffer.append("\n");
                        count = 1;
                    }
                }
                p = peopleBuffer.toString();
            }
            p = p.substring(0, p.length() - 2);
            Formatter c = new Formatter(map.getFname(), "latest.png", map.getWidth(), map.getHeight(), Constants.tilePixelWidth, Constants.tilePixelWidth, d, p, people);
            String tmxFName = String.format("tmx/map_%s_%s.tmx", map.getId(), map.getCity().getName().replace(" ", ""));
            FileUtils.writeStringToFile(new File(tmxFName), c.toString());
            System.out.printf("Wrote: %s\n", tmxFName);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("DONE");
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) Tile(objects.Tile) JAXBContext(javax.xml.bind.JAXBContext) TileSet(objects.TileSet) BaseMap(objects.BaseMap) FileInputStream(java.io.FileInputStream) TextureAtlasData(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData) MapSet(objects.MapSet) Region(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File) Person(objects.Person)

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