OSDN Git Service

fixed merge conflicts, removed evil e.printStackTrace()...
authorMario Zechner <contact@badlogicgames.com>
Sat, 16 Feb 2013 14:51:31 +0000 (15:51 +0100)
committerMario Zechner <contact@badlogicgames.com>
Sat, 16 Feb 2013 14:51:31 +0000 (15:51 +0100)
1  2 
gdx/src/com/badlogic/gdx/maps/Map.java
gdx/src/com/badlogic/gdx/maps/tiled/TiledMap.java
gdx/src/com/badlogic/gdx/maps/tiled/TmxMapLoader.java

@@@ -1,9 -1,27 +1,30 @@@
  package com.badlogic.gdx.maps;
  
 -public class Map {
 -      
++import com.badlogic.gdx.assets.AssetManager;
++import com.badlogic.gdx.graphics.Texture;
 +import com.badlogic.gdx.utils.Disposable;
 +
+ /**
+  * @brief Generic map
+  * 
+  * A Map instance contains the following data
+  * 
+  * <ul>
+  * <li> MapLayers<ul>
+  *    <li>MapLayer<ul>
+  *            <li>MapObjects<ul>
+  *                    <li>MapObject<ul>
+  *                            <li>Can be: TextureMapObject, CircleMapObject, RectangleMapObject, PolygonMapObject or PolylineMapObject</li>
+  *                            <li>MapProperties</li>
+  *                    </ul></li>
+  *            </ul></li>
+  *            <li>MapProperties</li>  
+  *    </ul></li>
+  * </ul></li>
+  * <li> MapProperties
+  * </ul>
+  */
-       
 +public class Map implements Disposable {
        private MapLayers layers = new MapLayers();
        private MapProperties properties = new MapProperties();
        
        public Map() {
                
        }
 -      
 +
++      /**
++       * Disposes all resources like {@link Texture} instances that
++       * the map owns. Not necessary if the Map was loaded via
++       * an {@link AssetManager}
++       */
 +      @Override
 +      public void dispose () {
 +      }
  }
@@@ -1,14 -1,19 +1,22 @@@
  package com.badlogic.gdx.maps.tiled;
  
 +import com.badlogic.gdx.assets.AssetManager;
 +import com.badlogic.gdx.graphics.Texture;
  import com.badlogic.gdx.maps.Map;
 +import com.badlogic.gdx.utils.Array;
  
+ /**
+  * @brief Represents a Tiled created map, adds the concept of tiles and tilesets
+  * 
+  * @see Map
+  */
  public class TiledMap extends Map {
 -      
        private TiledMapTileSets tilesets;
 +      private Array<Texture> ownedTextures;
        
+       /**
+        * @return collection of tilesets for this map
+        */
        public TiledMapTileSets getTileSets() {
                return tilesets;
        }
@@@ -45,124 -48,96 +48,135 @@@ public class TmxMapLoader extends Synch
        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);