My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/

package com.badlogic.gdx.tests;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.tiled.TileAtlas;
import com.badlogic.gdx.graphics.g2d.tiled.TileMapRenderer;
import com.badlogic.gdx.graphics.g2d.tiled.TiledLoader;
import com.badlogic.gdx.graphics.g2d.tiled.TiledMap;
import com.badlogic.gdx.graphics.g2d.tiled.TiledObject;
import com.badlogic.gdx.graphics.g2d.tiled.TiledObjectGroup;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.tests.utils.GdxTest;
import com.badlogic.gdx.tests.utils.OrthoCamController;

/** @author David Fraska */
public class TiledMapTest extends GdxTest {

private static final boolean automove = true;

private static final int[] layersList = {2, 3};

SpriteBatch spriteBatch;
BitmapFont font;

OrthographicCamera cam;
OrthoCamController camController;
Vector3 camDirection = new Vector3(1, 1, 0);
Vector2 maxCamPosition = new Vector2(0, 0);

TileMapRenderer tileMapRenderer;
TiledMap map;
TileAtlas atlas;

long startTime = System.nanoTime();
Vector3 tmp = new Vector3();

@Override
public void render () {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

if (automove) {
updateCameraPosition();
}

cam.zoom = 0.9f;
cam.update();
// tileMapRenderer.getProjectionMatrix().set(cam.combined); //Not required when using tileMapRenderer.render(cam)
tileMapRenderer.render(cam);// , layersList);

spriteBatch.begin();
font.draw(spriteBatch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 20, 20);
font.draw(spriteBatch, "InitialCol, LastCol: " + tileMapRenderer.getInitialCol() + "," + tileMapRenderer.getLastCol(), 20,
40);
font.draw(spriteBatch, "InitialRow, LastRow: " + tileMapRenderer.getInitialRow() + "," + tileMapRenderer.getLastRow(), 20,
60);

tmp.set(0, 0, 0);
cam.unproject(tmp);
font.draw(spriteBatch, "Location: " + tmp.x + "," + tmp.y, 20, 80);
spriteBatch.end();
}

private void updateCameraPosition () {
cam.position.add(camDirection.tmp().mul(Gdx.graphics.getDeltaTime()).mul(5 * tileMapRenderer.getUnitsPerTileX()));

if (cam.position.x < 0) {
cam.position.x = 0;
camDirection.x = 1;
}
if (cam.position.x > maxCamPosition.x) {
cam.position.x = maxCamPosition.x;
camDirection.x = -1;
}
if (cam.position.y < 0) {
cam.position.y = 0;
camDirection.y = 1;
}
if (cam.position.y > maxCamPosition.y) {
cam.position.y = maxCamPosition.y;
camDirection.y = -1;
}
}

@Override
public void create () {
int i;
long startTime, endTime;
font = new BitmapFont();
font.setColor(Color.RED);

spriteBatch = new SpriteBatch();

final String path = "data/tiledmap/";
final String mapname = "tilemap csv";

FileHandle mapHandle = Gdx.files.internal(path + mapname + ".tmx");
FileHandle baseDir = Gdx.files.internal(path);

startTime = System.currentTimeMillis();
map = TiledLoader.createMap(mapHandle);
endTime = System.currentTimeMillis();
System.out.println("Loaded map in " + (endTime - startTime) + "mS");

atlas = new TileAtlas(map, baseDir);

int blockWidth = 10;
int blockHeight = 12;

startTime = System.currentTimeMillis();

tileMapRenderer = new TileMapRenderer(map, atlas, blockWidth, blockHeight, 5, 5);
endTime = System.currentTimeMillis();
System.out.println("Created cache in " + (endTime - startTime) + "mS");

for (TiledObjectGroup group : map.objectGroups) {
for (TiledObject object : group.objects) {
// TODO: Draw sprites where objects occur
System.out.println("Object " + object.name + " x,y = " + object.x + "," + object.y + " width,height = "
+ object.width + "," + object.height);
}
}

float aspectRatio = (float)Gdx.graphics.getWidth() / (float)Gdx.graphics.getHeight();
cam = new OrthographicCamera(100f * aspectRatio, 100f);

cam.position.set(tileMapRenderer.getMapWidthUnits() / 2, tileMapRenderer.getMapHeightUnits() / 2, 0);
camController = new OrthoCamController(cam);
Gdx.input.setInputProcessor(camController);

maxCamPosition.set(tileMapRenderer.getMapWidthUnits(), tileMapRenderer.getMapHeightUnits());
}

@Override
public boolean needsGL20 () {
return false;
}
}

Change log

r2518 by nathan.sweet on Aug 13, 2011   Diff
[updated] Everything to use the latest
source formatter. Yay! Use it, love it!
Go to: 
Project members, sign in to write a code review

Older revisions

r2104 by dfraska on May 27, 2011   Diff
[changed] TiledLoader now backed by
Nate's super awesome xml class!
r2082 by dfraska on May 25, 2011   Diff
[fixed] TileMapRenderer.render(Camera 
cam)->TileMapRenderer.render(Orthograp
hicCamera cam), now properly handles
OrthographicCamera's zoom option.
r2066 by dfraska on May 24, 2011   Diff
[added] TileMapRenderer can now accept
any unit for drawing tile maps
(through the unitsPerTileX and
unitsPerTileY arguments).
[added] TileMapRenderer.render(Camera)
...
All revisions of this file

File info

Size: 5351 bytes, 159 lines
Powered by Google Project Hosting