|
| 1 | +--- |
| 2 | +title: "Tiled Map Packer" |
| 3 | +--- |
| 4 | + |
| 5 | +# TiledMapPacker |
| 6 | + |
| 7 | +> **Warning**, while the latest TiledMapPacker is available for download. |
| 8 | +> In order to use maps created with it. You must be using the latest SNAPSHOT version of libGDX, currently *1.13.2-SNAPSHOT*. |
| 9 | +
|
| 10 | +You can download the runnable JAR release of **TiledMapPacker**, [here](https://libgdx-nightlies.s3.eu-central-1.amazonaws.com/libgdx-runnables/runnable-tiledmappacker.jar). |
| 11 | + |
| 12 | +*Offline tool for processing one or more Tiled maps (`.tmx` or `.tmj`) into a single optimized `TextureAtlas`, combining tilesets, |
| 13 | +image layers, and individual images into an atlas for use with the `AtlasTmxMapLoader` or `AtlasTmjMapLoader`.* |
| 14 | + |
| 15 | + |
| 16 | +## Why use TiledMapPacker? |
| 17 | + |
| 18 | +TiledMapPacker is designed to optimize your Tiled maps for better runtime performance: |
| 19 | + |
| 20 | +- **Fewer draw calls** – By combining multiple tilesets, images, and layers into a single optimized `TextureAtlas`, |
| 21 | +your game can render entire maps faster with fewer texture switches. This is especially helpful for parallax backgrounds made using imagelayers, and maps that use a variety of tilesets. |
| 22 | +- **Optional tile stripping** – Unused tiles can be omitted to reduce file size and memory use. |
| 23 | + |
| 24 | + |
| 25 | +## Running TiledMapPacker |
| 26 | + |
| 27 | +### Stand‑alone JAR |
| 28 | + |
| 29 | +``` |
| 30 | +# Creates an output directory named "output" next to INPUTDIR |
| 31 | +java -jar runnable-tiledmappacker.jar ./maps |
| 32 | +
|
| 33 | +# Explicit output directory |
| 34 | +java -jar runnable-tiledmappacker.jar ./maps ./maps-packed |
| 35 | +
|
| 36 | +# Explicit output directory, as well maps needing .tiled-project files to load |
| 37 | +java -jar runnable-tiledmappacker.jar ./maps ./maps-packed ./customClass.tiled-project |
| 38 | +
|
| 39 | +# Various options are available as well such as verbose logging and stripping unused tiles |
| 40 | +java -jar runnable-tiledmappacker.jar ./maps ./maps-packed -v --strip-unused` |
| 41 | +``` |
| 42 | + |
| 43 | +> **Note:** TiledMapPacker internally uses `TmxMapLoader` and `TmjMapLoader`, which require a valid OpenGL context. The runnable JAR automatically creates a minimal LwjglApplication that opens a small window. |
| 44 | +
|
| 45 | + |
| 46 | +## Positional arguments |
| 47 | + |
| 48 | +| Argument | Description | |
| 49 | +|------------------- |-------------| |
| 50 | +| **INPUTDIR** | Folder that contains the maps. Searches recursively for `.tmx` and `.tmj` files.| |
| 51 | +| **OUTPUTDIR** | *(optional)* Directory where the processed maps and atlases are written. Defaults to a sibling directory named **output**.| |
| 52 | +| **PROJECTFILEPATH** | *(optional)* Path to a Tiled `.tiled-project` file when your maps rely on *custom classes*.| |
| 53 | + |
| 54 | +## Flags |
| 55 | + |
| 56 | +| Flag | Effect | |
| 57 | +| -------------------- |-------------------------------------------------------------------------------| |
| 58 | +| `--strip-unused` | Omit tiles that never appear in any layer. | |
| 59 | +| `--combine-tilesets` | Combine all tilesets from all maps into a single mega‑atlas (not recommended). | |
| 60 | +| `--ignore-coi` | Skip "*collection‑of‑images*" tilesets. | |
| 61 | +| `-v` | Verbose output (lists every tile or image that is packed/stripped). | |
| 62 | + |
| 63 | + |
| 64 | +## Default Per‑Map Atlases vs Combined |
| 65 | + |
| 66 | +- **Per‑map** (default) – Each individual map gets its own atlas. |
| 67 | + |
| 68 | + *Command*: `java -jar tiledmappacker.jar ./maps`. |
| 69 | + |
| 70 | +- **Combined** – All tilesets across every map are packed into a single giant |
| 71 | + atlas, so you only need one atlas for all maps. This option has been around since the inception of the TiledMapPacker but still *not recommended* for use as nested folders and absolute tileset paths may break it. |
| 72 | + |
| 73 | + *Option*: `--combine-tilesets`. |
| 74 | + |
| 75 | + |
| 76 | +## Typical Workflow |
| 77 | + |
| 78 | +1. Design your map(s) in Tiled as usual. |
| 79 | +2. Run **TiledMapPacker** whenever your art or map files change. |
| 80 | +3. The packer creates updated map files (`.tmx` or `.tmj`) and a `tileset/` directory containing the generated `.atlas` and images. |
| 81 | +4. Make sure you keep your original tileset definitions (`.tsx` or `.tsj`) and place them alongside the newly generated map and atlas files. |
| 82 | +5. Then load the map exactly as you would a regular map: |
| 83 | + ```java |
| 84 | + TiledMap map = new AtlasTmxMapLoader().load("maps/testLevel.tmx"); |
| 85 | + ``` |
| 86 | + Or through the AssetManager class as well, like any other map. |
| 87 | + ```java |
| 88 | + assetManager.setLoader(TiledMap.class, new AtlasTmxMapLoader(new InternalFileHandleResolver())); |
| 89 | + assetManager.load("maps/testLevel.tmx", TiledMap.class); |
| 90 | + ``` |
| 91 | + |
| 92 | + |
| 93 | +## Handling ImageLayers |
| 94 | + |
| 95 | +Image‑layers are detected automatically. Each image is renamed to a safe region |
| 96 | +ID (prefixed with `atlas_imagelayer_`), packed into the atlas, and the map file |
| 97 | +is rewritten. The modified atlas map loaders resolve these names automatically. |
| 98 | + |
| 99 | + |
| 100 | +## Limitations |
| 101 | + |
| 102 | +- **XML tile‑layer encoding** is not supported – stick to CSV or Base64 layers. |
| 103 | +- Having a `.tmx` *and* `.tmj` with the **same basename** in the same folder |
| 104 | + (e.g. `level1.tmx` and `level1.tmj`) will cause region‑name collisions and the |
| 105 | + packer will abort. |
| 106 | +- **Collection‑of‑images** tilesets are packed automatically, but if they are |
| 107 | + not used in tile layers and not meant to be rendered you can exclude them with `--ignore-coi`. |
| 108 | +- `--combine-tilesets` is experimental. Complex folder hierarchies or absolute |
| 109 | + tileset paths may fail to resolve correctly. |
| 110 | + |
| 111 | +## Results |
| 112 | + |
| 113 | +In a worse case scenario we have below a map created with 3 different tilesets, one of those tilesets being a |
| 114 | +"collection-of-images". As well as 8 image layers being used to for parallax background effects. |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +You can see the amount of draw calls being wasted if this map were loaded through the `TmxMapLoader`. |
| 119 | +In the below example we range 12 to 14 depending on where we are on the screen. |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +But choosing to use the TiledMapPacker and an `AtlasTmxMapLoader`. We can significantly reduce our draw calls. |
| 124 | +In this case, down to 1. |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +## Example Tests |
| 131 | +* [Running TiledMapPacker](https://github.com/libgdx/libgdx/blob/master/extensions/gdx-tools/src/com/badlogic/gdx/tiledmappacker/TiledMapPackerTest.java) |
| 132 | +* [Rendering Packed Atlas Tiled Maps](https://github.com/libgdx/libgdx/blob/master/extensions/gdx-tools/src/com/badlogic/gdx/tiledmappacker/TiledMapPackerTestRender.java) |
| 133 | + |
0 commit comments