diff options
Diffstat (limited to 'libs/cocos2d/ccMacros.h')
-rwxr-xr-x | libs/cocos2d/ccMacros.h | 253 |
1 files changed, 253 insertions, 0 deletions
diff --git a/libs/cocos2d/ccMacros.h b/libs/cocos2d/ccMacros.h new file mode 100755 index 0000000..4e08725 --- /dev/null +++ b/libs/cocos2d/ccMacros.h | |||
@@ -0,0 +1,253 @@ | |||
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 <math.h> | ||
28 | #import "ccConfig.h" | ||
29 | |||
30 | #import <Foundation/Foundation.h> | ||
31 | #import <Availability.h> | ||
32 | |||
33 | /** | ||
34 | @file | ||
35 | cocos2d helper macros | ||
36 | */ | ||
37 | |||
38 | /* | ||
39 | * if COCOS2D_DEBUG is not defined, or if it is 0 then | ||
40 | * all CCLOGXXX macros will be disabled | ||
41 | * | ||
42 | * if COCOS2D_DEBUG==1 then: | ||
43 | * CCLOG() will be enabled | ||
44 | * CCLOGERROR() will be enabled | ||
45 | * CCLOGINFO() will be disabled | ||
46 | * | ||
47 | * if COCOS2D_DEBUG==2 or higher then: | ||
48 | * CCLOG() will be enabled | ||
49 | * CCLOGERROR() will be enabled | ||
50 | * CCLOGINFO() will be enabled | ||
51 | */ | ||
52 | #if !defined(COCOS2D_DEBUG) || COCOS2D_DEBUG == 0 | ||
53 | #define CCLOG(...) do {} while (0) | ||
54 | #define CCLOGINFO(...) do {} while (0) | ||
55 | #define CCLOGERROR(...) do {} while (0) | ||
56 | |||
57 | #elif COCOS2D_DEBUG == 1 | ||
58 | #define CCLOG(...) NSLog(__VA_ARGS__) | ||
59 | #define CCLOGERROR(...) NSLog(__VA_ARGS__) | ||
60 | #define CCLOGINFO(...) do {} while (0) | ||
61 | |||
62 | #elif COCOS2D_DEBUG > 1 | ||
63 | #define CCLOG(...) NSLog(__VA_ARGS__) | ||
64 | #define CCLOGERROR(...) NSLog(__VA_ARGS__) | ||
65 | #define CCLOGINFO(...) NSLog(__VA_ARGS__) | ||
66 | #endif // COCOS2D_DEBUG | ||
67 | |||
68 | /** @def CC_SWAP | ||
69 | simple macro that swaps 2 variables | ||
70 | */ | ||
71 | #define CC_SWAP( x, y ) \ | ||
72 | ({ __typeof__(x) temp = (x); \ | ||
73 | x = y; y = temp; \ | ||
74 | }) | ||
75 | |||
76 | |||
77 | /** @def CCRANDOM_MINUS1_1 | ||
78 | returns a random float between -1 and 1 | ||
79 | */ | ||
80 | #define CCRANDOM_MINUS1_1() ((random() / (float)0x3fffffff )-1.0f) | ||
81 | |||
82 | /** @def CCRANDOM_0_1 | ||
83 | returns a random float between 0 and 1 | ||
84 | */ | ||
85 | #define CCRANDOM_0_1() ((random() / (float)0x7fffffff )) | ||
86 | |||
87 | /** @def CC_DEGREES_TO_RADIANS | ||
88 | converts degrees to radians | ||
89 | */ | ||
90 | #define CC_DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) * 0.01745329252f) // PI / 180 | ||
91 | |||
92 | /** @def CC_RADIANS_TO_DEGREES | ||
93 | converts radians to degrees | ||
94 | */ | ||
95 | #define CC_RADIANS_TO_DEGREES(__ANGLE__) ((__ANGLE__) * 57.29577951f) // PI * 180 | ||
96 | |||
97 | /** @def CC_BLEND_SRC | ||
98 | default gl blend src function. Compatible with premultiplied alpha images. | ||
99 | */ | ||
100 | #if CC_OPTIMIZE_BLEND_FUNC_FOR_PREMULTIPLIED_ALPHA | ||
101 | #define CC_BLEND_SRC GL_ONE | ||
102 | #define CC_BLEND_DST GL_ONE_MINUS_SRC_ALPHA | ||
103 | #else | ||
104 | #define CC_BLEND_SRC GL_SRC_ALPHA | ||
105 | #define CC_BLEND_DST GL_ONE_MINUS_SRC_ALPHA | ||
106 | #endif // ! CC_OPTIMIZE_BLEND_FUNC_FOR_PREMULTIPLIED_ALPHA | ||
107 | |||
108 | /** @def CC_ENABLE_DEFAULT_GL_STATES | ||
109 | GL states that are enabled: | ||
110 | - GL_TEXTURE_2D | ||
111 | - GL_VERTEX_ARRAY | ||
112 | - GL_TEXTURE_COORD_ARRAY | ||
113 | - GL_COLOR_ARRAY | ||
114 | */ | ||
115 | #define CC_ENABLE_DEFAULT_GL_STATES() { \ | ||
116 | glEnableClientState(GL_VERTEX_ARRAY); \ | ||
117 | glEnableClientState(GL_COLOR_ARRAY); \ | ||
118 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); \ | ||
119 | glEnable(GL_TEXTURE_2D); \ | ||
120 | } | ||
121 | |||
122 | /** @def CC_DISABLE_DEFAULT_GL_STATES | ||
123 | Disable default GL states: | ||
124 | - GL_TEXTURE_2D | ||
125 | - GL_VERTEX_ARRAY | ||
126 | - GL_TEXTURE_COORD_ARRAY | ||
127 | - GL_COLOR_ARRAY | ||
128 | */ | ||
129 | #define CC_DISABLE_DEFAULT_GL_STATES() { \ | ||
130 | glDisable(GL_TEXTURE_2D); \ | ||
131 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); \ | ||
132 | glDisableClientState(GL_COLOR_ARRAY); \ | ||
133 | glDisableClientState(GL_VERTEX_ARRAY); \ | ||
134 | } | ||
135 | |||
136 | /** @def CC_DIRECTOR_INIT | ||
137 | - Initializes an EAGLView with 0-bit depth format, and RGB565 render buffer. | ||
138 | - The EAGLView view will have multiple touches disabled. | ||
139 | - It will create a UIWindow and it will assign it the 'window' variable. 'window' must be declared before calling this marcro. | ||
140 | - It will parent the EAGLView to the created window | ||
141 | - If the firmware >= 3.1 it will create a Display Link Director. Else it will create an NSTimer director. | ||
142 | - It will try to run at 60 FPS. | ||
143 | - The FPS won't be displayed. | ||
144 | - The orientation will be portrait. | ||
145 | - It will connect the director with the EAGLView. | ||
146 | |||
147 | IMPORTANT: If you want to use another type of render buffer (eg: RGBA8) | ||
148 | or if you want to use a 16-bit or 24-bit depth buffer, you should NOT | ||
149 | use this macro. Instead, you should create the EAGLView manually. | ||
150 | |||
151 | @since v0.99.4 | ||
152 | */ | ||
153 | |||
154 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
155 | |||
156 | #define CC_DIRECTOR_INIT() \ | ||
157 | do { \ | ||
158 | window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; \ | ||
159 | if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] ) \ | ||
160 | [CCDirector setDirectorType:kCCDirectorTypeNSTimer]; \ | ||
161 | CCDirector *__director = [CCDirector sharedDirector]; \ | ||
162 | [__director setDeviceOrientation:kCCDeviceOrientationPortrait]; \ | ||
163 | [__director setDisplayFPS:NO]; \ | ||
164 | [__director setAnimationInterval:1.0/60]; \ | ||
165 | EAGLView *__glView = [EAGLView viewWithFrame:[window bounds] \ | ||
166 | pixelFormat:kEAGLColorFormatRGB565 \ | ||
167 | depthFormat:0 /* GL_DEPTH_COMPONENT24_OES */ \ | ||
168 | preserveBackbuffer:NO \ | ||
169 | sharegroup:nil \ | ||
170 | multiSampling:NO \ | ||
171 | numberOfSamples:0 \ | ||
172 | ]; \ | ||
173 | [__director setOpenGLView:__glView]; \ | ||
174 | [window addSubview:__glView]; \ | ||
175 | [window makeKeyAndVisible]; \ | ||
176 | } while(0) | ||
177 | |||
178 | |||
179 | #elif __MAC_OS_X_VERSION_MAX_ALLOWED | ||
180 | |||
181 | #import "Platforms/Mac/MacWindow.h" | ||
182 | |||
183 | #define CC_DIRECTOR_INIT(__WINSIZE__) \ | ||
184 | do { \ | ||
185 | NSRect frameRect = NSMakeRect(0, 0, (__WINSIZE__).width, (__WINSIZE__).height); \ | ||
186 | self.window = [[MacWindow alloc] initWithFrame:frameRect fullscreen:NO]; \ | ||
187 | self.glView = [[MacGLView alloc] initWithFrame:frameRect shareContext:nil]; \ | ||
188 | [self.window setContentView:self.glView]; \ | ||
189 | CCDirector *__director = [CCDirector sharedDirector]; \ | ||
190 | [__director setDisplayFPS:NO]; \ | ||
191 | [__director setOpenGLView:self.glView]; \ | ||
192 | [(CCDirectorMac*)__director setOriginalWinSize:__WINSIZE__]; \ | ||
193 | [self.window makeMainWindow]; \ | ||
194 | [self.window makeKeyAndOrderFront:self]; \ | ||
195 | } while(0) | ||
196 | |||
197 | #endif | ||
198 | |||
199 | |||
200 | /** @def CC_DIRECTOR_END | ||
201 | Stops and removes the director from memory. | ||
202 | Removes the EAGLView from its parent | ||
203 | |||
204 | @since v0.99.4 | ||
205 | */ | ||
206 | #define CC_DIRECTOR_END() \ | ||
207 | do { \ | ||
208 | CCDirector *__director = [CCDirector sharedDirector]; \ | ||
209 | CC_GLVIEW *__view = [__director openGLView]; \ | ||
210 | [__view removeFromSuperview]; \ | ||
211 | [__director end]; \ | ||
212 | } while(0) | ||
213 | |||
214 | |||
215 | #if CC_IS_RETINA_DISPLAY_SUPPORTED | ||
216 | |||
217 | /****************************/ | ||
218 | /** RETINA DISPLAY ENABLED **/ | ||
219 | /****************************/ | ||
220 | |||
221 | /** @def CC_CONTENT_SCALE_FACTOR | ||
222 | On Mac it returns 1; | ||
223 | On iPhone it returns 2 if RetinaDisplay is On. Otherwise it returns 1 | ||
224 | */ | ||
225 | #import "Platforms/iOS/CCDirectorIOS.h" | ||
226 | #define CC_CONTENT_SCALE_FACTOR() __ccContentScaleFactor | ||
227 | |||
228 | |||
229 | /** @def CC_RECT_PIXELS_TO_POINTS | ||
230 | Converts a rect in pixels to points | ||
231 | */ | ||
232 | #define CC_RECT_PIXELS_TO_POINTS(__pixels__) \ | ||
233 | CGRectMake( (__pixels__).origin.x / CC_CONTENT_SCALE_FACTOR(), (__pixels__).origin.y / CC_CONTENT_SCALE_FACTOR(), \ | ||
234 | (__pixels__).size.width / CC_CONTENT_SCALE_FACTOR(), (__pixels__).size.height / CC_CONTENT_SCALE_FACTOR() ) | ||
235 | |||
236 | /** @def CC_RECT_POINTS_TO_PIXELS | ||
237 | Converts a rect in points to pixels | ||
238 | */ | ||
239 | #define CC_RECT_POINTS_TO_PIXELS(__points__) \ | ||
240 | CGRectMake( (__points__).origin.x * CC_CONTENT_SCALE_FACTOR(), (__points__).origin.y * CC_CONTENT_SCALE_FACTOR(), \ | ||
241 | (__points__).size.width * CC_CONTENT_SCALE_FACTOR(), (__points__).size.height * CC_CONTENT_SCALE_FACTOR() ) | ||
242 | |||
243 | #else // retina disabled | ||
244 | |||
245 | /*****************************/ | ||
246 | /** RETINA DISPLAY DISABLED **/ | ||
247 | /*****************************/ | ||
248 | |||
249 | #define CC_CONTENT_SCALE_FACTOR() 1 | ||
250 | #define CC_RECT_PIXELS_TO_POINTS(__pixels__) __pixels__ | ||
251 | #define CC_RECT_POINTS_TO_PIXELS(__points__) __points__ | ||
252 | |||
253 | #endif // CC_IS_RETINA_DISPLAY_SUPPORTED | ||