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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*******************************************************************************
* 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.graphics.g2d.tiled;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.zip.DataFormatException;
import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Base64Coder;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.XmlReader;

/** Loads a Tiled Map from a tmx file
* @author David Fraska */
public class TiledLoader {

/** Loads a <code>TiledMap</code> from a <code>String</code>.
* @param tmxData The tmx file's content. */
public static TiledMap createMap (String tmxData) {
return createMap(null, tmxData);
}

/** Loads a Tiled Map from a tmx file
* @param tmxFile the map's tmx file */
public static TiledMap createMap (FileHandle tmxFile) {
return createMap(tmxFile, null);
}

/** Loads a TiledMap from a tmx file.
* @param tmxFile The tmx file. NULL to force load from <code>tmxData</code>.
* @param tmxData The tmx file's content. NULL to force load from <code>tmxFile</code>. */
private static TiledMap createMap (FileHandle tmxFile, String tmxData) {
final TiledMap map;

map = new TiledMap();
map.tmxFile = tmxFile;

try {
XmlReader xmlReader = new XmlReader() {

Stack<String> currBranch = new Stack<String>();

boolean awaitingData = false;
TiledLayer currLayer;
int currLayerWidth = 0, currLayerHeight = 0;
TileSet currTileSet;
TiledObjectGroup currObjectGroup;
TiledObject currObject;
int currTile;

class Polyline {
String name;
String points;

public Polyline( String name ) {
this.name = name;
}

public Polyline() {
}
}

Polyline polyline,polygon;

class Property {
String parentType, name, value;
}

Property currProperty;

String encoding, dataString, compression;
byte[] data;

int dataCounter = 0, row, col;

@Override
protected void open (String name) {
currBranch.push(name);

if ("layer".equals(name)) {
currLayer = new TiledLayer();
return;
}

if ("tileset".equals(name)) {
currTileSet = new TileSet();
return;
}

if ("data".equals(name)) {
dataString = ""; // clear the string for new data
awaitingData = true;
return;
}

if ("objectgroup".equals(name)) {
currObjectGroup = new TiledObjectGroup();
return;
}

if ("object".equals(name)) {
currObject = new TiledObject();
return;
}

if ("property".equals(name)) {
currProperty = new Property();
currProperty.parentType = currBranch.get(currBranch.size() - 3);
return;
}

if( "polyline".equals( name ) ) {
polyline = new Polyline("polyline");
return;
}

if( "polygon".equals( name ) ) {
polygon = new Polyline("polygon");
return;
}
}

@Override
protected void attribute (String name, String value) {
String element = currBranch.peek();

if ("layer".equals(element)) {
if ("width".equals(name)) {
currLayerWidth = Integer.parseInt(value);
} else if ("height".equals(name)) {
currLayerHeight = Integer.parseInt(value);
}

if (currLayerWidth != 0 && currLayerHeight != 0) {
currLayer.tiles = new int[currLayerHeight][currLayerWidth];
}
if ("name".equals(name)) {
currLayer.name = value;
}
return;
}

if ("tileset".equals(element)) {
if ("firstgid".equals(name)) {
currTileSet.firstgid = Integer.parseInt(value);
return;
}
if ("tilewidth".equals(name)) {
currTileSet.tileWidth = Integer.parseInt(value);
return;
}
if ("tileheight".equals(name)) {
currTileSet.tileHeight = Integer.parseInt(value);
return;
}
if ("name".equals(name)) {
currTileSet.name = value;
return;
}
if ("spacing".equals(name)) {
currTileSet.spacing = Integer.parseInt(value);
return;
}
if ("margin".equals(name)) {
currTileSet.margin = Integer.parseInt(value);
return;
}
return;
}

if ("image".equals(element)) {
if ("source".equals(name)) {
currTileSet.imageName = value;
return;
}
return;
}

if ("data".equals(element)) {
if ("encoding".equals(name)) {
encoding = value;
return;
}
if ("compression".equals(name)) {
compression = value;
return;
}
return;
}

if ("objectgroup".equals(element)) {
if ("name".equals(name)) {
currObjectGroup.name = value;
return;
}
if ("height".equals(name)) {
currObjectGroup.height = Integer.parseInt(value);
return;
}
if ("width".equals(name)) {
currObjectGroup.width = Integer.parseInt(value);
return;
}
return;
}

if ("object".equals(element)) {
if ("name".equals(name)) {
currObject.name = value;
return;
}
if ("type".equals(name)) {
currObject.type = value;
return;
}
if ("x".equals(name)) {
currObject.x = Integer.parseInt(value);
return;
}
if ("y".equals(name)) {
currObject.y = Integer.parseInt(value);
return;
}
if ("width".equals(name)) {
currObject.width = Integer.parseInt(value);
return;
}
if ("height".equals(name)) {
currObject.height = Integer.parseInt(value);
return;
}
if ("gid".equals(name)) {
currObject.gid = Integer.parseInt(value);
return;
}
return;
}

if ("map".equals(element)) {
if ("orientation".equals(name)) {
map.orientation = value;
return;
}
if ("width".equals(name)) {
map.width = Integer.parseInt(value);
return;
}
if ("height".equals(name)) {
map.height = Integer.parseInt(value);
return;
}
if ("tilewidth".equals(name)) {
map.tileWidth = Integer.parseInt(value);
return;
}
if ("tileheight".equals(name)) {
map.tileHeight = Integer.parseInt(value);
return;
}
return;
}

if ("tile".equals(element)) {
if (awaitingData) { // Actually getting tile data
if ("gid".equals(name)) {
col = dataCounter % currLayerWidth;
row = dataCounter / currLayerWidth;
if (row < currLayerHeight) {
currLayer.tiles[row][col] = Integer.parseInt(value);
} else {
Gdx.app.log("TiledLoader", "Warning: extra XML gid values ignored! Your map is likely corrupt!");
}
dataCounter++;
}
} else { // Not getting tile data, must be a tile Id (for properties)
if ("id".equals(name)) {
currTile = Integer.parseInt(value);
}
}
return;
}

if ("property".equals(element)) {
if ("name".equals(name)) {
currProperty.name = value;
return;
}
if ("value".equals(name)) {
currProperty.value = value;
return;
}
return;
}

if( "polyline".equals( element ) ) {
if( "points".equals( name ) ) {
polyline.points = value;
return;
}
return;
}

if( "polygon".equals( element ) ) {
if( "points".equals( name ) ) {
polygon.points = value;
return;
}
return;
}
}

@Override
protected void text (String text) {
if (awaitingData) {
dataString = dataString.concat(text);
}
}

@Override
protected void close () {
String element = currBranch.pop();

if ("layer".equals(element)) {
map.layers.add(currLayer);
currLayer = null;
return;
}

if ("tileset".equals(element)) {
map.tileSets.add(currTileSet);
currTileSet = null;
return;
}

if ("object".equals(element)) {
currObjectGroup.objects.add(currObject);
currObject = null;
return;
}

if ("objectgroup".equals(element)) {
map.objectGroups.add(currObjectGroup);
currObjectGroup = null;
return;
}

if ("property".equals(element)) {
putProperty(currProperty);
currProperty = null;
return;
}

if( "polyline".equals( element ) ) {
putPolyLine( polyline );
polyline = null;
return;
}

if( "polygon".equals( element ) ) {
putPolyLine( polygon );
polygon = null;
return;
}

if ("data".equals(element)) {

// decode and uncompress the data
if ("base64".equals(encoding)) {
if (dataString == null | "".equals(dataString.trim())) return;

data = Base64Coder.decode(dataString.trim());

if ("gzip".equals(compression)) {
unGZip();
} else if ("zlib".equals(compression)) {
unZlib();
} else if (compression == null) {
arrangeData();
}

} else if ("csv".equals(encoding) && compression == null) {
fromCSV();

} else if (encoding == null && compression == null) {
// startElement() handles most of this
dataCounter = 0;// reset counter in case another layer comes through
} else {
throw new GdxRuntimeException("Unsupported encoding and/or compression format");
}

awaitingData = false;
return;
}

if ("property".equals(element)) {
putProperty(currProperty);
currProperty = null;
}
}

private void putPolyLine( Polyline polyLine ) {
if( polyLine == null ) {
return;
}

if( "polyline".equals( polyLine.name ) ) {
currObject.polyline = polyLine.points;
return;
}

if( "polygon".equals( polyLine.name ) ) {
currObject.polygon = polyLine.points;
return;
}

return;
}

private void putProperty (Property property) {
if ("tile".equals(property.parentType)) {
map.setTileProperty(currTile + currTileSet.firstgid, property.name, property.value);
return;
}

if ("map".equals(property.parentType)) {
map.properties.put(property.name, property.value);
return;
}

if ("layer".equals(property.parentType)) {
currLayer.properties.put(property.name, property.value);
return;
}

if ("objectgroup".equals(property.parentType)) {
currObjectGroup.properties.put(property.name, property.value);
return;
}

if ("object".equals(property.parentType)) {
currObject.properties.put(property.name, property.value);
return;
}
}

private void fromCSV () {
StringTokenizer st = new StringTokenizer(dataString.trim(), ",");
for (int row = 0; row < currLayerHeight; row++) {
for (int col = 0; col < currLayerWidth; col++) {
currLayer.tiles[row][col] = Integer.parseInt(st.nextToken().trim());
}
}
}

private void arrangeData () {
int byteCounter = 0;
for (int row = 0; row < currLayerHeight; row++) {
for (int col = 0; col < currLayerWidth; col++) {
currLayer.tiles[row][col] = unsignedByteToInt(data[byteCounter++])
| unsignedByteToInt(data[byteCounter++]) << 8 | unsignedByteToInt(data[byteCounter++]) << 16
| unsignedByteToInt(data[byteCounter++]) << 24;
}
}
}

private void unZlib () {
Inflater zlib = new Inflater();
byte[] readTemp = new byte[4];

zlib.setInput(data, 0, data.length);

for (int row = 0; row < currLayerHeight; row++) {
for (int col = 0; col < currLayerWidth; col++) {
try {
zlib.inflate(readTemp, 0, 4);
currLayer.tiles[row][col] = unsignedByteToInt(readTemp[0]) | unsignedByteToInt(readTemp[1]) << 8
| unsignedByteToInt(readTemp[2]) << 16 | unsignedByteToInt(readTemp[3]) << 24;
} catch (DataFormatException e) {
throw new GdxRuntimeException("Error Reading TMX Layer Data.", e);
}
}
}
}

private void unGZip () {
GZIPInputStream GZIS = null;
try {
GZIS = new GZIPInputStream(new ByteArrayInputStream(data), data.length);
} catch (IOException e) {
throw new GdxRuntimeException("Error Reading TMX Layer Data - IOException: " + e.getMessage());
}

// Read the GZIS data into an array, 4 bytes = 1 GID
byte[] readTemp = new byte[4];
for (int row = 0; row < currLayerHeight; row++) {
for (int col = 0; col < currLayerWidth; col++) {
try {
GZIS.read(readTemp, 0, 4);
currLayer.tiles[row][col] = unsignedByteToInt(readTemp[0]) | unsignedByteToInt(readTemp[1]) << 8
| unsignedByteToInt(readTemp[2]) << 16 | unsignedByteToInt(readTemp[3]) << 24;
} catch (IOException e) {
throw new GdxRuntimeException("Error Reading TMX Layer Data.", e);
}
}
}
}
};
// Is it a file?
if (tmxFile != null) {
xmlReader.parse(tmxFile);
} else {
xmlReader.parse(tmxData);
}
} catch (IOException e) {
throw new GdxRuntimeException("Error Parsing TMX file", e);
}

return map;
}

static int unsignedByteToInt (byte b) {
return (int)b & 0xFF;
}
}

Change log

r3439 by badlogicgames on Feb 28, 2012   Diff
[fixed]  issue 701 , polyline support for
tilemaps.
Go to: 
Project members, sign in to write a code review

Older revisions

r2883 by nathan.sweet on Nov 6, 2011   Diff
Organize imports and format.
r2755 by badlogicgames on Sep 30, 2011   Diff
[patched] TiledLoader can take a
string as well now (could override
FileHandle but meh :)).
[fixed] parts of SkinPacker. Still no
worky.
r2679 by tamasjano on Sep 9, 2011   Diff
FIX -  Issue 401 . (TiledLoader doesn't
load layer names)
All revisions of this file

File info

Size: 14122 bytes, 541 lines
Powered by Google Project Hosting