summary refs log tree commit diff stats
path: root/libs/cocos2d/CCDrawingPrimitives.m
diff options
context:
space:
mode:
Diffstat (limited to 'libs/cocos2d/CCDrawingPrimitives.m')
-rwxr-xr-xlibs/cocos2d/CCDrawingPrimitives.m272
1 files changed, 272 insertions, 0 deletions
diff --git a/libs/cocos2d/CCDrawingPrimitives.m b/libs/cocos2d/CCDrawingPrimitives.m new file mode 100755 index 0000000..f7df2b6 --- /dev/null +++ b/libs/cocos2d/CCDrawingPrimitives.m
@@ -0,0 +1,272 @@
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#import <math.h>
27#import <stdlib.h>
28#import <string.h>
29
30#import "CCDrawingPrimitives.h"
31#import "ccTypes.h"
32#import "ccMacros.h"
33#import "Platforms/CCGL.h"
34
35void ccDrawPoint( CGPoint point )
36{
37 ccVertex2F p = (ccVertex2F) {point.x * CC_CONTENT_SCALE_FACTOR(), point.y * CC_CONTENT_SCALE_FACTOR() };
38
39 // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
40 // Needed states: GL_VERTEX_ARRAY,
41 // Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY
42 glDisable(GL_TEXTURE_2D);
43 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
44 glDisableClientState(GL_COLOR_ARRAY);
45
46 glVertexPointer(2, GL_FLOAT, 0, &p);
47 glDrawArrays(GL_POINTS, 0, 1);
48
49 // restore default state
50 glEnableClientState(GL_COLOR_ARRAY);
51 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
52 glEnable(GL_TEXTURE_2D);
53}
54
55void ccDrawPoints( const CGPoint *points, NSUInteger numberOfPoints )
56{
57 // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
58 // Needed states: GL_VERTEX_ARRAY,
59 // Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY
60 glDisable(GL_TEXTURE_2D);
61 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
62 glDisableClientState(GL_COLOR_ARRAY);
63
64 ccVertex2F newPoints[numberOfPoints];
65
66 // iPhone and 32-bit machines optimization
67 if( sizeof(CGPoint) == sizeof(ccVertex2F) ) {
68
69 // points ?
70 if( CC_CONTENT_SCALE_FACTOR() != 1 ) {
71 for( NSUInteger i=0; i<numberOfPoints;i++)
72 newPoints[i] = (ccVertex2F){ points[i].x * CC_CONTENT_SCALE_FACTOR(), points[i].y * CC_CONTENT_SCALE_FACTOR() };
73
74 glVertexPointer(2, GL_FLOAT, 0, newPoints);
75
76 } else
77 glVertexPointer(2, GL_FLOAT, 0, points);
78
79 glDrawArrays(GL_POINTS, 0, (GLsizei) numberOfPoints);
80
81 } else {
82
83 // Mac on 64-bit
84 for( NSUInteger i=0; i<numberOfPoints;i++)
85 newPoints[i] = (ccVertex2F) { points[i].x, points[i].y };
86
87 glVertexPointer(2, GL_FLOAT, 0, newPoints);
88 glDrawArrays(GL_POINTS, 0, (GLsizei) numberOfPoints);
89
90 }
91
92
93 // restore default state
94 glEnableClientState(GL_COLOR_ARRAY);
95 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
96 glEnable(GL_TEXTURE_2D);
97}
98
99
100void ccDrawLine( CGPoint origin, CGPoint destination )
101{
102 ccVertex2F vertices[2] = {
103 {origin.x * CC_CONTENT_SCALE_FACTOR(), origin.y * CC_CONTENT_SCALE_FACTOR() },
104 {destination.x * CC_CONTENT_SCALE_FACTOR(), destination.y * CC_CONTENT_SCALE_FACTOR() }
105 };
106
107 // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
108 // Needed states: GL_VERTEX_ARRAY,
109 // Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY
110 glDisable(GL_TEXTURE_2D);
111 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
112 glDisableClientState(GL_COLOR_ARRAY);
113
114 glVertexPointer(2, GL_FLOAT, 0, vertices);
115 glDrawArrays(GL_LINES, 0, 2);
116
117 // restore default state
118 glEnableClientState(GL_COLOR_ARRAY);
119 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
120 glEnable(GL_TEXTURE_2D);
121}
122
123
124void ccDrawPoly( const CGPoint *poli, NSUInteger numberOfPoints, BOOL closePolygon )
125{
126 ccVertex2F newPoint[numberOfPoints];
127
128 // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
129 // Needed states: GL_VERTEX_ARRAY,
130 // Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY
131 glDisable(GL_TEXTURE_2D);
132 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
133 glDisableClientState(GL_COLOR_ARRAY);
134
135
136 // iPhone and 32-bit machines
137 if( sizeof(CGPoint) == sizeof(ccVertex2F) ) {
138
139 // convert to pixels ?
140 if( CC_CONTENT_SCALE_FACTOR() != 1 ) {
141 memcpy( newPoint, poli, numberOfPoints * sizeof(ccVertex2F) );
142 for( NSUInteger i=0; i<numberOfPoints;i++)
143 newPoint[i] = (ccVertex2F) { poli[i].x * CC_CONTENT_SCALE_FACTOR(), poli[i].y * CC_CONTENT_SCALE_FACTOR() };
144
145 glVertexPointer(2, GL_FLOAT, 0, newPoint);
146
147 } else
148 glVertexPointer(2, GL_FLOAT, 0, poli);
149
150
151 } else {
152 // 64-bit machines (Mac)
153
154 for( NSUInteger i=0; i<numberOfPoints;i++)
155 newPoint[i] = (ccVertex2F) { poli[i].x, poli[i].y };
156
157 glVertexPointer(2, GL_FLOAT, 0, newPoint );
158
159 }
160
161 if( closePolygon )
162 glDrawArrays(GL_LINE_LOOP, 0, (GLsizei) numberOfPoints);
163 else
164 glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) numberOfPoints);
165
166 // restore default state
167 glEnableClientState(GL_COLOR_ARRAY);
168 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
169 glEnable(GL_TEXTURE_2D);
170}
171
172void ccDrawCircle( CGPoint center, float r, float a, NSUInteger segs, BOOL drawLineToCenter)
173{
174 int additionalSegment = 1;
175 if (drawLineToCenter)
176 additionalSegment++;
177
178 const float coef = 2.0f * (float)M_PI/segs;
179
180 GLfloat *vertices = calloc( sizeof(GLfloat)*2*(segs+2), 1);
181 if( ! vertices )
182 return;
183
184 for(NSUInteger i=0;i<=segs;i++)
185 {
186 float rads = i*coef;
187 GLfloat j = r * cosf(rads + a) + center.x;
188 GLfloat k = r * sinf(rads + a) + center.y;
189
190 vertices[i*2] = j * CC_CONTENT_SCALE_FACTOR();
191 vertices[i*2+1] =k * CC_CONTENT_SCALE_FACTOR();
192 }
193 vertices[(segs+1)*2] = center.x * CC_CONTENT_SCALE_FACTOR();
194 vertices[(segs+1)*2+1] = center.y * CC_CONTENT_SCALE_FACTOR();
195
196 // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
197 // Needed states: GL_VERTEX_ARRAY,
198 // Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY
199 glDisable(GL_TEXTURE_2D);
200 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
201 glDisableClientState(GL_COLOR_ARRAY);
202
203 glVertexPointer(2, GL_FLOAT, 0, vertices);
204 glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segs+additionalSegment);
205
206 // restore default state
207 glEnableClientState(GL_COLOR_ARRAY);
208 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
209 glEnable(GL_TEXTURE_2D);
210
211 free( vertices );
212}
213
214void ccDrawQuadBezier(CGPoint origin, CGPoint control, CGPoint destination, NSUInteger segments)
215{
216 ccVertex2F vertices[segments + 1];
217
218 float t = 0.0f;
219 for(NSUInteger i = 0; i < segments; i++)
220 {
221 GLfloat x = powf(1 - t, 2) * origin.x + 2.0f * (1 - t) * t * control.x + t * t * destination.x;
222 GLfloat y = powf(1 - t, 2) * origin.y + 2.0f * (1 - t) * t * control.y + t * t * destination.y;
223 vertices[i] = (ccVertex2F) {x * CC_CONTENT_SCALE_FACTOR(), y * CC_CONTENT_SCALE_FACTOR() };
224 t += 1.0f / segments;
225 }
226 vertices[segments] = (ccVertex2F) {destination.x * CC_CONTENT_SCALE_FACTOR(), destination.y * CC_CONTENT_SCALE_FACTOR() };
227
228 // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
229 // Needed states: GL_VERTEX_ARRAY,
230 // Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY
231 glDisable(GL_TEXTURE_2D);
232 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
233 glDisableClientState(GL_COLOR_ARRAY);
234
235 glVertexPointer(2, GL_FLOAT, 0, vertices);
236 glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segments + 1);
237
238 // restore default state
239 glEnableClientState(GL_COLOR_ARRAY);
240 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
241 glEnable(GL_TEXTURE_2D);
242}
243
244void ccDrawCubicBezier(CGPoint origin, CGPoint control1, CGPoint control2, CGPoint destination, NSUInteger segments)
245{
246 ccVertex2F vertices[segments + 1];
247
248 float t = 0;
249 for(NSUInteger i = 0; i < segments; i++)
250 {
251 GLfloat x = powf(1 - t, 3) * origin.x + 3.0f * powf(1 - t, 2) * t * control1.x + 3.0f * (1 - t) * t * t * control2.x + t * t * t * destination.x;
252 GLfloat y = powf(1 - t, 3) * origin.y + 3.0f * powf(1 - t, 2) * t * control1.y + 3.0f * (1 - t) * t * t * control2.y + t * t * t * destination.y;
253 vertices[i] = (ccVertex2F) {x * CC_CONTENT_SCALE_FACTOR(), y * CC_CONTENT_SCALE_FACTOR() };
254 t += 1.0f / segments;
255 }
256 vertices[segments] = (ccVertex2F) {destination.x * CC_CONTENT_SCALE_FACTOR(), destination.y * CC_CONTENT_SCALE_FACTOR() };
257
258 // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
259 // Needed states: GL_VERTEX_ARRAY,
260 // Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY
261 glDisable(GL_TEXTURE_2D);
262 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
263 glDisableClientState(GL_COLOR_ARRAY);
264
265 glVertexPointer(2, GL_FLOAT, 0, vertices);
266 glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segments + 1);
267
268 // restore default state
269 glEnableClientState(GL_COLOR_ARRAY);
270 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
271 glEnable(GL_TEXTURE_2D);
272}