Permalink
Browse files

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...
slycrel committed Aug 31, 2011
1 parent 1ac1fdb commit 722c6ddfbd2fc224e55984e7e4afd10f75d5f3a4
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE map SYSTEM "http://mapeditor.org/dtd/1.0/map.dtd">
+<map version="1.0" orientation="orthogonal" width="12" height="12" tilewidth="101" tileheight="81">
+ <tileset firstgid="1" source="orthogonal-test1.tsx"/>
+ <layer name="Backgr" width="12" height="12">
+ <data encoding="base64" compression="gzip">
+ H4sIAAAAAAAAA1NhYGBQGcWjmEwMAOTHmZJAAgAA
+ </data>
+ </layer>
+ <layer name="Layer 0" width="12" height="12">
+ <data encoding="base64" compression="gzip">
+ H4sIAAAAAAAAA63PQQrAIAwEwIDUg6AHi2e/4tP2aXlaV5LSXtqCNDAQCVmiiMhuQEpDfq5kQJoW8wN3g+9W9vWWEw1I43r+BM+fcM42A9JpJf+tsgFp9vsb383n3eBh/bOKAWm58rV5z+zRF/91AHeSesdAAgAA
+ </data>
+ </layer>
+</map>
View
@@ -36,6 +36,7 @@
#import "CCTextureCache.h"
#import "Support/CGPointExtension.h"
+
#pragma mark -
#pragma mark CCSpriteBatchNode Extension
@@ -332,11 +333,26 @@ -(uint32_t) tileGIDAt:(CGPoint)pos
NSAssert( tiles_ && atlasIndexArray_, @"TMXLayer: the tiles map has been released");
NSInteger idx = pos.x + pos.y * layerSize_.width;
- return tiles_[ idx ];
+
+ // Bits on the far end of the 32-bit global tile ID are used for tile flags
+ return (tiles_[ idx ] & kFlippedMask);
}
#pragma mark CCTMXLayer - adding helper methods
+- (void) setupReusedTile:(CGPoint)pos withGID:(uint32_t)gid
+{
+ [reusedTile_ setPositionInPixels: [self positionAt:pos]];
+ [reusedTile_ setVertexZ: [self vertexZForPos:pos]];
+ reusedTile_.anchorPoint = CGPointZero;
+ [reusedTile_ setOpacity:opacity_];
+
+ if (gid & kFlippedHorizontallyFlag)
+ reusedTile_.flipX = YES;
+ if (gid & kFlippedVerticallyFlag)
+ reusedTile_.flipY = YES;
+}
+
-(CCSprite*) insertTileForGID:(uint32_t)gid at:(CGPoint)pos
{
CGRect rect = [tileset_ rectForGID:gid];
@@ -348,10 +364,7 @@ -(CCSprite*) insertTileForGID:(uint32_t)gid at:(CGPoint)pos
else
[reusedTile_ initWithBatchNode:self rectInPixels:rect];
- [reusedTile_ setPositionInPixels: [self positionAt:pos]];
- [reusedTile_ setVertexZ: [self vertexZForPos:pos]];
- reusedTile_.anchorPoint = CGPointZero;
- [reusedTile_ setOpacity:opacity_];
+ [self setupReusedTile:pos withGID:gid];
// get atlas index
NSUInteger indexForZ = [self atlasIndexForNewZ:z];
@@ -386,10 +399,7 @@ -(CCSprite*) updateTileForGID:(uint32_t)gid at:(CGPoint)pos
else
[reusedTile_ initWithBatchNode:self rectInPixels:rect];
- [reusedTile_ setPositionInPixels: [self positionAt:pos]];
- [reusedTile_ setVertexZ: [self vertexZForPos:pos]];
- reusedTile_.anchorPoint = CGPointZero;
- [reusedTile_ setOpacity:opacity_];
+ [self setupReusedTile:pos withGID:gid];
// get atlas index
NSUInteger indexForZ = [self atlasIndexForExistantZ:z];
@@ -416,10 +426,7 @@ -(CCSprite*) appendTileForGID:(uint32_t)gid at:(CGPoint)pos
else
[reusedTile_ initWithBatchNode:self rectInPixels:rect];
- [reusedTile_ setPositionInPixels: [self positionAt:pos]];
- [reusedTile_ setVertexZ: [self vertexZForPos:pos]];
- reusedTile_.anchorPoint = CGPointZero;
- [reusedTile_ setOpacity:opacity_];
+ [self setupReusedTile:pos withGID:gid];
// optimization:
// The difference between appendTileForGID and insertTileforGID is that append is faster, since
View
@@ -56,6 +56,11 @@ enum {
TMXPropertyTile
};
+// Bits on the far end of the 32-bit global tile ID (GID's) are used for tile flags
+#define kFlippedHorizontallyFlag 0x80000000
+#define kFlippedVerticallyFlag 0x40000000
+#define kFlippedMask ~(kFlippedHorizontallyFlag|kFlippedVerticallyFlag)
+
/* CCTMXLayerInfo contains the information about the layers like:
- Layer name
- Layer size
View
@@ -97,6 +97,7 @@ -(CGRect) rectForGID:(unsigned int)gid
CGRect rect;
rect.size = tileSize_;
+ gid &= kFlippedMask;
gid = gid - firstGid_;
int max_x = (imageSize_.width - margin_*2 + spacing_) / (tileSize_.width + spacing_);
View
@@ -139,6 +139,10 @@
{}
@end
+@interface TMXOrthoFlipTest : TileDemo
+{}
+@end
+
@interface TMXBug987 : TileDemo
{}
@end
View
@@ -37,6 +37,7 @@
@"TMXResizeTest",
@"TMXIsoMoveLayer",
@"TMXOrthoMoveLayer",
+ @"TMXOrthoFlipTest",
@"TMXBug987",
@"TMXBug787",
@@ -1363,6 +1364,36 @@ -(NSString *) subtitle
}
@end
+#pragma mark -
+#pragma mark TMXOrthoFlipTest
+
+@implementation TMXOrthoFlipTest
+-(id) init
+{
+ if( (self=[super init]) ) {
+ CCTMXTiledMap *map = [CCTMXTiledMap tiledMapWithTMXFile:@"TileMaps/ortho-rotation-test.tmx"];
+ [self addChild:map z:0 tag:kTagTileMap];
+
+ CGSize s = map.contentSize;
+ NSLog(@"ContentSize: %f, %f", s.width,s.height);
+
+ for( CCSpriteBatchNode* child in [map children] ) {
+ [[child texture] setAntiAliasTexParameters];
+ }
+
+ id action = [CCScaleBy actionWithDuration:2 scale:0.5f];
+ [map runAction:action];
+ }
+ return self;
+}
+
+-(NSString *) title
+{
+ return @"TMX tile flip test";
+}
+@end
+
+
#pragma mark -
#pragma mark TMXBug987

0 comments on commit 722c6dd

Please sign in to comment.