OSDN Git Service

Why oh why, did these not get included in my other commit. Silly git...
authorJustin Shapcott <justin@mobidevelop.com>
Mon, 3 Sep 2012 20:02:42 +0000 (13:02 -0700)
committerJustin Shapcott <justin@mobidevelop.com>
Mon, 3 Sep 2012 20:02:59 +0000 (13:02 -0700)
gdx/src/com/badlogic/gdx/graphics/g2d/tiled/TileMapRenderer.java
tests/gdx-tests/src/com/badlogic/gdx/tests/TiledMapTest.java

index e8da836..981d422 100644 (file)
@@ -24,6 +24,7 @@ import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
 import com.badlogic.gdx.graphics.g2d.TextureRegion;\r
 import com.badlogic.gdx.graphics.glutils.ShaderProgram;\r
 import com.badlogic.gdx.math.MathUtils;\r
+import com.badlogic.gdx.math.Matrix3;\r
 import com.badlogic.gdx.math.Matrix4;\r
 import com.badlogic.gdx.math.Vector3;\r
 import com.badlogic.gdx.utils.Disposable;\r
@@ -240,6 +241,11 @@ public class TileMapRenderer implements Disposable {
                }\r
        }\r
 \r
+       private static final int FLAG_FLIP_X = 0x80000000;\r
+       private static final int FLAG_FLIP_Y = 0x40000000;\r
+       private static final int FLAG_ROTATE = 0x20000000;              \r
+       private static final int MASK_CLEAR  = 0xE0000000;\r
+       \r
        private int addBlock (int[][] layer, int blockRow, int blockCol, boolean blended) {\r
                cache.beginCache();\r
 \r
@@ -251,20 +257,81 @@ public class TileMapRenderer implements Disposable {
                for (int row = firstRow; row < lastRow && row < layer.length; row++) {\r
                        for (int col = firstCol; col < lastCol && col < layer[row].length; col++) {\r
                                int tile = layer[row][col];\r
+                               \r
+                               boolean flipX = ((tile & FLAG_FLIP_X) != 0);\r
+                               boolean flipY = ((tile & FLAG_FLIP_Y) != 0);\r
+                               boolean rotate = ((tile & FLAG_ROTATE) != 0);\r
+                               \r
+                               tile = tile & ~MASK_CLEAR;\r
+                               \r
                                if (tile != 0) {\r
                                        if (blended == blendedTiles.contains(tile)) {\r
                                                TextureRegion reg = atlas.getRegion(tile);\r
                                                if (reg != null) {\r
-                                                       if (!isSimpleTileAtlas) {\r
-                                                               AtlasRegion region = (AtlasRegion)reg;\r
-                                                               cache.add(region, col * unitsPerTileX, (layer.length - row - 1) * unitsPerTileY, (float)region.offsetX\r
-                                                                       * unitsPerTileX / tileWidth, (float)(region.offsetY) * unitsPerTileY / (float)tileHeight,\r
-                                                                       region.packedWidth, region.packedHeight, unitsPerTileX / (float)tileWidth, unitsPerTileY\r
-                                                                               / (float)tileHeight, (region.rotate) ? 90 : 0);\r
+                                                       \r
+                                                       float x = col * unitsPerTileX;\r
+                                                       float y = (layer.length - row - 1) * unitsPerTileY;\r
+                                                       float width = reg.getRegionWidth();\r
+                                                       float height = reg.getRegionHeight();                                                   \r
+                                                       float originX = width * 0.5f;\r
+                                                       float originY = height * 0.5f;\r
+                                                       float scaleX = unitsPerTileX / tileWidth;\r
+                                                       float scaleY = unitsPerTileY / tileHeight;\r
+                                                       float rotation = 0;\r
+                                                       int sourceX = reg.getRegionX();\r
+                                                       int sourceY = reg.getRegionY();\r
+                                                       int sourceWidth = reg.getRegionWidth();\r
+                                                       int sourceHeight = reg.getRegionHeight();\r
+                                                       \r
+                                                       if (rotate) {\r
+                                                               if (flipX && flipY) {\r
+                                                                       rotation = -90;\r
+                                                                       sourceX += sourceWidth;\r
+                                                                       sourceWidth = -sourceWidth;                                                                     \r
+                                                               }\r
+                                                               else\r
+                                                               if (flipX && !flipY) {\r
+                                                                       rotation = -90;\r
+                                                               }\r
+                                                               else\r
+                                                               if (flipY && !flipX) {\r
+                                                                       rotation = +90;\r
+                                                               }\r
+                                                               else\r
+                                                               if (!flipY && !flipX) {\r
+                                                                       rotation = -90;\r
+                                                                       sourceY += sourceHeight;\r
+                                                                       sourceHeight = -sourceHeight;                                                                   \r
+                                                               }\r
                                                        } else {\r
-                                                               cache.add(reg, col * unitsPerTileX, (layer.length - row - 1) * unitsPerTileY, 0, 0, reg.getRegionWidth(),\r
-                                                                       reg.getRegionHeight(), unitsPerTileX / tileWidth, unitsPerTileY / tileHeight, 0);\r
+                                                               if (flipX) {\r
+                                                                       sourceX += sourceWidth;\r
+                                                                       sourceWidth = -sourceWidth;\r
+                                                               }\r
+                                                               if (flipY) {\r
+                                                                       sourceY += sourceHeight;\r
+                                                                       sourceHeight = -sourceHeight;\r
+                                                               }\r
                                                        }\r
+                                                       \r
+                                                       cache.add(\r
+                                                               reg.getTexture(),\r
+                                                               x,\r
+                                                               y,\r
+                                                               originX,\r
+                                                               originY,\r
+                                                               width,\r
+                                                               height,\r
+                                                               scaleX,\r
+                                                               scaleY,\r
+                                                               rotation,\r
+                                                               sourceX,\r
+                                                               sourceY,\r
+                                                               sourceWidth,\r
+                                                               sourceHeight,\r
+                                                               false,\r
+                                                               false\r
+                                                       );\r
                                                }\r
                                        }\r
                                }\r
index b1b9594..d4f6516 100644 (file)
@@ -37,7 +37,7 @@ import com.badlogic.gdx.tests.utils.OrthoCamController;
 /** @author David Fraska */\r
 public class TiledMapTest extends GdxTest {\r
 \r
-       private static final boolean automove = true;\r
+       private static final boolean automove = false;\r
 \r
        private static final int[] layersList = {2, 3};\r
 \r
@@ -112,8 +112,8 @@ public class TiledMapTest extends GdxTest {
 \r
                spriteBatch = new SpriteBatch();\r
 \r
-               final String path = "data/tiledmap/";\r
-               final String mapname = "tilemap csv";\r
+               final String path = "data/tiledmap/flip_rotate/";\r
+               final String mapname = "flip_rotate";\r
 \r
                FileHandle mapHandle = Gdx.files.internal(path + mapname + ".tmx");\r
                FileHandle baseDir = Gdx.files.internal(path);\r
@@ -125,12 +125,12 @@ public class TiledMapTest extends GdxTest {
 \r
                atlas = new TileAtlas(map, baseDir);\r
 \r
-               int blockWidth = 10;\r
-               int blockHeight = 12;\r
+               int blockWidth = 15;\r
+               int blockHeight = 10;\r
 \r
                startTime = System.currentTimeMillis();\r
 \r
-               tileMapRenderer = new TileMapRenderer(map, atlas, blockWidth, blockHeight, 5, 5);\r
+               tileMapRenderer = new TileMapRenderer(map, atlas, blockWidth, blockHeight, 16, 16);\r
                endTime = System.currentTimeMillis();\r
                System.out.println("Created cache in " + (endTime - startTime) + "mS");\r
 \r