• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * *****************************************************************************
3  * Copyright 2011 See AUTHORS file.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6  * use this file except in compliance with the License. You may obtain a copy of
7  * the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations under
15  * the License.
16  * ****************************************************************************
17  */
18 
19 package com.badlogic.gdx.tests;
20 
21 import com.badlogic.gdx.Gdx;
22 import com.badlogic.gdx.graphics.Color;
23 import com.badlogic.gdx.graphics.GL20;
24 import com.badlogic.gdx.graphics.OrthographicCamera;
25 import com.badlogic.gdx.graphics.g2d.BitmapFont;
26 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
27 import com.badlogic.gdx.graphics.g2d.TextureRegion;
28 import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
29 import com.badlogic.gdx.maps.MapLayer;
30 import com.badlogic.gdx.maps.MapObject;
31 import com.badlogic.gdx.maps.MapProperties;
32 import com.badlogic.gdx.maps.objects.EllipseMapObject;
33 import com.badlogic.gdx.maps.objects.PolygonMapObject;
34 import com.badlogic.gdx.maps.objects.RectangleMapObject;
35 import com.badlogic.gdx.maps.objects.TextureMapObject;
36 import com.badlogic.gdx.maps.tiled.TiledMap;
37 import com.badlogic.gdx.maps.tiled.TmxMapLoader;
38 import com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject;
39 import com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile;
40 import com.badlogic.gdx.math.Ellipse;
41 import com.badlogic.gdx.math.Polygon;
42 import com.badlogic.gdx.math.Rectangle;
43 import com.badlogic.gdx.tests.utils.GdxTest;
44 import com.badlogic.gdx.tests.utils.OrthoCamController;
45 
46 public class TiledMapObjectLoadingTest extends GdxTest {
47 
48 	private TiledMap map;
49 	private ShapeRenderer shapeRenderer;
50 	private OrthographicCamera camera;
51 	private OrthoCamController cameraController;
52 	private BitmapFont font;
53 	private SpriteBatch batch;
54 
55 	@Override
create()56 	public void create () {
57 		float w = Gdx.graphics.getWidth();
58 		float h = Gdx.graphics.getHeight();
59 
60 		camera = new OrthographicCamera();
61 		camera.setToOrtho(false, (w / h) * 100, 100);
62 		camera.zoom = 2;
63 		camera.update();
64 
65 		cameraController = new OrthoCamController(camera);
66 		Gdx.input.setInputProcessor(cameraController);
67 
68 		font = new BitmapFont();
69 		batch = new SpriteBatch();
70 		map = new TmxMapLoader().load("data/maps/tiled-objects/test-load-mapobjects.tmx");
71 		MapProperties properties = map.getProperties();
72 		shapeRenderer = new ShapeRenderer();
73 	}
74 
75 	@Override
render()76 	public void render () {
77 		Gdx.gl.glClearColor(0.55f, 0.55f, 0.55f, 1f);
78 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
79 		camera.update();
80 		shapeRenderer.setProjectionMatrix(camera.combined);
81 		batch.setProjectionMatrix(camera.combined);
82 		shapeRenderer.setColor(Color.BLUE);
83 		Gdx.gl20.glLineWidth(2);
84 		MapLayer layer = map.getLayers().get("Objects");
85 		AnimatedTiledMapTile.updateAnimationBaseTime();
86 		for (MapObject mapObject : layer.getObjects()) {
87 			if (mapObject instanceof TiledMapTileMapObject) {
88 				batch.begin();
89 				TiledMapTileMapObject tmtObject = (TiledMapTileMapObject)mapObject;
90 				TextureRegion textureRegion = tmtObject.getTile().getTextureRegion();
91 				// TilEd rotation is clockwise, we need counter-clockwise.
92 				float rotation = -tmtObject.getRotation();
93 				float scaleX = tmtObject.getScaleX();
94 				float scaleY = tmtObject.getScaleY();
95 				float xPos = tmtObject.getX();
96 				float yPos = tmtObject.getY();
97 				textureRegion.flip(tmtObject.isFlipHorizontally(), tmtObject.isFlipVertically());
98 				batch.draw(textureRegion, xPos, yPos, tmtObject.getOriginX() * scaleX, tmtObject.getOriginY() * scaleY,
99 					textureRegion.getRegionWidth() * scaleX, textureRegion.getRegionHeight() * scaleY, 1f, 1f, rotation);
100 				// We flip back to the original state.
101 				textureRegion.flip(tmtObject.isFlipHorizontally(), tmtObject.isFlipVertically());
102 				batch.end();
103 			} else if (mapObject instanceof EllipseMapObject) {
104 				shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
105 				Ellipse ellipse = ((EllipseMapObject)mapObject).getEllipse();
106 				shapeRenderer.ellipse(ellipse.x, ellipse.y, ellipse.width, ellipse.height);
107 				shapeRenderer.end();
108 			} else if (mapObject instanceof RectangleMapObject) {
109 				shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
110 				Rectangle rectangle = ((RectangleMapObject)mapObject).getRectangle();
111 				shapeRenderer.rect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
112 				shapeRenderer.end();
113 			} else if (mapObject instanceof PolygonMapObject) {
114 				shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
115 				Polygon polygon = ((PolygonMapObject)mapObject).getPolygon();
116 				shapeRenderer.polygon(polygon.getTransformedVertices());
117 				shapeRenderer.end();
118 			}
119 		}
120 		batch.begin();
121 		font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 20);
122 		batch.end();
123 	}
124 
125 	@Override
dispose()126 	public void dispose () {
127 		map.dispose();
128 		shapeRenderer.dispose();
129 	}
130 }
131