diff options
Diffstat (limited to 'libs/cocos2d/CCLabelAtlas.m')
-rwxr-xr-x | libs/cocos2d/CCLabelAtlas.m | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/libs/cocos2d/CCLabelAtlas.m b/libs/cocos2d/CCLabelAtlas.m new file mode 100755 index 0000000..386f8c3 --- /dev/null +++ b/libs/cocos2d/CCLabelAtlas.m | |||
@@ -0,0 +1,170 @@ | |||
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 | |||
28 | #import "ccConfig.h" | ||
29 | #import "ccMacros.h" | ||
30 | #import "CCDrawingPrimitives.h" | ||
31 | #import "CCLabelAtlas.h" | ||
32 | #import "Support/CGPointExtension.h" | ||
33 | |||
34 | |||
35 | |||
36 | @implementation CCLabelAtlas | ||
37 | |||
38 | #pragma mark CCLabelAtlas - Creation & Init | ||
39 | +(id) labelWithString:(NSString*)string charMapFile:(NSString*)charmapfile itemWidth:(NSUInteger)w itemHeight:(NSUInteger)h startCharMap:(unsigned char)c | ||
40 | { | ||
41 | return [[[self alloc] initWithString:string charMapFile:charmapfile itemWidth:w itemHeight:h startCharMap:c] autorelease]; | ||
42 | } | ||
43 | |||
44 | // XXX DEPRECATED. Remove it in 1.0.1 | ||
45 | +(id) labelAtlasWithString:(NSString*) string charMapFile: (NSString*) charmapfile itemWidth:(NSUInteger)w itemHeight:(NSUInteger)h startCharMap:(unsigned char)c | ||
46 | { | ||
47 | return [self labelWithString:string charMapFile:charmapfile itemWidth:w itemHeight:h startCharMap:c]; | ||
48 | } | ||
49 | |||
50 | |||
51 | -(id) initWithString:(NSString*) theString charMapFile: (NSString*) charmapfile itemWidth:(NSUInteger)w itemHeight:(NSUInteger)h startCharMap:(unsigned char)c | ||
52 | { | ||
53 | |||
54 | if ((self=[super initWithTileFile:charmapfile tileWidth:w tileHeight:h itemsToRender:[theString length] ]) ) { | ||
55 | |||
56 | mapStartChar_ = c; | ||
57 | [self setString: theString]; | ||
58 | } | ||
59 | |||
60 | return self; | ||
61 | } | ||
62 | |||
63 | -(void) dealloc | ||
64 | { | ||
65 | [string_ release]; | ||
66 | |||
67 | [super dealloc]; | ||
68 | } | ||
69 | |||
70 | #pragma mark CCLabelAtlas - Atlas generation | ||
71 | |||
72 | -(void) updateAtlasValues | ||
73 | { | ||
74 | NSUInteger n = [string_ length]; | ||
75 | |||
76 | ccV3F_C4B_T2F_Quad quad; | ||
77 | |||
78 | const unsigned char *s = (unsigned char*) [string_ UTF8String]; | ||
79 | |||
80 | CCTexture2D *texture = [textureAtlas_ texture]; | ||
81 | float textureWide = [texture pixelsWide]; | ||
82 | float textureHigh = [texture pixelsHigh]; | ||
83 | |||
84 | for( NSUInteger i=0; i<n; i++) { | ||
85 | unsigned char a = s[i] - mapStartChar_; | ||
86 | float row = (a % itemsPerRow_); | ||
87 | float col = (a / itemsPerRow_); | ||
88 | |||
89 | #if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL | ||
90 | // Issue #938. Don't use texStepX & texStepY | ||
91 | float left = (2*row*itemWidth_+1)/(2*textureWide); | ||
92 | float right = left+(itemWidth_*2-2)/(2*textureWide); | ||
93 | float top = (2*col*itemHeight_+1)/(2*textureHigh); | ||
94 | float bottom = top+(itemHeight_*2-2)/(2*textureHigh); | ||
95 | #else | ||
96 | float left = row*itemWidth_/textureWide; | ||
97 | float right = left+itemWidth_/textureWide; | ||
98 | float top = col*itemHeight_/textureHigh; | ||
99 | float bottom = top+itemHeight_/textureHigh; | ||
100 | #endif // ! CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL | ||
101 | |||
102 | quad.tl.texCoords.u = left; | ||
103 | quad.tl.texCoords.v = top; | ||
104 | quad.tr.texCoords.u = right; | ||
105 | quad.tr.texCoords.v = top; | ||
106 | quad.bl.texCoords.u = left; | ||
107 | quad.bl.texCoords.v = bottom; | ||
108 | quad.br.texCoords.u = right; | ||
109 | quad.br.texCoords.v = bottom; | ||
110 | |||
111 | quad.bl.vertices.x = (int) (i * itemWidth_); | ||
112 | quad.bl.vertices.y = 0; | ||
113 | quad.bl.vertices.z = 0.0f; | ||
114 | quad.br.vertices.x = (int)(i * itemWidth_ + itemWidth_); | ||
115 | quad.br.vertices.y = 0; | ||
116 | quad.br.vertices.z = 0.0f; | ||
117 | quad.tl.vertices.x = (int)(i * itemWidth_); | ||
118 | quad.tl.vertices.y = (int)(itemHeight_); | ||
119 | quad.tl.vertices.z = 0.0f; | ||
120 | quad.tr.vertices.x = (int)(i * itemWidth_ + itemWidth_); | ||
121 | quad.tr.vertices.y = (int)(itemHeight_); | ||
122 | quad.tr.vertices.z = 0.0f; | ||
123 | |||
124 | [textureAtlas_ updateQuad:&quad atIndex:i]; | ||
125 | } | ||
126 | } | ||
127 | |||
128 | #pragma mark CCLabelAtlas - CCLabelProtocol | ||
129 | |||
130 | - (void) setString:(NSString*) newString | ||
131 | { | ||
132 | NSUInteger len = [newString length]; | ||
133 | if( len > textureAtlas_.capacity ) | ||
134 | [textureAtlas_ resizeCapacity:len]; | ||
135 | |||
136 | [string_ release]; | ||
137 | string_ = [newString copy]; | ||
138 | [self updateAtlasValues]; | ||
139 | |||
140 | CGSize s; | ||
141 | s.width = len * itemWidth_; | ||
142 | s.height = itemHeight_; | ||
143 | [self setContentSizeInPixels:s]; | ||
144 | |||
145 | self.quadsToDraw = len; | ||
146 | } | ||
147 | |||
148 | -(NSString*) string | ||
149 | { | ||
150 | return string_; | ||
151 | } | ||
152 | |||
153 | #pragma mark CCLabelAtlas - DebugDraw | ||
154 | |||
155 | #if CC_LABELATLAS_DEBUG_DRAW | ||
156 | - (void) draw | ||
157 | { | ||
158 | [super draw]; | ||
159 | |||
160 | CGSize s = [self contentSize]; | ||
161 | CGPoint vertices[4]={ | ||
162 | ccp(0,0),ccp(s.width,0), | ||
163 | ccp(s.width,s.height),ccp(0,s.height), | ||
164 | }; | ||
165 | ccDrawPoly(vertices, 4, YES); | ||
166 | |||
167 | } | ||
168 | #endif // CC_LABELATLAS_DEBUG_DRAW | ||
169 | |||
170 | @end | ||