Java tutorial
/******************************************************************************* * Copyright (c) HALive, 2015. * For Licence information see LICENSE.md ******************************************************************************/ package halive.shootinoutside.game.map; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.maps.MapRenderer; import com.badlogic.gdx.math.Matrix4; import com.badlogic.gdx.math.Rectangle; import halive.shootinoutside.common.core.game.map.GameMap; import halive.shootinoutside.common.core.game.map.GameMapTileType; import static com.badlogic.gdx.graphics.g2d.Batch.*; public class GameMapRenderer implements MapRenderer { private Pixmap textureSheetPixmap; private Texture textureSheet; private GameMapTileTextureHandler textureHandler; private GameMap map; private Rectangle viewBounds; private SpriteBatch batch; private float[] tempVert = new float[20]; public GameMapRenderer(GameMap map) { this.map = map; textureSheetPixmap = new Pixmap(map.getTextureSheet(), 0, map.getTextureSheet().length); textureSheet = new Texture(textureSheetPixmap); textureHandler = new GameMapTileTextureHandler(textureSheet); init(); } public void init() { batch = new SpriteBatch(); viewBounds = new Rectangle(); } public void renderTileMap() { GameMapTileType[][] tiles = map.getTiles(); int minX = (int) Math.max(0, viewBounds.x / GameMap.TILE_WIDTH); int maxX = (int) Math.min(tiles.length, (viewBounds.x + viewBounds.width + GameMap.TILE_WIDTH) / GameMap.TILE_WIDTH); int minY = (int) Math.max(0, viewBounds.y / GameMap.TILE_HEIGHT); int maxY = (int) Math.min(tiles[0].length, (viewBounds.y + viewBounds.height + GameMap.TILE_HEIGHT) / GameMap.TILE_HEIGHT); batch.begin(); for (int x = minX; x < maxX; x++) { for (int y = minY; y < maxY; y++) { //System.out.printf("x=%d y=%d\n",x,y); if (x < tiles.length && y < tiles[x].length) { renderTile(x, y, tiles[x][y]); } } } batch.end(); } private void renderTile(int x, int y, GameMapTileType t) { int tX = x * GameMap.TILE_WIDTH; int tY = y * GameMap.TILE_HEIGHT; TextureRegion region = textureHandler.getRegionFromType(t); //batch.draw(region, tX, tY, GameMap.TILE_WIDTH, GameMap.TILE_HEIGHT); int tX2 = tX + GameMap.TILE_WIDTH; int tY2 = tY + GameMap.TILE_HEIGHT; float u1 = region.getU(); float v1 = region.getV2(); float u2 = region.getU2(); float v2 = region.getV(); Color batchColor = batch.getColor(); float color = Color.toFloatBits(batchColor.r, batchColor.g, batchColor.b, batchColor.a); tempVert[X1] = tX; tempVert[Y1] = tY; tempVert[C1] = color; tempVert[U1] = u1; tempVert[V1] = v1; tempVert[X2] = tX; tempVert[Y2] = tY2; tempVert[C2] = color; tempVert[U2] = u1; tempVert[V2] = v2; tempVert[X3] = tX2; tempVert[Y3] = tY2; tempVert[C3] = color; tempVert[U3] = u2; tempVert[V3] = v2; tempVert[X4] = tX2; tempVert[Y4] = tY; tempVert[C4] = color; tempVert[U4] = u2; tempVert[V4] = v1; batch.draw(region.getTexture(), tempVert, 0, 20); } @Override public void setView(OrthographicCamera camera) { if (camera != null) { 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); } } @Override public void setView(Matrix4 projectionMatrix, float viewboundsX, float viewboundsY, float viewboundsWidth, float viewboundsHeight) { batch.setProjectionMatrix(projectionMatrix); viewBounds.set(viewboundsX, viewboundsY, viewboundsWidth, viewboundsHeight); } @Override public void render() { renderTileMap(); } @Override public void render(int[] layers) { render(); } }