/libs/cocos2d/CCTMXTiledMap.m
Objective C | 201 lines | 120 code | 40 blank | 41 comment | 18 complexity | 9a5c69d4ef710f743fd82710b25bd8b7 MD5 | raw file
Possible License(s): Apache-2.0
1/* 2 * cocos2d for iPhone: http://www.cocos2d-iphone.org 3 * 4 * Copyright (c) 2009-2010 Ricardo Quesada 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 * 24 * 25 * TMX Tiled Map support: 26 * http://www.mapeditor.org 27 * 28 */ 29 30#import "CCTMXTiledMap.h" 31#import "CCTMXXMLParser.h" 32#import "CCTMXLayer.h" 33#import "CCTMXObjectGroup.h" 34#import "CCSprite.h" 35#import "CCSpriteSheet.h" 36#import "CCTextureCache.h" 37#import "Support/CGPointExtension.h" 38 39 40#pragma mark - 41#pragma mark CCTMXTiledMap 42 43@interface CCTMXTiledMap (Private) 44-(id) parseLayer:(CCTMXLayerInfo*)layer map:(CCTMXMapInfo*)mapInfo; 45-(CCTMXTilesetInfo*) tilesetForLayer:(CCTMXLayerInfo*)layerInfo map:(CCTMXMapInfo*)mapInfo; 46@end 47 48@implementation CCTMXTiledMap 49@synthesize mapSize=mapSize_; 50@synthesize tileSize=tileSize_; 51@synthesize mapOrientation=mapOrientation_; 52@synthesize objectGroups=objectGroups_; 53@synthesize properties=properties_; 54 55+(id) tiledMapWithTMXFile:(NSString*)tmxFile 56{ 57 return [[[self alloc] initWithTMXFile:tmxFile] autorelease]; 58} 59 60-(id) initWithTMXFile:(NSString*)tmxFile 61{ 62 NSAssert(tmxFile != nil, @"TMXTiledMap: tmx file should not bi nil"); 63 64 if ((self=[super init])) { 65 66 [self setContentSize:CGSizeZero]; 67 68 CCTMXMapInfo *mapInfo = [CCTMXMapInfo formatWithTMXFile:tmxFile]; 69 70 NSAssert( [mapInfo.tilesets count] != 0, @"TMXTiledMap: Map not found. Please check the filename."); 71 72 mapSize_ = mapInfo.mapSize; 73 tileSize_ = mapInfo.tileSize; 74 mapOrientation_ = mapInfo.orientation; 75 objectGroups_ = [mapInfo.objectGroups retain]; 76 properties_ = [mapInfo.properties retain]; 77 tileProperties_ = [mapInfo.tileProperties retain]; 78 79 int idx=0; 80 81 for( CCTMXLayerInfo *layerInfo in mapInfo.layers ) { 82 83 if( layerInfo.visible ) { 84 id child = [self parseLayer:layerInfo map:mapInfo]; 85 [self addChild:child z:idx tag:idx]; 86 87 // update content size with the max size 88 CGSize childSize = [child contentSize]; 89 CGSize currentSize = [self contentSize]; 90 currentSize.width = MAX( currentSize.width, childSize.width ); 91 currentSize.height = MAX( currentSize.height, childSize.height ); 92 [self setContentSize:currentSize]; 93 94 idx++; 95 } 96 } 97 } 98 99 return self; 100} 101 102-(void) dealloc 103{ 104 [objectGroups_ release]; 105 [properties_ release]; 106 [tileProperties_ release]; 107 [super dealloc]; 108} 109 110// private 111-(id) parseLayer:(CCTMXLayerInfo*)layerInfo map:(CCTMXMapInfo*)mapInfo 112{ 113 CCTMXTilesetInfo *tileset = [self tilesetForLayer:layerInfo map:mapInfo]; 114 CCTMXLayer *layer = [CCTMXLayer layerWithTilesetInfo:tileset layerInfo:layerInfo mapInfo:mapInfo]; 115 116 // tell the layerinfo to release the ownership of the tiles map. 117 layerInfo.ownTiles = NO; 118 119 [layer setupTiles]; 120 121 return layer; 122} 123 124-(CCTMXTilesetInfo*) tilesetForLayer:(CCTMXLayerInfo*)layerInfo map:(CCTMXMapInfo*)mapInfo 125{ 126 CFByteOrder o = CFByteOrderGetCurrent(); 127 128 CGSize size = layerInfo.layerSize; 129 130 id iter = [mapInfo.tilesets reverseObjectEnumerator]; 131 for( CCTMXTilesetInfo* tileset in iter) { 132 for( unsigned int y=0; y < size.height; y++ ) { 133 for( unsigned int x=0; x < size.width; x++ ) { 134 135 unsigned int pos = x + size.width * y; 136 unsigned int gid = layerInfo.tiles[ pos ]; 137 138 // gid are stored in little endian. 139 // if host is big endian, then swap 140 if( o == CFByteOrderBigEndian ) 141 gid = CFSwapInt32( gid ); 142 143 // XXX: gid == 0 --> empty tile 144 if( gid != 0 ) { 145 146 // Optimization: quick return 147 // if the layer is invalid (more than 1 tileset per layer) an assert will be thrown later 148 if( gid >= tileset.firstGid ) 149 return tileset; 150 } 151 } 152 } 153 } 154 155 // If all the tiles are 0, return empty tileset 156 CCLOG(@"cocos2d: Warning: TMX Layer '%@' has no tiles", layerInfo.name); 157 return nil; 158} 159 160 161// public 162 163-(CCTMXLayer*) layerNamed:(NSString *)layerName 164{ 165 for( CCTMXLayer *layer in children_ ) { 166 if([layer isKindOfClass:[CCTMXLayer class]]){ 167 if( [layer.layerName isEqual:layerName] ) 168 return layer; 169 } 170 } 171 172 // layer not found 173 return nil; 174} 175 176-(CCTMXObjectGroup*) objectGroupNamed:(NSString *)groupName 177{ 178 for( CCTMXObjectGroup *objectGroup in objectGroups_ ) { 179 if( [objectGroup.groupName isEqual:groupName] ) 180 return objectGroup; 181 } 182 183 // objectGroup not found 184 return nil; 185} 186 187// XXX deprecated 188-(CCTMXObjectGroup*) groupNamed:(NSString *)groupName 189{ 190 return [self objectGroupNamed:groupName]; 191} 192 193-(id) propertyNamed:(NSString *)propertyName 194{ 195 return [properties_ valueForKey:propertyName]; 196} 197-(NSDictionary*)propertiesForGID:(unsigned int)GID{ 198 return [tileProperties_ objectForKey:[NSNumber numberWithInt:GID]]; 199} 200@end 201