summary refs log tree commit diff stats
path: root/libs/cocos2d/CCTMXTiledMap.m
diff options
context:
space:
mode:
Diffstat (limited to 'libs/cocos2d/CCTMXTiledMap.m')
-rwxr-xr-xlibs/cocos2d/CCTMXTiledMap.m201
1 files changed, 201 insertions, 0 deletions
diff --git a/libs/cocos2d/CCTMXTiledMap.m b/libs/cocos2d/CCTMXTiledMap.m new file mode 100755 index 0000000..7f86c6a --- /dev/null +++ b/libs/cocos2d/CCTMXTiledMap.m
@@ -0,0 +1,201 @@
1/*
2 * cocos2d for iPhone: http://www.cocos2d-iphone.org
3 *
4 * Copyright (c) 2009-2010 Ricardo Quesada
5 * Copyright (c) 2011 Zynga Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 *
25 *
26 * TMX Tiled Map support:
27 * http://www.mapeditor.org
28 *
29 */
30
31#import "CCTMXTiledMap.h"
32#import "CCTMXXMLParser.h"
33#import "CCTMXLayer.h"
34#import "CCTMXObjectGroup.h"
35#import "CCSprite.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 CCNode *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 CCTMXLayer *layer;
166 CCARRAY_FOREACH(children_, layer) {
167 if([layer isKindOfClass:[CCTMXLayer class]])
168 if([layer.layerName isEqual:layerName])
169 return layer;
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