private static final int FLAG_FLIP_DIAGONALLY = 0x20000000;
private static final int MASK_CLEAR = 0xE0000000;
- private AssetManager assetManager;
- private FileHandle tmx;
- private XmlReader xml;
+ private XmlReader xml = new XmlReader();
+ private Element root;
+
+ public TmxMapLoader() {
+ super(new InternalFileHandleResolver());
+ }
+ /**
+ * Creates loader
+ *
+ * @param resolver
+ */
public TmxMapLoader(FileHandleResolver resolver) {
super(resolver);
}
/**
- * Loads a .tmx file
- *
- * @param assetManager
- * @param fileName
- * @param parameter not used for now
- * @return loaded TiledMap instance
+ * Loads the {@link TiledMap} from the given file. The file is
+ * resolved via the {@link FileHandleResolver} set in the constructor
+ * of this class. By default it will resolve to an internal file.
+ * @param fileName the filename
+ * @return the TiledMap
*/
- @Override
- public TiledMap load(AssetManager assetManager, String fileName, Parameters parameter) {
- this.assetManager = assetManager;
- this.tmx = resolve(fileName);
- this.xml = new XmlReader();
+ public TiledMap load(String fileName) {
try {
- XmlReader.Element root = xml.parse(tmx);
- TiledMap map = new TiledMap();
- Element properties = root.getChildByName("properties");
- if (properties != null) {
- loadProperties(map.getProperties(), properties);
- }
- Array<Element> tilesets = root.getChildrenByName("tileset");
- for (Element element : tilesets) {
- loadTileSet(map, element);
- root.removeChild(element);
- }
- for (int i = 0, j = root.getChildCount(); i < j; i++) {
- Element element = root.getChild(i);
- String name = element.getName();
- if (name.equals("layer")) {
- loadTileLayer(map, element);
- } else if (name.equals("objectgroup")) {
- loadObjectGroup(map, element);
- }
+ FileHandle tmxFile = resolve(fileName);
+ root = xml.parse(tmxFile);
+ ObjectMap<String, Texture> textures = new ObjectMap<String, Texture>();
+ for(FileHandle textureFile: loadTilesets(root, tmxFile)) {
+ textures.put(textureFile.path(), new Texture(textureFile));
}
+ DirectImageResolver imageResolver = new DirectImageResolver(textures);
+ TiledMap map = loadTilemap(root, tmxFile, imageResolver);
+ map.setOwnedTextures(textures.values().toArray());
return map;
+ } catch(IOException e) {
+ throw new GdxRuntimeException("Couldn't load tilemap '" + fileName + "'", e);
+ }
+ }
+
+ @Override
+ public TiledMap load(AssetManager assetManager, String fileName, Parameters parameter) {
+ FileHandle tmxFile = resolve(fileName);
+ try {
+ return loadTilemap(root, tmxFile, new AssetManagerImageResolver(assetManager));
} catch (Exception e) {
-- e.printStackTrace();
++ throw new GdxRuntimeException("Couldn't load tilemap '" + fileName + "'", e);
}
-- return null;
}
+ /**
+ * Retrieves TiledMap resource dependencies
+ *
+ * @param fileName
+ * @param parameter not used for now
+ * @return dependencies for the given .tmx file
+ */
@Override
public Array<AssetDescriptor> getDependencies(String fileName, Parameters parameter) {
Array<AssetDescriptor> dependencies = new Array<AssetDescriptor>();
- XmlReader xml = new XmlReader();
try {
- FileHandle tmx = resolve(fileName);
- Element root = xml.parse(tmx);
- Array<Element> tilesets = root.getChildrenByName("tileset");
- for (Element tileset : tilesets) {
- String source = tileset.getAttribute("source", null);
- FileHandle image = null;
- if (source != null) {
- FileHandle tsx = getRelativeFileHandle(tmx, source);
- tileset = xml.parse(tsx);
- String imageSource = tileset.getChildByName("image").getAttribute("source");
- image = getRelativeFileHandle(tsx, imageSource);
- } else {
- String imageSource = tileset.getChildByName("image").getAttribute("source");
- image = getRelativeFileHandle(tmx, imageSource);
- }
+ FileHandle tmxFile = resolve(fileName);
+ root = xml.parse(tmxFile);
+ for(FileHandle image: loadTilesets(root, tmxFile)) {
dependencies.add(new AssetDescriptor(image.path(), Texture.class));
}
++ return dependencies;
} catch (IOException e) {
-- e.printStackTrace();
++ throw new GdxRuntimeException("Couldn't load tilemap '" + fileName + "'", e);
+ }
- return dependencies;
+ }
+
+ /**
+ * Loads the map data, given the XML root element and an {@link ImageResolver} used
+ * to return the tileset Textures
+ * @param root the XML root element
+ * @param tmxFile the Filehandle of the tmx file
+ * @param imageResolver the {@link ImageResolver}
+ * @return the {@link TiledMap}
+ */
+ private TiledMap loadTilemap(Element root, FileHandle tmxFile, ImageResolver imageResolver) {
+ TiledMap map = new TiledMap();
+ Element properties = root.getChildByName("properties");
+ if (properties != null) {
+ loadProperties(map.getProperties(), properties);
+ }
+ Array<Element> tilesets = root.getChildrenByName("tileset");
+ for (Element element : tilesets) {
+ loadTileSet(map, element, tmxFile, imageResolver);
+ root.removeChild(element);
+ }
+ for (int i = 0, j = root.getChildCount(); i < j; i++) {
+ Element element = root.getChild(i);
+ String name = element.getName();
+ if (name.equals("layer")) {
+ loadTileLayer(map, element);
+ } else if (name.equals("objectgroup")) {
+ loadObjectGroup(map, element);
+ }
+ }
+ return map;
+ }
+
+ /**
+ * Loads the tilesets
+ * @param root the root XML element
+ * @return a list of filenames for images containing tiles
+ * @throws IOException
+ */
+ private Array<FileHandle> loadTilesets(Element root, FileHandle tmxFile) throws IOException {
+ Array<FileHandle> images = new Array<FileHandle>();
+ for (Element tileset : root.getChildrenByName("tileset")) {
+ String source = tileset.getAttribute("source", null);
+ FileHandle image = null;
+ if (source != null) {
+ FileHandle tsx = getRelativeFileHandle(tmxFile, source);
+ tileset = xml.parse(tsx);
+ String imageSource = tileset.getChildByName("image").getAttribute("source");
+ image = getRelativeFileHandle(tsx, imageSource);
+ } else {
+ String imageSource = tileset.getChildByName("image").getAttribute("source");
+ image = getRelativeFileHandle(tmxFile, imageSource);
+ }
+ images.add(image);
}
- return dependencies;
+ return images;
}
- public void loadTileSet(TiledMap map, Element element) {
+ private void loadTileSet(TiledMap map, Element element, FileHandle tmxFile, ImageResolver imageResolver) {
if (element.getName().equals("tileset")) {
String name = element.get("name", null);
int firstgid = element.getIntAttribute("firstgid", 1);