Skip to content

Commit

Permalink
Flipped TMX Tile support, issue 1233
Browse files Browse the repository at this point in the history
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 722c6dd
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 13 deletions.
15 changes: 15 additions & 0 deletions Resources/TileMaps/ortho-rotation-test.tmx
Original file line number Diff line number Diff line change
@@ -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>
33 changes: 20 additions & 13 deletions cocos2d/CCTMXLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#import "CCTextureCache.h"
#import "Support/CGPointExtension.h"


#pragma mark -
#pragma mark CCSpriteBatchNode Extension

Expand Down Expand Up @@ -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];
Expand All @@ -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];
Expand Down Expand Up @@ -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];
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions cocos2d/CCTMXXMLParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cocos2d/CCTMXXMLParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
Expand Down
4 changes: 4 additions & 0 deletions tests/TileMapTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@
{}
@end

@interface TMXOrthoFlipTest : TileDemo
{}
@end

@interface TMXBug987 : TileDemo
{}
@end
Expand Down
31 changes: 31 additions & 0 deletions tests/TileMapTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
@"TMXResizeTest",
@"TMXIsoMoveLayer",
@"TMXOrthoMoveLayer",
@"TMXOrthoFlipTest",
@"TMXBug987",
@"TMXBug787",

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 722c6dd

Please sign in to comment.