Browse files

libtiled-java: Added support for caching tilesets

For example, when reusing the TMXMapReader to load several TMX files
that all have the same tileset definitions, the loader does not have to
load the tileset images several times, thus making the loading process
much faster.
  • Loading branch information...
1 parent 6a7177d commit 78d98ba75484e00d89a0fe77a6d74cae895c0a02 @oskarwiksten oskarwiksten committed with Feb 17, 2013
Showing with 21 additions and 2 deletions.
  1. +21 −2 util/java/libtiled-java/src/tiled/io/TMXMapReader.java
View
23 util/java/libtiled-java/src/tiled/io/TMXMapReader.java
@@ -66,6 +66,12 @@
private String error;
private final EntityResolver entityResolver = new MapEntityResolver();
private TreeMap<Integer, TileSet> tilesetPerFirstGid;
+ public final TMXMapReaderSettings settings = new TMXMapReaderSettings();
+ private final HashMap<String, TileSet> cachedTilesets = new HashMap<String, TileSet>();
+
+ public static final class TMXMapReaderSettings {
+ public boolean reuseCachedTilesets = false;
+ }
public TMXMapReader() {
}
@@ -318,9 +324,22 @@ private TileSet unmarshalTileset(Node t) throws Exception {
final int tileSpacing = getAttribute(t, "spacing", 0);
final int tileMargin = getAttribute(t, "margin", 0);
- TileSet set = new TileSet();
+ final String name = getAttributeValue(t, "name");
+
+ TileSet set;
+ if (settings.reuseCachedTilesets) {
+ set = cachedTilesets.get(name);
+ if (set != null) {
+ setFirstGidForTileset(set, firstGid);
+ return set;
+ }
+ set = new TileSet();
+ cachedTilesets.put(name, set);
+ } else {
+ set = new TileSet();
+ }
- set.setName(getAttributeValue(t, "name"));
+ set.setName(name);
set.setBaseDir(basedir);
setFirstGidForTileset(set, firstGid);

0 comments on commit 78d98ba

Please sign in to comment.