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.tests; 18 19 import com.badlogic.gdx.Gdx; 20 import com.badlogic.gdx.graphics.GL20; 21 import com.badlogic.gdx.graphics.OrthographicCamera; 22 import com.badlogic.gdx.graphics.Texture; 23 import com.badlogic.gdx.graphics.g2d.TextureRegion; 24 import com.badlogic.gdx.maps.MapLayers; 25 import com.badlogic.gdx.maps.tiled.TiledMap; 26 import com.badlogic.gdx.maps.tiled.TiledMapTile; 27 import com.badlogic.gdx.maps.tiled.TiledMapTileLayer; 28 import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell; 29 import com.badlogic.gdx.maps.tiled.renderers.HexagonalTiledMapRenderer; 30 import com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile; 31 import com.badlogic.gdx.tests.utils.GdxTest; 32 import com.badlogic.gdx.tests.utils.OrthoCamController; 33 34 public class HexagonalTiledMapTest extends GdxTest { 35 TiledMap map; 36 OrthographicCamera camera; 37 OrthoCamController cameraController; 38 HexagonalTiledMapRenderer renderer; 39 Texture hexture; 40 41 @Override create()42 public void create () { 43 super.create(); 44 float w = Gdx.graphics.getWidth(); 45 float h = Gdx.graphics.getHeight(); 46 47 camera = new OrthographicCamera(); 48 camera.setToOrtho(false, (w / h) * 480, 480); 49 camera.update(); 50 51 cameraController = new OrthoCamController(camera); 52 Gdx.input.setInputProcessor(cameraController); 53 54 hexture = new Texture(Gdx.files.internal("data/maps/tiled/hex/hexes.png")); 55 TextureRegion[][] hexes = TextureRegion.split(hexture, 112, 97); 56 map = new TiledMap(); 57 MapLayers layers = map.getLayers(); 58 TiledMapTile[] tiles = new TiledMapTile[3]; 59 tiles[0] = new StaticTiledMapTile(new TextureRegion(hexes[0][0])); 60 tiles[1] = new StaticTiledMapTile(new TextureRegion(hexes[0][1])); 61 tiles[2] = new StaticTiledMapTile(new TextureRegion(hexes[1][0])); 62 63 for (int l = 0; l < 1; l++) { 64 TiledMapTileLayer layer = new TiledMapTileLayer(45, 30, 112, 97); 65 for (int y = 0; y < 30; y++) { 66 for (int x = 0; x < 45; x++) { 67 int id = (int)(Math.random() * 3); 68 Cell cell = new Cell(); 69 cell.setTile(tiles[id]); 70 layer.setCell(x, y, cell); 71 } 72 } 73 layers.add(layer); 74 } 75 76 renderer = new HexagonalTiledMapRenderer(map); 77 } 78 79 @Override render()80 public void render () { 81 Gdx.gl.glClearColor(0.25f, 0.25f, 0.25f, 1f); 82 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 83 camera.update(); 84 renderer.setView(camera); 85 renderer.render(); 86 } 87 88 @Override dispose()89 public void dispose () { 90 renderer.dispose(); 91 hexture.dispose(); 92 map.dispose(); 93 } 94 95 } 96