Search in sources :

Example 1 with TiledMapTileSet

use of com.badlogic.gdx.maps.tiled.TiledMapTileSet in project libgdx by libgdx.

the class TiledMapPacker method processInputDir.

/** You can either run the {@link TiledMapPacker#main(String[])} method or reference this class in your own project and call
	 * this method. If working with libGDX sources, you can also run this file to create a run configuration then export it as a
	 * Runnable Jar. To run from a nightly build:
	 * 
	 * <code> <br><br>
	 * Linux / OS X <br>
	   java -cp gdx.jar:gdx-natives.jar:gdx-backend-lwjgl.jar:gdx-backend-lwjgl-natives.jar:gdx-tiled-preprocessor.jar:extensions/gdx-tools/gdx-tools.jar
	    com.badlogic.gdx.tiledmappacker.TiledMapPacker inputDir [outputDir]  [--strip-unused] [--combine-tilesets] [-v]
	 * <br><br>
	 * 
	 * Windows <br>
	   java -cp gdx.jar;gdx-natives.jar;gdx-backend-lwjgl.jar;gdx-backend-lwjgl-natives.jar;gdx-tiled-preprocessor.jar;extensions/gdx-tools/gdx-tools.jar
	    com.badlogic.gdx.tiledmappacker.TiledMapPacker inputDir [outputDir]  [--strip-unused] [--combine-tilesets] [-v]
	 * <br><br> </code>
	 * 
	 * Keep in mind that this preprocessor will need to load the maps by using the {@link TmxMapLoader} loader and this in turn
	 * will need a valid OpenGL context to work.
	 * 
	 * Process a directory containing TMX map files representing Tiled maps and produce multiple, or a single, TextureAtlas as well
	 * as new processed TMX map files, correctly referencing the generated {@link TextureAtlas} by using the "atlas" custom map
	 * property. */
public void processInputDir(Settings texturePackerSettings) throws IOException {
    FileHandle inputDirHandle = new FileHandle(inputDir.getCanonicalPath());
    File[] mapFilesInCurrentDir = inputDir.listFiles(new TmxFilter());
    tilesetsToPack = new ObjectMap<String, TiledMapTileSet>();
    // Processes the maps inside inputDir
    for (File mapFile : mapFilesInCurrentDir) {
        processSingleMap(mapFile, inputDirHandle, texturePackerSettings);
    }
    processSubdirectories(inputDirHandle, texturePackerSettings);
    boolean combineTilesets = this.settings.combineTilesets;
    if (combineTilesets == true) {
        packTilesets(inputDirHandle, texturePackerSettings);
    }
}
Also used : TiledMapTileSet(com.badlogic.gdx.maps.tiled.TiledMapTileSet) FileHandle(com.badlogic.gdx.files.FileHandle) File(java.io.File)

Example 2 with TiledMapTileSet

use of com.badlogic.gdx.maps.tiled.TiledMapTileSet in project libgdx by libgdx.

the class TiledMapPacker method packTilesets.

/** Traverse the specified tilesets, optionally lookup the used ids and pass every tile image to the {@link TexturePacker},
	 * optionally ignoring unused tile ids */
private void packTilesets(FileHandle inputDirHandle, Settings texturePackerSettings) throws IOException {
    BufferedImage tile;
    Vector2 tileLocation;
    Graphics g;
    packer = new TexturePacker(texturePackerSettings);
    for (TiledMapTileSet set : tilesetsToPack.values()) {
        String tilesetName = set.getName();
        System.out.println("Processing tileset " + tilesetName);
        IntArray usedIds = this.settings.stripUnusedTiles ? getUsedIdsBucket(tilesetName, -1) : null;
        int tileWidth = set.getProperties().get("tilewidth", Integer.class);
        int tileHeight = set.getProperties().get("tileheight", Integer.class);
        int firstgid = set.getProperties().get("firstgid", Integer.class);
        String imageName = set.getProperties().get("imagesource", String.class);
        TileSetLayout layout = new TileSetLayout(firstgid, set, inputDirHandle);
        for (int gid = layout.firstgid, i = 0; i < layout.numTiles; gid++, i++) {
            boolean verbose = this.settings.verbose;
            if (usedIds != null && !usedIds.contains(gid)) {
                if (verbose) {
                    System.out.println("Stripped id #" + gid + " from tileset \"" + tilesetName + "\"");
                }
                continue;
            }
            tileLocation = layout.getLocation(gid);
            tile = new BufferedImage(tileWidth, tileHeight, BufferedImage.TYPE_4BYTE_ABGR);
            g = tile.createGraphics();
            g.drawImage(layout.image, 0, 0, tileWidth, tileHeight, (int) tileLocation.x, (int) tileLocation.y, (int) tileLocation.x + tileWidth, (int) tileLocation.y + tileHeight, null);
            if (verbose) {
                System.out.println("Adding " + tileWidth + "x" + tileHeight + " (" + (int) tileLocation.x + ", " + (int) tileLocation.y + ")");
            }
            // AtlasTmxMapLoader expects every tileset's index to begin at zero for the first tile in every tileset.
            // so the region's adjusted gid is (gid - layout.firstgid). firstgid will be added back in AtlasTmxMapLoader on load
            int adjustedGid = gid - layout.firstgid;
            final String separator = "_";
            String regionName = tilesetName + separator + adjustedGid;
            packer.addImage(tile, regionName);
        }
    }
    String tilesetOutputDir = outputDir.toString() + "/" + this.settings.tilesetOutputDirectory;
    File relativeTilesetOutputDir = new File(tilesetOutputDir);
    File outputDirTilesets = new File(relativeTilesetOutputDir.getCanonicalPath());
    outputDirTilesets.mkdirs();
    packer.pack(outputDirTilesets, this.settings.atlasOutputName + ".atlas");
}
Also used : Graphics(java.awt.Graphics) IntArray(com.badlogic.gdx.utils.IntArray) Vector2(com.badlogic.gdx.math.Vector2) TiledMapTileSet(com.badlogic.gdx.maps.tiled.TiledMapTileSet) TexturePacker(com.badlogic.gdx.tools.texturepacker.TexturePacker) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 3 with TiledMapTileSet

use of com.badlogic.gdx.maps.tiled.TiledMapTileSet in project libgdx by libgdx.

the class TiledMapAssetManagerTest method create.

@Override
public void create() {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
    camera = new OrthographicCamera();
    camera.setToOrtho(false, (w / h) * 10, 10);
    camera.zoom = 2;
    camera.update();
    cameraController = new OrthoCamController(camera);
    Gdx.input.setInputProcessor(cameraController);
    font = new BitmapFont();
    batch = new SpriteBatch();
    assetManager = new AssetManager();
    assetManager.setLoader(TiledMap.class, new TmxMapLoader(new InternalFileHandleResolver()));
    assetManager.load("data/maps/tiled/isometric_grass_and_water.tmx", TiledMap.class);
    assetManager.finishLoading();
    map = assetManager.get("data/maps/tiled/isometric_grass_and_water.tmx");
    renderer = new IsometricTiledMapRenderer(map, 1f / 64f);
    String mapCustomValue = map.getProperties().get(MAP_PROPERTY_NAME, String.class);
    Gdx.app.log("TiledMapAssetManagerTest", "Property : " + MAP_PROPERTY_NAME + ", Value : " + mapCustomValue);
    if (!MAP_PROPERTY_VALUE.equals(mapCustomValue)) {
        throw new RuntimeException("Failed to get map properties");
    }
    boolean boolCustomValue = map.getProperties().get(BOOL_PROPERTY_NAME, Boolean.class);
    Gdx.app.log("TiledMapAssetManagerTest", "Property : " + BOOL_PROPERTY_NAME + ", Value : " + boolCustomValue);
    if (boolCustomValue != BOOL_PROPERTY_VALUE) {
        throw new RuntimeException("Failed to get boolean map properties");
    }
    int intCustomValue = map.getProperties().get(INT_PROPERTY_NAME, Integer.class);
    Gdx.app.log("TiledMapAssetManagerTest", "Property : " + INT_PROPERTY_NAME + ", Value : " + intCustomValue);
    if (intCustomValue != INT_PROPERTY_VALUE) {
        throw new RuntimeException("Failed to get int map properties");
    }
    float floatCustomValue = map.getProperties().get(FLOAT_PROPERTY_NAME, Float.class);
    Gdx.app.log("TiledMapAssetManagerTest", "Property : " + FLOAT_PROPERTY_NAME + ", Value : " + floatCustomValue);
    if (floatCustomValue != FLOAT_PROPERTY_VALUE) {
        throw new RuntimeException("Failed to get float map properties");
    }
    TiledMapTileSet tileset = map.getTileSets().getTileSet(0);
    String tilesetCustomValue = tileset.getProperties().get(TILESET_PROPERTY_NAME, String.class);
    if (!TILESET_PROPERTY_VALUE.equals(tilesetCustomValue)) {
        throw new RuntimeException("Failed to get tileset properties");
    }
    TiledMapTile tile = tileset.getTile(1);
    String tileCustomValue = tile.getProperties().get(TILE_PROPERTY_NAME, String.class);
    if (!TILE_PROPERTY_VALUE.equals(tileCustomValue)) {
        throw new RuntimeException("Failed to get tile properties");
    }
    MapLayer layer = map.getLayers().get(0);
    String layerCustomValue = layer.getProperties().get(LAYER_PROPERTY_NAME, String.class);
    if (!LAYER_PROPERTY_VALUE.equals(layerCustomValue)) {
        throw new RuntimeException("Failed to get layer properties");
    }
}
Also used : TmxMapLoader(com.badlogic.gdx.maps.tiled.TmxMapLoader) AssetManager(com.badlogic.gdx.assets.AssetManager) TiledMapTileSet(com.badlogic.gdx.maps.tiled.TiledMapTileSet) MapLayer(com.badlogic.gdx.maps.MapLayer) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) OrthoCamController(com.badlogic.gdx.tests.utils.OrthoCamController) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) InternalFileHandleResolver(com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver) TiledMapTile(com.badlogic.gdx.maps.tiled.TiledMapTile) IsometricTiledMapRenderer(com.badlogic.gdx.maps.tiled.renderers.IsometricTiledMapRenderer) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Example 4 with TiledMapTileSet

use of com.badlogic.gdx.maps.tiled.TiledMapTileSet in project libgdx by libgdx.

the class TiledMapPacker method processSingleMap.

private void processSingleMap(File mapFile, FileHandle dirHandle, Settings texturePackerSettings) throws IOException {
    boolean combineTilesets = this.settings.combineTilesets;
    if (combineTilesets == false) {
        tilesetUsedIds = new HashMap<String, IntArray>();
        tilesetsToPack = new ObjectMap<String, TiledMapTileSet>();
    }
    map = mapLoader.load(mapFile.getCanonicalPath());
    // if enabled, build a list of used tileids for the tileset used by this map
    boolean stripUnusedTiles = this.settings.stripUnusedTiles;
    if (stripUnusedTiles) {
        stripUnusedTiles();
    } else {
        for (TiledMapTileSet tileset : map.getTileSets()) {
            String tilesetName = tileset.getName();
            if (!tilesetsToPack.containsKey(tilesetName)) {
                tilesetsToPack.put(tilesetName, tileset);
            }
        }
    }
    if (combineTilesets == false) {
        FileHandle tmpHandle = new FileHandle(mapFile.getName());
        this.settings.atlasOutputName = tmpHandle.nameWithoutExtension();
        packTilesets(dirHandle, texturePackerSettings);
    }
    FileHandle tmxFile = new FileHandle(mapFile.getCanonicalPath());
    writeUpdatedTMX(map, tmxFile);
}
Also used : IntArray(com.badlogic.gdx.utils.IntArray) TiledMapTileSet(com.badlogic.gdx.maps.tiled.TiledMapTileSet) FileHandle(com.badlogic.gdx.files.FileHandle)

Example 5 with TiledMapTileSet

use of com.badlogic.gdx.maps.tiled.TiledMapTileSet in project AmazingMaze by TheVirtualMachine.

the class Assets method loadMapResources.

/** Helper method for loading the map resources. */
private void loadMapResources() {
    manager.load(GAME_ATLAS_LOCATION, TextureAtlas.class);
    manager.finishLoadingAsset(GAME_ATLAS_LOCATION);
    // Reference used for readability.
    TextureAtlas atlas = manager.get(Assets.GAME_ATLAS_LOCATION, TextureAtlas.class);
    tiles = new TiledMapTileSet();
    StaticTiledMapTile background = new StaticTiledMapTile(atlas.findRegion(BACKGROUND));
    StaticTiledMapTile barrier = new StaticTiledMapTile(atlas.findRegion(BARRIER));
    StaticTiledMapTile placeholder = new StaticTiledMapTile(atlas.findRegion(PLACEHOLDER));
    background.setId(TileIDs.computeID(TileIDs.BACKGROUND));
    barrier.setId(TileIDs.computeID(TileIDs.BARRIER));
    placeholder.setId(TileIDs.computeID(TileIDs.PLACEHOLDER));
    tiles.putTile(background.getId(), background);
    tiles.putTile(barrier.getId(), barrier);
    tiles.putTile(placeholder.getId(), placeholder);
    StaticTiledMapTile verticalOn = new StaticTiledMapTile(atlas.findRegion(VERTICAL + ON_MODIFIER));
    StaticTiledMapTile verticalOff = new StaticTiledMapTile(atlas.findRegion(VERTICAL + OFF_MODIFIER));
    StaticTiledMapTile verticalUnknown = new StaticTiledMapTile(atlas.findRegion(VERTICAL + UNKNOWN_MODIFIER));
    verticalOn.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.VERTICAL, TileIDs.ON));
    verticalOff.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.VERTICAL, TileIDs.OFF));
    verticalUnknown.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.VERTICAL, TileIDs.UNKNOWN));
    tiles.putTile(verticalOn.getId(), verticalOn);
    tiles.putTile(verticalOff.getId(), verticalOff);
    tiles.putTile(verticalUnknown.getId(), verticalUnknown);
    loadGates(atlas, ON_MODIFIER, UP_MODIFIER, TileIDs.ON, TileIDs.UP_GATE);
    loadGates(atlas, ON_MODIFIER, DOWN_MODIFIER, TileIDs.ON, TileIDs.DOWN_GATE);
    loadGates(atlas, OFF_MODIFIER, UP_MODIFIER, TileIDs.OFF, TileIDs.UP_GATE);
    loadGates(atlas, OFF_MODIFIER, DOWN_MODIFIER, TileIDs.OFF, TileIDs.DOWN_GATE);
    loadGates(atlas, UNKNOWN_MODIFIER, UP_MODIFIER, TileIDs.UNKNOWN, TileIDs.UP_GATE);
    loadGates(atlas, UNKNOWN_MODIFIER, DOWN_MODIFIER, TileIDs.UNKNOWN, TileIDs.DOWN_GATE);
    StaticTiledMapTile turnOnUpLeft = new StaticTiledMapTile(atlas.findRegion(TURN + ON_MODIFIER + UP_MODIFIER + LEFT_MODIFIER));
    StaticTiledMapTile turnOffUpLeft = new StaticTiledMapTile(atlas.findRegion(TURN + OFF_MODIFIER + UP_MODIFIER + LEFT_MODIFIER));
    StaticTiledMapTile turnOnUpRight = new StaticTiledMapTile(atlas.findRegion(TURN + ON_MODIFIER + UP_MODIFIER + RIGHT_MODIFIER));
    StaticTiledMapTile turnOffUpRight = new StaticTiledMapTile(atlas.findRegion(TURN + OFF_MODIFIER + UP_MODIFIER + RIGHT_MODIFIER));
    StaticTiledMapTile turnOnDownLeft = new StaticTiledMapTile(atlas.findRegion(TURN + ON_MODIFIER + DOWN_MODIFIER + LEFT_MODIFIER));
    StaticTiledMapTile turnOffDownLeft = new StaticTiledMapTile(atlas.findRegion(TURN + OFF_MODIFIER + DOWN_MODIFIER + LEFT_MODIFIER));
    StaticTiledMapTile turnOnDownRight = new StaticTiledMapTile(atlas.findRegion(TURN + ON_MODIFIER + DOWN_MODIFIER + RIGHT_MODIFIER));
    StaticTiledMapTile turnOffDownRight = new StaticTiledMapTile(atlas.findRegion(TURN + OFF_MODIFIER + DOWN_MODIFIER + RIGHT_MODIFIER));
    turnOnUpLeft.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.TURN, TileIDs.ON, TileIDs.UP_LEFT));
    turnOffUpLeft.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.TURN, TileIDs.OFF, TileIDs.UP_LEFT));
    turnOnUpRight.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.TURN, TileIDs.ON, TileIDs.UP_RIGHT));
    turnOffUpRight.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.TURN, TileIDs.OFF, TileIDs.UP_RIGHT));
    turnOnDownLeft.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.TURN, TileIDs.ON, TileIDs.DOWN_LEFT));
    turnOffDownLeft.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.TURN, TileIDs.OFF, TileIDs.DOWN_LEFT));
    turnOnDownRight.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.TURN, TileIDs.ON, TileIDs.DOWN_RIGHT));
    turnOffDownRight.setId(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.TURN, TileIDs.OFF, TileIDs.DOWN_RIGHT));
    tiles.putTile(turnOnUpLeft.getId(), turnOnUpLeft);
    tiles.putTile(turnOffUpLeft.getId(), turnOffUpLeft);
    tiles.putTile(turnOnUpRight.getId(), turnOnUpRight);
    tiles.putTile(turnOffUpRight.getId(), turnOffUpRight);
    tiles.putTile(turnOnDownLeft.getId(), turnOnDownLeft);
    tiles.putTile(turnOffDownLeft.getId(), turnOffDownLeft);
    tiles.putTile(turnOnDownRight.getId(), turnOnDownRight);
    tiles.putTile(turnOffDownRight.getId(), turnOffDownRight);
    loadFish(atlas, BLUE_MODIFIER, TileIDs.BLUE);
    loadFish(atlas, PURPLE_MODIFIER, TileIDs.PURPLE);
    loadFish(atlas, GREEN_MODIFIER, TileIDs.GREEN);
    loadFish(atlas, RED_MODIFIER, TileIDs.RED);
    loadFish(atlas, ORANGE_MODIFIER, TileIDs.ORANGE);
    StaticTiledMapTile cheese = new StaticTiledMapTile(atlas.findRegion(CHEESE));
    cheese.setId(TileIDs.computeID(TileIDs.POWERUP_RANGE, TileIDs.CHEESE));
    tiles.putTile(cheese.getId(), cheese);
}
Also used : TiledMapTileSet(com.badlogic.gdx.maps.tiled.TiledMapTileSet) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile)

Aggregations

TiledMapTileSet (com.badlogic.gdx.maps.tiled.TiledMapTileSet)5 FileHandle (com.badlogic.gdx.files.FileHandle)2 IntArray (com.badlogic.gdx.utils.IntArray)2 File (java.io.File)2 AssetManager (com.badlogic.gdx.assets.AssetManager)1 InternalFileHandleResolver (com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver)1 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)1 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)1 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)1 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)1 MapLayer (com.badlogic.gdx.maps.MapLayer)1 TiledMapTile (com.badlogic.gdx.maps.tiled.TiledMapTile)1 TmxMapLoader (com.badlogic.gdx.maps.tiled.TmxMapLoader)1 IsometricTiledMapRenderer (com.badlogic.gdx.maps.tiled.renderers.IsometricTiledMapRenderer)1 StaticTiledMapTile (com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile)1 Vector2 (com.badlogic.gdx.math.Vector2)1 OrthoCamController (com.badlogic.gdx.tests.utils.OrthoCamController)1 TexturePacker (com.badlogic.gdx.tools.texturepacker.TexturePacker)1 Graphics (java.awt.Graphics)1 BufferedImage (java.awt.image.BufferedImage)1