diff options
Diffstat (limited to 'libs/cocos2d/CCAtlasNode.m')
-rwxr-xr-x | libs/cocos2d/CCAtlasNode.m | 211 |
1 files changed, 211 insertions, 0 deletions
diff --git a/libs/cocos2d/CCAtlasNode.m b/libs/cocos2d/CCAtlasNode.m new file mode 100755 index 0000000..3f44633 --- /dev/null +++ b/libs/cocos2d/CCAtlasNode.m | |||
@@ -0,0 +1,211 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 2008-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 | |||
27 | #import "CCAtlasNode.h" | ||
28 | #import "ccMacros.h" | ||
29 | |||
30 | |||
31 | @interface CCAtlasNode () | ||
32 | -(void) calculateMaxItems; | ||
33 | -(void) updateBlendFunc; | ||
34 | -(void) updateOpacityModifyRGB; | ||
35 | @end | ||
36 | |||
37 | @implementation CCAtlasNode | ||
38 | |||
39 | @synthesize textureAtlas = textureAtlas_; | ||
40 | @synthesize blendFunc = blendFunc_; | ||
41 | @synthesize quadsToDraw = quadsToDraw_; | ||
42 | |||
43 | #pragma mark CCAtlasNode - Creation & Init | ||
44 | +(id) atlasWithTileFile:(NSString*)tile tileWidth:(NSUInteger)w tileHeight:(NSUInteger)h itemsToRender: (NSUInteger) c | ||
45 | { | ||
46 | return [[[self alloc] initWithTileFile:tile tileWidth:w tileHeight:h itemsToRender:c] autorelease]; | ||
47 | } | ||
48 | |||
49 | -(id) initWithTileFile:(NSString*)tile tileWidth:(NSUInteger)w tileHeight:(NSUInteger)h itemsToRender: (NSUInteger) c | ||
50 | { | ||
51 | if( (self=[super init]) ) { | ||
52 | |||
53 | itemWidth_ = w * CC_CONTENT_SCALE_FACTOR(); | ||
54 | itemHeight_ = h * CC_CONTENT_SCALE_FACTOR(); | ||
55 | |||
56 | opacity_ = 255; | ||
57 | color_ = colorUnmodified_ = ccWHITE; | ||
58 | opacityModifyRGB_ = YES; | ||
59 | |||
60 | blendFunc_.src = CC_BLEND_SRC; | ||
61 | blendFunc_.dst = CC_BLEND_DST; | ||
62 | |||
63 | // double retain to avoid the autorelease pool | ||
64 | // also, using: self.textureAtlas supports re-initialization without leaking | ||
65 | self.textureAtlas = [[CCTextureAtlas alloc] initWithFile:tile capacity:c]; | ||
66 | [textureAtlas_ release]; | ||
67 | |||
68 | if( ! textureAtlas_ ) { | ||
69 | CCLOG(@"cocos2d: Could not initialize CCAtlasNode. Invalid Texture"); | ||
70 | [self release]; | ||
71 | return nil; | ||
72 | } | ||
73 | |||
74 | [self updateBlendFunc]; | ||
75 | [self updateOpacityModifyRGB]; | ||
76 | |||
77 | [self calculateMaxItems]; | ||
78 | |||
79 | self.quadsToDraw = c; | ||
80 | |||
81 | } | ||
82 | return self; | ||
83 | } | ||
84 | |||
85 | -(void) dealloc | ||
86 | { | ||
87 | [textureAtlas_ release]; | ||
88 | |||
89 | [super dealloc]; | ||
90 | } | ||
91 | |||
92 | #pragma mark CCAtlasNode - Atlas generation | ||
93 | |||
94 | -(void) calculateMaxItems | ||
95 | { | ||
96 | CGSize s = [[textureAtlas_ texture] contentSizeInPixels]; | ||
97 | itemsPerColumn_ = s.height / itemHeight_; | ||
98 | itemsPerRow_ = s.width / itemWidth_; | ||
99 | } | ||
100 | |||
101 | -(void) updateAtlasValues | ||
102 | { | ||
103 | [NSException raise:@"CCAtlasNode:Abstract" format:@"updateAtlasValue not overriden"]; | ||
104 | } | ||
105 | |||
106 | #pragma mark CCAtlasNode - draw | ||
107 | - (void) draw | ||
108 | { | ||
109 | [super draw]; | ||
110 | |||
111 | // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY | ||
112 | // Needed states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY | ||
113 | // Unneeded states: GL_COLOR_ARRAY | ||
114 | glDisableClientState(GL_COLOR_ARRAY); | ||
115 | |||
116 | glColor4ub( color_.r, color_.g, color_.b, opacity_); | ||
117 | |||
118 | BOOL newBlend = blendFunc_.src != CC_BLEND_SRC || blendFunc_.dst != CC_BLEND_DST; | ||
119 | if( newBlend ) | ||
120 | glBlendFunc( blendFunc_.src, blendFunc_.dst ); | ||
121 | |||
122 | [textureAtlas_ drawNumberOfQuads:quadsToDraw_ fromIndex:0]; | ||
123 | |||
124 | if( newBlend ) | ||
125 | glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST); | ||
126 | |||
127 | // is this chepear than saving/restoring color state ? | ||
128 | // XXX: There is no need to restore the color to (255,255,255,255). Objects should use the color | ||
129 | // XXX: that they need | ||
130 | // glColor4ub( 255, 255, 255, 255); | ||
131 | |||
132 | // restore default GL state | ||
133 | glEnableClientState(GL_COLOR_ARRAY); | ||
134 | |||
135 | } | ||
136 | |||
137 | #pragma mark CCAtlasNode - RGBA protocol | ||
138 | |||
139 | - (ccColor3B) color | ||
140 | { | ||
141 | if(opacityModifyRGB_) | ||
142 | return colorUnmodified_; | ||
143 | |||
144 | return color_; | ||
145 | } | ||
146 | |||
147 | -(void) setColor:(ccColor3B)color3 | ||
148 | { | ||
149 | color_ = colorUnmodified_ = color3; | ||
150 | |||
151 | if( opacityModifyRGB_ ){ | ||
152 | color_.r = color3.r * opacity_/255; | ||
153 | color_.g = color3.g * opacity_/255; | ||
154 | color_.b = color3.b * opacity_/255; | ||
155 | } | ||
156 | } | ||
157 | |||
158 | -(GLubyte) opacity | ||
159 | { | ||
160 | return opacity_; | ||
161 | } | ||
162 | |||
163 | -(void) setOpacity:(GLubyte) anOpacity | ||
164 | { | ||
165 | opacity_ = anOpacity; | ||
166 | |||
167 | // special opacity for premultiplied textures | ||
168 | if( opacityModifyRGB_ ) | ||
169 | [self setColor: colorUnmodified_]; | ||
170 | } | ||
171 | |||
172 | -(void) setOpacityModifyRGB:(BOOL)modify | ||
173 | { | ||
174 | ccColor3B oldColor = self.color; | ||
175 | opacityModifyRGB_ = modify; | ||
176 | self.color = oldColor; | ||
177 | } | ||
178 | |||
179 | -(BOOL) doesOpacityModifyRGB | ||
180 | { | ||
181 | return opacityModifyRGB_; | ||
182 | } | ||
183 | |||
184 | -(void) updateOpacityModifyRGB | ||
185 | { | ||
186 | opacityModifyRGB_ = [textureAtlas_.texture hasPremultipliedAlpha]; | ||
187 | } | ||
188 | |||
189 | #pragma mark CCAtlasNode - CocosNodeTexture protocol | ||
190 | |||
191 | -(void) updateBlendFunc | ||
192 | { | ||
193 | if( ! [textureAtlas_.texture hasPremultipliedAlpha] ) { | ||
194 | blendFunc_.src = GL_SRC_ALPHA; | ||
195 | blendFunc_.dst = GL_ONE_MINUS_SRC_ALPHA; | ||
196 | } | ||
197 | } | ||
198 | |||
199 | -(void) setTexture:(CCTexture2D*)texture | ||
200 | { | ||
201 | textureAtlas_.texture = texture; | ||
202 | [self updateBlendFunc]; | ||
203 | [self updateOpacityModifyRGB]; | ||
204 | } | ||
205 | |||
206 | -(CCTexture2D*) texture | ||
207 | { | ||
208 | return textureAtlas_.texture; | ||
209 | } | ||
210 | |||
211 | @end | ||