OSDN Git Service

fixed MapRenderer interface, removed begin/end, added setView, fixed renderer impleme...
authorMario Zechner <contact@badlogicgames.com>
Sun, 17 Feb 2013 17:02:43 +0000 (18:02 +0100)
committerMario Zechner <contact@badlogicgames.com>
Sun, 17 Feb 2013 17:02:43 +0000 (18:02 +0100)
gdx/src/com/badlogic/gdx/maps/MapRenderer.java
gdx/src/com/badlogic/gdx/maps/gleed/GleedMapLoader.java
gdx/src/com/badlogic/gdx/maps/gleed/GleedMapRenderer.java
gdx/src/com/badlogic/gdx/maps/tiled/TiledMapRenderer.java
tests/gdx-tests-lwjgl/src/com/badlogic/gdx/tests/lwjgl/LwjglDebugStarter.java
tests/gdx-tests/src/com/badlogic/gdx/tests/GLEEDTest.java
tests/gdx-tests/src/com/badlogic/gdx/tests/TiledMapAssetManagerTest.java
tests/gdx-tests/src/com/badlogic/gdx/tests/TiledMapDirectLoaderTest.java
tests/gdx-tests/src/com/badlogic/gdx/tests/bench/TiledMapBench.java

index c0a550a..d79e183 100644 (file)
@@ -1,42 +1,42 @@
 package com.badlogic.gdx.maps;
 
+import com.badlogic.gdx.graphics.OrthographicCamera;
 import com.badlogic.gdx.math.Matrix4;
 
 /**
  * @brief models a common way of rendering Map objects
  */
 public interface MapRenderer {
-
-       public void setProjectionMatrix(Matrix4 projectionMatrix);
-       
        /**
-        * Begin rendering.
+        * Sets the projection matrix and viewbounds from the given camera. If
+        * the camera changes, you have to call this method again. The viewbounds
+        * are taken from the camera's position and viewport size as well as the 
+        * scale. This method will only work if the camera's direction vector
+        * is (0,0,-1) and its up vector is (0, 1, 0), which are the defaults. 
+        * @param camera the {@link OrthographicCamera}
         */
-       public void begin();
+       public void setView(OrthographicCamera camera);
        
        /**
-        * End rendering.
-        */
-       public void end();
-       
-       /**
-        * Renders all the layers of a map using the given viewbounds.
-        * 
+        * Sets the projection matrix for rendering, as well as the bounds of the map
+        * which should be rendered. Make sure that the frustum spanned by the projection
+        * matrix coincides with the viewbounds.
+        * @param projectionMatrix
         * @param viewboundsX
         * @param viewboundsY
         * @param viewboundsWidth
         * @param viewboundsHeight
         */
-       public void render(float viewboundsX, float viewboundsY, float viewboundsWidth, float viewboundsHeight);
+       public void setView(Matrix4 projectionMatrix, float viewboundsX, float viewboundsY, float viewboundsWidth, float viewboundsHeight);
+       
+       /**
+        * Renders all the layers of a map.
+        */
+       public void render();
        
-       /** Renders the given layers of a map using the given viewbounds.
+       /** Renders the given layers of a map.
         * 
-        * @param viewboundsX
-        * @param viewboundsY
-        * @param viewboundsWidth
-        * @param viewboundsHeight
         * @param layers
         */
-       public void render(float viewboundsX, float viewboundsY, float viewboundsWidth, float viewboundsHeight, int[] layers);
-       
+       public void render(int[] layers);
 }
index ae24e4d..ee04f85 100644 (file)
@@ -38,6 +38,7 @@ import com.badlogic.gdx.maps.objects.TextureMapObject;
 import com.badlogic.gdx.math.Polygon;
 import com.badlogic.gdx.math.Vector2;
 import com.badlogic.gdx.utils.Array;
+import com.badlogic.gdx.utils.GdxRuntimeException;
 import com.badlogic.gdx.utils.Logger;
 import com.badlogic.gdx.utils.XmlReader;
 import com.badlogic.gdx.utils.XmlReader.Element;
@@ -53,20 +54,11 @@ public class GleedMapLoader  extends AsynchronousAssetLoader<Map, GleedMapLoader
        static public class Parameters extends AssetLoaderParameters<Map> {
        }
        
-       static private Logger s_logger = new Logger("GleedMapLoader");
-       
-       private String m_atlasFile = "";
-       private TextureAtlas m_atlas = null;
-       private String m_pathRoot = "data";
-       private Map m_map = null;
-       private AssetManager m_assetManager = null;
-       
-       /**
-        * @param loggingLevel logger level, output more or less information
-        */
-       public static void setLoggingLevel(int loggingLevel) {
-               s_logger.setLevel(loggingLevel);
-       }
+       private String atlasFile = "";
+       private TextureAtlas atlas = null;
+       private String pathRoot = "data";
+       private Map map = null;
+       private AssetManager assetManager = null;
        
        /**
         * @param resolver
@@ -84,27 +76,21 @@ public class GleedMapLoader  extends AsynchronousAssetLoader<Map, GleedMapLoader
         */
        @Override
        public void loadAsync (AssetManager manager, String fileName, Parameters parameter) {
-               m_assetManager = manager;
-               
-               s_logger.info("loading file " + fileName);
+               assetManager = manager;
                
                try {
                        XmlReader reader = new XmlReader();
                        Element root = reader.parse(Gdx.files.internal(fileName));
                        
-                       s_logger.info("loading level properties");
-                       
-                       if (m_map == null) {
-                               m_map = new Map();
-                               loadProperties(root, m_map.getProperties());
+                       if (map == null) {
+                               map = new Map();
+                               loadProperties(root, map.getProperties());
                        }
                        
-                       if (!m_atlasFile.isEmpty()) {
-                               s_logger.info("fetching texture atlas " + m_atlasFile);
-                               m_atlas = manager.get(m_atlasFile, TextureAtlas.class);
+                       if (!atlasFile.isEmpty()) {
+                               atlas = manager.get(atlasFile, TextureAtlas.class);
                        }
                        
-                       s_logger.info("loading layers");
                        Array<Element> layerElements = root.getChildByName("Layers").getChildrenByName("Layer");
                        
                        for (int i = 0; i < layerElements.size; ++i) {
@@ -113,7 +99,7 @@ public class GleedMapLoader  extends AsynchronousAssetLoader<Map, GleedMapLoader
                        }
                        
                } catch (Exception e) {
-                       s_logger.error("error loading file " + fileName + " " + e.getMessage());
+                       throw new GdxRuntimeException("Couldn't load Gleed map '" + fileName + "'", e);
                }
        }
        
@@ -128,7 +114,7 @@ public class GleedMapLoader  extends AsynchronousAssetLoader<Map, GleedMapLoader
         */
        @Override
        public Map loadSync (AssetManager manager, String fileName, Parameters parameter) {
-               return m_map;
+               return map;
        }
        
        /**
@@ -139,30 +125,27 @@ public class GleedMapLoader  extends AsynchronousAssetLoader<Map, GleedMapLoader
         */
        @Override
        public Array<AssetDescriptor> getDependencies (String fileName, Parameters parameter) {
-               s_logger.info("getting asset dependencies for " + fileName);
                Array<AssetDescriptor> dependencies = new Array<AssetDescriptor>();
                
                try {
                        XmlReader reader = new XmlReader();
                        Element root = reader.parse(Gdx.files.internal(fileName));
                        
-                       if (m_map == null) {
-                               m_map = new Map();
+                       if (map == null) {
+                               map = new Map();
                                
-                               loadProperties(root, m_map.getProperties());
+                               loadProperties(root, map.getProperties());
                        }
                        
-                       MapProperties properties = m_map.getProperties();
+                       MapProperties properties = map.getProperties();
                        
-                       m_atlasFile = properties.getAsString("atlas", "");
-                       m_pathRoot = properties.getAsString("assetRoot", "data");
+                       atlasFile = properties.getAsString("atlas", "");
+                       pathRoot = properties.getAsString("assetRoot", "data");
                        
-                       if (!m_atlasFile.isEmpty()) {
-                               s_logger.info("texture atlas dependency " + m_atlasFile);
-                               dependencies.add(new AssetDescriptor(m_atlasFile, TextureAtlas.class));
+                       if (!atlasFile.isEmpty()) {
+                               dependencies.add(new AssetDescriptor(atlasFile, TextureAtlas.class));
                        }
                        else {
-                               s_logger.info("textures asset folder " + m_pathRoot);
                                Array<Element> elements = root.getChildrenByNameRecursively("Item");
                                
                                for (int i = 0; i < elements.size; ++i) {
@@ -170,14 +153,13 @@ public class GleedMapLoader  extends AsynchronousAssetLoader<Map, GleedMapLoader
                                        
                                        if (element.getAttribute("xsi:type", "").equals("TextureItem")) {
                                                String[] pathParts = element.getChildByName("texture_filename").getText().split("\\\\");
-                                               s_logger.info("texture dependency " + m_pathRoot + "/" + pathParts[pathParts.length - 1]);
-                                               dependencies.add(new AssetDescriptor(m_pathRoot + "/" + pathParts[pathParts.length - 1], Texture.class));
+                                               dependencies.add(new AssetDescriptor(pathRoot + "/" + pathParts[pathParts.length - 1], Texture.class));
                                        }
                                }
                        }
                        
                } catch (Exception e) {
-                       s_logger.error("error loading asset dependencies " + fileName + " " + e.getMessage());
+                       throw new GdxRuntimeException("Couldn't load dependencies of Gleed map '" + fileName + "'", e);
                }
                
                return dependencies;
@@ -238,7 +220,6 @@ public class GleedMapLoader  extends AsynchronousAssetLoader<Map, GleedMapLoader
                layer.setName(element.getAttribute("Name", ""));
                layer.setVisible(Boolean.parseBoolean(element.getAttribute("Visible", "true")));
                
-               s_logger.info("loading layer " + layer.getName());
                Array<Element> items = element.getChildByName("Items").getChildrenByName("Item");
                
                for (int i = 0; i < items.size; ++i) {
@@ -271,15 +252,13 @@ public class GleedMapLoader  extends AsynchronousAssetLoader<Map, GleedMapLoader
                        layer.getObjects().addObject(mapObject);
                }
                
-               m_map.getLayers().addLayer(layer);
+               map.getLayers().addLayer(layer);
        }
        
        private void loadObject(Element element, MapObject mapObject) {
                mapObject.setName(element.getAttribute("Name", ""));
                mapObject.setVisible(Boolean.parseBoolean(element.getAttribute("Visible", "true")));
                loadProperties(element, mapObject.getProperties());
-               
-               s_logger.info("loading element " + mapObject.getName());
        }
        
        private TextureMapObject loadTexture(Element item) {
@@ -292,13 +271,13 @@ public class GleedMapLoader  extends AsynchronousAssetLoader<Map, GleedMapLoader
                
                TextureRegion region;
                
-               if (m_atlasFile.isEmpty()) {
+               if (atlasFile.isEmpty()) {
                        String[] pathParts = item.getChildByName("texture_filename").getText().split("\\\\");
-                       region = new TextureRegion(m_assetManager.get(m_pathRoot + "/" + pathParts[pathParts.length - 1], Texture.class));
+                       region = new TextureRegion(assetManager.get(pathRoot + "/" + pathParts[pathParts.length - 1], Texture.class));
                }
                else {
                        String[] assetParts = item.getChildByName("asset_name").getText().split("\\\\");
-                       region = new TextureRegion(m_atlas.findRegion(assetParts[assetParts.length - 1]));
+                       region = new TextureRegion(atlas.findRegion(assetParts[assetParts.length - 1]));
                }
                
                region.flip(Boolean.parseBoolean(item.getChildByName("FlipHorizontally").getText()),
index 6adf8ed..72cf396 100644 (file)
@@ -40,20 +40,17 @@ import com.badlogic.gdx.utils.Disposable;
  * @brief Renderer for GLEED maps
  */
 public class GleedMapRenderer implements MapRenderer, Disposable {
-
-       // Renderer data
-       private Map m_map;
-       private SpriteBatch m_batch;
-       private boolean m_ownSpriteBatch;
-       private float m_units;
+       private Map map;
+       private SpriteBatch batch;
+       private boolean ownSpriteBatch;
+       private float units;
        
-       // Aux data for fustrum culling
-       private Rectangle m_box = new Rectangle();
-       private Rectangle m_cameraRectangle = new Rectangle();
-       private Vector2 m_a = new Vector2();
-       private Vector2 m_b = new Vector2();
-       private Vector2 m_c = new Vector2();
-       private Vector2 m_d = new Vector2();
+       private Rectangle box = new Rectangle();
+       private Rectangle viewBounds = new Rectangle();
+       private Vector2 a = new Vector2();
+       private Vector2 b = new Vector2();
+       private Vector2 c = new Vector2();
+       private Vector2 d = new Vector2();
        
        /**
         * @param map map data that will be used to render
@@ -62,7 +59,7 @@ public class GleedMapRenderer implements MapRenderer, Disposable {
         */
        public GleedMapRenderer(Map map) {
                this(map, new SpriteBatch(), 1.0f);
-               m_ownSpriteBatch = true;
+               ownSpriteBatch = true;
        }
        
        /**
@@ -71,133 +68,60 @@ public class GleedMapRenderer implements MapRenderer, Disposable {
         * @param units metres per pixel (used to scale textures, defaults to 1.0f)
         */
        public GleedMapRenderer(Map map, SpriteBatch batch, float units) {
-               m_map = map;
-               m_units = units;
+               this.map = map;
+               this.units = units;
                if (batch != null) {
-                       m_batch = batch;
-                       m_ownSpriteBatch = false;
+                       this.batch = batch;
+                       ownSpriteBatch = false;
                }
                else
                {
-                       m_batch = new SpriteBatch();
-                       m_ownSpriteBatch = true;
+                       batch = new SpriteBatch();
+                       ownSpriteBatch = true;
                }
        }
        
-       /**
-        * @param projectionMatrix sets the projection matrix to the map's batch
-        */
-       @Override
-       public void setProjectionMatrix (Matrix4 projectionMatrix) {
-               m_batch.setProjectionMatrix(projectionMatrix);
-       }
-
-       /**
-        * Gets the rendering process ready
-        */
        @Override
-       public void begin () {
-               m_batch.begin();
-               m_batch.enableBlending();
-       }
-
-       /**
-        * Finishes the rendering process for that frame
-        */
-       @Override
-       public void end () {
-               m_batch.end();
+       public void setView(OrthographicCamera camera) {
+               batch.setProjectionMatrix(camera.combined);
+               float width = camera.viewportWidth * camera.zoom;
+               float height = camera.viewportHeight * camera.zoom;
+               viewBounds.set(camera.position.x - width / 2, camera.position.y - height / 2, width, height);
        }
        
-       /**
-        * Renders all the map layers
-        * 
-        * @param camera 2D camera used to render
-        */
-       public void render(OrthographicCamera camera) {
-               m_cameraRectangle.x = camera.position.x - camera.viewportWidth * 0.5f * camera.zoom;
-               m_cameraRectangle.y = camera.position.y - camera.viewportHeight * 0.5f * camera.zoom;
-               m_cameraRectangle.width = camera.viewportWidth *  camera.zoom;
-               m_cameraRectangle.height = camera.viewportHeight *  camera.zoom;
-               
-               setProjectionMatrix(camera.combined);
-               
-               MapLayers layers = m_map.getLayers();
-               
-               for (MapLayer layer : layers) {
-                       renderLayer(layer);
-               }
-       }
-       
-       /**
-        * @param camera camera 2D camera used to render
-        * @param layers layers indices to be rendered
-        */
-       public void render(OrthographicCamera camera, int[] layers) {
-               m_cameraRectangle.x = camera.position.x - camera.viewportWidth * 0.5f * camera.zoom;
-               m_cameraRectangle.y = camera.position.y - camera.viewportHeight * 0.5f * camera.zoom;
-               m_cameraRectangle.width = camera.viewportWidth *  camera.zoom;
-               m_cameraRectangle.height = camera.viewportHeight *  camera.zoom;
-
-               setProjectionMatrix(camera.combined);
-               
-               MapLayers mapLayers = m_map.getLayers();
-               
-               for (int i = 0; i < layers.length; i++) {
-                       renderLayer(mapLayers.getLayer(i));
-               }
+       @Override
+       public void setView (Matrix4 projection, float x, float y, float width, float height) {
+               batch.setProjectionMatrix(projection);
+               viewBounds.set(x, y, width, height);
        }
        
-       /**
-        * Renders all the layers
-        * 
-        * @param viewboundsX bottom left x coordinate of the frustum
-        * @param viewboundsY bottom left y coordinate of the frustrum
-        * @param viewboundsWidth frustum width 
-        * @param viewboundsHeight frustum height
-        */
        @Override
-       public void render (float viewboundsX, float viewboundsY, float viewboundsWidth, float viewboundsHeight) {
-               m_cameraRectangle.x = viewboundsX;
-               m_cameraRectangle.y = viewboundsY;
-               m_cameraRectangle.width = viewboundsWidth;
-               m_cameraRectangle.height = viewboundsHeight;
-               
-               MapLayers layers = m_map.getLayers();
-               
+       public void render () {
+               batch.begin();
+               batch.enableBlending();
+               MapLayers layers = map.getLayers();
                for (MapLayer layer : layers) {
                        renderLayer(layer);
                }
+               batch.end();
        }
 
-       /**
-        * @param viewboundsX bottom left x coordinate of the frustum
-        * @param viewboundsY bottom left y coordinate of the frustrum
-        * @param viewboundsWidth frustum width 
-        * @param viewboundsHeight frustum height
-        * @param layers layers indices to be rendered
-        */
        @Override
-       public void render (float viewboundsX, float viewboundsY, float viewboundsWidth, float viewboundsHeight, int[] layers) {
-               m_cameraRectangle.x = viewboundsX;
-               m_cameraRectangle.y = viewboundsY;
-               m_cameraRectangle.width = viewboundsWidth;
-               m_cameraRectangle.height = viewboundsHeight;
-               
-               MapLayers mapLayers = m_map.getLayers();
+       public void render (int[] layers) {
+               batch.begin();
+               batch.enableBlending();
+               MapLayers mapLayers = map.getLayers();
                
                for (int i = 0; i < layers.length; i++) {
                        renderLayer(mapLayers.getLayer(i));
                }
+               batch.end();
        }
        
-       /**
-        * Disposes the sprite batch in case the renderer owns it
-        */
        @Override
        public void dispose () {
-               if (m_ownSpriteBatch) {
-                       m_batch.dispose();
+               if (ownSpriteBatch) {
+                       batch.dispose();
                }
        }
        
@@ -226,29 +150,29 @@ public class GleedMapRenderer implements MapRenderer, Disposable {
                        setBounds(texture);
 
                        // If the image is in the frustum, draw it (culling)
-                       if (m_cameraRectangle.overlaps(m_box) ||
-                                m_cameraRectangle.contains(m_box) ||
-                                m_box.contains(m_cameraRectangle)) {
+                       if (viewBounds.overlaps(box) ||
+                                viewBounds.contains(box) ||
+                                box.contains(viewBounds)) {
                        
                                // Skip complex rendering if there is no scaling nor rotation
                                if (texture.getRotation() == 0 &&
                                         texture.getScaleX() == 1.0f &&
                                         texture.getScaleY() == 1.0f) {
                                        
-                                       m_batch.draw(texture.getTextureRegion(),
-                                                                        texture.getX() * m_units - texture.getOriginX(),
-                                                                        texture.getY() * m_units - texture.getOriginY());
+                                       batch.draw(texture.getTextureRegion(),
+                                                                        texture.getX() * units - texture.getOriginX(),
+                                                                        texture.getY() * units - texture.getOriginY());
                                }
                                else {
-                                       m_batch.draw(texture.getTextureRegion(),
-                                                                        texture.getX() * m_units - texture.getOriginX(),
-                                                                        texture.getY() * m_units - texture.getOriginY(),
+                                       batch.draw(texture.getTextureRegion(),
+                                                                        texture.getX() * units - texture.getOriginX(),
+                                                                        texture.getY() * units - texture.getOriginY(),
                                                                         texture.getOriginX(),
                                                                         texture.getOriginY(),
                                                                         texture.getTextureRegion().getRegionWidth(),
                                                                         texture.getTextureRegion().getRegionHeight(),
-                                                                        texture.getScaleX() * m_units,
-                                                                        texture.getScaleY() * m_units,
+                                                                        texture.getScaleX() * units,
+                                                                        texture.getScaleY() * units,
                                                                         -MathUtils.radiansToDegrees * texture.getRotation());
                                }
                        }
@@ -258,65 +182,65 @@ public class GleedMapRenderer implements MapRenderer, Disposable {
        private void setBounds(TextureMapObject texture) {
                TextureRegion region = texture.getTextureRegion();
                
-               float x1 = -region.getRegionWidth() * 0.5f * m_units * texture.getScaleX();
+               float x1 = -region.getRegionWidth() * 0.5f * units * texture.getScaleX();
                float x2 = -x1;
-               float y1 = -region.getRegionHeight() * 0.5f * m_units * texture.getScaleY();
+               float y1 = -region.getRegionHeight() * 0.5f * units * texture.getScaleY();
                float y2 = -y1;
                
                float rotation = texture.getRotation();
                
                // Skip sin and cos calculations if we don't have rotation
                if (rotation == 0.0f) {
-                       m_a.x = x1;
-                       m_a.y = y1;
+                       a.x = x1;
+                       a.y = y1;
                        
-                       m_b.x = x2;
-                       m_b.y = y1;
+                       b.x = x2;
+                       b.y = y1;
                        
-                       m_c.x = x2;
-                       m_c.y = y2;
+                       c.x = x2;
+                       c.y = y2;
                        
-                       m_d.x = x1;
-                       m_d.y = y2;
+                       d.x = x1;
+                       d.y = y2;
                }
                else {
                        float sin = (float)Math.sin(rotation);
                        float cos = (float)Math.cos(rotation);
                        
-                       m_a.x = x1 * cos - y1 * sin;
-                       m_a.y = x1 * sin - y1 * cos;
+                       a.x = x1 * cos - y1 * sin;
+                       a.y = x1 * sin - y1 * cos;
                        
-                       m_b.x = x2 * cos - y1 * sin;
-                       m_b.y = x2 * sin - y1 * cos;
+                       b.x = x2 * cos - y1 * sin;
+                       b.y = x2 * sin - y1 * cos;
                        
-                       m_c.x = x2 * cos - y2 * sin;
-                       m_c.y = x2 * sin - y2 * cos;
+                       c.x = x2 * cos - y2 * sin;
+                       c.y = x2 * sin - y2 * cos;
                        
-                       m_d.x = x1 * cos - y2 * sin;
-                       m_d.y = x1 * sin - y2 * cos;
+                       d.x = x1 * cos - y2 * sin;
+                       d.y = x1 * sin - y2 * cos;
                }
                
                
                float posX = texture.getX();
                float posY = texture.getY();
                
-               m_a.x += posX * m_units;
-               m_a.y += posY * m_units;
-               m_b.x += posX * m_units;
-               m_b.y += posY * m_units;
-               m_c.x += posX * m_units;
-               m_c.y += posY * m_units;
-               m_d.x += posX * m_units;
-               m_d.y += posY * m_units;
-               
-               float minX = Math.min(Math.min(Math.min(m_a.x, m_b.x), m_c.x), m_d.x);
-               float minY = Math.min(Math.min(Math.min(m_a.y, m_b.y), m_c.y), m_d.y);
-               float maxX = Math.max(Math.max(Math.max(m_a.x, m_b.x), m_c.x), m_d.x);
-               float maxY = Math.max(Math.max(Math.max(m_a.y, m_b.y), m_c.y), m_d.y);
-               
-               m_box.x = minX;
-               m_box.y = minY;
-               m_box.width = maxX - minX;
-               m_box.height = maxY - minY;
+               a.x += posX * units;
+               a.y += posY * units;
+               b.x += posX * units;
+               b.y += posY * units;
+               c.x += posX * units;
+               c.y += posY * units;
+               d.x += posX * units;
+               d.y += posY * units;
+               
+               float minX = Math.min(Math.min(Math.min(a.x, b.x), c.x), d.x);
+               float minY = Math.min(Math.min(Math.min(a.y, b.y), c.y), d.y);
+               float maxX = Math.max(Math.max(Math.max(a.x, b.x), c.x), d.x);
+               float maxY = Math.max(Math.max(Math.max(a.y, b.y), c.y), d.y);
+               
+               box.x = minX;
+               box.y = minY;
+               box.width = maxX - minX;
+               box.height = maxY - minY;
        }
 }
index e3e89da..688c305 100644 (file)
@@ -6,31 +6,25 @@ import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.graphics.Color;
 import com.badlogic.gdx.graphics.GL10;
 import com.badlogic.gdx.graphics.GL11;
+import com.badlogic.gdx.graphics.OrthographicCamera;
 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 import com.badlogic.gdx.graphics.g2d.SpriteCache;
 import com.badlogic.gdx.graphics.g2d.TextureRegion;
 import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
 import com.badlogic.gdx.maps.MapLayer;
 import com.badlogic.gdx.maps.MapObject;
+import com.badlogic.gdx.maps.MapRenderer;
 import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
 import com.badlogic.gdx.math.Matrix4;
 import com.badlogic.gdx.math.Polygon;
 import com.badlogic.gdx.math.Rectangle;
 
-public interface TiledMapRenderer {
+public interface TiledMapRenderer extends MapRenderer {
        
-       public void setViewBounds(float x, float y, float width, float height);
-       
-       public void setProjectionMatrix(Matrix4 projection);
-       
-       public void begin();
-       public void end();
-       
-       public void render();
        public void renderObject(MapObject object);
        public void renderTileLayer(TiledMapTileLayer layer);
        
-       public class BatchTiledMapRenderer implements TiledMapRenderer {
+       public abstract class BatchTiledMapRenderer implements TiledMapRenderer {
                
                protected TiledMap map;
 
@@ -71,27 +65,22 @@ public interface TiledMapRenderer {
                }
                
                @Override
-               public void setViewBounds (float x, float y, float width, float height) {
-                       viewBounds.set(x, y, width, height);
+               public void setView(OrthographicCamera camera) {
+                       spriteBatch.setProjectionMatrix(camera.combined);
+                       float width = camera.viewportWidth * camera.zoom;
+                       float height = camera.viewportHeight * camera.zoom;
+                       viewBounds.set(camera.position.x - width / 2, camera.position.y - height / 2, width, height);
                }
                
                @Override
-               public void setProjectionMatrix (Matrix4 projection) {
+               public void setView (Matrix4 projection, float x, float y, float width, float height) {
                        spriteBatch.setProjectionMatrix(projection);
-               }
-               
-               @Override
-               public void begin () {
-                       spriteBatch.begin();
-               }
-
-               @Override
-               public void end () {
-                       spriteBatch.end();
+                       viewBounds.set(x, y, width, height);
                }
                
                @Override
                public void render () {
+                       spriteBatch.begin();
                        for (MapLayer layer : map.getLayers()) {
                                if (layer.getVisible()) {
                                        if (layer instanceof TiledMapTileLayer) {
@@ -102,83 +91,15 @@ public interface TiledMapRenderer {
                                                }
                                        }                                       
                                }                               
-                       }                       
-               }
-
-               @Override
-               public void renderObject (MapObject object) {
-                       // Do nothing
-               }
-
-               @Override
-               public void renderTileLayer (TiledMapTileLayer layer) {
-                       // Do nothing                   
-               }
-
-       }
-       
-       public class CacheTiledMapRenderer implements TiledMapRenderer {
-               protected TiledMap map;
-
-               protected float unitScale;
-               
-               protected SpriteCache spriteCache;
-               
-               protected Rectangle viewBounds; 
-
-               public TiledMap getMap() {
-                       return map;                     
-               }
-               
-               public float getUnitScale() {
-                       return unitScale;
-               }
-               
-               public SpriteCache getSpriteCache() {
-                       return spriteCache;
-               }
-
-               public Rectangle getViewBounds() {
-                       return viewBounds;
-               }
-               
-               public CacheTiledMapRenderer(TiledMap map) {
-                       this.map = map;
-                       this.unitScale = 1;
-                       this.spriteCache = new SpriteCache();
-                       this.viewBounds = new Rectangle();
-               }
-               
-               public CacheTiledMapRenderer(TiledMap map, float unitScale) {
-                       this.map = map;
-                       this.unitScale = unitScale;
-                       this.viewBounds = new Rectangle();
-                       this.spriteCache = new SpriteCache();
-               }
-               
-               @Override
-               public void setViewBounds (float x, float y, float width, float height) {
-                       viewBounds.set(x, y, width, height);
-               }
-               
-               @Override
-               public void setProjectionMatrix (Matrix4 projection) {
-                       spriteCache.setProjectionMatrix(projection);
-               }
-               
-               @Override
-               public void begin () {
-                       spriteCache.begin();
-               }
-
-               @Override
-               public void end () {
-                       spriteCache.end();
+                       }
+                       spriteBatch.end();
                }
                
                @Override
-               public void render () {
-                       for (MapLayer layer : map.getLayers()) {
+               public void render (int[] layers) {
+                       spriteBatch.begin();
+                       for (int layerIdx : layers) {
+                               MapLayer layer = map.getLayers().getLayer(layerIdx);
                                if (layer.getVisible()) {
                                        if (layer instanceof TiledMapTileLayer) {
                                                renderTileLayer((TiledMapTileLayer) layer);
@@ -188,20 +109,11 @@ public interface TiledMapRenderer {
                                                }
                                        }                                       
                                }                               
-                       }                       
-               }
-
-               @Override
-               public void renderObject (MapObject object) {
-                       // Do nothing
-               }
-
-               @Override
-               public void renderTileLayer (TiledMapTileLayer layer) {
-                       // Do nothing                   
+                       }               
+                       spriteBatch.end();
                }
-               
        }
+       
        public class IsometricTiledMapRenderer extends  BatchTiledMapRenderer {
 
                private TiledMap map;
@@ -534,19 +446,24 @@ public interface TiledMapRenderer {
                        this.unitScale = unitScale;
                        this.viewBounds = new Rectangle();
                        this.spriteCache = new SpriteCache(4000, true);
-               }       
+               }
                
                @Override
-               public void setViewBounds (float x, float y, float width, float height) {
-                       viewBounds.set(x, y, width, height);
+               public void setView(OrthographicCamera camera) {
+                       spriteCache.setProjectionMatrix(camera.combined);
+                       float width = camera.viewportWidth * camera.zoom;
+                       float height = camera.viewportHeight * camera.zoom;
+                       viewBounds.set(camera.position.x - width / 2, camera.position.y - height / 2, width, height);
+                       recache = true;
                }
-
+               
                @Override
-               public void setProjectionMatrix (Matrix4 projection) {
+               public void setView (Matrix4 projection, float x, float y, float width, float height) {
                        spriteCache.setProjectionMatrix(projection);
+                       viewBounds.set(x, y, width, height);
+                       recache = true;
                }
 
-               @Override
                public void begin () {
                        if (recache) {
                                cached = false;
@@ -563,7 +480,6 @@ public interface TiledMapRenderer {
                        
                }
 
-               @Override
                public void end () {
                        if (!cached) {
                                spriteCache.endCache();
@@ -579,6 +495,7 @@ public interface TiledMapRenderer {
 
                @Override
                public void render () {
+                       begin();
                        if (cached) {
                                spriteCache.draw(0);
                        } else {
@@ -594,7 +511,13 @@ public interface TiledMapRenderer {
                                        }                               
                                }                               
                        }
-
+                       end();
+               }
+               
+               @Override
+               public void render (int[] layers) {
+                       // FIXME not implemented
+                       throw new UnsupportedOperationException("Not implemented");
                }
 
                @Override
index 1927beb..d0a835c 100644 (file)
@@ -39,7 +39,7 @@ public class LwjglDebugStarter {
                new SharedLibraryLoader("../../extensions/gdx-controllers/gdx-controllers-desktop/libs/gdx-controllers-desktop-natives.jar").load("gdx-controllers-desktop");\r
                new SharedLibraryLoader("../../gdx/libs/gdx-natives.jar").load("gdx");\r
 \r
-               GdxTest test = new TiledMapDirectLoaderTest();\r
+               GdxTest test = new GleedTest();\r
                LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();\r
                config.useGL20 = test.needsGL20();\r
                new LwjglApplication(test, config);\r
index 36eec89..303a4db 100644 (file)
@@ -63,7 +63,6 @@ public class GleedTest extends GdxTest {
                manager = new AssetManager();
                camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
                camera.zoom = 2.0f;
-               GleedMapLoader.setLoggingLevel(Logger.INFO);
                manager.setLoader(Map.class, new GleedMapLoader(new InternalFileHandleResolver()));
                manager.load("data/gleedtest.xml", Map.class);
                manager.load("data/font.fnt", BitmapFont.class);
@@ -90,9 +89,8 @@ public class GleedTest extends GdxTest {
                }
                
                if (state == State.Running) {
-                       renderer.begin();
-                       renderer.render(camera);
-                       renderer.end();
+                       renderer.setView(camera);
+                       renderer.render();
                        
                        if (Gdx.input.isKeyPressed(Keys.UP)) {
                                camera.position.y += 5.0f;
index d5af65f..8afae80 100644 (file)
@@ -20,10 +20,9 @@ public class TiledMapAssetManagerTest extends GdxTest {
        private TiledMapRenderer renderer;
        private OrthographicCamera camera;
        private OrthoCamController cameraController;
-       
-       AssetManager assetManager;
-       BitmapFont font;
-       SpriteBatch batch;
+       private AssetManager assetManager;
+       private BitmapFont font;
+       private SpriteBatch batch;
        
        @Override
        public void create() {          
@@ -53,11 +52,8 @@ public class TiledMapAssetManagerTest extends GdxTest {
                Gdx.gl.glClearColor(100f / 255f, 100f / 255f, 250f / 255f, 1f);
                Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
                camera.update();
-               renderer.setProjectionMatrix(camera.combined);
-               renderer.setViewBounds(camera.position.x - camera.viewportWidth * 0.5f, camera.position.y - camera.viewportHeight * 0.5f, camera.viewportWidth, camera.viewportHeight);
-               renderer.begin();
+               renderer.setView(camera);
                renderer.render();
-               renderer.end();
                batch.begin();
                font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 20); 
                batch.end();
index 1a30ec3..1f75320 100755 (executable)
@@ -1,23 +1,14 @@
 package com.badlogic.gdx.tests;\r
 \r
 import com.badlogic.gdx.Gdx;\r
-import com.badlogic.gdx.InputAdapter;\r
-import com.badlogic.gdx.assets.AssetManager;\r
-import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;\r
 import com.badlogic.gdx.graphics.GL10;\r
 import com.badlogic.gdx.graphics.OrthographicCamera;\r
-import com.badlogic.gdx.graphics.Texture;\r
 import com.badlogic.gdx.graphics.g2d.BitmapFont;\r
 import com.badlogic.gdx.graphics.g2d.SpriteBatch;\r
-import com.badlogic.gdx.maps.MapLayer;\r
 import com.badlogic.gdx.maps.tiled.TiledMap;\r
 import com.badlogic.gdx.maps.tiled.TiledMapRenderer;\r
-import com.badlogic.gdx.maps.tiled.TiledMapRenderer;\r
+import com.badlogic.gdx.maps.tiled.TiledMapRenderer.OrthogonalTiledMapRenderer;\r
 import com.badlogic.gdx.maps.tiled.TmxMapLoader;\r
-import com.badlogic.gdx.maps.tiled.TiledMapRenderer.IsometricTiledMapRenderer;\r
-import com.badlogic.gdx.maps.tiled.TiledMapRenderer.OrthogonalTiledMapRenderer2;\r
-import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;\r
-import com.badlogic.gdx.math.Vector3;\r
 import com.badlogic.gdx.tests.utils.GdxTest;\r
 import com.badlogic.gdx.tests.utils.OrthoCamController;\r
 \r
@@ -27,9 +18,8 @@ public class TiledMapDirectLoaderTest extends GdxTest {
        private TiledMapRenderer renderer;\r
        private OrthographicCamera camera;\r
        private OrthoCamController cameraController;\r
-       \r
-       BitmapFont font;\r
-       SpriteBatch batch;\r
+       private BitmapFont font;\r
+       private SpriteBatch batch;\r
        \r
        @Override\r
        public void create() {          \r
@@ -47,7 +37,7 @@ public class TiledMapDirectLoaderTest extends GdxTest {
                batch = new SpriteBatch();\r
                \r
                map = new TmxMapLoader().load("data/maps/tiles.tmx");\r
-               renderer = new OrthogonalTiledMapRenderer2(map, 1f / 32f);\r
+               renderer = new OrthogonalTiledMapRenderer(map, 1f / 32f);\r
        }\r
 \r
        @Override\r
@@ -55,11 +45,8 @@ public class TiledMapDirectLoaderTest extends GdxTest {
                Gdx.gl.glClearColor(0.55f, 0.55f, 0.55f, 1f);\r
                Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);\r
                camera.update();\r
-               renderer.setProjectionMatrix(camera.combined);\r
-               renderer.setViewBounds(camera.position.x - camera.viewportWidth * 0.5f, camera.position.y - camera.viewportHeight * 0.5f, camera.viewportWidth, camera.viewportHeight);\r
-               renderer.begin();\r
+               renderer.setView(camera);\r
                renderer.render();\r
-               renderer.end();\r
                batch.begin();\r
                font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 20); \r
                batch.end();\r
index 4079b1a..e8d0532 100644 (file)
@@ -18,6 +18,7 @@ import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
 import com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile;
 import com.badlogic.gdx.math.Vector3;
 import com.badlogic.gdx.tests.utils.GdxTest;
+import com.badlogic.gdx.tests.utils.OrthoCamController;
 
 public class TiledMapBench extends GdxTest {
        
@@ -25,15 +26,11 @@ public class TiledMapBench extends GdxTest {
        private TiledMapRenderer renderer;
        private OrthographicCamera camera;
        private OrthoCamController cameraController;
-       
-       AssetManager assetManager;
-       
-       Texture tiles;
-       
-       Texture texture;
-       
-       BitmapFont font;
-       SpriteBatch batch;
+       private AssetManager assetManager;
+       private Texture tiles;
+       private Texture texture;
+       private BitmapFont font;
+       private SpriteBatch batch;
        
        @Override
        public void create() {          
@@ -68,7 +65,7 @@ public class TiledMapBench extends GdxTest {
                        }
                }
                
-               renderer = new OrthogonalTiledMapRenderer2(map);
+               renderer = new OrthogonalTiledMapRenderer(map);
 
        }
 
@@ -76,57 +73,11 @@ public class TiledMapBench extends GdxTest {
        public void render() {
                Gdx.gl.glClearColor(100f / 255f, 100f / 255f, 250f / 255f, 1f);
                Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
-               if (cameraController.dirty) {
-                       camera.update();
-                       renderer.setProjectionMatrix(camera.combined);
-                       cameraController.dirty = false;
-                       ((OrthogonalTiledMapRenderer2) renderer).recache = true;
-               }
-               renderer.setViewBounds(camera.position.x - camera.viewportWidth * 0.5f, camera.position.y - camera.viewportHeight * 0.5f, camera.viewportWidth, camera.viewportHeight);
-               renderer.begin();
+               camera.update();
+               renderer.setView(camera);
                renderer.render();
-               renderer.end();
                batch.begin();
                font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 20); 
                batch.end();
        }
-       
-       public class OrthoCamController extends InputAdapter {
-               final OrthographicCamera camera;
-               final Vector3 curr = new Vector3();
-               final Vector3 last = new Vector3(-1, -1, -1);
-               final Vector3 delta = new Vector3();
-
-               boolean dirty = true;
-               
-               public OrthoCamController (OrthographicCamera camera) {
-                       this.camera = camera;
-               }
-
-               @Override
-               public boolean touchDragged (int x, int y, int pointer) {
-                       camera.unproject(curr.set(x, y, 0));
-                       if (!(last.x == -1 && last.y == -1 && last.z == -1)) {
-                               camera.unproject(delta.set(last.x, last.y, 0));
-                               delta.sub(curr);
-                               camera.position.add(delta.x, delta.y, 0);
-                               dirty = true;
-                       }
-                       last.set(x, y, 0);
-                       return false;
-               }
-
-               @Override
-               public boolean touchUp (int x, int y, int pointer, int button) {
-                       last.set(-1, -1, -1);
-                       return false;
-               }
-       }
-
-       @Override
-       public boolean needsGL20 () {
-               // TODO Auto-generated method stub
-               return false;
-       }
-       
 }