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; 18 19 import com.badlogic.gdx.Gdx; 20 import com.badlogic.gdx.assets.AssetDescriptor; 21 import com.badlogic.gdx.assets.AssetErrorListener; 22 import com.badlogic.gdx.assets.AssetManager; 23 import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver; 24 import com.badlogic.gdx.graphics.GL20; 25 import com.badlogic.gdx.graphics.OrthographicCamera; 26 import com.badlogic.gdx.graphics.Texture.TextureFilter; 27 import com.badlogic.gdx.graphics.g2d.BitmapFont; 28 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 29 import com.badlogic.gdx.maps.tiled.AtlasTmxMapLoader; 30 import com.badlogic.gdx.maps.tiled.AtlasTmxMapLoader.AtlasTiledMapLoaderParameters; 31 import com.badlogic.gdx.maps.tiled.TiledMap; 32 import com.badlogic.gdx.maps.tiled.TiledMapRenderer; 33 import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; 34 import com.badlogic.gdx.tests.utils.GdxTest; 35 import com.badlogic.gdx.tests.utils.OrthoCamController; 36 37 public class TiledMapAtlasAssetManagerTest 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 BitmapFont font; 45 private SpriteBatch batch; 46 String errorMessage; 47 private String fileName = "data/maps/tiled-atlas-processed/test.tmx"; 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) * 10, 10); 56 camera.zoom = 2; 57 camera.update(); 58 59 cameraController = new OrthoCamController(camera); 60 Gdx.input.setInputProcessor(cameraController); 61 62 font = new BitmapFont(); 63 batch = new SpriteBatch(); 64 65 AtlasTiledMapLoaderParameters params = new AtlasTiledMapLoaderParameters(); 66 params.forceTextureFilters = true; 67 params.textureMinFilter = TextureFilter.Linear; 68 params.textureMagFilter = TextureFilter.Linear; 69 70 assetManager = new AssetManager(); 71 assetManager.setErrorListener(new AssetErrorListener() { 72 @Override 73 public void error (AssetDescriptor asset, Throwable throwable) { 74 errorMessage = throwable.getMessage(); 75 } 76 }); 77 78 assetManager.setLoader(TiledMap.class, new AtlasTmxMapLoader(new InternalFileHandleResolver())); 79 assetManager.load(fileName, TiledMap.class); 80 } 81 82 @Override render()83 public void render () { 84 Gdx.gl.glClearColor(100f / 255f, 100f / 255f, 250f / 255f, 1f); 85 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 86 camera.update(); 87 assetManager.update(); 88 if (renderer == null && assetManager.isLoaded(fileName)) { 89 map = assetManager.get(fileName); 90 renderer = new OrthogonalTiledMapRenderer(map, 1f / 32f); 91 } else if (renderer != null) { 92 renderer.setView(camera); 93 renderer.render(); 94 } 95 batch.begin(); 96 if (errorMessage != null) font.draw(batch, "ERROR (OK if running in GWT): " + errorMessage, 10, 50); 97 font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 20); 98 batch.end(); 99 } 100 } 101