/libs/cocos2d/CCTMXTiledMap.m

http://github.com/kstenerud/ObjectAL-for-iPhone · Objective C · 201 lines · 120 code · 40 blank · 41 comment · 18 complexity · 9a5c69d4ef710f743fd82710b25bd8b7 MD5 · raw file

  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. #import "CCTMXTiledMap.h"
  30. #import "CCTMXXMLParser.h"
  31. #import "CCTMXLayer.h"
  32. #import "CCTMXObjectGroup.h"
  33. #import "CCSprite.h"
  34. #import "CCSpriteSheet.h"
  35. #import "CCTextureCache.h"
  36. #import "Support/CGPointExtension.h"
  37. #pragma mark -
  38. #pragma mark CCTMXTiledMap
  39. @interface CCTMXTiledMap (Private)
  40. -(id) parseLayer:(CCTMXLayerInfo*)layer map:(CCTMXMapInfo*)mapInfo;
  41. -(CCTMXTilesetInfo*) tilesetForLayer:(CCTMXLayerInfo*)layerInfo map:(CCTMXMapInfo*)mapInfo;
  42. @end
  43. @implementation CCTMXTiledMap
  44. @synthesize mapSize=mapSize_;
  45. @synthesize tileSize=tileSize_;
  46. @synthesize mapOrientation=mapOrientation_;
  47. @synthesize objectGroups=objectGroups_;
  48. @synthesize properties=properties_;
  49. +(id) tiledMapWithTMXFile:(NSString*)tmxFile
  50. {
  51. return [[[self alloc] initWithTMXFile:tmxFile] autorelease];
  52. }
  53. -(id) initWithTMXFile:(NSString*)tmxFile
  54. {
  55. NSAssert(tmxFile != nil, @"TMXTiledMap: tmx file should not bi nil");
  56. if ((self=[super init])) {
  57. [self setContentSize:CGSizeZero];
  58. CCTMXMapInfo *mapInfo = [CCTMXMapInfo formatWithTMXFile:tmxFile];
  59. NSAssert( [mapInfo.tilesets count] != 0, @"TMXTiledMap: Map not found. Please check the filename.");
  60. mapSize_ = mapInfo.mapSize;
  61. tileSize_ = mapInfo.tileSize;
  62. mapOrientation_ = mapInfo.orientation;
  63. objectGroups_ = [mapInfo.objectGroups retain];
  64. properties_ = [mapInfo.properties retain];
  65. tileProperties_ = [mapInfo.tileProperties retain];
  66. int idx=0;
  67. for( CCTMXLayerInfo *layerInfo in mapInfo.layers ) {
  68. if( layerInfo.visible ) {
  69. id child = [self parseLayer:layerInfo map:mapInfo];
  70. [self addChild:child z:idx tag:idx];
  71. // update content size with the max size
  72. CGSize childSize = [child contentSize];
  73. CGSize currentSize = [self contentSize];
  74. currentSize.width = MAX( currentSize.width, childSize.width );
  75. currentSize.height = MAX( currentSize.height, childSize.height );
  76. [self setContentSize:currentSize];
  77. idx++;
  78. }
  79. }
  80. }
  81. return self;
  82. }
  83. -(void) dealloc
  84. {
  85. [objectGroups_ release];
  86. [properties_ release];
  87. [tileProperties_ release];
  88. [super dealloc];
  89. }
  90. // private
  91. -(id) parseLayer:(CCTMXLayerInfo*)layerInfo map:(CCTMXMapInfo*)mapInfo
  92. {
  93. CCTMXTilesetInfo *tileset = [self tilesetForLayer:layerInfo map:mapInfo];
  94. CCTMXLayer *layer = [CCTMXLayer layerWithTilesetInfo:tileset layerInfo:layerInfo mapInfo:mapInfo];
  95. // tell the layerinfo to release the ownership of the tiles map.
  96. layerInfo.ownTiles = NO;
  97. [layer setupTiles];
  98. return layer;
  99. }
  100. -(CCTMXTilesetInfo*) tilesetForLayer:(CCTMXLayerInfo*)layerInfo map:(CCTMXMapInfo*)mapInfo
  101. {
  102. CFByteOrder o = CFByteOrderGetCurrent();
  103. CGSize size = layerInfo.layerSize;
  104. id iter = [mapInfo.tilesets reverseObjectEnumerator];
  105. for( CCTMXTilesetInfo* tileset in iter) {
  106. for( unsigned int y=0; y < size.height; y++ ) {
  107. for( unsigned int x=0; x < size.width; x++ ) {
  108. unsigned int pos = x + size.width * y;
  109. unsigned int gid = layerInfo.tiles[ pos ];
  110. // gid are stored in little endian.
  111. // if host is big endian, then swap
  112. if( o == CFByteOrderBigEndian )
  113. gid = CFSwapInt32( gid );
  114. // XXX: gid == 0 --> empty tile
  115. if( gid != 0 ) {
  116. // Optimization: quick return
  117. // if the layer is invalid (more than 1 tileset per layer) an assert will be thrown later
  118. if( gid >= tileset.firstGid )
  119. return tileset;
  120. }
  121. }
  122. }
  123. }
  124. // If all the tiles are 0, return empty tileset
  125. CCLOG(@"cocos2d: Warning: TMX Layer '%@' has no tiles", layerInfo.name);
  126. return nil;
  127. }
  128. // public
  129. -(CCTMXLayer*) layerNamed:(NSString *)layerName
  130. {
  131. for( CCTMXLayer *layer in children_ ) {
  132. if([layer isKindOfClass:[CCTMXLayer class]]){
  133. if( [layer.layerName isEqual:layerName] )
  134. return layer;
  135. }
  136. }
  137. // layer not found
  138. return nil;
  139. }
  140. -(CCTMXObjectGroup*) objectGroupNamed:(NSString *)groupName
  141. {
  142. for( CCTMXObjectGroup *objectGroup in objectGroups_ ) {
  143. if( [objectGroup.groupName isEqual:groupName] )
  144. return objectGroup;
  145. }
  146. // objectGroup not found
  147. return nil;
  148. }
  149. // XXX deprecated
  150. -(CCTMXObjectGroup*) groupNamed:(NSString *)groupName
  151. {
  152. return [self objectGroupNamed:groupName];
  153. }
  154. -(id) propertyNamed:(NSString *)propertyName
  155. {
  156. return [properties_ valueForKey:propertyName];
  157. }
  158. -(NSDictionary*)propertiesForGID:(unsigned int)GID{
  159. return [tileProperties_ objectForKey:[NSNumber numberWithInt:GID]];
  160. }
  161. @end