1 /******************************************************************************* 2 * Copyright 2013 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.maps.tiled.renderers; 18 19 import static com.badlogic.gdx.graphics.g2d.Batch.C1; 20 import static com.badlogic.gdx.graphics.g2d.Batch.C2; 21 import static com.badlogic.gdx.graphics.g2d.Batch.C3; 22 import static com.badlogic.gdx.graphics.g2d.Batch.C4; 23 import static com.badlogic.gdx.graphics.g2d.Batch.U1; 24 import static com.badlogic.gdx.graphics.g2d.Batch.U2; 25 import static com.badlogic.gdx.graphics.g2d.Batch.U3; 26 import static com.badlogic.gdx.graphics.g2d.Batch.U4; 27 import static com.badlogic.gdx.graphics.g2d.Batch.V1; 28 import static com.badlogic.gdx.graphics.g2d.Batch.V2; 29 import static com.badlogic.gdx.graphics.g2d.Batch.V3; 30 import static com.badlogic.gdx.graphics.g2d.Batch.V4; 31 import static com.badlogic.gdx.graphics.g2d.Batch.X1; 32 import static com.badlogic.gdx.graphics.g2d.Batch.X2; 33 import static com.badlogic.gdx.graphics.g2d.Batch.X3; 34 import static com.badlogic.gdx.graphics.g2d.Batch.X4; 35 import static com.badlogic.gdx.graphics.g2d.Batch.Y1; 36 import static com.badlogic.gdx.graphics.g2d.Batch.Y2; 37 import static com.badlogic.gdx.graphics.g2d.Batch.Y3; 38 import static com.badlogic.gdx.graphics.g2d.Batch.Y4; 39 40 import com.badlogic.gdx.graphics.Color; 41 import com.badlogic.gdx.graphics.g2d.Batch; 42 import com.badlogic.gdx.graphics.g2d.TextureRegion; 43 import com.badlogic.gdx.maps.tiled.TiledMap; 44 import com.badlogic.gdx.maps.tiled.TiledMapTile; 45 import com.badlogic.gdx.maps.tiled.TiledMapTileLayer; 46 import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell; 47 48 public class OrthogonalTiledMapRenderer extends BatchTiledMapRenderer { 49 50 public OrthogonalTiledMapRenderer (TiledMap map) { 51 super(map); 52 } 53 54 public OrthogonalTiledMapRenderer (TiledMap map, Batch batch) { 55 super(map, batch); 56 } 57 58 public OrthogonalTiledMapRenderer (TiledMap map, float unitScale) { 59 super(map, unitScale); 60 } 61 62 public OrthogonalTiledMapRenderer (TiledMap map, float unitScale, Batch batch) { 63 super(map, unitScale, batch); 64 } 65 66 @Override 67 public void renderTileLayer (TiledMapTileLayer layer) { 68 final Color batchColor = batch.getColor(); 69 final float color = Color.toFloatBits(batchColor.r, batchColor.g, batchColor.b, batchColor.a * layer.getOpacity()); 70 71 final int layerWidth = layer.getWidth(); 72 final int layerHeight = layer.getHeight(); 73 74 final float layerTileWidth = layer.getTileWidth() * unitScale; 75 final float layerTileHeight = layer.getTileHeight() * unitScale; 76 77 final int col1 = Math.max(0, (int)(viewBounds.x / layerTileWidth)); 78 final int col2 = Math.min(layerWidth, (int)((viewBounds.x + viewBounds.width + layerTileWidth) / layerTileWidth)); 79 80 final int row1 = Math.max(0, (int)(viewBounds.y / layerTileHeight)); 81 final int row2 = Math.min(layerHeight, (int)((viewBounds.y + viewBounds.height + layerTileHeight) / layerTileHeight)); 82 83 float y = row2 * layerTileHeight; 84 float xStart = col1 * layerTileWidth; 85 final float[] vertices = this.vertices; 86 87 for (int row = row2; row >= row1; row--) { 88 float x = xStart; 89 for (int col = col1; col < col2; col++) { 90 final TiledMapTileLayer.Cell cell = layer.getCell(col, row); 91 if (cell == null) { 92 x += layerTileWidth; 93 continue; 94 } 95 final TiledMapTile tile = cell.getTile(); 96 97 if (tile != null) { 98 final boolean flipX = cell.getFlipHorizontally(); 99 final boolean flipY = cell.getFlipVertically(); 100 final int rotations = cell.getRotation(); 101 102 TextureRegion region = tile.getTextureRegion(); 103 104 float x1 = x + tile.getOffsetX() * unitScale; 105 float y1 = y + tile.getOffsetY() * unitScale; 106 float x2 = x1 + region.getRegionWidth() * unitScale; 107 float y2 = y1 + region.getRegionHeight() * unitScale; 108 109 float u1 = region.getU(); 110 float v1 = region.getV2(); 111 float u2 = region.getU2(); 112 float v2 = region.getV(); 113 114 vertices[X1] = x1; 115 vertices[Y1] = y1; 116 vertices[C1] = color; 117 vertices[U1] = u1; 118 vertices[V1] = v1; 119 120 vertices[X2] = x1; 121 vertices[Y2] = y2; 122 vertices[C2] = color; 123 vertices[U2] = u1; 124 vertices[V2] = v2; 125 126 vertices[X3] = x2; 127 vertices[Y3] = y2; 128 vertices[C3] = color; 129 vertices[U3] = u2; 130 vertices[V3] = v2; 131 132 vertices[X4] = x2; 133 vertices[Y4] = y1; 134 vertices[C4] = color; 135 vertices[U4] = u2; 136 vertices[V4] = v1; 137 138 if (flipX) { 139 float temp = vertices[U1]; 140 vertices[U1] = vertices[U3]; 141 vertices[U3] = temp; 142 temp = vertices[U2]; 143 vertices[U2] = vertices[U4]; 144 vertices[U4] = temp; 145 } 146 if (flipY) { 147 float temp = vertices[V1]; 148 vertices[V1] = vertices[V3]; 149 vertices[V3] = temp; 150 temp = vertices[V2]; 151 vertices[V2] = vertices[V4]; 152 vertices[V4] = temp; 153 } 154 if (rotations != 0) { 155 switch (rotations) { 156 case Cell.ROTATE_90: { 157 float tempV = vertices[V1]; 158 vertices[V1] = vertices[V2]; 159 vertices[V2] = vertices[V3]; 160 vertices[V3] = vertices[V4]; 161 vertices[V4] = tempV; 162 163 float tempU = vertices[U1]; 164 vertices[U1] = vertices[U2]; 165 vertices[U2] = vertices[U3]; 166 vertices[U3] = vertices[U4]; 167 vertices[U4] = tempU; 168 break; 169 } 170 case Cell.ROTATE_180: { 171 float tempU = vertices[U1]; 172 vertices[U1] = vertices[U3]; 173 vertices[U3] = tempU; 174 tempU = vertices[U2]; 175 vertices[U2] = vertices[U4]; 176 vertices[U4] = tempU; 177 float tempV = vertices[V1]; 178 vertices[V1] = vertices[V3]; 179 vertices[V3] = tempV; 180 tempV = vertices[V2]; 181 vertices[V2] = vertices[V4]; 182 vertices[V4] = tempV; 183 break; 184 } 185 case Cell.ROTATE_270: { 186 float tempV = vertices[V1]; 187 vertices[V1] = vertices[V4]; 188 vertices[V4] = vertices[V3]; 189 vertices[V3] = vertices[V2]; 190 vertices[V2] = tempV; 191 192 float tempU = vertices[U1]; 193 vertices[U1] = vertices[U4]; 194 vertices[U4] = vertices[U3]; 195 vertices[U3] = vertices[U2]; 196 vertices[U2] = tempU; 197 break; 198 } 199 } 200 } 201 batch.draw(region.getTexture(), vertices, 0, NUM_VERTICES); 202 } 203 x += layerTileWidth; 204 } 205 y -= layerTileHeight; 206 } 207 } 208 } 209