1 /******************************************************************************* 2 * Copyright 2011 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.bench; 18 19 import com.badlogic.gdx.Gdx; 20 import com.badlogic.gdx.assets.AssetManager; 21 import com.badlogic.gdx.graphics.GL20; 22 import com.badlogic.gdx.graphics.OrthographicCamera; 23 import com.badlogic.gdx.graphics.Texture; 24 import com.badlogic.gdx.graphics.g2d.BitmapFont; 25 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 26 import com.badlogic.gdx.graphics.g2d.TextureRegion; 27 import com.badlogic.gdx.maps.MapLayers; 28 import com.badlogic.gdx.maps.tiled.TiledMap; 29 import com.badlogic.gdx.maps.tiled.TiledMapRenderer; 30 import com.badlogic.gdx.maps.tiled.TiledMapTileLayer; 31 import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell; 32 import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; 33 import com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile; 34 import com.badlogic.gdx.tests.utils.GdxTest; 35 import com.badlogic.gdx.tests.utils.OrthoCamController; 36 37 public class TiledMapBench extends GdxTest { 38 39 private TiledMap map; 40 private TiledMapRenderer renderer; 41 private OrthographicCamera camera; 42 private OrthoCamController cameraController; 43 private AssetManager assetManager; 44 private Texture tiles; 45 private Texture texture; 46 private BitmapFont font; 47 private SpriteBatch batch; 48 49 @Override create()50 public void create () { 51 float w = Gdx.graphics.getWidth(); 52 float h = Gdx.graphics.getHeight(); 53 54 camera = new OrthographicCamera(); 55 camera.setToOrtho(false, (w / h) * 320, 320); 56 camera.update(); 57 58 cameraController = new OrthoCamController(camera); 59 Gdx.input.setInputProcessor(cameraController); 60 61 font = new BitmapFont(); 62 batch = new SpriteBatch(); 63 64 { 65 tiles = new Texture(Gdx.files.internal("data/maps/tiled/tiles.png")); 66 TextureRegion[][] splitTiles = TextureRegion.split(tiles, 32, 32); 67 map = new TiledMap(); 68 MapLayers layers = map.getLayers(); 69 for (int l = 0; l < 20; l++) { 70 TiledMapTileLayer layer = new TiledMapTileLayer(150, 100, 32, 32); 71 for (int x = 0; x < 150; x++) { 72 for (int y = 0; y < 100; y++) { 73 int ty = (int)(Math.random() * splitTiles.length); 74 int tx = (int)(Math.random() * splitTiles[ty].length); 75 Cell cell = new Cell(); 76 cell.setTile(new StaticTiledMapTile(splitTiles[ty][tx])); 77 layer.setCell(x, y, cell); 78 } 79 } 80 layers.add(layer); 81 } 82 } 83 84 renderer = new OrthogonalTiledMapRenderer(map); 85 86 } 87 88 @Override render()89 public void render () { 90 Gdx.gl.glClearColor(100f / 255f, 100f / 255f, 250f / 255f, 1f); 91 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 92 camera.update(); 93 renderer.setView(camera); 94 renderer.render(); 95 batch.begin(); 96 font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 20); 97 batch.end(); 98 } 99 } 100