1 /******************************************************************************* 2 * Copyright 2015 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.tiledmappacker; 18 19 import java.io.File; 20 21 import com.badlogic.gdx.ApplicationAdapter; 22 import com.badlogic.gdx.Gdx; 23 import com.badlogic.gdx.Input.Keys; 24 import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver; 25 import com.badlogic.gdx.backends.lwjgl.LwjglApplication; 26 import com.badlogic.gdx.files.FileHandle; 27 import com.badlogic.gdx.graphics.GL20; 28 import com.badlogic.gdx.graphics.OrthographicCamera; 29 import com.badlogic.gdx.maps.tiled.AtlasTmxMapLoader; 30 import com.badlogic.gdx.maps.tiled.TiledMap; 31 import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; 32 import com.badlogic.gdx.utils.viewport.FitViewport; 33 import com.badlogic.gdx.utils.viewport.Viewport; 34 35 /** Renders and, optionally, deletes maps processed by TiledMapPackerTest. Run TiledMapPackerTest before running this */ 36 public class TiledMapPackerTestRender extends ApplicationAdapter { 37 38 // --WARNING!-- 39 // Please do not edit the MAP_PATH. This deletes the folder recursively and could be very dangerous. The default location is: 40 // MAP_PATH = "../../tests/gdx-tests-android/assets/data/maps/tiled-atlas-processed/deleteMe/"; 41 42 private final boolean DELETE_DELETEME_FOLDER_ON_EXIT = false; // read warning before setting to true 43 private final static String MAP_PATH = "../../tests/gdx-tests-android/assets/data/maps/tiled-atlas-processed/deleteMe/"; 44 45 private final String MAP_NAME = "test.tmx"; 46 private final String TMX_LOC = MAP_PATH + MAP_NAME; 47 private final boolean CENTER_CAM = true; 48 private final float WORLD_WIDTH = 32; 49 private final float WORLD_HEIGHT = 18; 50 private final float PIXELS_PER_METER = 32; 51 private final float UNIT_SCALE = 1f / PIXELS_PER_METER; 52 private AtlasTmxMapLoader.AtlasTiledMapLoaderParameters params; 53 private AtlasTmxMapLoader atlasTmxMapLoader; 54 private TiledMap map; 55 private Viewport viewport; 56 private OrthogonalTiledMapRenderer mapRenderer; 57 private OrthographicCamera cam; 58 59 @Override create()60 public void create () { 61 atlasTmxMapLoader = new AtlasTmxMapLoader(new InternalFileHandleResolver()); 62 params = new AtlasTmxMapLoader.AtlasTiledMapLoaderParameters(); 63 64 params.generateMipMaps = false; 65 params.convertObjectToTileSpace = false; 66 params.flipY = true; 67 68 viewport = new FitViewport(WORLD_WIDTH, WORLD_HEIGHT); 69 cam = (OrthographicCamera)viewport.getCamera(); 70 71 map = atlasTmxMapLoader.load(TMX_LOC, params); 72 mapRenderer = new OrthogonalTiledMapRenderer(map, UNIT_SCALE); 73 } 74 75 @Override render()76 public void render () { 77 Gdx.gl.glClearColor(0, 0, 0, 1f); 78 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 79 80 viewport.apply(); 81 mapRenderer.setView(cam); 82 mapRenderer.render(); 83 84 if (Gdx.input.isKeyPressed(Keys.ESCAPE)) { 85 if (DELETE_DELETEME_FOLDER_ON_EXIT) { 86 FileHandle deleteMeHandle = Gdx.files.local(MAP_PATH); 87 deleteMeHandle.deleteDirectory(); 88 } 89 90 dispose(); 91 Gdx.app.exit(); 92 } 93 } 94 95 @Override resize(int width, int height)96 public void resize (int width, int height) { 97 viewport.update(width, height, CENTER_CAM); 98 } 99 100 @Override dispose()101 public void dispose () { 102 map.dispose(); 103 } 104 main(String[] args)105 public static void main (String[] args) throws Exception { 106 File file = new File(MAP_PATH); 107 if (!file.exists()) { 108 System.out.println("Please run TiledMapPackerTest."); 109 return; 110 } 111 new LwjglApplication(new TiledMapPackerTestRender(), "", 640, 480); 112 } 113 } 114