Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Flipped TMX Tile support, issue 1233

Added flipped tile support for TMX maps and a test case for it.  Also
cleaned up some duplicate code.
  • Loading branch information...
commit 722c6ddfbd2fc224e55984e7e4afd10f75d5f3a4 1 parent 1ac1fdb
authored August 30, 2011
15  Resources/TileMaps/ortho-rotation-test.tmx
... ...
@@ -0,0 +1,15 @@
  1
+<?xml version="1.0" encoding="UTF-8"?>
  2
+<!DOCTYPE map SYSTEM "http://mapeditor.org/dtd/1.0/map.dtd">
  3
+<map version="1.0" orientation="orthogonal" width="12" height="12" tilewidth="101" tileheight="81">
  4
+ <tileset firstgid="1" source="orthogonal-test1.tsx"/>
  5
+ <layer name="Backgr" width="12" height="12">
  6
+  <data encoding="base64" compression="gzip">
  7
+   H4sIAAAAAAAAA1NhYGBQGcWjmEwMAOTHmZJAAgAA
  8
+  </data>
  9
+ </layer>
  10
+ <layer name="Layer 0" width="12" height="12">
  11
+  <data encoding="base64" compression="gzip">
  12
+   H4sIAAAAAAAAA63PQQrAIAwEwIDUg6AHi2e/4tP2aXlaV5LSXtqCNDAQCVmiiMhuQEpDfq5kQJoW8wN3g+9W9vWWEw1I43r+BM+fcM42A9JpJf+tsgFp9vsb383n3eBh/bOKAWm58rV5z+zRF/91AHeSesdAAgAA
  13
+  </data>
  14
+ </layer>
  15
+</map>
33  cocos2d/CCTMXLayer.m
@@ -36,6 +36,7 @@
36 36
 #import "CCTextureCache.h"
37 37
 #import "Support/CGPointExtension.h"
38 38
 
  39
+
39 40
 #pragma mark -
40 41
 #pragma mark CCSpriteBatchNode Extension
41 42
 
@@ -332,11 +333,26 @@ -(uint32_t) tileGIDAt:(CGPoint)pos
332 333
 	NSAssert( tiles_ && atlasIndexArray_, @"TMXLayer: the tiles map has been released");
333 334
 	
334 335
 	NSInteger idx = pos.x + pos.y * layerSize_.width;
335  
-	return tiles_[ idx ];
  336
+	
  337
+	// Bits on the far end of the 32-bit global tile ID are used for tile flags
  338
+	return (tiles_[ idx ] & kFlippedMask);
336 339
 }
337 340
 
338 341
 #pragma mark CCTMXLayer - adding helper methods
339 342
 
  343
+- (void) setupReusedTile:(CGPoint)pos withGID:(uint32_t)gid
  344
+{
  345
+	[reusedTile_ setPositionInPixels: [self positionAt:pos]];
  346
+	[reusedTile_ setVertexZ: [self vertexZForPos:pos]];
  347
+	reusedTile_.anchorPoint = CGPointZero;
  348
+	[reusedTile_ setOpacity:opacity_];
  349
+	
  350
+	if (gid & kFlippedHorizontallyFlag)
  351
+		reusedTile_.flipX = YES;
  352
+	if (gid & kFlippedVerticallyFlag)
  353
+		reusedTile_.flipY = YES;
  354
+}
  355
+
340 356
 -(CCSprite*) insertTileForGID:(uint32_t)gid at:(CGPoint)pos
341 357
 {
342 358
 	CGRect rect = [tileset_ rectForGID:gid];
@@ -348,10 +364,7 @@ -(CCSprite*) insertTileForGID:(uint32_t)gid at:(CGPoint)pos
348 364
 	else
349 365
 		[reusedTile_ initWithBatchNode:self rectInPixels:rect];
350 366
 	
351  
-	[reusedTile_ setPositionInPixels: [self positionAt:pos]];
352  
-	[reusedTile_ setVertexZ: [self vertexZForPos:pos]];
353  
-	reusedTile_.anchorPoint = CGPointZero;
354  
-	[reusedTile_ setOpacity:opacity_];
  367
+	[self setupReusedTile:pos withGID:gid];
355 368
 	
356 369
 	// get atlas index
357 370
 	NSUInteger indexForZ = [self atlasIndexForNewZ:z];
@@ -386,10 +399,7 @@ -(CCSprite*) updateTileForGID:(uint32_t)gid at:(CGPoint)pos
386 399
 	else
387 400
 		[reusedTile_ initWithBatchNode:self rectInPixels:rect];
388 401
 	
389  
-	[reusedTile_ setPositionInPixels: [self positionAt:pos]];
390  
-	[reusedTile_ setVertexZ: [self vertexZForPos:pos]];
391  
-	reusedTile_.anchorPoint = CGPointZero;
392  
-	[reusedTile_ setOpacity:opacity_];
  402
+	[self setupReusedTile:pos withGID:gid];
393 403
 	
394 404
 	// get atlas index
395 405
 	NSUInteger indexForZ = [self atlasIndexForExistantZ:z];
@@ -416,10 +426,7 @@ -(CCSprite*) appendTileForGID:(uint32_t)gid at:(CGPoint)pos
416 426
 	else
417 427
 		[reusedTile_ initWithBatchNode:self rectInPixels:rect];
418 428
 	
419  
-	[reusedTile_ setPositionInPixels: [self positionAt:pos]];
420  
-	[reusedTile_ setVertexZ: [self vertexZForPos:pos]];
421  
-	reusedTile_.anchorPoint = CGPointZero;
422  
-	[reusedTile_ setOpacity:opacity_];
  429
+	[self setupReusedTile:pos withGID:gid];
423 430
 	
424 431
 	// optimization:
425 432
 	// The difference between appendTileForGID and insertTileforGID is that append is faster, since
5  cocos2d/CCTMXXMLParser.h
@@ -56,6 +56,11 @@ enum {
56 56
 	TMXPropertyTile
57 57
 };
58 58
 
  59
+// Bits on the far end of the 32-bit global tile ID (GID's) are used for tile flags
  60
+#define kFlippedHorizontallyFlag	0x80000000
  61
+#define kFlippedVerticallyFlag		0x40000000
  62
+#define kFlippedMask				~(kFlippedHorizontallyFlag|kFlippedVerticallyFlag)
  63
+
59 64
 /* CCTMXLayerInfo contains the information about the layers like:
60 65
  - Layer name
61 66
  - Layer size
1  cocos2d/CCTMXXMLParser.m
@@ -97,6 +97,7 @@ -(CGRect) rectForGID:(unsigned int)gid
97 97
 	CGRect rect;
98 98
 	rect.size = tileSize_;
99 99
 	
  100
+	gid &= kFlippedMask;
100 101
 	gid = gid - firstGid_;
101 102
 	
102 103
 	int max_x = (imageSize_.width - margin_*2 + spacing_) / (tileSize_.width + spacing_);
4  tests/TileMapTest.h
@@ -139,6 +139,10 @@
139 139
 {}
140 140
 @end
141 141
 
  142
+@interface TMXOrthoFlipTest : TileDemo
  143
+{}
  144
+@end
  145
+
142 146
 @interface TMXBug987 : TileDemo
143 147
 {}
144 148
 @end
31  tests/TileMapTest.m
@@ -37,6 +37,7 @@
37 37
 	@"TMXResizeTest",
38 38
 	@"TMXIsoMoveLayer",
39 39
 	@"TMXOrthoMoveLayer",
  40
+	@"TMXOrthoFlipTest",
40 41
 	@"TMXBug987",
41 42
 	@"TMXBug787",
42 43
 
@@ -1364,6 +1365,36 @@ -(NSString *) subtitle
1364 1365
 @end
1365 1366
 
1366 1367
 #pragma mark -
  1368
+#pragma mark TMXOrthoFlipTest
  1369
+
  1370
+@implementation TMXOrthoFlipTest
  1371
+-(id) init
  1372
+{
  1373
+	if( (self=[super init]) ) {		
  1374
+		CCTMXTiledMap *map = [CCTMXTiledMap tiledMapWithTMXFile:@"TileMaps/ortho-rotation-test.tmx"];
  1375
+		[self addChild:map z:0 tag:kTagTileMap];
  1376
+		
  1377
+		CGSize s = map.contentSize;
  1378
+		NSLog(@"ContentSize: %f, %f", s.width,s.height);
  1379
+		
  1380
+		for( CCSpriteBatchNode* child in [map children] ) {
  1381
+			[[child texture] setAntiAliasTexParameters];
  1382
+		}
  1383
+		
  1384
+		id action = [CCScaleBy actionWithDuration:2 scale:0.5f];
  1385
+		[map runAction:action];
  1386
+	}	
  1387
+	return self;
  1388
+}
  1389
+
  1390
+-(NSString *) title
  1391
+{
  1392
+	return @"TMX tile flip test";
  1393
+}
  1394
+@end
  1395
+
  1396
+
  1397
+#pragma mark -
1367 1398
 #pragma mark TMXBug987
1368 1399
 
1369 1400
 @implementation TMXBug987

0 notes on commit 722c6dd

Please sign in to comment.
Something went wrong with that request. Please try again.