diff options
Diffstat (limited to 'libs/cocos2d/Platforms')
24 files changed, 4896 insertions, 0 deletions
diff --git a/libs/cocos2d/Platforms/CCGL.h b/libs/cocos2d/Platforms/CCGL.h new file mode 100755 index 0000000..0725f89 --- /dev/null +++ b/libs/cocos2d/Platforms/CCGL.h | |||
@@ -0,0 +1,83 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 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 | // Common layer for OpenGL stuff | ||
28 | // | ||
29 | |||
30 | #import <Availability.h> | ||
31 | |||
32 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
33 | #import <OpenGLES/ES1/gl.h> | ||
34 | #import <OpenGLES/ES1/glext.h> | ||
35 | #import <OpenGLES/EAGL.h> | ||
36 | #import "iOS/glu.h" | ||
37 | #import "iOS/EAGLView.h" | ||
38 | |||
39 | #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) | ||
40 | #import <OpenGL/gl.h> | ||
41 | #import <OpenGL/glu.h> | ||
42 | #import <Cocoa/Cocoa.h> // needed for NSOpenGLView | ||
43 | #import "Mac/MacGLView.h" | ||
44 | #endif | ||
45 | |||
46 | |||
47 | // iOS | ||
48 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
49 | #define CC_GLVIEW EAGLView | ||
50 | #define ccglOrtho glOrthof | ||
51 | #define ccglClearDepth glClearDepthf | ||
52 | #define ccglGenerateMipmap glGenerateMipmapOES | ||
53 | #define ccglGenFramebuffers glGenFramebuffersOES | ||
54 | #define ccglBindFramebuffer glBindFramebufferOES | ||
55 | #define ccglFramebufferTexture2D glFramebufferTexture2DOES | ||
56 | #define ccglDeleteFramebuffers glDeleteFramebuffersOES | ||
57 | #define ccglCheckFramebufferStatus glCheckFramebufferStatusOES | ||
58 | #define ccglTranslate glTranslatef | ||
59 | |||
60 | #define CC_GL_FRAMEBUFFER GL_FRAMEBUFFER_OES | ||
61 | #define CC_GL_FRAMEBUFFER_BINDING GL_FRAMEBUFFER_BINDING_OES | ||
62 | #define CC_GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0_OES | ||
63 | #define CC_GL_FRAMEBUFFER_COMPLETE GL_FRAMEBUFFER_COMPLETE_OES | ||
64 | |||
65 | // Mac | ||
66 | #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) | ||
67 | #define CC_GLVIEW MacGLView | ||
68 | #define ccglOrtho glOrtho | ||
69 | #define ccglClearDepth glClearDepth | ||
70 | #define ccglGenerateMipmap glGenerateMipmap | ||
71 | #define ccglGenFramebuffers glGenFramebuffers | ||
72 | #define ccglBindFramebuffer glBindFramebuffer | ||
73 | #define ccglFramebufferTexture2D glFramebufferTexture2D | ||
74 | #define ccglDeleteFramebuffers glDeleteFramebuffers | ||
75 | #define ccglCheckFramebufferStatus glCheckFramebufferStatus | ||
76 | #define ccglTranslate glTranslated | ||
77 | |||
78 | #define CC_GL_FRAMEBUFFER GL_FRAMEBUFFER | ||
79 | #define CC_GL_FRAMEBUFFER_BINDING GL_FRAMEBUFFER_BINDING | ||
80 | #define CC_GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0 | ||
81 | #define CC_GL_FRAMEBUFFER_COMPLETE GL_FRAMEBUFFER_COMPLETE | ||
82 | |||
83 | #endif | ||
diff --git a/libs/cocos2d/Platforms/CCNS.h b/libs/cocos2d/Platforms/CCNS.h new file mode 100755 index 0000000..c595a18 --- /dev/null +++ b/libs/cocos2d/Platforms/CCNS.h | |||
@@ -0,0 +1,78 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 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 | // Common layer for NS (Next-Step) stuff | ||
28 | // | ||
29 | |||
30 | #import <Availability.h> | ||
31 | |||
32 | #import <Foundation/Foundation.h> // for NSObject | ||
33 | |||
34 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
35 | |||
36 | #define CCRectFromString(__r__) CGRectFromString(__r__) | ||
37 | #define CCPointFromString(__p__) CGPointFromString(__p__) | ||
38 | #define CCSizeFromString(__s__) CGSizeFromString(__s__) | ||
39 | #define CCNSSizeToCGSize | ||
40 | #define CCNSRectToCGRect | ||
41 | #define CCNSPointToCGPoint | ||
42 | #define CCTextAlignment UITextAlignment | ||
43 | #define CCTextAlignmentCenter UITextAlignmentCenter | ||
44 | #define CCTextAlignmentLeft UITextAlignmentLeft | ||
45 | #define CCTextAlignmentRight UITextAlignmentRight | ||
46 | #define CCLineBreakMode UILineBreakMode | ||
47 | #define CCLineBreakModeWordWrap UILineBreakModeWordWrap | ||
48 | #define CCLineBreakModeCharacterWrap UILineBreakModeCharacterWrap | ||
49 | #define CCLineBreakModeClip UILineBreakModeClip | ||
50 | #define CCLineBreakModeHeadTruncation UILineBreakModeHeadTruncation | ||
51 | #define CCLineBreakModeTailTruncation UILineBreakModeTailTruncation | ||
52 | #define CCLineBreakModeMiddleTruncation UILineBreakModeMiddleTruncation | ||
53 | |||
54 | |||
55 | |||
56 | #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) | ||
57 | |||
58 | #define CCRectFromString(__r__) NSRectToCGRect( NSRectFromString(__r__) ) | ||
59 | #define CCPointFromString(__p__) NSPointToCGPoint( NSPointFromString(__p__) ) | ||
60 | #define CCSizeFromString(__s__) NSSizeToCGSize( NSSizeFromString(__s__) ) | ||
61 | #define CCNSSizeToCGSize NSSizeToCGSize | ||
62 | #define CCNSRectToCGRect NSRectToCGRect | ||
63 | #define CCNSPointToCGPoint NSPointToCGPoint | ||
64 | #define CCTextAlignment NSTextAlignment | ||
65 | #define CCTextAlignmentCenter NSCenterTextAlignment | ||
66 | #define CCTextAlignmentLeft NSLeftTextAlignment | ||
67 | #define CCTextAlignmentRight NSRightTextAlignment | ||
68 | #define CCLineBreakMode NSLineBreakMode | ||
69 | #define CCLineBreakModeWordWrap NSLineBreakByWordWrapping | ||
70 | #define CCLineBreakModeClip -1 | ||
71 | #define CCLineBreakModeHeadTruncation -1 | ||
72 | #define CCLineBreakModeTailTruncation -1 | ||
73 | #define CCLineBreakModeMiddleTruncation -1 | ||
74 | |||
75 | |||
76 | #endif | ||
77 | |||
78 | |||
diff --git a/libs/cocos2d/Platforms/Mac/CCDirectorMac.h b/libs/cocos2d/Platforms/Mac/CCDirectorMac.h new file mode 100755 index 0000000..0d623b4 --- /dev/null +++ b/libs/cocos2d/Platforms/Mac/CCDirectorMac.h | |||
@@ -0,0 +1,103 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 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 | // Only compile this code on Mac. These files should not be included on your iOS project. | ||
28 | // But in case they are included, it won't be compiled. | ||
29 | #import <Availability.h> | ||
30 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
31 | #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) | ||
32 | |||
33 | #import <QuartzCore/CVDisplayLink.h> | ||
34 | #import "../../CCDirector.h" | ||
35 | |||
36 | enum { | ||
37 | /// If the window is resized, it won't be autoscaled | ||
38 | kCCDirectorResize_NoScale, | ||
39 | /// If the window is resized, it will be autoscaled (default behavior) | ||
40 | kCCDirectorResize_AutoScale, | ||
41 | }; | ||
42 | |||
43 | @interface CCDirector (MacExtension) | ||
44 | /** converts an NSEvent to GL coordinates */ | ||
45 | -(CGPoint) convertEventToGL:(NSEvent*)event; | ||
46 | @end | ||
47 | |||
48 | /** Base class of Mac directors | ||
49 | @since v0.99.5 | ||
50 | */ | ||
51 | @interface CCDirectorMac : CCDirector | ||
52 | { | ||
53 | BOOL isFullScreen_; | ||
54 | int resizeMode_; | ||
55 | CGPoint winOffset_; | ||
56 | CGSize originalWinSize_; | ||
57 | |||
58 | NSWindow *fullScreenWindow_; | ||
59 | |||
60 | // cache | ||
61 | NSWindow *windowGLView_; | ||
62 | NSView *superViewGLView_; | ||
63 | NSRect originalWinRect_; // Original size and position | ||
64 | } | ||
65 | |||
66 | // whether or not the view is in fullscreen mode | ||
67 | @property (nonatomic, readonly) BOOL isFullScreen; | ||
68 | |||
69 | // resize mode: with or without scaling | ||
70 | @property (nonatomic, readwrite) int resizeMode; | ||
71 | |||
72 | @property (nonatomic, readwrite) CGSize originalWinSize; | ||
73 | |||
74 | /** Sets the view in fullscreen or window mode */ | ||
75 | - (void) setFullScreen:(BOOL)fullscreen; | ||
76 | |||
77 | /** Converts window size coordiantes to logical coordinates. | ||
78 | Useful only if resizeMode is kCCDirectorResize_Scale. | ||
79 | If resizeMode is kCCDirectorResize_NoScale, then no conversion will be done. | ||
80 | */ | ||
81 | - (CGPoint) convertToLogicalCoordinates:(CGPoint)coordinates; | ||
82 | @end | ||
83 | |||
84 | |||
85 | /** DisplayLinkDirector is a Director that synchronizes timers with the refresh rate of the display. | ||
86 | * | ||
87 | * Features and Limitations: | ||
88 | * - Only available on 3.1+ | ||
89 | * - Scheduled timers & drawing are synchronizes with the refresh rate of the display | ||
90 | * - Only supports animation intervals of 1/60 1/30 & 1/15 | ||
91 | * | ||
92 | * It is the recommended Director if the SDK is 3.1 or newer | ||
93 | * | ||
94 | * @since v0.8.2 | ||
95 | */ | ||
96 | @interface CCDirectorDisplayLink : CCDirectorMac | ||
97 | { | ||
98 | CVDisplayLinkRef displayLink; | ||
99 | } | ||
100 | @end | ||
101 | |||
102 | #endif // __MAC_OS_X_VERSION_MAX_ALLOWED | ||
103 | |||
diff --git a/libs/cocos2d/Platforms/Mac/CCDirectorMac.m b/libs/cocos2d/Platforms/Mac/CCDirectorMac.m new file mode 100755 index 0000000..477081e --- /dev/null +++ b/libs/cocos2d/Platforms/Mac/CCDirectorMac.m | |||
@@ -0,0 +1,479 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 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 | // Only compile this code on Mac. These files should not be included on your iOS project. | ||
27 | // But in case they are included, it won't be compiled. | ||
28 | #import <Availability.h> | ||
29 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
30 | #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) | ||
31 | |||
32 | #import <sys/time.h> | ||
33 | |||
34 | #import "CCDirectorMac.h" | ||
35 | #import "CCEventDispatcher.h" | ||
36 | #import "MacGLView.h" | ||
37 | |||
38 | #import "../../CCNode.h" | ||
39 | #import "../../CCScheduler.h" | ||
40 | #import "../../ccMacros.h" | ||
41 | |||
42 | #pragma mark - | ||
43 | #pragma mark Director Mac extensions | ||
44 | |||
45 | |||
46 | @interface CCDirector () | ||
47 | -(void) setNextScene; | ||
48 | -(void) showFPS; | ||
49 | -(void) calculateDeltaTime; | ||
50 | @end | ||
51 | |||
52 | @implementation CCDirector (MacExtension) | ||
53 | -(CGPoint) convertEventToGL:(NSEvent*)event | ||
54 | { | ||
55 | NSPoint point = [openGLView_ convertPoint:[event locationInWindow] fromView:nil]; | ||
56 | CGPoint p = NSPointToCGPoint(point); | ||
57 | |||
58 | return [(CCDirectorMac*)self convertToLogicalCoordinates:p]; | ||
59 | } | ||
60 | |||
61 | @end | ||
62 | |||
63 | #pragma mark - | ||
64 | #pragma mark Director Mac | ||
65 | |||
66 | @implementation CCDirectorMac | ||
67 | |||
68 | @synthesize isFullScreen = isFullScreen_; | ||
69 | @synthesize originalWinSize = originalWinSize_; | ||
70 | |||
71 | -(id) init | ||
72 | { | ||
73 | if( (self = [super init]) ) { | ||
74 | isFullScreen_ = NO; | ||
75 | resizeMode_ = kCCDirectorResize_AutoScale; | ||
76 | |||
77 | originalWinSize_ = CGSizeZero; | ||
78 | fullScreenWindow_ = nil; | ||
79 | windowGLView_ = nil; | ||
80 | winOffset_ = CGPointZero; | ||
81 | } | ||
82 | |||
83 | return self; | ||
84 | } | ||
85 | |||
86 | - (void) dealloc | ||
87 | { | ||
88 | [superViewGLView_ release]; | ||
89 | [fullScreenWindow_ release]; | ||
90 | [windowGLView_ release]; | ||
91 | [super dealloc]; | ||
92 | } | ||
93 | |||
94 | // | ||
95 | // setFullScreen code taken from GLFullScreen example by Apple | ||
96 | // | ||
97 | - (void) setFullScreen:(BOOL)fullscreen | ||
98 | { | ||
99 | // Mac OS X 10.6 and later offer a simplified mechanism to create full-screen contexts | ||
100 | #if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_5 | ||
101 | |||
102 | if (isFullScreen_ == fullscreen) return; | ||
103 | |||
104 | if( fullscreen ) { | ||
105 | originalWinRect_ = [openGLView_ frame]; | ||
106 | |||
107 | // Cache normal window and superview of openGLView | ||
108 | if(!windowGLView_) | ||
109 | windowGLView_ = [[openGLView_ window] retain]; | ||
110 | |||
111 | [superViewGLView_ release]; | ||
112 | superViewGLView_ = [[openGLView_ superview] retain]; | ||
113 | |||
114 | |||
115 | // Get screen size | ||
116 | NSRect displayRect = [[NSScreen mainScreen] frame]; | ||
117 | |||
118 | // Create a screen-sized window on the display you want to take over | ||
119 | fullScreenWindow_ = [[MacWindow alloc] initWithFrame:displayRect fullscreen:YES]; | ||
120 | |||
121 | // Remove glView from window | ||
122 | [openGLView_ removeFromSuperview]; | ||
123 | |||
124 | // Set new frame | ||
125 | [openGLView_ setFrame:displayRect]; | ||
126 | |||
127 | // Attach glView to fullscreen window | ||
128 | [fullScreenWindow_ setContentView:openGLView_]; | ||
129 | |||
130 | // Show the fullscreen window | ||
131 | [fullScreenWindow_ makeKeyAndOrderFront:self]; | ||
132 | [fullScreenWindow_ makeMainWindow]; | ||
133 | |||
134 | } else { | ||
135 | |||
136 | // Remove glView from fullscreen window | ||
137 | [openGLView_ removeFromSuperview]; | ||
138 | |||
139 | // Release fullscreen window | ||
140 | [fullScreenWindow_ release]; | ||
141 | fullScreenWindow_ = nil; | ||
142 | |||
143 | // Attach glView to superview | ||
144 | [superViewGLView_ addSubview:openGLView_]; | ||
145 | |||
146 | // Set new frame | ||
147 | [openGLView_ setFrame:originalWinRect_]; | ||
148 | |||
149 | // Show the window | ||
150 | [windowGLView_ makeKeyAndOrderFront:self]; | ||
151 | [windowGLView_ makeMainWindow]; | ||
152 | } | ||
153 | isFullScreen_ = fullscreen; | ||
154 | |||
155 | [openGLView_ retain]; // Retain +1 | ||
156 | |||
157 | // re-configure glView | ||
158 | [self setOpenGLView:openGLView_]; | ||
159 | |||
160 | [openGLView_ release]; // Retain -1 | ||
161 | |||
162 | [openGLView_ setNeedsDisplay:YES]; | ||
163 | #else | ||
164 | #error Full screen is not supported for Mac OS 10.5 or older yet | ||
165 | #error If you don't want FullScreen support, you can safely remove these 2 lines | ||
166 | #endif | ||
167 | } | ||
168 | |||
169 | -(void) setOpenGLView:(MacGLView *)view | ||
170 | { | ||
171 | [super setOpenGLView:view]; | ||
172 | |||
173 | // cache the NSWindow and NSOpenGLView created from the NIB | ||
174 | if( !isFullScreen_ && CGSizeEqualToSize(originalWinSize_, CGSizeZero)) | ||
175 | { | ||
176 | originalWinSize_ = winSizeInPixels_; | ||
177 | } | ||
178 | } | ||
179 | |||
180 | -(int) resizeMode | ||
181 | { | ||
182 | return resizeMode_; | ||
183 | } | ||
184 | |||
185 | -(void) setResizeMode:(int)mode | ||
186 | { | ||
187 | if( mode != resizeMode_ ) { | ||
188 | |||
189 | resizeMode_ = mode; | ||
190 | |||
191 | [self setProjection:projection_]; | ||
192 | [openGLView_ setNeedsDisplay: YES]; | ||
193 | } | ||
194 | } | ||
195 | |||
196 | -(void) setProjection:(ccDirectorProjection)projection | ||
197 | { | ||
198 | CGSize size = winSizeInPixels_; | ||
199 | |||
200 | CGPoint offset = CGPointZero; | ||
201 | float widthAspect = size.width; | ||
202 | float heightAspect = size.height; | ||
203 | |||
204 | |||
205 | if( resizeMode_ == kCCDirectorResize_AutoScale && ! CGSizeEqualToSize(originalWinSize_, CGSizeZero ) ) { | ||
206 | |||
207 | size = originalWinSize_; | ||
208 | |||
209 | float aspect = originalWinSize_.width / originalWinSize_.height; | ||
210 | widthAspect = winSizeInPixels_.width; | ||
211 | heightAspect = winSizeInPixels_.width / aspect; | ||
212 | |||
213 | if( heightAspect > winSizeInPixels_.height ) { | ||
214 | widthAspect = winSizeInPixels_.height * aspect; | ||
215 | heightAspect = winSizeInPixels_.height; | ||
216 | } | ||
217 | |||
218 | winOffset_.x = (winSizeInPixels_.width - widthAspect) / 2; | ||
219 | winOffset_.y = (winSizeInPixels_.height - heightAspect) / 2; | ||
220 | |||
221 | offset = winOffset_; | ||
222 | |||
223 | } | ||
224 | |||
225 | switch (projection) { | ||
226 | case kCCDirectorProjection2D: | ||
227 | glViewport(offset.x, offset.y, widthAspect, heightAspect); | ||
228 | glMatrixMode(GL_PROJECTION); | ||
229 | glLoadIdentity(); | ||
230 | ccglOrtho(0, size.width, 0, size.height, -1024, 1024); | ||
231 | glMatrixMode(GL_MODELVIEW); | ||
232 | glLoadIdentity(); | ||
233 | break; | ||
234 | |||
235 | case kCCDirectorProjection3D: | ||
236 | glViewport(offset.x, offset.y, widthAspect, heightAspect); | ||
237 | glMatrixMode(GL_PROJECTION); | ||
238 | glLoadIdentity(); | ||
239 | gluPerspective(60, (GLfloat)widthAspect/heightAspect, 0.1f, 1500.0f); | ||
240 | |||
241 | glMatrixMode(GL_MODELVIEW); | ||
242 | glLoadIdentity(); | ||
243 | |||
244 | float eyeZ = size.height * [self getZEye] / winSizeInPixels_.height; | ||
245 | |||
246 | gluLookAt( size.width/2, size.height/2, eyeZ, | ||
247 | size.width/2, size.height/2, 0, | ||
248 | 0.0f, 1.0f, 0.0f); | ||
249 | break; | ||
250 | |||
251 | case kCCDirectorProjectionCustom: | ||
252 | if( projectionDelegate_ ) | ||
253 | [projectionDelegate_ updateProjection]; | ||
254 | break; | ||
255 | |||
256 | default: | ||
257 | CCLOG(@"cocos2d: Director: unrecognized projecgtion"); | ||
258 | break; | ||
259 | } | ||
260 | |||
261 | projection_ = projection; | ||
262 | } | ||
263 | |||
264 | // If scaling is supported, then it should always return the original size | ||
265 | // otherwise it should return the "real" size. | ||
266 | -(CGSize) winSize | ||
267 | { | ||
268 | if( resizeMode_ == kCCDirectorResize_AutoScale ) | ||
269 | return originalWinSize_; | ||
270 | |||
271 | return winSizeInPixels_; | ||
272 | } | ||
273 | |||
274 | -(CGSize) winSizeInPixels | ||
275 | { | ||
276 | return [self winSize]; | ||
277 | } | ||
278 | |||
279 | - (CGPoint) convertToLogicalCoordinates:(CGPoint)coords | ||
280 | { | ||
281 | CGPoint ret; | ||
282 | |||
283 | if( resizeMode_ == kCCDirectorResize_NoScale ) | ||
284 | ret = coords; | ||
285 | |||
286 | else { | ||
287 | |||
288 | float x_diff = originalWinSize_.width / (winSizeInPixels_.width - winOffset_.x * 2); | ||
289 | float y_diff = originalWinSize_.height / (winSizeInPixels_.height - winOffset_.y * 2); | ||
290 | |||
291 | float adjust_x = (winSizeInPixels_.width * x_diff - originalWinSize_.width ) / 2; | ||
292 | float adjust_y = (winSizeInPixels_.height * y_diff - originalWinSize_.height ) / 2; | ||
293 | |||
294 | ret = CGPointMake( (x_diff * coords.x) - adjust_x, ( y_diff * coords.y ) - adjust_y ); | ||
295 | } | ||
296 | |||
297 | return ret; | ||
298 | } | ||
299 | @end | ||
300 | |||
301 | |||
302 | #pragma mark - | ||
303 | #pragma mark DirectorDisplayLink | ||
304 | |||
305 | |||
306 | @implementation CCDirectorDisplayLink | ||
307 | |||
308 | - (CVReturn) getFrameForTime:(const CVTimeStamp*)outputTime | ||
309 | { | ||
310 | #if CC_DIRECTOR_MAC_USE_DISPLAY_LINK_THREAD | ||
311 | if( ! runningThread_ ) | ||
312 | runningThread_ = [NSThread currentThread]; | ||
313 | |||
314 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | ||
315 | |||
316 | [self drawScene]; | ||
317 | [[CCEventDispatcher sharedDispatcher] dispatchQueuedEvents]; | ||
318 | |||
319 | [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:nil]; | ||
320 | |||
321 | [pool release]; | ||
322 | |||
323 | #else | ||
324 | [self performSelector:@selector(drawScene) onThread:runningThread_ withObject:nil waitUntilDone:YES]; | ||
325 | #endif | ||
326 | |||
327 | return kCVReturnSuccess; | ||
328 | } | ||
329 | |||
330 | // This is the renderer output callback function | ||
331 | static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* displayLinkContext) | ||
332 | { | ||
333 | CVReturn result = [(CCDirectorDisplayLink*)displayLinkContext getFrameForTime:outputTime]; | ||
334 | return result; | ||
335 | } | ||
336 | |||
337 | - (void) startAnimation | ||
338 | { | ||
339 | #if ! CC_DIRECTOR_MAC_USE_DISPLAY_LINK_THREAD | ||
340 | runningThread_ = [[NSThread alloc] initWithTarget:self selector:@selector(mainLoop) object:nil]; | ||
341 | [runningThread_ start]; | ||
342 | #endif | ||
343 | |||
344 | gettimeofday( &lastUpdate_, NULL); | ||
345 | |||
346 | // Create a display link capable of being used with all active displays | ||
347 | CVDisplayLinkCreateWithActiveCGDisplays(&displayLink); | ||
348 | |||
349 | // Set the renderer output callback function | ||
350 | CVDisplayLinkSetOutputCallback(displayLink, &MyDisplayLinkCallback, self); | ||
351 | |||
352 | // Set the display link for the current renderer | ||
353 | CGLContextObj cglContext = [[openGLView_ openGLContext] CGLContextObj]; | ||
354 | CGLPixelFormatObj cglPixelFormat = [[openGLView_ pixelFormat] CGLPixelFormatObj]; | ||
355 | CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, cglContext, cglPixelFormat); | ||
356 | |||
357 | // Activate the display link | ||
358 | CVDisplayLinkStart(displayLink); | ||
359 | } | ||
360 | |||
361 | - (void) stopAnimation | ||
362 | { | ||
363 | if( displayLink ) { | ||
364 | CVDisplayLinkStop(displayLink); | ||
365 | CVDisplayLinkRelease(displayLink); | ||
366 | displayLink = NULL; | ||
367 | |||
368 | #if ! CC_DIRECTOR_MAC_USE_DISPLAY_LINK_THREAD | ||
369 | [runningThread_ cancel]; | ||
370 | [runningThread_ release]; | ||
371 | runningThread_ = nil; | ||
372 | #endif | ||
373 | } | ||
374 | } | ||
375 | |||
376 | -(void) dealloc | ||
377 | { | ||
378 | if( displayLink ) { | ||
379 | CVDisplayLinkStop(displayLink); | ||
380 | CVDisplayLinkRelease(displayLink); | ||
381 | } | ||
382 | [super dealloc]; | ||
383 | } | ||
384 | |||
385 | // | ||
386 | // Mac Director has its own thread | ||
387 | // | ||
388 | -(void) mainLoop | ||
389 | { | ||
390 | while( ![[NSThread currentThread] isCancelled] ) { | ||
391 | // There is no autorelease pool when this method is called because it will be called from a background thread | ||
392 | // It's important to create one or you will leak objects | ||
393 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | ||
394 | |||
395 | [[NSRunLoop currentRunLoop] run]; | ||
396 | |||
397 | [pool release]; | ||
398 | } | ||
399 | } | ||
400 | |||
401 | // | ||
402 | // Draw the Scene | ||
403 | // | ||
404 | - (void) drawScene | ||
405 | { | ||
406 | // We draw on a secondary thread through the display link | ||
407 | // When resizing the view, -reshape is called automatically on the main thread | ||
408 | // Add a mutex around to avoid the threads accessing the context simultaneously when resizing | ||
409 | CGLLockContext([[openGLView_ openGLContext] CGLContextObj]); | ||
410 | [[openGLView_ openGLContext] makeCurrentContext]; | ||
411 | |||
412 | /* calculate "global" dt */ | ||
413 | [self calculateDeltaTime]; | ||
414 | |||
415 | /* tick before glClear: issue #533 */ | ||
416 | if( ! isPaused_ ) { | ||
417 | [[CCScheduler sharedScheduler] tick: dt]; | ||
418 | } | ||
419 | |||
420 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | ||
421 | |||
422 | /* to avoid flickr, nextScene MUST be here: after tick and before draw. | ||
423 | XXX: Which bug is this one. It seems that it can't be reproduced with v0.9 */ | ||
424 | if( nextScene_ ) | ||
425 | [self setNextScene]; | ||
426 | |||
427 | glPushMatrix(); | ||
428 | |||
429 | |||
430 | // By default enable VertexArray, ColorArray, TextureCoordArray and Texture2D | ||
431 | CC_ENABLE_DEFAULT_GL_STATES(); | ||
432 | |||
433 | /* draw the scene */ | ||
434 | [runningScene_ visit]; | ||
435 | |||
436 | /* draw the notification node */ | ||
437 | [notificationNode_ visit]; | ||
438 | |||
439 | if( displayFPS_ ) | ||
440 | [self showFPS]; | ||
441 | |||
442 | #if CC_ENABLE_PROFILERS | ||
443 | [self showProfilers]; | ||
444 | #endif | ||
445 | |||
446 | CC_DISABLE_DEFAULT_GL_STATES(); | ||
447 | |||
448 | glPopMatrix(); | ||
449 | |||
450 | [[openGLView_ openGLContext] flushBuffer]; | ||
451 | CGLUnlockContext([[openGLView_ openGLContext] CGLContextObj]); | ||
452 | } | ||
453 | |||
454 | // set the event dispatcher | ||
455 | -(void) setOpenGLView:(MacGLView *)view | ||
456 | { | ||
457 | if( view != openGLView_ ) { | ||
458 | |||
459 | [super setOpenGLView:view]; | ||
460 | |||
461 | CCEventDispatcher *eventDispatcher = [CCEventDispatcher sharedDispatcher]; | ||
462 | [openGLView_ setEventDelegate: eventDispatcher]; | ||
463 | [eventDispatcher setDispatchEvents: YES]; | ||
464 | |||
465 | // Enable Touches. Default no. | ||
466 | [view setAcceptsTouchEvents:NO]; | ||
467 | // [view setAcceptsTouchEvents:YES]; | ||
468 | |||
469 | |||
470 | // Synchronize buffer swaps with vertical refresh rate | ||
471 | [[view openGLContext] makeCurrentContext]; | ||
472 | GLint swapInt = 1; | ||
473 | [[view openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; | ||
474 | } | ||
475 | } | ||
476 | |||
477 | @end | ||
478 | |||
479 | #endif // __MAC_OS_X_VERSION_MAX_ALLOWED | ||
diff --git a/libs/cocos2d/Platforms/Mac/CCEventDispatcher.h b/libs/cocos2d/Platforms/Mac/CCEventDispatcher.h new file mode 100755 index 0000000..06889e8 --- /dev/null +++ b/libs/cocos2d/Platforms/Mac/CCEventDispatcher.h | |||
@@ -0,0 +1,277 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 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 | // Only compile this code on Mac. These files should not be included on your iOS project. | ||
27 | // But in case they are included, it won't be compiled. | ||
28 | #import <Availability.h> | ||
29 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
30 | #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) | ||
31 | |||
32 | #import <Cocoa/Cocoa.h> | ||
33 | |||
34 | #import "MacGLView.h" | ||
35 | #import "../../Support/uthash.h" // hack: uthash needs to be imported before utlist to prevent warning | ||
36 | #import "../../Support/utlist.h" | ||
37 | #import "../../ccConfig.h" | ||
38 | |||
39 | #pragma mark - | ||
40 | #pragma mark CCMouseEventDelegate | ||
41 | |||
42 | /** CCMouseEventDelegate protocol. | ||
43 | Implement it in your node to receive any of mouse events | ||
44 | */ | ||
45 | @protocol CCMouseEventDelegate <NSObject> | ||
46 | @optional | ||
47 | |||
48 | // | ||
49 | // left | ||
50 | // | ||
51 | /** called when the "mouseDown" event is received. | ||
52 | Return YES to avoid propagating the event to other delegates. | ||
53 | */ | ||
54 | -(BOOL) ccMouseDown:(NSEvent*)event; | ||
55 | |||
56 | /** called when the "mouseDragged" event is received. | ||
57 | Return YES to avoid propagating the event to other delegates. | ||
58 | */ | ||
59 | -(BOOL) ccMouseDragged:(NSEvent*)event; | ||
60 | |||
61 | /** called when the "mouseMoved" event is received. | ||
62 | Return YES to avoid propagating the event to other delegates. | ||
63 | By default, "mouseMoved" is disabled. To enable it, send the "setAcceptsMouseMovedEvents:YES" message to the main window. | ||
64 | */ | ||
65 | -(BOOL) ccMouseMoved:(NSEvent*)event; | ||
66 | |||
67 | /** called when the "mouseUp" event is received. | ||
68 | Return YES to avoid propagating the event to other delegates. | ||
69 | */ | ||
70 | -(BOOL) ccMouseUp:(NSEvent*)event; | ||
71 | |||
72 | |||
73 | // | ||
74 | // right | ||
75 | // | ||
76 | |||
77 | /** called when the "rightMouseDown" event is received. | ||
78 | Return YES to avoid propagating the event to other delegates. | ||
79 | */ | ||
80 | -(BOOL) ccRightMouseDown:(NSEvent*)event; | ||
81 | |||
82 | /** called when the "rightMouseDragged" event is received. | ||
83 | Return YES to avoid propagating the event to other delegates. | ||
84 | */ | ||
85 | -(BOOL) ccRightMouseDragged:(NSEvent*)event; | ||
86 | |||
87 | /** called when the "rightMouseUp" event is received. | ||
88 | Return YES to avoid propagating the event to other delegates. | ||
89 | */ | ||
90 | -(BOOL) ccRightMouseUp:(NSEvent*)event; | ||
91 | |||
92 | // | ||
93 | // other | ||
94 | // | ||
95 | |||
96 | /** called when the "otherMouseDown" event is received. | ||
97 | Return YES to avoid propagating the event to other delegates. | ||
98 | */ | ||
99 | -(BOOL) ccOtherMouseDown:(NSEvent*)event; | ||
100 | |||
101 | /** called when the "otherMouseDragged" event is received. | ||
102 | Return YES to avoid propagating the event to other delegates. | ||
103 | */ | ||
104 | -(BOOL) ccOtherMouseDragged:(NSEvent*)event; | ||
105 | |||
106 | /** called when the "otherMouseUp" event is received. | ||
107 | Return YES to avoid propagating the event to other delegates. | ||
108 | */ | ||
109 | -(BOOL) ccOtherMouseUp:(NSEvent*)event; | ||
110 | |||
111 | // | ||
112 | // scroll wheel | ||
113 | // | ||
114 | |||
115 | /** called when the "scrollWheel" event is received. | ||
116 | Return YES to avoid propagating the event to other delegates. | ||
117 | */ | ||
118 | - (BOOL)ccScrollWheel:(NSEvent *)theEvent; | ||
119 | |||
120 | |||
121 | // | ||
122 | // enter / exit | ||
123 | // | ||
124 | |||
125 | /** called when the "mouseEntered" event is received. | ||
126 | Return YES to avoid propagating the event to other delegates. | ||
127 | */ | ||
128 | - (void)ccMouseEntered:(NSEvent *)theEvent; | ||
129 | |||
130 | /** called when the "mouseExited" event is received. | ||
131 | Return YES to avoid propagating the event to other delegates. | ||
132 | */ | ||
133 | - (void)ccMouseExited:(NSEvent *)theEvent; | ||
134 | |||
135 | @end | ||
136 | |||
137 | #pragma mark - | ||
138 | #pragma mark CCKeyboardEventDelegate | ||
139 | |||
140 | /** CCKeyboardEventDelegate protocol. | ||
141 | Implement it in your node to receive any of keyboard events | ||
142 | */ | ||
143 | @protocol CCKeyboardEventDelegate <NSObject> | ||
144 | @optional | ||
145 | /** called when the "keyUp" event is received. | ||
146 | Return YES to avoid propagating the event to other delegates. | ||
147 | */ | ||
148 | -(BOOL) ccKeyUp:(NSEvent*)event; | ||
149 | |||
150 | /** called when the "keyDown" event is received. | ||
151 | Return YES to avoid propagating the event to other delegates. | ||
152 | */ | ||
153 | -(BOOL) ccKeyDown:(NSEvent*)event; | ||
154 | /** called when the "flagsChanged" event is received. | ||
155 | Return YES to avoid propagating the event to other delegates. | ||
156 | */ | ||
157 | -(BOOL) ccFlagsChanged:(NSEvent*)event; | ||
158 | @end | ||
159 | |||
160 | #pragma mark - | ||
161 | #pragma mark CCTouchEventDelegate | ||
162 | |||
163 | /** CCTouchEventDelegate protocol. | ||
164 | Implement it in your node to receive any of touch events | ||
165 | */ | ||
166 | @protocol CCTouchEventDelegate <NSObject> | ||
167 | @optional | ||
168 | /** called when the "touchesBegan" event is received. | ||
169 | Return YES to avoid propagating the event to other delegates. | ||
170 | */ | ||
171 | - (BOOL)ccTouchesBeganWithEvent:(NSEvent *)event; | ||
172 | |||
173 | /** called when the "touchesMoved" event is received. | ||
174 | Return YES to avoid propagating the event to other delegates. | ||
175 | */ | ||
176 | - (BOOL)ccTouchesMovedWithEvent:(NSEvent *)event; | ||
177 | |||
178 | /** called when the "touchesEnded" event is received. | ||
179 | Return YES to avoid propagating the event to other delegates. | ||
180 | */ | ||
181 | - (BOOL)ccTouchesEndedWithEvent:(NSEvent *)event; | ||
182 | |||
183 | /** called when the "touchesCancelled" event is received. | ||
184 | Return YES to avoid propagating the event to other delegates. | ||
185 | */ | ||
186 | - (BOOL)ccTouchesCancelledWithEvent:(NSEvent *)event; | ||
187 | |||
188 | @end | ||
189 | |||
190 | |||
191 | #pragma mark - | ||
192 | #pragma mark CCEventDispatcher | ||
193 | |||
194 | struct _listEntry; | ||
195 | |||
196 | /** CCEventDispatcher | ||
197 | |||
198 | This is object is responsible for dispatching the events: | ||
199 | - Mouse events | ||
200 | - Keyboard events | ||
201 | - Touch events | ||
202 | |||
203 | Only available on Mac | ||
204 | */ | ||
205 | @interface CCEventDispatcher : NSObject <MacEventDelegate> { | ||
206 | |||
207 | BOOL dispatchEvents_; | ||
208 | |||
209 | struct _listEntry *keyboardDelegates_; | ||
210 | struct _listEntry *mouseDelegates_; | ||
211 | struct _listEntry *touchDelegates_; | ||
212 | } | ||
213 | |||
214 | @property (nonatomic, readwrite) BOOL dispatchEvents; | ||
215 | |||
216 | |||
217 | /** CCEventDispatcher singleton */ | ||
218 | +(CCEventDispatcher*) sharedDispatcher; | ||
219 | |||
220 | #pragma mark CCEventDispatcher - Mouse | ||
221 | |||
222 | /** Adds a mouse delegate to the dispatcher's list. | ||
223 | Delegates with a lower priority value will be called before higher priority values. | ||
224 | All the events will be propgated to all the delegates, unless the one delegate returns YES. | ||
225 | |||
226 | IMPORTANT: The delegate will be retained. | ||
227 | */ | ||
228 | -(void) addMouseDelegate:(id<CCMouseEventDelegate>) delegate priority:(NSInteger)priority; | ||
229 | |||
230 | /** removes a mouse delegate */ | ||
231 | -(void) removeMouseDelegate:(id) delegate; | ||
232 | |||
233 | /** Removes all mouse delegates, releasing all the delegates */ | ||
234 | -(void) removeAllMouseDelegates; | ||
235 | |||
236 | #pragma mark CCEventDispatcher - Keyboard | ||
237 | |||
238 | /** Adds a Keyboard delegate to the dispatcher's list. | ||
239 | Delegates with a lower priority value will be called before higher priority values. | ||
240 | All the events will be propgated to all the delegates, unless the one delegate returns YES. | ||
241 | |||
242 | IMPORTANT: The delegate will be retained. | ||
243 | */ | ||
244 | -(void) addKeyboardDelegate:(id<CCKeyboardEventDelegate>) delegate priority:(NSInteger)priority; | ||
245 | |||
246 | /** removes a mouse delegate */ | ||
247 | -(void) removeKeyboardDelegate:(id) delegate; | ||
248 | |||
249 | /** Removes all mouse delegates, releasing all the delegates */ | ||
250 | -(void) removeAllKeyboardDelegates; | ||
251 | |||
252 | #pragma mark CCEventDispatcher - Touches | ||
253 | |||
254 | /** Adds a Touch delegate to the dispatcher's list. | ||
255 | Delegates with a lower priority value will be called before higher priority values. | ||
256 | All the events will be propgated to all the delegates, unless the one delegate returns YES. | ||
257 | |||
258 | IMPORTANT: The delegate will be retained. | ||
259 | */ | ||
260 | - (void)addTouchDelegate:(id<CCTouchEventDelegate>)delegate priority:(NSInteger)priority; | ||
261 | |||
262 | /** Removes a touch delegate */ | ||
263 | - (void)removeTouchDelegate:(id) delegate; | ||
264 | |||
265 | /** Removes all touch delegates, releasing all the delegates */ | ||
266 | - (void)removeAllTouchDelegates; | ||
267 | |||
268 | #pragma mark CCEventDispatcher - Dispatch Events | ||
269 | |||
270 | #if CC_DIRECTOR_MAC_USE_DISPLAY_LINK_THREAD | ||
271 | -(void) dispatchQueuedEvents; | ||
272 | #endif | ||
273 | |||
274 | @end | ||
275 | |||
276 | |||
277 | #endif // __MAC_OS_X_VERSION_MAX_ALLOWED | ||
diff --git a/libs/cocos2d/Platforms/Mac/CCEventDispatcher.m b/libs/cocos2d/Platforms/Mac/CCEventDispatcher.m new file mode 100755 index 0000000..1d1740e --- /dev/null +++ b/libs/cocos2d/Platforms/Mac/CCEventDispatcher.m | |||
@@ -0,0 +1,645 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 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 | // Only compile this code on Mac. These files should not be included on your iOS project. | ||
27 | // But in case they are included, it won't be compiled. | ||
28 | #import <Availability.h> | ||
29 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
30 | #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) | ||
31 | |||
32 | #import "CCEventDispatcher.h" | ||
33 | #import "../../ccConfig.h" | ||
34 | |||
35 | static CCEventDispatcher *sharedDispatcher = nil; | ||
36 | |||
37 | enum { | ||
38 | // mouse | ||
39 | kCCImplementsMouseDown = 1 << 0, | ||
40 | kCCImplementsMouseMoved = 1 << 1, | ||
41 | kCCImplementsMouseDragged = 1 << 2, | ||
42 | kCCImplementsMouseUp = 1 << 3, | ||
43 | kCCImplementsRightMouseDown = 1 << 4, | ||
44 | kCCImplementsRightMouseDragged = 1 << 5, | ||
45 | kCCImplementsRightMouseUp = 1 << 6, | ||
46 | kCCImplementsOtherMouseDown = 1 << 7, | ||
47 | kCCImplementsOtherMouseDragged = 1 << 8, | ||
48 | kCCImplementsOtherMouseUp = 1 << 9, | ||
49 | kCCImplementsScrollWheel = 1 << 10, | ||
50 | kCCImplementsMouseEntered = 1 << 11, | ||
51 | kCCImplementsMouseExited = 1 << 12, | ||
52 | |||
53 | kCCImplementsTouchesBegan = 1 << 13, | ||
54 | kCCImplementsTouchesMoved = 1 << 14, | ||
55 | kCCImplementsTouchesEnded = 1 << 15, | ||
56 | kCCImplementsTouchesCancelled = 1 << 16, | ||
57 | |||
58 | // keyboard | ||
59 | kCCImplementsKeyUp = 1 << 0, | ||
60 | kCCImplementsKeyDown = 1 << 1, | ||
61 | kCCImplementsFlagsChanged = 1 << 2, | ||
62 | }; | ||
63 | |||
64 | |||
65 | typedef struct _listEntry | ||
66 | { | ||
67 | struct _listEntry *prev, *next; | ||
68 | id delegate; | ||
69 | NSInteger priority; | ||
70 | NSUInteger flags; | ||
71 | } tListEntry; | ||
72 | |||
73 | |||
74 | #if CC_DIRECTOR_MAC_USE_DISPLAY_LINK_THREAD | ||
75 | |||
76 | #define QUEUE_EVENT_MAX 128 | ||
77 | struct _eventQueue { | ||
78 | SEL selector; | ||
79 | NSEvent *event; | ||
80 | }; | ||
81 | |||
82 | static struct _eventQueue eventQueue[QUEUE_EVENT_MAX]; | ||
83 | static int eventQueueCount; | ||
84 | |||
85 | #endif // CC_DIRECTOR_MAC_USE_DISPLAY_LINK_THREAD | ||
86 | |||
87 | |||
88 | @implementation CCEventDispatcher | ||
89 | |||
90 | @synthesize dispatchEvents=dispatchEvents_; | ||
91 | |||
92 | |||
93 | +(CCEventDispatcher*) sharedDispatcher | ||
94 | { | ||
95 | @synchronized(self) { | ||
96 | if (sharedDispatcher == nil) | ||
97 | sharedDispatcher = [[self alloc] init]; // assignment not done here | ||
98 | } | ||
99 | return sharedDispatcher; | ||
100 | } | ||
101 | |||
102 | +(id) allocWithZone:(NSZone *)zone | ||
103 | { | ||
104 | @synchronized(self) { | ||
105 | NSAssert(sharedDispatcher == nil, @"Attempted to allocate a second instance of a singleton."); | ||
106 | return [super allocWithZone:zone]; | ||
107 | } | ||
108 | return nil; // on subsequent allocation attempts return nil | ||
109 | } | ||
110 | |||
111 | -(id) init | ||
112 | { | ||
113 | if( (self = [super init]) ) | ||
114 | { | ||
115 | // events enabled by default | ||
116 | dispatchEvents_ = YES; | ||
117 | |||
118 | // delegates | ||
119 | keyboardDelegates_ = NULL; | ||
120 | mouseDelegates_ = NULL; | ||
121 | touchDelegates_ = NULL; | ||
122 | |||
123 | #if CC_DIRECTOR_MAC_USE_DISPLAY_LINK_THREAD | ||
124 | eventQueueCount = 0; | ||
125 | #endif | ||
126 | } | ||
127 | |||
128 | return self; | ||
129 | } | ||
130 | |||
131 | - (void) dealloc | ||
132 | { | ||
133 | [super dealloc]; | ||
134 | } | ||
135 | |||
136 | #pragma mark CCEventDispatcher - add / remove delegates | ||
137 | |||
138 | -(void) addDelegate:(id)delegate priority:(NSInteger)priority flags:(NSUInteger)flags list:(tListEntry**)list | ||
139 | { | ||
140 | tListEntry *listElement = malloc( sizeof(*listElement) ); | ||
141 | |||
142 | listElement->delegate = [delegate retain]; | ||
143 | listElement->priority = priority; | ||
144 | listElement->flags = flags; | ||
145 | listElement->next = listElement->prev = NULL; | ||
146 | |||
147 | // empty list ? | ||
148 | if( ! *list ) { | ||
149 | DL_APPEND( *list, listElement ); | ||
150 | |||
151 | } else { | ||
152 | BOOL added = NO; | ||
153 | |||
154 | for( tListEntry *elem = *list; elem ; elem = elem->next ) { | ||
155 | if( priority < elem->priority ) { | ||
156 | |||
157 | if( elem == *list ) | ||
158 | DL_PREPEND(*list, listElement); | ||
159 | else { | ||
160 | listElement->next = elem; | ||
161 | listElement->prev = elem->prev; | ||
162 | |||
163 | elem->prev->next = listElement; | ||
164 | elem->prev = listElement; | ||
165 | } | ||
166 | |||
167 | added = YES; | ||
168 | break; | ||
169 | } | ||
170 | } | ||
171 | |||
172 | // Not added? priority has the higher value. Append it. | ||
173 | if( !added ) | ||
174 | DL_APPEND(*list, listElement); | ||
175 | } | ||
176 | } | ||
177 | |||
178 | -(void) removeDelegate:(id)delegate fromList:(tListEntry**)list | ||
179 | { | ||
180 | tListEntry *entry, *tmp; | ||
181 | |||
182 | // updates with priority < 0 | ||
183 | DL_FOREACH_SAFE( *list, entry, tmp ) { | ||
184 | if( entry->delegate == delegate ) { | ||
185 | DL_DELETE( *list, entry ); | ||
186 | [delegate release]; | ||
187 | free(entry); | ||
188 | break; | ||
189 | } | ||
190 | } | ||
191 | } | ||
192 | |||
193 | -(void) removeAllDelegatesFromList:(tListEntry**)list | ||
194 | { | ||
195 | tListEntry *entry, *tmp; | ||
196 | |||
197 | DL_FOREACH_SAFE( *list, entry, tmp ) { | ||
198 | DL_DELETE( *list, entry ); | ||
199 | free(entry); | ||
200 | } | ||
201 | } | ||
202 | |||
203 | |||
204 | -(void) addMouseDelegate:(id<CCMouseEventDelegate>) delegate priority:(NSInteger)priority | ||
205 | { | ||
206 | NSUInteger flags = 0; | ||
207 | |||
208 | flags |= ( [delegate respondsToSelector:@selector(ccMouseDown:)] ? kCCImplementsMouseDown : 0 ); | ||
209 | flags |= ( [delegate respondsToSelector:@selector(ccMouseDragged:)] ? kCCImplementsMouseDragged : 0 ); | ||
210 | flags |= ( [delegate respondsToSelector:@selector(ccMouseMoved:)] ? kCCImplementsMouseMoved : 0 ); | ||
211 | flags |= ( [delegate respondsToSelector:@selector(ccMouseUp:)] ? kCCImplementsMouseUp : 0 ); | ||
212 | |||
213 | flags |= ( [delegate respondsToSelector:@selector(ccRightMouseDown:)] ? kCCImplementsRightMouseDown : 0 ); | ||
214 | flags |= ( [delegate respondsToSelector:@selector(ccRightMouseDragged:)] ? kCCImplementsRightMouseDragged : 0 ); | ||
215 | flags |= ( [delegate respondsToSelector:@selector(ccRightMouseUp:)] ? kCCImplementsRightMouseUp : 0 ); | ||
216 | |||
217 | flags |= ( [delegate respondsToSelector:@selector(ccOtherMouseDown:)] ? kCCImplementsOtherMouseDown : 0 ); | ||
218 | flags |= ( [delegate respondsToSelector:@selector(ccOtherMouseDragged:)] ? kCCImplementsOtherMouseDragged : 0 ); | ||
219 | flags |= ( [delegate respondsToSelector:@selector(ccOtherMouseUp:)] ? kCCImplementsOtherMouseUp : 0 ); | ||
220 | |||
221 | flags |= ( [delegate respondsToSelector:@selector(ccMouseEntered:)] ? kCCImplementsMouseEntered : 0 ); | ||
222 | flags |= ( [delegate respondsToSelector:@selector(ccMouseExited:)] ? kCCImplementsMouseExited : 0 ); | ||
223 | |||
224 | flags |= ( [delegate respondsToSelector:@selector(ccScrollWheel:)] ? kCCImplementsScrollWheel : 0 ); | ||
225 | |||
226 | [self addDelegate:delegate priority:priority flags:flags list:&mouseDelegates_]; | ||
227 | } | ||
228 | |||
229 | -(void) removeMouseDelegate:(id) delegate | ||
230 | { | ||
231 | [self removeDelegate:delegate fromList:&mouseDelegates_]; | ||
232 | } | ||
233 | |||
234 | -(void) removeAllMouseDelegates | ||
235 | { | ||
236 | [self removeAllDelegatesFromList:&mouseDelegates_]; | ||
237 | } | ||
238 | |||
239 | -(void) addKeyboardDelegate:(id<CCKeyboardEventDelegate>) delegate priority:(NSInteger)priority | ||
240 | { | ||
241 | NSUInteger flags = 0; | ||
242 | |||
243 | flags |= ( [delegate respondsToSelector:@selector(ccKeyUp:)] ? kCCImplementsKeyUp : 0 ); | ||
244 | flags |= ( [delegate respondsToSelector:@selector(ccKeyDown:)] ? kCCImplementsKeyDown : 0 ); | ||
245 | flags |= ( [delegate respondsToSelector:@selector(ccFlagsChanged:)] ? kCCImplementsFlagsChanged : 0 ); | ||
246 | |||
247 | [self addDelegate:delegate priority:priority flags:flags list:&keyboardDelegates_]; | ||
248 | } | ||
249 | |||
250 | -(void) removeKeyboardDelegate:(id) delegate | ||
251 | { | ||
252 | [self removeDelegate:delegate fromList:&keyboardDelegates_]; | ||
253 | } | ||
254 | |||
255 | -(void) removeAllKeyboardDelegates | ||
256 | { | ||
257 | [self removeAllDelegatesFromList:&keyboardDelegates_]; | ||
258 | } | ||
259 | |||
260 | -(void) addTouchDelegate:(id<CCTouchEventDelegate>) delegate priority:(NSInteger)priority | ||
261 | { | ||
262 | NSUInteger flags = 0; | ||
263 | |||
264 | flags |= ( [delegate respondsToSelector:@selector(ccTouchesBeganWithEvent:)] ? kCCImplementsTouchesBegan : 0 ); | ||
265 | flags |= ( [delegate respondsToSelector:@selector(ccTouchesMovedWithEvent:)] ? kCCImplementsTouchesMoved : 0 ); | ||
266 | flags |= ( [delegate respondsToSelector:@selector(ccTouchesEndedWithEvent:)] ? kCCImplementsTouchesEnded : 0 ); | ||
267 | flags |= ( [delegate respondsToSelector:@selector(ccTouchesCancelledWithEvent:)] ? kCCImplementsTouchesCancelled : 0 ); | ||
268 | |||
269 | [self addDelegate:delegate priority:priority flags:flags list:&touchDelegates_]; | ||
270 | } | ||
271 | |||
272 | -(void) removeTouchDelegate:(id) delegate | ||
273 | { | ||
274 | [self removeDelegate:delegate fromList:&touchDelegates_]; | ||
275 | } | ||
276 | |||
277 | -(void) removeAllTouchDelegates | ||
278 | { | ||
279 | [self removeAllDelegatesFromList:&touchDelegates_]; | ||
280 | } | ||
281 | |||
282 | |||
283 | #pragma mark CCEventDispatcher - Mouse events | ||
284 | // | ||
285 | // Mouse events | ||
286 | // | ||
287 | |||
288 | // | ||
289 | // Left | ||
290 | // | ||
291 | - (void)mouseDown:(NSEvent *)event | ||
292 | { | ||
293 | if( dispatchEvents_ ) { | ||
294 | tListEntry *entry, *tmp; | ||
295 | |||
296 | DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) { | ||
297 | if ( entry->flags & kCCImplementsMouseDown ) { | ||
298 | void *swallows = [entry->delegate performSelector:@selector(ccMouseDown:) withObject:event]; | ||
299 | if( swallows ) | ||
300 | break; | ||
301 | } | ||
302 | } | ||
303 | } | ||
304 | } | ||
305 | |||
306 | - (void)mouseMoved:(NSEvent *)event | ||
307 | { | ||
308 | if( dispatchEvents_ ) { | ||
309 | tListEntry *entry, *tmp; | ||
310 | |||
311 | DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) { | ||
312 | if ( entry->flags & kCCImplementsMouseMoved ) { | ||
313 | void *swallows = [entry->delegate performSelector:@selector(ccMouseMoved:) withObject:event]; | ||
314 | if( swallows ) | ||
315 | break; | ||
316 | } | ||
317 | } | ||
318 | } | ||
319 | } | ||
320 | |||
321 | - (void)mouseDragged:(NSEvent *)event | ||
322 | { | ||
323 | if( dispatchEvents_ ) { | ||
324 | tListEntry *entry, *tmp; | ||
325 | |||
326 | DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) { | ||
327 | if ( entry->flags & kCCImplementsMouseDragged ) { | ||
328 | void *swallows = [entry->delegate performSelector:@selector(ccMouseDragged:) withObject:event]; | ||
329 | if( swallows ) | ||
330 | break; | ||
331 | } | ||
332 | } | ||
333 | } | ||
334 | } | ||
335 | |||
336 | - (void)mouseUp:(NSEvent *)event | ||
337 | { | ||
338 | if( dispatchEvents_ ) { | ||
339 | tListEntry *entry, *tmp; | ||
340 | |||
341 | DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) { | ||
342 | if ( entry->flags & kCCImplementsMouseUp ) { | ||
343 | void *swallows = [entry->delegate performSelector:@selector(ccMouseUp:) withObject:event]; | ||
344 | if( swallows ) | ||
345 | break; | ||
346 | } | ||
347 | } | ||
348 | } | ||
349 | } | ||
350 | |||
351 | // | ||
352 | // Mouse Right | ||
353 | // | ||
354 | - (void)rightMouseDown:(NSEvent *)event | ||
355 | { | ||
356 | if( dispatchEvents_ ) { | ||
357 | tListEntry *entry, *tmp; | ||
358 | |||
359 | DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) { | ||
360 | if ( entry->flags & kCCImplementsRightMouseDown ) { | ||
361 | void *swallows = [entry->delegate performSelector:@selector(ccRightMouseDown:) withObject:event]; | ||
362 | if( swallows ) | ||
363 | break; | ||
364 | } | ||
365 | } | ||
366 | } | ||
367 | } | ||
368 | |||
369 | - (void)rightMouseDragged:(NSEvent *)event | ||
370 | { | ||
371 | if( dispatchEvents_ ) { | ||
372 | tListEntry *entry, *tmp; | ||
373 | |||
374 | DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) { | ||
375 | if ( entry->flags & kCCImplementsRightMouseDragged ) { | ||
376 | void *swallows = [entry->delegate performSelector:@selector(ccRightMouseDragged:) withObject:event]; | ||
377 | if( swallows ) | ||
378 | break; | ||
379 | } | ||
380 | } | ||
381 | } | ||
382 | } | ||
383 | |||
384 | - (void)rightMouseUp:(NSEvent *)event | ||
385 | { | ||
386 | if( dispatchEvents_ ) { | ||
387 | tListEntry *entry, *tmp; | ||
388 | |||
389 | DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) { | ||
390 | if ( entry->flags & kCCImplementsRightMouseUp ) { | ||
391 | void *swallows = [entry->delegate performSelector:@selector(ccRightMouseUp:) withObject:event]; | ||
392 | if( swallows ) | ||
393 | break; | ||
394 | } | ||
395 | } | ||
396 | } | ||
397 | } | ||
398 | |||
399 | // | ||
400 | // Mouse Other | ||
401 | // | ||
402 | - (void)otherMouseDown:(NSEvent *)event | ||
403 | { | ||
404 | if( dispatchEvents_ ) { | ||
405 | tListEntry *entry, *tmp; | ||
406 | |||
407 | DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) { | ||
408 | if ( entry->flags & kCCImplementsOtherMouseDown ) { | ||
409 | void *swallows = [entry->delegate performSelector:@selector(ccOtherMouseDown:) withObject:event]; | ||
410 | if( swallows ) | ||
411 | break; | ||
412 | } | ||
413 | } | ||
414 | } | ||
415 | } | ||
416 | |||
417 | - (void)otherMouseDragged:(NSEvent *)event | ||
418 | { | ||
419 | if( dispatchEvents_ ) { | ||
420 | tListEntry *entry, *tmp; | ||
421 | |||
422 | DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) { | ||
423 | if ( entry->flags & kCCImplementsOtherMouseDragged ) { | ||
424 | void *swallows = [entry->delegate performSelector:@selector(ccOtherMouseDragged:) withObject:event]; | ||
425 | if( swallows ) | ||
426 | break; | ||
427 | } | ||
428 | } | ||
429 | } | ||
430 | } | ||
431 | |||
432 | - (void)otherMouseUp:(NSEvent *)event | ||
433 | { | ||
434 | if( dispatchEvents_ ) { | ||
435 | tListEntry *entry, *tmp; | ||
436 | |||
437 | DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) { | ||
438 | if ( entry->flags & kCCImplementsOtherMouseUp ) { | ||
439 | void *swallows = [entry->delegate performSelector:@selector(ccOtherMouseUp:) withObject:event]; | ||
440 | if( swallows ) | ||
441 | break; | ||
442 | } | ||
443 | } | ||
444 | } | ||
445 | } | ||
446 | |||
447 | // | ||
448 | // Scroll Wheel | ||
449 | // | ||
450 | - (void)scrollWheel:(NSEvent *)event | ||
451 | { | ||
452 | if( dispatchEvents_ ) { | ||
453 | tListEntry *entry, *tmp; | ||
454 | |||
455 | DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) { | ||
456 | if ( entry->flags & kCCImplementsScrollWheel ) { | ||
457 | void *swallows = [entry->delegate performSelector:@selector(ccScrollWheel:) withObject:event]; | ||
458 | if( swallows ) | ||
459 | break; | ||
460 | } | ||
461 | } | ||
462 | } | ||
463 | } | ||
464 | |||
465 | // | ||
466 | // Mouse enter / exit | ||
467 | - (void)mouseExited:(NSEvent *)event | ||
468 | { | ||
469 | if( dispatchEvents_ ) { | ||
470 | tListEntry *entry, *tmp; | ||
471 | |||
472 | DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) { | ||
473 | if ( entry->flags & kCCImplementsMouseEntered ) { | ||
474 | void *swallows = [entry->delegate performSelector:@selector(ccMouseEntered:) withObject:event]; | ||
475 | if( swallows ) | ||
476 | break; | ||
477 | } | ||
478 | } | ||
479 | } | ||
480 | } | ||
481 | |||
482 | - (void)mouseEntered:(NSEvent *)event | ||
483 | { | ||
484 | if( dispatchEvents_ ) { | ||
485 | tListEntry *entry, *tmp; | ||
486 | |||
487 | DL_FOREACH_SAFE( mouseDelegates_, entry, tmp ) { | ||
488 | if ( entry->flags & kCCImplementsMouseExited) { | ||
489 | void *swallows = [entry->delegate performSelector:@selector(ccMouseExited:) withObject:event]; | ||
490 | if( swallows ) | ||
491 | break; | ||
492 | } | ||
493 | } | ||
494 | } | ||
495 | } | ||
496 | |||
497 | |||
498 | #pragma mark CCEventDispatcher - Keyboard events | ||
499 | |||
500 | // Keyboard events | ||
501 | - (void)keyDown:(NSEvent *)event | ||
502 | { | ||
503 | if( dispatchEvents_ ) { | ||
504 | tListEntry *entry, *tmp; | ||
505 | |||
506 | DL_FOREACH_SAFE( keyboardDelegates_, entry, tmp ) { | ||
507 | if ( entry->flags & kCCImplementsKeyDown ) { | ||
508 | void *swallows = [entry->delegate performSelector:@selector(ccKeyDown:) withObject:event]; | ||
509 | if( swallows ) | ||
510 | break; | ||
511 | } | ||
512 | } | ||
513 | } | ||
514 | } | ||
515 | |||
516 | - (void)keyUp:(NSEvent *)event | ||
517 | { | ||
518 | if( dispatchEvents_ ) { | ||
519 | tListEntry *entry, *tmp; | ||
520 | |||
521 | DL_FOREACH_SAFE( keyboardDelegates_, entry, tmp ) { | ||
522 | if ( entry->flags & kCCImplementsKeyUp ) { | ||
523 | void *swallows = [entry->delegate performSelector:@selector(ccKeyUp:) withObject:event]; | ||
524 | if( swallows ) | ||
525 | break; | ||
526 | } | ||
527 | } | ||
528 | } | ||
529 | } | ||
530 | |||
531 | - (void)flagsChanged:(NSEvent *)event | ||
532 | { | ||
533 | if( dispatchEvents_ ) { | ||
534 | tListEntry *entry, *tmp; | ||
535 | |||
536 | DL_FOREACH_SAFE( keyboardDelegates_, entry, tmp ) { | ||
537 | if ( entry->flags & kCCImplementsFlagsChanged ) { | ||
538 | void *swallows = [entry->delegate performSelector:@selector(ccFlagsChanged:) withObject:event]; | ||
539 | if( swallows ) | ||
540 | break; | ||
541 | } | ||
542 | } | ||
543 | } | ||
544 | } | ||
545 | |||
546 | |||
547 | #pragma mark CCEventDispatcher - Touch events | ||
548 | |||
549 | - (void)touchesBeganWithEvent:(NSEvent *)event | ||
550 | { | ||
551 | if( dispatchEvents_ ) { | ||
552 | tListEntry *entry, *tmp; | ||
553 | |||
554 | DL_FOREACH_SAFE( touchDelegates_, entry, tmp ) { | ||
555 | if ( entry->flags & kCCImplementsTouchesBegan) { | ||
556 | void *swallows = [entry->delegate performSelector:@selector(ccTouchesBeganWithEvent:) withObject:event]; | ||
557 | if( swallows ) | ||
558 | break; | ||
559 | } | ||
560 | } | ||
561 | } | ||
562 | } | ||
563 | |||
564 | - (void)touchesMovedWithEvent:(NSEvent *)event | ||
565 | { | ||
566 | if( dispatchEvents_ ) { | ||
567 | tListEntry *entry, *tmp; | ||
568 | |||
569 | DL_FOREACH_SAFE( touchDelegates_, entry, tmp ) { | ||
570 | if ( entry->flags & kCCImplementsTouchesMoved) { | ||
571 | void *swallows = [entry->delegate performSelector:@selector(ccTouchesMovedWithEvent:) withObject:event]; | ||
572 | if( swallows ) | ||
573 | break; | ||
574 | } | ||
575 | } | ||
576 | } | ||
577 | } | ||
578 | |||
579 | - (void)touchesEndedWithEvent:(NSEvent *)event | ||
580 | { | ||
581 | if( dispatchEvents_ ) { | ||
582 | tListEntry *entry, *tmp; | ||
583 | |||
584 | DL_FOREACH_SAFE( touchDelegates_, entry, tmp ) { | ||
585 | if ( entry->flags & kCCImplementsTouchesEnded) { | ||
586 | void *swallows = [entry->delegate performSelector:@selector(ccTouchesEndedWithEvent:) withObject:event]; | ||
587 | if( swallows ) | ||
588 | break; | ||
589 | } | ||
590 | } | ||
591 | } | ||
592 | } | ||
593 | |||
594 | - (void)touchesCancelledWithEvent:(NSEvent *)event | ||
595 | { | ||
596 | if( dispatchEvents_ ) { | ||
597 | tListEntry *entry, *tmp; | ||
598 | |||
599 | DL_FOREACH_SAFE( touchDelegates_, entry, tmp ) { | ||
600 | if ( entry->flags & kCCImplementsTouchesCancelled) { | ||
601 | void *swallows = [entry->delegate performSelector:@selector(ccTouchesCancelledWithEvent:) withObject:event]; | ||
602 | if( swallows ) | ||
603 | break; | ||
604 | } | ||
605 | } | ||
606 | } | ||
607 | } | ||
608 | |||
609 | |||
610 | #pragma mark CCEventDispatcher - queue events | ||
611 | |||
612 | #if CC_DIRECTOR_MAC_USE_DISPLAY_LINK_THREAD | ||
613 | -(void) queueEvent:(NSEvent*)event selector:(SEL)selector | ||
614 | { | ||
615 | NSAssert( eventQueueCount < QUEUE_EVENT_MAX, @"CCEventDispatcher: recompile. Increment QUEUE_EVENT_MAX value"); | ||
616 | |||
617 | @synchronized (self) { | ||
618 | eventQueue[eventQueueCount].selector = selector; | ||
619 | eventQueue[eventQueueCount].event = [event copy]; | ||
620 | |||
621 | eventQueueCount++; | ||
622 | } | ||
623 | } | ||
624 | |||
625 | -(void) dispatchQueuedEvents | ||
626 | { | ||
627 | @synchronized (self) { | ||
628 | for( int i=0; i < eventQueueCount; i++ ) { | ||
629 | SEL sel = eventQueue[i].selector; | ||
630 | NSEvent *event = eventQueue[i].event; | ||
631 | |||
632 | [self performSelector:sel withObject:event]; | ||
633 | |||
634 | [event release]; | ||
635 | } | ||
636 | |||
637 | eventQueueCount = 0; | ||
638 | } | ||
639 | } | ||
640 | #endif // CC_DIRECTOR_MAC_USE_DISPLAY_LINK_THREAD | ||
641 | |||
642 | |||
643 | @end | ||
644 | |||
645 | #endif // __MAC_OS_X_VERSION_MAX_ALLOWED | ||
diff --git a/libs/cocos2d/Platforms/Mac/MacGLView.h b/libs/cocos2d/Platforms/Mac/MacGLView.h new file mode 100755 index 0000000..8099273 --- /dev/null +++ b/libs/cocos2d/Platforms/Mac/MacGLView.h | |||
@@ -0,0 +1,89 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 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 | // Only compile this code on Mac. These files should not be included on your iOS project. | ||
27 | // But in case they are included, it won't be compiled. | ||
28 | #import <Availability.h> | ||
29 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
30 | #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) | ||
31 | |||
32 | #import <Cocoa/Cocoa.h> | ||
33 | |||
34 | #import "../../ccConfig.h" | ||
35 | |||
36 | //PROTOCOLS: | ||
37 | |||
38 | @protocol MacEventDelegate <NSObject> | ||
39 | // Mouse | ||
40 | - (void)mouseDown:(NSEvent *)theEvent; | ||
41 | - (void)mouseUp:(NSEvent *)theEvent; | ||
42 | - (void)mouseMoved:(NSEvent *)theEvent; | ||
43 | - (void)mouseDragged:(NSEvent *)theEvent; | ||
44 | - (void)rightMouseDown:(NSEvent*)event; | ||
45 | - (void)rightMouseDragged:(NSEvent*)event; | ||
46 | - (void)rightMouseUp:(NSEvent*)event; | ||
47 | - (void)otherMouseDown:(NSEvent*)event; | ||
48 | - (void)otherMouseDragged:(NSEvent*)event; | ||
49 | - (void)otherMouseUp:(NSEvent*)event; | ||
50 | - (void)scrollWheel:(NSEvent *)theEvent; | ||
51 | - (void)mouseEntered:(NSEvent *)theEvent; | ||
52 | - (void)mouseExited:(NSEvent *)theEvent; | ||
53 | |||
54 | |||
55 | // Keyboard | ||
56 | - (void)keyDown:(NSEvent *)theEvent; | ||
57 | - (void)keyUp:(NSEvent *)theEvent; | ||
58 | - (void)flagsChanged:(NSEvent *)theEvent; | ||
59 | |||
60 | // Touches | ||
61 | - (void)touchesBeganWithEvent:(NSEvent *)event; | ||
62 | - (void)touchesMovedWithEvent:(NSEvent *)event; | ||
63 | - (void)touchesEndedWithEvent:(NSEvent *)event; | ||
64 | - (void)touchesCancelledWithEvent:(NSEvent *)event; | ||
65 | |||
66 | #if CC_DIRECTOR_MAC_USE_DISPLAY_LINK_THREAD | ||
67 | - (void)queueEvent:(NSEvent*)event selector:(SEL)selector; | ||
68 | #endif | ||
69 | |||
70 | @end | ||
71 | |||
72 | /** MacGLView | ||
73 | |||
74 | Only available for Mac OS X | ||
75 | */ | ||
76 | @interface MacGLView : NSOpenGLView { | ||
77 | id<MacEventDelegate> eventDelegate_; | ||
78 | } | ||
79 | |||
80 | @property (nonatomic, readwrite, assign) id<MacEventDelegate> eventDelegate; | ||
81 | |||
82 | // initializes the MacGLView with a frame rect and an OpenGL context | ||
83 | - (id) initWithFrame:(NSRect)frameRect shareContext:(NSOpenGLContext*)context; | ||
84 | |||
85 | // private | ||
86 | +(void) load_; | ||
87 | @end | ||
88 | |||
89 | #endif // __MAC_OS_X_VERSION_MAX_ALLOWED \ No newline at end of file | ||
diff --git a/libs/cocos2d/Platforms/Mac/MacGLView.m b/libs/cocos2d/Platforms/Mac/MacGLView.m new file mode 100755 index 0000000..a041dc8 --- /dev/null +++ b/libs/cocos2d/Platforms/Mac/MacGLView.m | |||
@@ -0,0 +1,242 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 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 | * Idea of subclassing NSOpenGLView was taken from "TextureUpload" Apple's sample | ||
28 | */ | ||
29 | |||
30 | // Only compile this code on Mac. These files should not be included on your iOS project. | ||
31 | // But in case they are included, it won't be compiled. | ||
32 | #import <Availability.h> | ||
33 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
34 | #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) | ||
35 | |||
36 | #import "MacGLView.h" | ||
37 | #import <OpenGL/gl.h> | ||
38 | |||
39 | #import "CCDirectorMac.h" | ||
40 | #import "../../ccConfig.h" | ||
41 | |||
42 | |||
43 | @implementation MacGLView | ||
44 | |||
45 | @synthesize eventDelegate = eventDelegate_; | ||
46 | |||
47 | +(void) load_ | ||
48 | { | ||
49 | NSLog(@"%@ loaded", self); | ||
50 | } | ||
51 | |||
52 | - (id) initWithFrame:(NSRect)frameRect | ||
53 | { | ||
54 | self = [self initWithFrame:frameRect shareContext:nil]; | ||
55 | return self; | ||
56 | } | ||
57 | |||
58 | - (id) initWithFrame:(NSRect)frameRect shareContext:(NSOpenGLContext*)context | ||
59 | { | ||
60 | NSOpenGLPixelFormatAttribute attribs[] = | ||
61 | { | ||
62 | NSOpenGLPFAAccelerated, | ||
63 | NSOpenGLPFANoRecovery, | ||
64 | NSOpenGLPFADoubleBuffer, | ||
65 | NSOpenGLPFADepthSize, 24, | ||
66 | |||
67 | 0 | ||
68 | }; | ||
69 | |||
70 | NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs]; | ||
71 | |||
72 | if (!pixelFormat) | ||
73 | NSLog(@"No OpenGL pixel format"); | ||
74 | |||
75 | if( (self = [super initWithFrame:frameRect pixelFormat:[pixelFormat autorelease]]) ) { | ||
76 | |||
77 | if( context ) | ||
78 | [self setOpenGLContext:context]; | ||
79 | |||
80 | // Synchronize buffer swaps with vertical refresh rate | ||
81 | GLint swapInt = 1; | ||
82 | [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; | ||
83 | |||
84 | // GLint order = -1; | ||
85 | // [[self openGLContext] setValues:&order forParameter:NSOpenGLCPSurfaceOrder]; | ||
86 | |||
87 | // event delegate | ||
88 | eventDelegate_ = nil; | ||
89 | } | ||
90 | |||
91 | return self; | ||
92 | } | ||
93 | |||
94 | - (void) reshape | ||
95 | { | ||
96 | // We draw on a secondary thread through the display link | ||
97 | // When resizing the view, -reshape is called automatically on the main thread | ||
98 | // Add a mutex around to avoid the threads accessing the context simultaneously when resizing | ||
99 | CGLLockContext([[self openGLContext] CGLContextObj]); | ||
100 | |||
101 | NSRect rect = [self bounds]; | ||
102 | |||
103 | CCDirector *director = [CCDirector sharedDirector]; | ||
104 | [director reshapeProjection: NSSizeToCGSize(rect.size) ]; | ||
105 | |||
106 | // avoid flicker | ||
107 | [director drawScene]; | ||
108 | // [self setNeedsDisplay:YES]; | ||
109 | |||
110 | CGLUnlockContext([[self openGLContext] CGLContextObj]); | ||
111 | } | ||
112 | |||
113 | - (void) dealloc | ||
114 | { | ||
115 | |||
116 | [super dealloc]; | ||
117 | } | ||
118 | |||
119 | #if CC_DIRECTOR_MAC_USE_DISPLAY_LINK_THREAD | ||
120 | #define DISPATCH_EVENT(__event__, __selector__) [eventDelegate_ queueEvent:__event__ selector:__selector__]; | ||
121 | #else | ||
122 | #define DISPATCH_EVENT(__event__, __selector__) \ | ||
123 | id obj = eventDelegate_; \ | ||
124 | [obj performSelector:__selector__ \ | ||
125 | onThread:[(CCDirectorMac*)[CCDirector sharedDirector] runningThread] \ | ||
126 | withObject:__event__ \ | ||
127 | waitUntilDone:NO]; | ||
128 | #endif | ||
129 | |||
130 | #pragma mark MacGLView - Mouse events | ||
131 | - (void)mouseDown:(NSEvent *)theEvent | ||
132 | { | ||
133 | DISPATCH_EVENT(theEvent, _cmd); | ||
134 | } | ||
135 | |||
136 | - (void)mouseMoved:(NSEvent *)theEvent | ||
137 | { | ||
138 | DISPATCH_EVENT(theEvent, _cmd); | ||
139 | } | ||
140 | |||
141 | - (void)mouseDragged:(NSEvent *)theEvent | ||
142 | { | ||
143 | DISPATCH_EVENT(theEvent, _cmd); | ||
144 | } | ||
145 | |||
146 | - (void)mouseUp:(NSEvent *)theEvent | ||
147 | { | ||
148 | DISPATCH_EVENT(theEvent, _cmd); | ||
149 | } | ||
150 | |||
151 | - (void)rightMouseDown:(NSEvent *)theEvent { | ||
152 | DISPATCH_EVENT(theEvent, _cmd); | ||
153 | } | ||
154 | |||
155 | - (void)rightMouseDragged:(NSEvent *)theEvent { | ||
156 | DISPATCH_EVENT(theEvent, _cmd); | ||
157 | } | ||
158 | |||
159 | - (void)rightMouseUp:(NSEvent *)theEvent { | ||
160 | DISPATCH_EVENT(theEvent, _cmd); | ||
161 | } | ||
162 | |||
163 | - (void)otherMouseDown:(NSEvent *)theEvent { | ||
164 | DISPATCH_EVENT(theEvent, _cmd); | ||
165 | } | ||
166 | |||
167 | - (void)otherMouseDragged:(NSEvent *)theEvent { | ||
168 | DISPATCH_EVENT(theEvent, _cmd); | ||
169 | } | ||
170 | |||
171 | - (void)otherMouseUp:(NSEvent *)theEvent { | ||
172 | DISPATCH_EVENT(theEvent, _cmd); | ||
173 | } | ||
174 | |||
175 | - (void)mouseEntered:(NSEvent *)theEvent { | ||
176 | DISPATCH_EVENT(theEvent, _cmd); | ||
177 | } | ||
178 | |||
179 | - (void)mouseExited:(NSEvent *)theEvent { | ||
180 | DISPATCH_EVENT(theEvent, _cmd); | ||
181 | } | ||
182 | |||
183 | -(void) scrollWheel:(NSEvent *)theEvent { | ||
184 | DISPATCH_EVENT(theEvent, _cmd); | ||
185 | } | ||
186 | |||
187 | #pragma mark MacGLView - Key events | ||
188 | |||
189 | -(BOOL) becomeFirstResponder | ||
190 | { | ||
191 | return YES; | ||
192 | } | ||
193 | |||
194 | -(BOOL) acceptsFirstResponder | ||
195 | { | ||
196 | return YES; | ||
197 | } | ||
198 | |||
199 | -(BOOL) resignFirstResponder | ||
200 | { | ||
201 | return YES; | ||
202 | } | ||
203 | |||
204 | - (void)keyDown:(NSEvent *)theEvent | ||
205 | { | ||
206 | DISPATCH_EVENT(theEvent, _cmd); | ||
207 | } | ||
208 | |||
209 | - (void)keyUp:(NSEvent *)theEvent | ||
210 | { | ||
211 | DISPATCH_EVENT(theEvent, _cmd); | ||
212 | } | ||
213 | |||
214 | - (void)flagsChanged:(NSEvent *)theEvent | ||
215 | { | ||
216 | DISPATCH_EVENT(theEvent, _cmd); | ||
217 | } | ||
218 | |||
219 | #pragma mark MacGLView - Touch events | ||
220 | - (void)touchesBeganWithEvent:(NSEvent *)theEvent | ||
221 | { | ||
222 | DISPATCH_EVENT(theEvent, _cmd); | ||
223 | } | ||
224 | |||
225 | - (void)touchesMovedWithEvent:(NSEvent *)theEvent | ||
226 | { | ||
227 | DISPATCH_EVENT(theEvent, _cmd); | ||
228 | } | ||
229 | |||
230 | - (void)touchesEndedWithEvent:(NSEvent *)theEvent | ||
231 | { | ||
232 | DISPATCH_EVENT(theEvent, _cmd); | ||
233 | } | ||
234 | |||
235 | - (void)touchesCancelledWithEvent:(NSEvent *)theEvent | ||
236 | { | ||
237 | DISPATCH_EVENT(theEvent, _cmd); | ||
238 | } | ||
239 | |||
240 | @end | ||
241 | |||
242 | #endif // __MAC_OS_X_VERSION_MAX_ALLOWED | ||
diff --git a/libs/cocos2d/Platforms/Mac/MacWindow.h b/libs/cocos2d/Platforms/Mac/MacWindow.h new file mode 100755 index 0000000..716fe9b --- /dev/null +++ b/libs/cocos2d/Platforms/Mac/MacWindow.h | |||
@@ -0,0 +1,42 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 2010 Ricardo Quesada | ||
5 | * | ||
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
7 | * of this software and associated documentation files (the "Software"), to deal | ||
8 | * in the Software without restriction, including without limitation the rights | ||
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
10 | * copies of the Software, and to permit persons to whom the Software is | ||
11 | * furnished to do so, subject to the following conditions: | ||
12 | * | ||
13 | * The above copyright notice and this permission notice shall be included in | ||
14 | * all copies or substantial portions of the Software. | ||
15 | * | ||
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
22 | * THE SOFTWARE. | ||
23 | */ | ||
24 | |||
25 | // Only compile this code on Mac. These files should not be included on your iOS project. | ||
26 | // But in case they are included, it won't be compiled. | ||
27 | #import <Availability.h> | ||
28 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
29 | #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) | ||
30 | |||
31 | #import <Cocoa/Cocoa.h> | ||
32 | |||
33 | |||
34 | @interface MacWindow : NSWindow | ||
35 | { | ||
36 | } | ||
37 | - (id) initWithFrame:(NSRect)frame fullscreen:(BOOL)fullscreen; | ||
38 | |||
39 | @end | ||
40 | |||
41 | |||
42 | #endif // __MAC_OS_X_VERSION_MAX_ALLOWED \ No newline at end of file | ||
diff --git a/libs/cocos2d/Platforms/Mac/MacWindow.m b/libs/cocos2d/Platforms/Mac/MacWindow.m new file mode 100755 index 0000000..28736a3 --- /dev/null +++ b/libs/cocos2d/Platforms/Mac/MacWindow.m | |||
@@ -0,0 +1,70 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 2010 Ricardo Quesada | ||
5 | * | ||
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
7 | * of this software and associated documentation files (the "Software"), to deal | ||
8 | * in the Software without restriction, including without limitation the rights | ||
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
10 | * copies of the Software, and to permit persons to whom the Software is | ||
11 | * furnished to do so, subject to the following conditions: | ||
12 | * | ||
13 | * The above copyright notice and this permission notice shall be included in | ||
14 | * all copies or substantial portions of the Software. | ||
15 | * | ||
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
22 | * THE SOFTWARE. | ||
23 | */ | ||
24 | |||
25 | // Only compile this code on Mac. These files should not be included on your iOS project. | ||
26 | // But in case they are included, it won't be compiled. | ||
27 | #import <Availability.h> | ||
28 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
29 | #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) | ||
30 | |||
31 | #import "MacWindow.h" | ||
32 | |||
33 | |||
34 | @implementation MacWindow | ||
35 | |||
36 | - (id) initWithFrame:(NSRect)frame fullscreen:(BOOL)fullscreen | ||
37 | { | ||
38 | int styleMask = fullscreen ? NSBackingStoreBuffered : ( NSTitledWindowMask | NSClosableWindowMask ); | ||
39 | self = [self initWithContentRect:frame | ||
40 | styleMask:styleMask | ||
41 | backing:NSBackingStoreBuffered | ||
42 | defer:YES]; | ||
43 | |||
44 | if (self != nil) | ||
45 | { | ||
46 | if(fullscreen) | ||
47 | { | ||
48 | [self setLevel:NSMainMenuWindowLevel+1]; | ||
49 | [self setHidesOnDeactivate:YES]; | ||
50 | [self setHasShadow:NO]; | ||
51 | } | ||
52 | |||
53 | [self setAcceptsMouseMovedEvents:NO]; | ||
54 | [self setOpaque:YES]; | ||
55 | } | ||
56 | return self; | ||
57 | } | ||
58 | |||
59 | - (BOOL) canBecomeKeyWindow | ||
60 | { | ||
61 | return YES; | ||
62 | } | ||
63 | |||
64 | - (BOOL) canBecomeMainWindow | ||
65 | { | ||
66 | return YES; | ||
67 | } | ||
68 | @end | ||
69 | |||
70 | #endif // __MAC_OS_X_VERSION_MAX_ALLOWED | ||
diff --git a/libs/cocos2d/Platforms/iOS/CCDirectorIOS.h b/libs/cocos2d/Platforms/iOS/CCDirectorIOS.h new file mode 100755 index 0000000..1c264e4 --- /dev/null +++ b/libs/cocos2d/Platforms/iOS/CCDirectorIOS.h | |||
@@ -0,0 +1,255 @@ | |||
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 | // Only compile this code on iOS. These files should NOT be included on your Mac project. | ||
28 | // But in case they are included, it won't be compiled. | ||
29 | #import <Availability.h> | ||
30 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
31 | |||
32 | #import "../../CCDirector.h" | ||
33 | |||
34 | /** @typedef ccDeviceOrientation | ||
35 | Possible device orientations | ||
36 | */ | ||
37 | typedef enum { | ||
38 | /// Device oriented vertically, home button on the bottom | ||
39 | kCCDeviceOrientationPortrait = UIDeviceOrientationPortrait, | ||
40 | /// Device oriented vertically, home button on the top | ||
41 | kCCDeviceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, | ||
42 | /// Device oriented horizontally, home button on the right | ||
43 | kCCDeviceOrientationLandscapeLeft = UIDeviceOrientationLandscapeLeft, | ||
44 | /// Device oriented horizontally, home button on the left | ||
45 | kCCDeviceOrientationLandscapeRight = UIDeviceOrientationLandscapeRight, | ||
46 | |||
47 | // Backward compatibility stuff | ||
48 | CCDeviceOrientationPortrait = kCCDeviceOrientationPortrait, | ||
49 | CCDeviceOrientationPortraitUpsideDown = kCCDeviceOrientationPortraitUpsideDown, | ||
50 | CCDeviceOrientationLandscapeLeft = kCCDeviceOrientationLandscapeLeft, | ||
51 | CCDeviceOrientationLandscapeRight = kCCDeviceOrientationLandscapeRight, | ||
52 | } ccDeviceOrientation; | ||
53 | |||
54 | /** @typedef ccDirectorType | ||
55 | Possible Director Types. | ||
56 | @since v0.8.2 | ||
57 | */ | ||
58 | typedef enum { | ||
59 | /** Will use a Director that triggers the main loop from an NSTimer object | ||
60 | * | ||
61 | * Features and Limitations: | ||
62 | * - Integrates OK with UIKit objects | ||
63 | * - It the slowest director | ||
64 | * - The invertal update is customizable from 1 to 60 | ||
65 | */ | ||
66 | kCCDirectorTypeNSTimer, | ||
67 | |||
68 | /** will use a Director that triggers the main loop from a custom main loop. | ||
69 | * | ||
70 | * Features and Limitations: | ||
71 | * - Faster than NSTimer Director | ||
72 | * - It doesn't integrate well with UIKit objecgts | ||
73 | * - The interval update can't be customizable | ||
74 | */ | ||
75 | kCCDirectorTypeMainLoop, | ||
76 | |||
77 | /** Will use a Director that triggers the main loop from a thread, but the main loop will be executed on the main thread. | ||
78 | * | ||
79 | * Features and Limitations: | ||
80 | * - Faster than NSTimer Director | ||
81 | * - It doesn't integrate well with UIKit objecgts | ||
82 | * - The interval update can't be customizable | ||
83 | */ | ||
84 | kCCDirectorTypeThreadMainLoop, | ||
85 | |||
86 | /** Will use a Director that synchronizes timers with the refresh rate of the display. | ||
87 | * | ||
88 | * Features and Limitations: | ||
89 | * - Faster than NSTimer Director | ||
90 | * - Only available on 3.1+ | ||
91 | * - Scheduled timers & drawing are synchronizes with the refresh rate of the display | ||
92 | * - Integrates OK with UIKit objects | ||
93 | * - The interval update can be 1/60, 1/30, 1/15 | ||
94 | */ | ||
95 | kCCDirectorTypeDisplayLink, | ||
96 | |||
97 | /** Default director is the NSTimer directory */ | ||
98 | kCCDirectorTypeDefault = kCCDirectorTypeNSTimer, | ||
99 | |||
100 | // backward compatibility stuff | ||
101 | CCDirectorTypeNSTimer = kCCDirectorTypeNSTimer, | ||
102 | CCDirectorTypeMainLoop = kCCDirectorTypeMainLoop, | ||
103 | CCDirectorTypeThreadMainLoop = kCCDirectorTypeThreadMainLoop, | ||
104 | CCDirectorTypeDisplayLink = kCCDirectorTypeDisplayLink, | ||
105 | CCDirectorTypeDefault = kCCDirectorTypeDefault, | ||
106 | |||
107 | |||
108 | } ccDirectorType; | ||
109 | |||
110 | /** CCDirector extensions for iPhone | ||
111 | */ | ||
112 | @interface CCDirector (iOSExtension) | ||
113 | |||
114 | // rotates the screen if an orientation differnent than Portrait is used | ||
115 | -(void) applyOrientation; | ||
116 | |||
117 | /** Sets the device orientation. | ||
118 | If the orientation is going to be controlled by an UIViewController, then the orientation should be Portrait | ||
119 | */ | ||
120 | -(void) setDeviceOrientation:(ccDeviceOrientation)orientation; | ||
121 | |||
122 | /** returns the device orientation */ | ||
123 | -(ccDeviceOrientation) deviceOrientation; | ||
124 | |||
125 | /** The size in pixels of the surface. It could be different than the screen size. | ||
126 | High-res devices might have a higher surface size than the screen size. | ||
127 | In non High-res device the contentScale will be emulated. | ||
128 | |||
129 | The recommend way to enable Retina Display is by using the "enableRetinaDisplay:(BOOL)enabled" method. | ||
130 | |||
131 | @since v0.99.4 | ||
132 | */ | ||
133 | -(void) setContentScaleFactor:(CGFloat)scaleFactor; | ||
134 | |||
135 | /** Will enable Retina Display on devices that supports it. | ||
136 | It will enable Retina Display on iPhone4 and iPod Touch 4. | ||
137 | It will return YES, if it could enabled it, otherwise it will return NO. | ||
138 | |||
139 | This is the recommened way to enable Retina Display. | ||
140 | @since v0.99.5 | ||
141 | */ | ||
142 | -(BOOL) enableRetinaDisplay:(BOOL)yes; | ||
143 | |||
144 | |||
145 | /** returns the content scale factor */ | ||
146 | -(CGFloat) contentScaleFactor; | ||
147 | @end | ||
148 | |||
149 | @interface CCDirector (iOSExtensionClassMethods) | ||
150 | |||
151 | /** There are 4 types of Director. | ||
152 | - kCCDirectorTypeNSTimer (default) | ||
153 | - kCCDirectorTypeMainLoop | ||
154 | - kCCDirectorTypeThreadMainLoop | ||
155 | - kCCDirectorTypeDisplayLink | ||
156 | |||
157 | Each Director has it's own benefits, limitations. | ||
158 | If you are using SDK 3.1 or newer it is recommed to use the DisplayLink director | ||
159 | |||
160 | This method should be called before any other call to the director. | ||
161 | |||
162 | It will return NO if the director type is kCCDirectorTypeDisplayLink and the running SDK is < 3.1. Otherwise it will return YES. | ||
163 | |||
164 | @since v0.8.2 | ||
165 | */ | ||
166 | +(BOOL) setDirectorType:(ccDirectorType) directorType; | ||
167 | @end | ||
168 | |||
169 | #pragma mark - | ||
170 | #pragma mark CCDirectorIOS | ||
171 | |||
172 | /** CCDirectorIOS: Base class of iOS directors | ||
173 | @since v0.99.5 | ||
174 | */ | ||
175 | @interface CCDirectorIOS : CCDirector | ||
176 | { | ||
177 | /* orientation */ | ||
178 | ccDeviceOrientation deviceOrientation_; | ||
179 | |||
180 | /* contentScaleFactor could be simulated */ | ||
181 | BOOL isContentScaleSupported_; | ||
182 | |||
183 | } | ||
184 | @end | ||
185 | |||
186 | /** FastDirector is a Director that triggers the main loop as fast as possible. | ||
187 | * | ||
188 | * Features and Limitations: | ||
189 | * - Faster than "normal" director | ||
190 | * - Consumes more battery than the "normal" director | ||
191 | * - It has some issues while using UIKit objects | ||
192 | */ | ||
193 | @interface CCDirectorFast : CCDirectorIOS | ||
194 | { | ||
195 | BOOL isRunning; | ||
196 | |||
197 | NSAutoreleasePool *autoreleasePool; | ||
198 | } | ||
199 | -(void) mainLoop; | ||
200 | @end | ||
201 | |||
202 | /** ThreadedFastDirector is a Director that triggers the main loop from a thread. | ||
203 | * | ||
204 | * Features and Limitations: | ||
205 | * - Faster than "normal" director | ||
206 | * - Consumes more battery than the "normal" director | ||
207 | * - It can be used with UIKit objects | ||
208 | * | ||
209 | * @since v0.8.2 | ||
210 | */ | ||
211 | @interface CCDirectorFastThreaded : CCDirectorIOS | ||
212 | { | ||
213 | BOOL isRunning; | ||
214 | } | ||
215 | -(void) mainLoop; | ||
216 | @end | ||
217 | |||
218 | /** DisplayLinkDirector is a Director that synchronizes timers with the refresh rate of the display. | ||
219 | * | ||
220 | * Features and Limitations: | ||
221 | * - Only available on 3.1+ | ||
222 | * - Scheduled timers & drawing are synchronizes with the refresh rate of the display | ||
223 | * - Only supports animation intervals of 1/60 1/30 & 1/15 | ||
224 | * | ||
225 | * It is the recommended Director if the SDK is 3.1 or newer | ||
226 | * | ||
227 | * @since v0.8.2 | ||
228 | */ | ||
229 | @interface CCDirectorDisplayLink : CCDirectorIOS | ||
230 | { | ||
231 | id displayLink; | ||
232 | } | ||
233 | -(void) mainLoop:(id)sender; | ||
234 | @end | ||
235 | |||
236 | /** TimerDirector is a Director that calls the main loop from an NSTimer object | ||
237 | * | ||
238 | * Features and Limitations: | ||
239 | * - Integrates OK with UIKit objects | ||
240 | * - It the slowest director | ||
241 | * - The invertal update is customizable from 1 to 60 | ||
242 | * | ||
243 | * It is the default Director. | ||
244 | */ | ||
245 | @interface CCDirectorTimer : CCDirectorIOS | ||
246 | { | ||
247 | NSTimer *animationTimer; | ||
248 | } | ||
249 | -(void) mainLoop; | ||
250 | @end | ||
251 | |||
252 | // optimization. Should only be used to read it. Never to write it. | ||
253 | extern CGFloat __ccContentScaleFactor; | ||
254 | |||
255 | #endif // __IPHONE_OS_VERSION_MAX_ALLOWED | ||
diff --git a/libs/cocos2d/Platforms/iOS/CCDirectorIOS.m b/libs/cocos2d/Platforms/iOS/CCDirectorIOS.m new file mode 100755 index 0000000..c483665 --- /dev/null +++ b/libs/cocos2d/Platforms/iOS/CCDirectorIOS.m | |||
@@ -0,0 +1,735 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 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 | // Only compile this code on iOS. These files should NOT be included on your Mac project. | ||
28 | // But in case they are included, it won't be compiled. | ||
29 | #import <Availability.h> | ||
30 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
31 | |||
32 | #import <unistd.h> | ||
33 | |||
34 | // cocos2d imports | ||
35 | #import "CCDirectorIOS.h" | ||
36 | #import "CCTouchDelegateProtocol.h" | ||
37 | #import "CCTouchDispatcher.h" | ||
38 | #import "../../CCScheduler.h" | ||
39 | #import "../../CCActionManager.h" | ||
40 | #import "../../CCTextureCache.h" | ||
41 | #import "../../ccMacros.h" | ||
42 | #import "../../CCScene.h" | ||
43 | |||
44 | // support imports | ||
45 | #import "glu.h" | ||
46 | #import "../../Support/OpenGL_Internal.h" | ||
47 | #import "../../Support/CGPointExtension.h" | ||
48 | |||
49 | #import "CCLayer.h" | ||
50 | |||
51 | #if CC_ENABLE_PROFILERS | ||
52 | #import "../../Support/CCProfiling.h" | ||
53 | #endif | ||
54 | |||
55 | |||
56 | #pragma mark - | ||
57 | #pragma mark Director - global variables (optimization) | ||
58 | |||
59 | CGFloat __ccContentScaleFactor = 1; | ||
60 | |||
61 | #pragma mark - | ||
62 | #pragma mark Director iOS | ||
63 | |||
64 | @interface CCDirector () | ||
65 | -(void) setNextScene; | ||
66 | -(void) showFPS; | ||
67 | -(void) calculateDeltaTime; | ||
68 | @end | ||
69 | |||
70 | @implementation CCDirector (iOSExtensionClassMethods) | ||
71 | |||
72 | +(Class) defaultDirector | ||
73 | { | ||
74 | return [CCDirectorTimer class]; | ||
75 | } | ||
76 | |||
77 | + (BOOL) setDirectorType:(ccDirectorType)type | ||
78 | { | ||
79 | if( type == CCDirectorTypeDisplayLink ) { | ||
80 | NSString *reqSysVer = @"3.1"; | ||
81 | NSString *currSysVer = [[UIDevice currentDevice] systemVersion]; | ||
82 | |||
83 | if([currSysVer compare:reqSysVer options:NSNumericSearch] == NSOrderedAscending) | ||
84 | return NO; | ||
85 | } | ||
86 | switch (type) { | ||
87 | case CCDirectorTypeNSTimer: | ||
88 | [CCDirectorTimer sharedDirector]; | ||
89 | break; | ||
90 | case CCDirectorTypeDisplayLink: | ||
91 | [CCDirectorDisplayLink sharedDirector]; | ||
92 | break; | ||
93 | case CCDirectorTypeMainLoop: | ||
94 | [CCDirectorFast sharedDirector]; | ||
95 | break; | ||
96 | case CCDirectorTypeThreadMainLoop: | ||
97 | [CCDirectorFastThreaded sharedDirector]; | ||
98 | break; | ||
99 | default: | ||
100 | NSAssert(NO,@"Unknown director type"); | ||
101 | } | ||
102 | |||
103 | return YES; | ||
104 | } | ||
105 | |||
106 | @end | ||
107 | |||
108 | |||
109 | |||
110 | #pragma mark - | ||
111 | #pragma mark CCDirectorIOS | ||
112 | |||
113 | @interface CCDirectorIOS () | ||
114 | -(void) updateContentScaleFactor; | ||
115 | |||
116 | @end | ||
117 | |||
118 | @implementation CCDirectorIOS | ||
119 | |||
120 | - (id) init | ||
121 | { | ||
122 | if( (self=[super init]) ) { | ||
123 | |||
124 | // portrait mode default | ||
125 | deviceOrientation_ = CCDeviceOrientationPortrait; | ||
126 | |||
127 | __ccContentScaleFactor = 1; | ||
128 | isContentScaleSupported_ = NO; | ||
129 | |||
130 | // running thread is main thread on iOS | ||
131 | runningThread_ = [NSThread currentThread]; | ||
132 | } | ||
133 | |||
134 | return self; | ||
135 | } | ||
136 | |||
137 | - (void) dealloc | ||
138 | { | ||
139 | [super dealloc]; | ||
140 | } | ||
141 | |||
142 | // | ||
143 | // Draw the Scene | ||
144 | // | ||
145 | - (void) drawScene | ||
146 | { | ||
147 | /* calculate "global" dt */ | ||
148 | [self calculateDeltaTime]; | ||
149 | |||
150 | /* tick before glClear: issue #533 */ | ||
151 | if( ! isPaused_ ) { | ||
152 | [[CCScheduler sharedScheduler] tick: dt]; | ||
153 | } | ||
154 | |||
155 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | ||
156 | |||
157 | /* to avoid flickr, nextScene MUST be here: after tick and before draw. | ||
158 | XXX: Which bug is this one. It seems that it can't be reproduced with v0.9 */ | ||
159 | if( nextScene_ ) | ||
160 | [self setNextScene]; | ||
161 | |||
162 | glPushMatrix(); | ||
163 | |||
164 | [self applyOrientation]; | ||
165 | |||
166 | // By default enable VertexArray, ColorArray, TextureCoordArray and Texture2D | ||
167 | CC_ENABLE_DEFAULT_GL_STATES(); | ||
168 | |||
169 | /* draw the scene */ | ||
170 | [runningScene_ visit]; | ||
171 | |||
172 | /* draw the notification node */ | ||
173 | [notificationNode_ visit]; | ||
174 | |||
175 | if( displayFPS_ ) | ||
176 | [self showFPS]; | ||
177 | |||
178 | #if CC_ENABLE_PROFILERS | ||
179 | [self showProfilers]; | ||
180 | #endif | ||
181 | |||
182 | CC_DISABLE_DEFAULT_GL_STATES(); | ||
183 | |||
184 | glPopMatrix(); | ||
185 | |||
186 | [openGLView_ swapBuffers]; | ||
187 | } | ||
188 | |||
189 | -(void) setProjection:(ccDirectorProjection)projection | ||
190 | { | ||
191 | CGSize size = winSizeInPixels_; | ||
192 | |||
193 | switch (projection) { | ||
194 | case kCCDirectorProjection2D: | ||
195 | glViewport(0, 0, size.width, size.height); | ||
196 | glMatrixMode(GL_PROJECTION); | ||
197 | glLoadIdentity(); | ||
198 | ccglOrtho(0, size.width, 0, size.height, -1024 * CC_CONTENT_SCALE_FACTOR(), 1024 * CC_CONTENT_SCALE_FACTOR()); | ||
199 | glMatrixMode(GL_MODELVIEW); | ||
200 | glLoadIdentity(); | ||
201 | break; | ||
202 | |||
203 | case kCCDirectorProjection3D: | ||
204 | { | ||
205 | float zeye = [self getZEye]; | ||
206 | |||
207 | glViewport(0, 0, size.width, size.height); | ||
208 | glMatrixMode(GL_PROJECTION); | ||
209 | glLoadIdentity(); | ||
210 | // gluPerspective(60, (GLfloat)size.width/size.height, zeye-size.height/2, zeye+size.height/2 ); | ||
211 | gluPerspective(60, (GLfloat)size.width/size.height, 0.5f, 1500); | ||
212 | |||
213 | glMatrixMode(GL_MODELVIEW); | ||
214 | glLoadIdentity(); | ||
215 | gluLookAt( size.width/2, size.height/2, zeye, | ||
216 | size.width/2, size.height/2, 0, | ||
217 | 0.0f, 1.0f, 0.0f); | ||
218 | break; | ||
219 | } | ||
220 | |||
221 | case kCCDirectorProjectionCustom: | ||
222 | if( projectionDelegate_ ) | ||
223 | [projectionDelegate_ updateProjection]; | ||
224 | break; | ||
225 | |||
226 | default: | ||
227 | CCLOG(@"cocos2d: Director: unrecognized projecgtion"); | ||
228 | break; | ||
229 | } | ||
230 | |||
231 | projection_ = projection; | ||
232 | } | ||
233 | |||
234 | #pragma mark Director Integration with a UIKit view | ||
235 | |||
236 | -(void) setOpenGLView:(EAGLView *)view | ||
237 | { | ||
238 | if( view != openGLView_ ) { | ||
239 | |||
240 | [super setOpenGLView:view]; | ||
241 | |||
242 | // set size | ||
243 | winSizeInPixels_ = CGSizeMake(winSizeInPoints_.width * __ccContentScaleFactor, winSizeInPoints_.height *__ccContentScaleFactor); | ||
244 | |||
245 | if( __ccContentScaleFactor != 1 ) | ||
246 | [self updateContentScaleFactor]; | ||
247 | |||
248 | CCTouchDispatcher *touchDispatcher = [CCTouchDispatcher sharedDispatcher]; | ||
249 | [openGLView_ setTouchDelegate: touchDispatcher]; | ||
250 | [touchDispatcher setDispatchEvents: YES]; | ||
251 | } | ||
252 | } | ||
253 | |||
254 | #pragma mark Director - Retina Display | ||
255 | |||
256 | -(CGFloat) contentScaleFactor | ||
257 | { | ||
258 | return __ccContentScaleFactor; | ||
259 | } | ||
260 | |||
261 | -(void) setContentScaleFactor:(CGFloat)scaleFactor | ||
262 | { | ||
263 | if( scaleFactor != __ccContentScaleFactor ) { | ||
264 | |||
265 | __ccContentScaleFactor = scaleFactor; | ||
266 | winSizeInPixels_ = CGSizeMake( winSizeInPoints_.width * scaleFactor, winSizeInPoints_.height * scaleFactor ); | ||
267 | |||
268 | if( openGLView_ ) | ||
269 | [self updateContentScaleFactor]; | ||
270 | |||
271 | // update projection | ||
272 | [self setProjection:projection_]; | ||
273 | } | ||
274 | } | ||
275 | |||
276 | -(void) updateContentScaleFactor | ||
277 | { | ||
278 | // Based on code snippet from: http://developer.apple.com/iphone/prerelease/library/snippets/sp2010/sp28.html | ||
279 | if ([openGLView_ respondsToSelector:@selector(setContentScaleFactor:)]) | ||
280 | { | ||
281 | [openGLView_ setContentScaleFactor: __ccContentScaleFactor]; | ||
282 | |||
283 | isContentScaleSupported_ = YES; | ||
284 | } | ||
285 | else | ||
286 | CCLOG(@"cocos2d: 'setContentScaleFactor:' is not supported on this device"); | ||
287 | } | ||
288 | |||
289 | -(BOOL) enableRetinaDisplay:(BOOL)enabled | ||
290 | { | ||
291 | // Already enabled ? | ||
292 | if( enabled && __ccContentScaleFactor == 2 ) | ||
293 | return YES; | ||
294 | |||
295 | // Already disabled | ||
296 | if( ! enabled && __ccContentScaleFactor == 1 ) | ||
297 | return YES; | ||
298 | |||
299 | // setContentScaleFactor is not supported | ||
300 | if (! [openGLView_ respondsToSelector:@selector(setContentScaleFactor:)]) | ||
301 | return NO; | ||
302 | |||
303 | // SD device | ||
304 | if ([[UIScreen mainScreen] scale] == 1.0) | ||
305 | return NO; | ||
306 | |||
307 | float newScale = enabled ? 2 : 1; | ||
308 | [self setContentScaleFactor:newScale]; | ||
309 | |||
310 | return YES; | ||
311 | } | ||
312 | |||
313 | // overriden, don't call super | ||
314 | -(void) reshapeProjection:(CGSize)size | ||
315 | { | ||
316 | winSizeInPoints_ = [openGLView_ bounds].size; | ||
317 | winSizeInPixels_ = CGSizeMake(winSizeInPoints_.width * __ccContentScaleFactor, winSizeInPoints_.height *__ccContentScaleFactor); | ||
318 | |||
319 | [self setProjection:projection_]; | ||
320 | } | ||
321 | |||
322 | #pragma mark Director Scene Landscape | ||
323 | |||
324 | -(CGPoint)convertToGL:(CGPoint)uiPoint | ||
325 | { | ||
326 | CGSize s = winSizeInPoints_; | ||
327 | float newY = s.height - uiPoint.y; | ||
328 | float newX = s.width - uiPoint.x; | ||
329 | |||
330 | CGPoint ret = CGPointZero; | ||
331 | switch ( deviceOrientation_) { | ||
332 | case CCDeviceOrientationPortrait: | ||
333 | ret = ccp( uiPoint.x, newY ); | ||
334 | break; | ||
335 | case CCDeviceOrientationPortraitUpsideDown: | ||
336 | ret = ccp(newX, uiPoint.y); | ||
337 | break; | ||
338 | case CCDeviceOrientationLandscapeLeft: | ||
339 | ret.x = uiPoint.y; | ||
340 | ret.y = uiPoint.x; | ||
341 | break; | ||
342 | case CCDeviceOrientationLandscapeRight: | ||
343 | ret.x = newY; | ||
344 | ret.y = newX; | ||
345 | break; | ||
346 | } | ||
347 | return ret; | ||
348 | } | ||
349 | |||
350 | -(CGPoint)convertToUI:(CGPoint)glPoint | ||
351 | { | ||
352 | CGSize winSize = winSizeInPoints_; | ||
353 | int oppositeX = winSize.width - glPoint.x; | ||
354 | int oppositeY = winSize.height - glPoint.y; | ||
355 | CGPoint uiPoint = CGPointZero; | ||
356 | switch ( deviceOrientation_) { | ||
357 | case CCDeviceOrientationPortrait: | ||
358 | uiPoint = ccp(glPoint.x, oppositeY); | ||
359 | break; | ||
360 | case CCDeviceOrientationPortraitUpsideDown: | ||
361 | uiPoint = ccp(oppositeX, glPoint.y); | ||
362 | break; | ||
363 | case CCDeviceOrientationLandscapeLeft: | ||
364 | uiPoint = ccp(glPoint.y, glPoint.x); | ||
365 | break; | ||
366 | case CCDeviceOrientationLandscapeRight: | ||
367 | // Can't use oppositeX/Y because x/y are flipped | ||
368 | uiPoint = ccp(winSize.width-glPoint.y, winSize.height-glPoint.x); | ||
369 | break; | ||
370 | } | ||
371 | return uiPoint; | ||
372 | } | ||
373 | |||
374 | // get the current size of the glview | ||
375 | -(CGSize) winSize | ||
376 | { | ||
377 | CGSize s = winSizeInPoints_; | ||
378 | |||
379 | if( deviceOrientation_ == CCDeviceOrientationLandscapeLeft || deviceOrientation_ == CCDeviceOrientationLandscapeRight ) { | ||
380 | // swap x,y in landscape mode | ||
381 | CGSize tmp = s; | ||
382 | s.width = tmp.height; | ||
383 | s.height = tmp.width; | ||
384 | } | ||
385 | return s; | ||
386 | } | ||
387 | |||
388 | -(CGSize) winSizeInPixels | ||
389 | { | ||
390 | CGSize s = [self winSize]; | ||
391 | |||
392 | s.width *= CC_CONTENT_SCALE_FACTOR(); | ||
393 | s.height *= CC_CONTENT_SCALE_FACTOR(); | ||
394 | |||
395 | return s; | ||
396 | } | ||
397 | |||
398 | -(ccDeviceOrientation) deviceOrientation | ||
399 | { | ||
400 | return deviceOrientation_; | ||
401 | } | ||
402 | |||
403 | - (void) setDeviceOrientation:(ccDeviceOrientation) orientation | ||
404 | { | ||
405 | if( deviceOrientation_ != orientation ) { | ||
406 | deviceOrientation_ = orientation; | ||
407 | switch( deviceOrientation_) { | ||
408 | case CCDeviceOrientationPortrait: | ||
409 | [[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationPortrait animated:NO]; | ||
410 | break; | ||
411 | case CCDeviceOrientationPortraitUpsideDown: | ||
412 | [[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationPortraitUpsideDown animated:NO]; | ||
413 | break; | ||
414 | case CCDeviceOrientationLandscapeLeft: | ||
415 | [[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeRight animated:NO]; | ||
416 | break; | ||
417 | case CCDeviceOrientationLandscapeRight: | ||
418 | [[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeLeft animated:NO]; | ||
419 | break; | ||
420 | default: | ||
421 | NSLog(@"Director: Unknown device orientation"); | ||
422 | break; | ||
423 | } | ||
424 | } | ||
425 | } | ||
426 | |||
427 | -(void) applyOrientation | ||
428 | { | ||
429 | CGSize s = winSizeInPixels_; | ||
430 | float w = s.width / 2; | ||
431 | float h = s.height / 2; | ||
432 | |||
433 | // XXX it's using hardcoded values. | ||
434 | // What if the the screen size changes in the future? | ||
435 | switch ( deviceOrientation_ ) { | ||
436 | case CCDeviceOrientationPortrait: | ||
437 | // nothing | ||
438 | break; | ||
439 | case CCDeviceOrientationPortraitUpsideDown: | ||
440 | // upside down | ||
441 | glTranslatef(w,h,0); | ||
442 | glRotatef(180,0,0,1); | ||
443 | glTranslatef(-w,-h,0); | ||
444 | break; | ||
445 | case CCDeviceOrientationLandscapeRight: | ||
446 | glTranslatef(w,h,0); | ||
447 | glRotatef(90,0,0,1); | ||
448 | glTranslatef(-h,-w,0); | ||
449 | break; | ||
450 | case CCDeviceOrientationLandscapeLeft: | ||
451 | glTranslatef(w,h,0); | ||
452 | glRotatef(-90,0,0,1); | ||
453 | glTranslatef(-h,-w,0); | ||
454 | break; | ||
455 | } | ||
456 | } | ||
457 | |||
458 | -(void) end | ||
459 | { | ||
460 | // don't release the event handlers | ||
461 | // They are needed in case the director is run again | ||
462 | [[CCTouchDispatcher sharedDispatcher] removeAllDelegates]; | ||
463 | |||
464 | [super end]; | ||
465 | } | ||
466 | |||
467 | @end | ||
468 | |||
469 | |||
470 | #pragma mark - | ||
471 | #pragma mark Director TimerDirector | ||
472 | |||
473 | @implementation CCDirectorTimer | ||
474 | - (void)startAnimation | ||
475 | { | ||
476 | NSAssert( animationTimer == nil, @"animationTimer must be nil. Calling startAnimation twice?"); | ||
477 | |||
478 | if( gettimeofday( &lastUpdate_, NULL) != 0 ) { | ||
479 | CCLOG(@"cocos2d: Director: Error in gettimeofday"); | ||
480 | } | ||
481 | |||
482 | animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval_ target:self selector:@selector(mainLoop) userInfo:nil repeats:YES]; | ||
483 | |||
484 | // | ||
485 | // If you want to attach the opengl view into UIScrollView | ||
486 | // uncomment this line to prevent 'freezing'. | ||
487 | // It doesn't work on with the Fast Director | ||
488 | // | ||
489 | // [[NSRunLoop currentRunLoop] addTimer:animationTimer | ||
490 | // forMode:NSRunLoopCommonModes]; | ||
491 | } | ||
492 | |||
493 | -(void) mainLoop | ||
494 | { | ||
495 | [self drawScene]; | ||
496 | } | ||
497 | |||
498 | - (void)stopAnimation | ||
499 | { | ||
500 | [animationTimer invalidate]; | ||
501 | animationTimer = nil; | ||
502 | } | ||
503 | |||
504 | - (void)setAnimationInterval:(NSTimeInterval)interval | ||
505 | { | ||
506 | animationInterval_ = interval; | ||
507 | |||
508 | if(animationTimer) { | ||
509 | [self stopAnimation]; | ||
510 | [self startAnimation]; | ||
511 | } | ||
512 | } | ||
513 | |||
514 | -(void) dealloc | ||
515 | { | ||
516 | [animationTimer release]; | ||
517 | [super dealloc]; | ||
518 | } | ||
519 | @end | ||
520 | |||
521 | |||
522 | #pragma mark - | ||
523 | #pragma mark Director DirectorFast | ||
524 | |||
525 | @implementation CCDirectorFast | ||
526 | |||
527 | - (id) init | ||
528 | { | ||
529 | if(( self = [super init] )) { | ||
530 | |||
531 | #if CC_DIRECTOR_DISPATCH_FAST_EVENTS | ||
532 | CCLOG(@"cocos2d: Fast Events enabled"); | ||
533 | #else | ||
534 | CCLOG(@"cocos2d: Fast Events disabled"); | ||
535 | #endif | ||
536 | isRunning = NO; | ||
537 | |||
538 | // XXX: | ||
539 | // XXX: Don't create any autorelease object before calling "fast director" | ||
540 | // XXX: else it will be leaked | ||
541 | // XXX: | ||
542 | autoreleasePool = [NSAutoreleasePool new]; | ||
543 | } | ||
544 | |||
545 | return self; | ||
546 | } | ||
547 | |||
548 | - (void) startAnimation | ||
549 | { | ||
550 | NSAssert( isRunning == NO, @"isRunning must be NO. Calling startAnimation twice?"); | ||
551 | |||
552 | // XXX: | ||
553 | // XXX: release autorelease objects created | ||
554 | // XXX: between "use fast director" and "runWithScene" | ||
555 | // XXX: | ||
556 | [autoreleasePool release]; | ||
557 | autoreleasePool = nil; | ||
558 | |||
559 | if ( gettimeofday( &lastUpdate_, NULL) != 0 ) { | ||
560 | CCLOG(@"cocos2d: Director: Error in gettimeofday"); | ||
561 | } | ||
562 | |||
563 | |||
564 | isRunning = YES; | ||
565 | |||
566 | SEL selector = @selector(mainLoop); | ||
567 | NSMethodSignature* sig = [[[CCDirector sharedDirector] class] | ||
568 | instanceMethodSignatureForSelector:selector]; | ||
569 | NSInvocation* invocation = [NSInvocation | ||
570 | invocationWithMethodSignature:sig]; | ||
571 | [invocation setTarget:[CCDirector sharedDirector]]; | ||
572 | [invocation setSelector:selector]; | ||
573 | [invocation performSelectorOnMainThread:@selector(invokeWithTarget:) | ||
574 | withObject:[CCDirector sharedDirector] waitUntilDone:NO]; | ||
575 | |||
576 | // NSInvocationOperation *loopOperation = [[[NSInvocationOperation alloc] | ||
577 | // initWithTarget:self selector:@selector(mainLoop) object:nil] | ||
578 | // autorelease]; | ||
579 | // | ||
580 | // [loopOperation performSelectorOnMainThread:@selector(start) withObject:nil | ||
581 | // waitUntilDone:NO]; | ||
582 | } | ||
583 | |||
584 | -(void) mainLoop | ||
585 | { | ||
586 | while (isRunning) { | ||
587 | |||
588 | NSAutoreleasePool *loopPool = [NSAutoreleasePool new]; | ||
589 | |||
590 | #if CC_DIRECTOR_DISPATCH_FAST_EVENTS | ||
591 | while( CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.004f, FALSE) == kCFRunLoopRunHandledSource); | ||
592 | #else | ||
593 | while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, TRUE) == kCFRunLoopRunHandledSource); | ||
594 | #endif | ||
595 | |||
596 | if (isPaused_) { | ||
597 | usleep(250000); // Sleep for a quarter of a second (250,000 microseconds) so that the framerate is 4 fps. | ||
598 | } | ||
599 | |||
600 | [self drawScene]; | ||
601 | |||
602 | #if CC_DIRECTOR_DISPATCH_FAST_EVENTS | ||
603 | while( CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.004f, FALSE) == kCFRunLoopRunHandledSource); | ||
604 | #else | ||
605 | while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, TRUE) == kCFRunLoopRunHandledSource); | ||
606 | #endif | ||
607 | |||
608 | [loopPool release]; | ||
609 | } | ||
610 | } | ||
611 | - (void) stopAnimation | ||
612 | { | ||
613 | isRunning = NO; | ||
614 | } | ||
615 | |||
616 | - (void)setAnimationInterval:(NSTimeInterval)interval | ||
617 | { | ||
618 | NSLog(@"FastDirectory doesn't support setAnimationInterval, yet"); | ||
619 | } | ||
620 | @end | ||
621 | |||
622 | #pragma mark - | ||
623 | #pragma mark Director DirectorThreadedFast | ||
624 | |||
625 | @implementation CCDirectorFastThreaded | ||
626 | |||
627 | - (id) init | ||
628 | { | ||
629 | if(( self = [super init] )) { | ||
630 | isRunning = NO; | ||
631 | } | ||
632 | |||
633 | return self; | ||
634 | } | ||
635 | |||
636 | - (void) startAnimation | ||
637 | { | ||
638 | NSAssert( isRunning == NO, @"isRunning must be NO. Calling startAnimation twice?"); | ||
639 | |||
640 | if ( gettimeofday( &lastUpdate_, NULL) != 0 ) { | ||
641 | CCLOG(@"cocos2d: ThreadedFastDirector: Error on gettimeofday"); | ||
642 | } | ||
643 | |||
644 | isRunning = YES; | ||
645 | |||
646 | NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(mainLoop) object:nil]; | ||
647 | [thread start]; | ||
648 | [thread release]; | ||
649 | } | ||
650 | |||
651 | -(void) mainLoop | ||
652 | { | ||
653 | while( ![[NSThread currentThread] isCancelled] ) { | ||
654 | if( isRunning ) | ||
655 | [self performSelectorOnMainThread:@selector(drawScene) withObject:nil waitUntilDone:YES]; | ||
656 | |||
657 | if (isPaused_) { | ||
658 | usleep(250000); // Sleep for a quarter of a second (250,000 microseconds) so that the framerate is 4 fps. | ||
659 | } else { | ||
660 | // usleep(2000); | ||
661 | } | ||
662 | } | ||
663 | } | ||
664 | - (void) stopAnimation | ||
665 | { | ||
666 | isRunning = NO; | ||
667 | } | ||
668 | |||
669 | - (void)setAnimationInterval:(NSTimeInterval)interval | ||
670 | { | ||
671 | NSLog(@"FastDirector doesn't support setAnimationInterval, yet"); | ||
672 | } | ||
673 | @end | ||
674 | |||
675 | #pragma mark - | ||
676 | #pragma mark DirectorDisplayLink | ||
677 | |||
678 | // Allows building DisplayLinkDirector for pre-3.1 SDKS | ||
679 | // without getting compiler warnings. | ||
680 | @interface NSObject(CADisplayLink) | ||
681 | + (id) displayLinkWithTarget:(id)arg1 selector:(SEL)arg2; | ||
682 | - (void) addToRunLoop:(id)arg1 forMode:(id)arg2; | ||
683 | - (void) setFrameInterval:(int)interval; | ||
684 | - (void) invalidate; | ||
685 | @end | ||
686 | |||
687 | @implementation CCDirectorDisplayLink | ||
688 | |||
689 | - (void)setAnimationInterval:(NSTimeInterval)interval | ||
690 | { | ||
691 | animationInterval_ = interval; | ||
692 | if(displayLink){ | ||
693 | [self stopAnimation]; | ||
694 | [self startAnimation]; | ||
695 | } | ||
696 | } | ||
697 | |||
698 | - (void) startAnimation | ||
699 | { | ||
700 | NSAssert( displayLink == nil, @"displayLink must be nil. Calling startAnimation twice?"); | ||
701 | |||
702 | if ( gettimeofday( &lastUpdate_, NULL) != 0 ) { | ||
703 | CCLOG(@"cocos2d: DisplayLinkDirector: Error on gettimeofday"); | ||
704 | } | ||
705 | |||
706 | // approximate frame rate | ||
707 | // assumes device refreshes at 60 fps | ||
708 | int frameInterval = (int) floor(animationInterval_ * 60.0f); | ||
709 | |||
710 | CCLOG(@"cocos2d: Frame interval: %d", frameInterval); | ||
711 | |||
712 | displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(mainLoop:)]; | ||
713 | [displayLink setFrameInterval:frameInterval]; | ||
714 | [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; | ||
715 | } | ||
716 | |||
717 | -(void) mainLoop:(id)sender | ||
718 | { | ||
719 | [self drawScene]; | ||
720 | } | ||
721 | |||
722 | - (void) stopAnimation | ||
723 | { | ||
724 | [displayLink invalidate]; | ||
725 | displayLink = nil; | ||
726 | } | ||
727 | |||
728 | -(void) dealloc | ||
729 | { | ||
730 | [displayLink release]; | ||
731 | [super dealloc]; | ||
732 | } | ||
733 | @end | ||
734 | |||
735 | #endif // __IPHONE_OS_VERSION_MAX_ALLOWED | ||
diff --git a/libs/cocos2d/Platforms/iOS/CCTouchDelegateProtocol.h b/libs/cocos2d/Platforms/iOS/CCTouchDelegateProtocol.h new file mode 100755 index 0000000..20ba036 --- /dev/null +++ b/libs/cocos2d/Platforms/iOS/CCTouchDelegateProtocol.h | |||
@@ -0,0 +1,75 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 2009 Valentin Milea | ||
5 | * | ||
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
7 | * of this software and associated documentation files (the "Software"), to deal | ||
8 | * in the Software without restriction, including without limitation the rights | ||
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
10 | * copies of the Software, and to permit persons to whom the Software is | ||
11 | * furnished to do so, subject to the following conditions: | ||
12 | * | ||
13 | * The above copyright notice and this permission notice shall be included in | ||
14 | * all copies or substantial portions of the Software. | ||
15 | * | ||
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
22 | * THE SOFTWARE. | ||
23 | * | ||
24 | */ | ||
25 | |||
26 | // Only compile this code on iOS. These files should NOT be included on your Mac project. | ||
27 | // But in case they are included, it won't be compiled. | ||
28 | #import <Availability.h> | ||
29 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
30 | |||
31 | #import <UIKit/UIKit.h> | ||
32 | |||
33 | /** | ||
34 | CCTargetedTouchDelegate. | ||
35 | |||
36 | Using this type of delegate results in two benefits: | ||
37 | 1. You don't need to deal with NSSets, the dispatcher does the job of splitting | ||
38 | them. You get exactly one UITouch per call. | ||
39 | 2. You can *claim* a UITouch by returning YES in ccTouchBegan. Updates of claimed | ||
40 | touches are sent only to the delegate(s) that claimed them. So if you get a move/ | ||
41 | ended/cancelled update you're sure it's your touch. This frees you from doing a | ||
42 | lot of checks when doing multi-touch. | ||
43 | |||
44 | (The name TargetedTouchDelegate relates to updates "targeting" their specific | ||
45 | handler, without bothering the other handlers.) | ||
46 | @since v0.8 | ||
47 | */ | ||
48 | @protocol CCTargetedTouchDelegate <NSObject> | ||
49 | |||
50 | /** Return YES to claim the touch. | ||
51 | @since v0.8 | ||
52 | */ | ||
53 | - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event; | ||
54 | @optional | ||
55 | // touch updates: | ||
56 | - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event; | ||
57 | - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event; | ||
58 | - (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event; | ||
59 | @end | ||
60 | |||
61 | /** | ||
62 | CCStandardTouchDelegate. | ||
63 | |||
64 | This type of delegate is the same one used by CocoaTouch. You will receive all the events (Began,Moved,Ended,Cancelled). | ||
65 | @since v0.8 | ||
66 | */ | ||
67 | @protocol CCStandardTouchDelegate <NSObject> | ||
68 | @optional | ||
69 | - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; | ||
70 | - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; | ||
71 | - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; | ||
72 | - (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; | ||
73 | @end | ||
74 | |||
75 | #endif // __IPHONE_OS_VERSION_MAX_ALLOWED | ||
diff --git a/libs/cocos2d/Platforms/iOS/CCTouchDispatcher.h b/libs/cocos2d/Platforms/iOS/CCTouchDispatcher.h new file mode 100755 index 0000000..9931189 --- /dev/null +++ b/libs/cocos2d/Platforms/iOS/CCTouchDispatcher.h | |||
@@ -0,0 +1,123 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 2009 Valentin Milea | ||
5 | * | ||
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
7 | * of this software and associated documentation files (the "Software"), to deal | ||
8 | * in the Software without restriction, including without limitation the rights | ||
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
10 | * copies of the Software, and to permit persons to whom the Software is | ||
11 | * furnished to do so, subject to the following conditions: | ||
12 | * | ||
13 | * The above copyright notice and this permission notice shall be included in | ||
14 | * all copies or substantial portions of the Software. | ||
15 | * | ||
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
22 | * THE SOFTWARE. | ||
23 | * | ||
24 | */ | ||
25 | |||
26 | // Only compile this code on iOS. These files should NOT be included on your Mac project. | ||
27 | // But in case they are included, it won't be compiled. | ||
28 | #import <Availability.h> | ||
29 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
30 | |||
31 | #import "CCTouchDelegateProtocol.h" | ||
32 | #import "EAGLView.h" | ||
33 | |||
34 | |||
35 | typedef enum | ||
36 | { | ||
37 | kCCTouchSelectorBeganBit = 1 << 0, | ||
38 | kCCTouchSelectorMovedBit = 1 << 1, | ||
39 | kCCTouchSelectorEndedBit = 1 << 2, | ||
40 | kCCTouchSelectorCancelledBit = 1 << 3, | ||
41 | kCCTouchSelectorAllBits = ( kCCTouchSelectorBeganBit | kCCTouchSelectorMovedBit | kCCTouchSelectorEndedBit | kCCTouchSelectorCancelledBit), | ||
42 | } ccTouchSelectorFlag; | ||
43 | |||
44 | |||
45 | enum { | ||
46 | kCCTouchBegan, | ||
47 | kCCTouchMoved, | ||
48 | kCCTouchEnded, | ||
49 | kCCTouchCancelled, | ||
50 | |||
51 | kCCTouchMax, | ||
52 | }; | ||
53 | |||
54 | struct ccTouchHandlerHelperData { | ||
55 | SEL touchesSel; | ||
56 | SEL touchSel; | ||
57 | ccTouchSelectorFlag type; | ||
58 | }; | ||
59 | |||
60 | /** CCTouchDispatcher. | ||
61 | Singleton that handles all the touch events. | ||
62 | The dispatcher dispatches events to the registered TouchHandlers. | ||
63 | There are 2 different type of touch handlers: | ||
64 | - Standard Touch Handlers | ||
65 | - Targeted Touch Handlers | ||
66 | |||
67 | The Standard Touch Handlers work like the CocoaTouch touch handler: a set of touches is passed to the delegate. | ||
68 | On the other hand, the Targeted Touch Handlers only receive 1 touch at the time, and they can "swallow" touches (avoid the propagation of the event). | ||
69 | |||
70 | Firstly, the dispatcher sends the received touches to the targeted touches. | ||
71 | These touches can be swallowed by the Targeted Touch Handlers. If there are still remaining touches, then the remaining touches will be sent | ||
72 | to the Standard Touch Handlers. | ||
73 | |||
74 | @since v0.8.0 | ||
75 | */ | ||
76 | @interface CCTouchDispatcher : NSObject <EAGLTouchDelegate> | ||
77 | { | ||
78 | NSMutableArray *targetedHandlers; | ||
79 | NSMutableArray *standardHandlers; | ||
80 | |||
81 | BOOL locked; | ||
82 | BOOL toAdd; | ||
83 | BOOL toRemove; | ||
84 | NSMutableArray *handlersToAdd; | ||
85 | NSMutableArray *handlersToRemove; | ||
86 | BOOL toQuit; | ||
87 | |||
88 | BOOL dispatchEvents; | ||
89 | |||
90 | // 4, 1 for each type of event | ||
91 | struct ccTouchHandlerHelperData handlerHelperData[kCCTouchMax]; | ||
92 | } | ||
93 | |||
94 | /** singleton of the CCTouchDispatcher */ | ||
95 | + (CCTouchDispatcher*)sharedDispatcher; | ||
96 | |||
97 | /** Whether or not the events are going to be dispatched. Default: YES */ | ||
98 | @property (nonatomic,readwrite, assign) BOOL dispatchEvents; | ||
99 | |||
100 | /** Adds a standard touch delegate to the dispatcher's list. | ||
101 | See StandardTouchDelegate description. | ||
102 | IMPORTANT: The delegate will be retained. | ||
103 | */ | ||
104 | -(void) addStandardDelegate:(id<CCStandardTouchDelegate>) delegate priority:(int)priority; | ||
105 | /** Adds a targeted touch delegate to the dispatcher's list. | ||
106 | See TargetedTouchDelegate description. | ||
107 | IMPORTANT: The delegate will be retained. | ||
108 | */ | ||
109 | -(void) addTargetedDelegate:(id<CCTargetedTouchDelegate>) delegate priority:(int)priority swallowsTouches:(BOOL)swallowsTouches; | ||
110 | /** Removes a touch delegate. | ||
111 | The delegate will be released | ||
112 | */ | ||
113 | -(void) removeDelegate:(id) delegate; | ||
114 | /** Removes all touch delegates, releasing all the delegates */ | ||
115 | -(void) removeAllDelegates; | ||
116 | /** Changes the priority of a previously added delegate. The lower the number, | ||
117 | the higher the priority */ | ||
118 | -(void) setPriority:(int) priority forDelegate:(id) delegate; | ||
119 | |||
120 | NSComparisonResult sortByPriority(id first, id second, void *context); | ||
121 | @end | ||
122 | |||
123 | #endif // __IPHONE_OS_VERSION_MAX_ALLOWED | ||
diff --git a/libs/cocos2d/Platforms/iOS/CCTouchDispatcher.m b/libs/cocos2d/Platforms/iOS/CCTouchDispatcher.m new file mode 100755 index 0000000..1553b48 --- /dev/null +++ b/libs/cocos2d/Platforms/iOS/CCTouchDispatcher.m | |||
@@ -0,0 +1,347 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 2009 Valentin Milea | ||
5 | * | ||
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
7 | * of this software and associated documentation files (the "Software"), to deal | ||
8 | * in the Software without restriction, including without limitation the rights | ||
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
10 | * copies of the Software, and to permit persons to whom the Software is | ||
11 | * furnished to do so, subject to the following conditions: | ||
12 | * | ||
13 | * The above copyright notice and this permission notice shall be included in | ||
14 | * all copies or substantial portions of the Software. | ||
15 | * | ||
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
22 | * THE SOFTWARE. | ||
23 | * | ||
24 | */ | ||
25 | |||
26 | // Only compile this code on iOS. These files should NOT be included on your Mac project. | ||
27 | // But in case they are included, it won't be compiled. | ||
28 | #import <Availability.h> | ||
29 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
30 | |||
31 | |||
32 | #import "CCTouchDispatcher.h" | ||
33 | #import "CCTouchHandler.h" | ||
34 | |||
35 | @implementation CCTouchDispatcher | ||
36 | |||
37 | @synthesize dispatchEvents; | ||
38 | |||
39 | static CCTouchDispatcher *sharedDispatcher = nil; | ||
40 | |||
41 | +(CCTouchDispatcher*) sharedDispatcher | ||
42 | { | ||
43 | @synchronized(self) { | ||
44 | if (sharedDispatcher == nil) | ||
45 | sharedDispatcher = [[self alloc] init]; // assignment not done here | ||
46 | } | ||
47 | return sharedDispatcher; | ||
48 | } | ||
49 | |||
50 | +(id) allocWithZone:(NSZone *)zone | ||
51 | { | ||
52 | @synchronized(self) { | ||
53 | NSAssert(sharedDispatcher == nil, @"Attempted to allocate a second instance of a singleton."); | ||
54 | return [super allocWithZone:zone]; | ||
55 | } | ||
56 | return nil; // on subsequent allocation attempts return nil | ||
57 | } | ||
58 | |||
59 | -(id) init | ||
60 | { | ||
61 | if((self = [super init])) { | ||
62 | |||
63 | dispatchEvents = YES; | ||
64 | targetedHandlers = [[NSMutableArray alloc] initWithCapacity:8]; | ||
65 | standardHandlers = [[NSMutableArray alloc] initWithCapacity:4]; | ||
66 | |||
67 | handlersToAdd = [[NSMutableArray alloc] initWithCapacity:8]; | ||
68 | handlersToRemove = [[NSMutableArray alloc] initWithCapacity:8]; | ||
69 | |||
70 | toRemove = NO; | ||
71 | toAdd = NO; | ||
72 | toQuit = NO; | ||
73 | locked = NO; | ||
74 | |||
75 | handlerHelperData[kCCTouchBegan] = (struct ccTouchHandlerHelperData) {@selector(ccTouchesBegan:withEvent:),@selector(ccTouchBegan:withEvent:),kCCTouchSelectorBeganBit}; | ||
76 | handlerHelperData[kCCTouchMoved] = (struct ccTouchHandlerHelperData) {@selector(ccTouchesMoved:withEvent:),@selector(ccTouchMoved:withEvent:),kCCTouchSelectorMovedBit}; | ||
77 | handlerHelperData[kCCTouchEnded] = (struct ccTouchHandlerHelperData) {@selector(ccTouchesEnded:withEvent:),@selector(ccTouchEnded:withEvent:),kCCTouchSelectorEndedBit}; | ||
78 | handlerHelperData[kCCTouchCancelled] = (struct ccTouchHandlerHelperData) {@selector(ccTouchesCancelled:withEvent:),@selector(ccTouchCancelled:withEvent:),kCCTouchSelectorCancelledBit}; | ||
79 | |||
80 | } | ||
81 | |||
82 | return self; | ||
83 | } | ||
84 | |||
85 | -(void) dealloc | ||
86 | { | ||
87 | [targetedHandlers release]; | ||
88 | [standardHandlers release]; | ||
89 | [handlersToAdd release]; | ||
90 | [handlersToRemove release]; | ||
91 | [super dealloc]; | ||
92 | } | ||
93 | |||
94 | // | ||
95 | // handlers management | ||
96 | // | ||
97 | |||
98 | #pragma mark TouchDispatcher - Add Hanlder | ||
99 | |||
100 | -(void) forceAddHandler:(CCTouchHandler*)handler array:(NSMutableArray*)array | ||
101 | { | ||
102 | NSUInteger i = 0; | ||
103 | |||
104 | for( CCTouchHandler *h in array ) { | ||
105 | if( h.priority < handler.priority ) | ||
106 | i++; | ||
107 | |||
108 | NSAssert( h.delegate != handler.delegate, @"Delegate already added to touch dispatcher."); | ||
109 | } | ||
110 | [array insertObject:handler atIndex:i]; | ||
111 | } | ||
112 | |||
113 | -(void) addStandardDelegate:(id<CCStandardTouchDelegate>) delegate priority:(int)priority | ||
114 | { | ||
115 | CCTouchHandler *handler = [CCStandardTouchHandler handlerWithDelegate:delegate priority:priority]; | ||
116 | if( ! locked ) { | ||
117 | [self forceAddHandler:handler array:standardHandlers]; | ||
118 | } else { | ||
119 | [handlersToAdd addObject:handler]; | ||
120 | toAdd = YES; | ||
121 | } | ||
122 | } | ||
123 | |||
124 | -(void) addTargetedDelegate:(id<CCTargetedTouchDelegate>) delegate priority:(int)priority swallowsTouches:(BOOL)swallowsTouches | ||
125 | { | ||
126 | CCTouchHandler *handler = [CCTargetedTouchHandler handlerWithDelegate:delegate priority:priority swallowsTouches:swallowsTouches]; | ||
127 | if( ! locked ) { | ||
128 | [self forceAddHandler:handler array:targetedHandlers]; | ||
129 | } else { | ||
130 | [handlersToAdd addObject:handler]; | ||
131 | toAdd = YES; | ||
132 | } | ||
133 | } | ||
134 | |||
135 | #pragma mark TouchDispatcher - removeDelegate | ||
136 | |||
137 | -(void) forceRemoveDelegate:(id)delegate | ||
138 | { | ||
139 | // XXX: remove it from both handlers ??? | ||
140 | |||
141 | for( CCTouchHandler *handler in targetedHandlers ) { | ||
142 | if( handler.delegate == delegate ) { | ||
143 | [targetedHandlers removeObject:handler]; | ||
144 | break; | ||
145 | } | ||
146 | } | ||
147 | |||
148 | for( CCTouchHandler *handler in standardHandlers ) { | ||
149 | if( handler.delegate == delegate ) { | ||
150 | [standardHandlers removeObject:handler]; | ||
151 | break; | ||
152 | } | ||
153 | } | ||
154 | } | ||
155 | |||
156 | -(void) removeDelegate:(id) delegate | ||
157 | { | ||
158 | if( delegate == nil ) | ||
159 | return; | ||
160 | |||
161 | if( ! locked ) { | ||
162 | [self forceRemoveDelegate:delegate]; | ||
163 | } else { | ||
164 | [handlersToRemove addObject:delegate]; | ||
165 | toRemove = YES; | ||
166 | } | ||
167 | } | ||
168 | |||
169 | #pragma mark TouchDispatcher - removeAllDelegates | ||
170 | |||
171 | -(void) forceRemoveAllDelegates | ||
172 | { | ||
173 | [standardHandlers removeAllObjects]; | ||
174 | [targetedHandlers removeAllObjects]; | ||
175 | } | ||
176 | -(void) removeAllDelegates | ||
177 | { | ||
178 | if( ! locked ) | ||
179 | [self forceRemoveAllDelegates]; | ||
180 | else | ||
181 | toQuit = YES; | ||
182 | } | ||
183 | |||
184 | #pragma mark Changing priority of added handlers | ||
185 | |||
186 | -(CCTouchHandler*) findHandler:(id)delegate | ||
187 | { | ||
188 | for( CCTouchHandler *handler in targetedHandlers ) { | ||
189 | if( handler.delegate == delegate ) { | ||
190 | return handler; | ||
191 | } | ||
192 | } | ||
193 | |||
194 | for( CCTouchHandler *handler in standardHandlers ) { | ||
195 | if( handler.delegate == delegate ) { | ||
196 | return handler; | ||
197 | } | ||
198 | } | ||
199 | return nil; | ||
200 | } | ||
201 | |||
202 | NSComparisonResult sortByPriority(id first, id second, void *context) | ||
203 | { | ||
204 | if (((CCTouchHandler*)first).priority < ((CCTouchHandler*)second).priority) | ||
205 | return NSOrderedAscending; | ||
206 | else if (((CCTouchHandler*)first).priority > ((CCTouchHandler*)second).priority) | ||
207 | return NSOrderedDescending; | ||
208 | else | ||
209 | return NSOrderedSame; | ||
210 | } | ||
211 | |||
212 | -(void) rearrangeHandlers:(NSMutableArray*)array | ||
213 | { | ||
214 | [array sortUsingFunction:sortByPriority context:nil]; | ||
215 | } | ||
216 | |||
217 | -(void) setPriority:(int) priority forDelegate:(id) delegate | ||
218 | { | ||
219 | NSAssert(delegate != nil, @"Got nil touch delegate!"); | ||
220 | |||
221 | CCTouchHandler *handler = nil; | ||
222 | handler = [self findHandler:delegate]; | ||
223 | |||
224 | NSAssert(handler != nil, @"Delegate not found!"); | ||
225 | |||
226 | handler.priority = priority; | ||
227 | |||
228 | [self rearrangeHandlers:targetedHandlers]; | ||
229 | [self rearrangeHandlers:standardHandlers]; | ||
230 | } | ||
231 | |||
232 | // | ||
233 | // dispatch events | ||
234 | // | ||
235 | -(void) touches:(NSSet*)touches withEvent:(UIEvent*)event withTouchType:(unsigned int)idx | ||
236 | { | ||
237 | NSAssert(idx < 4, @"Invalid idx value"); | ||
238 | |||
239 | id mutableTouches; | ||
240 | locked = YES; | ||
241 | |||
242 | // optimization to prevent a mutable copy when it is not necessary | ||
243 | unsigned int targetedHandlersCount = [targetedHandlers count]; | ||
244 | unsigned int standardHandlersCount = [standardHandlers count]; | ||
245 | BOOL needsMutableSet = (targetedHandlersCount && standardHandlersCount); | ||
246 | |||
247 | mutableTouches = (needsMutableSet ? [touches mutableCopy] : touches); | ||
248 | |||
249 | struct ccTouchHandlerHelperData helper = handlerHelperData[idx]; | ||
250 | // | ||
251 | // process the target handlers 1st | ||
252 | // | ||
253 | if( targetedHandlersCount > 0 ) { | ||
254 | for( UITouch *touch in touches ) { | ||
255 | for(CCTargetedTouchHandler *handler in targetedHandlers) { | ||
256 | |||
257 | BOOL claimed = NO; | ||
258 | if( idx == kCCTouchBegan ) { | ||
259 | claimed = [handler.delegate ccTouchBegan:touch withEvent:event]; | ||
260 | if( claimed ) | ||
261 | [handler.claimedTouches addObject:touch]; | ||
262 | } | ||
263 | |||
264 | // else (moved, ended, cancelled) | ||
265 | else if( [handler.claimedTouches containsObject:touch] ) { | ||
266 | claimed = YES; | ||
267 | if( handler.enabledSelectors & helper.type ) | ||
268 | [handler.delegate performSelector:helper.touchSel withObject:touch withObject:event]; | ||
269 | |||
270 | if( helper.type & (kCCTouchSelectorCancelledBit | kCCTouchSelectorEndedBit) ) | ||
271 | [handler.claimedTouches removeObject:touch]; | ||
272 | } | ||
273 | |||
274 | if( claimed && handler.swallowsTouches ) { | ||
275 | if( needsMutableSet ) | ||
276 | [mutableTouches removeObject:touch]; | ||
277 | break; | ||
278 | } | ||
279 | } | ||
280 | } | ||
281 | } | ||
282 | |||
283 | // | ||
284 | // process standard handlers 2nd | ||
285 | // | ||
286 | if( standardHandlersCount > 0 && [mutableTouches count]>0 ) { | ||
287 | for( CCTouchHandler *handler in standardHandlers ) { | ||
288 | if( handler.enabledSelectors & helper.type ) | ||
289 | [handler.delegate performSelector:helper.touchesSel withObject:mutableTouches withObject:event]; | ||
290 | } | ||
291 | } | ||
292 | if( needsMutableSet ) | ||
293 | [mutableTouches release]; | ||
294 | |||
295 | // | ||
296 | // Optimization. To prevent a [handlers copy] which is expensive | ||
297 | // the add/removes/quit is done after the iterations | ||
298 | // | ||
299 | locked = NO; | ||
300 | if( toRemove ) { | ||
301 | toRemove = NO; | ||
302 | for( id delegate in handlersToRemove ) | ||
303 | [self forceRemoveDelegate:delegate]; | ||
304 | [handlersToRemove removeAllObjects]; | ||
305 | } | ||
306 | if( toAdd ) { | ||
307 | toAdd = NO; | ||
308 | for( CCTouchHandler *handler in handlersToAdd ) { | ||
309 | Class targetedClass = [CCTargetedTouchHandler class]; | ||
310 | if( [handler isKindOfClass:targetedClass] ) | ||
311 | [self forceAddHandler:handler array:targetedHandlers]; | ||
312 | else | ||
313 | [self forceAddHandler:handler array:standardHandlers]; | ||
314 | } | ||
315 | [handlersToAdd removeAllObjects]; | ||
316 | } | ||
317 | if( toQuit ) { | ||
318 | toQuit = NO; | ||
319 | [self forceRemoveAllDelegates]; | ||
320 | } | ||
321 | } | ||
322 | |||
323 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event | ||
324 | { | ||
325 | if( dispatchEvents ) | ||
326 | [self touches:touches withEvent:event withTouchType:kCCTouchBegan]; | ||
327 | } | ||
328 | - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event | ||
329 | { | ||
330 | if( dispatchEvents ) | ||
331 | [self touches:touches withEvent:event withTouchType:kCCTouchMoved]; | ||
332 | } | ||
333 | |||
334 | - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event | ||
335 | { | ||
336 | if( dispatchEvents ) | ||
337 | [self touches:touches withEvent:event withTouchType:kCCTouchEnded]; | ||
338 | } | ||
339 | |||
340 | - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event | ||
341 | { | ||
342 | if( dispatchEvents ) | ||
343 | [self touches:touches withEvent:event withTouchType:kCCTouchCancelled]; | ||
344 | } | ||
345 | @end | ||
346 | |||
347 | #endif // __IPHONE_OS_VERSION_MAX_ALLOWED | ||
diff --git a/libs/cocos2d/Platforms/iOS/CCTouchHandler.h b/libs/cocos2d/Platforms/iOS/CCTouchHandler.h new file mode 100755 index 0000000..31a3e36 --- /dev/null +++ b/libs/cocos2d/Platforms/iOS/CCTouchHandler.h | |||
@@ -0,0 +1,93 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 2009 Valentin Milea | ||
5 | * | ||
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
7 | * of this software and associated documentation files (the "Software"), to deal | ||
8 | * in the Software without restriction, including without limitation the rights | ||
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
10 | * copies of the Software, and to permit persons to whom the Software is | ||
11 | * furnished to do so, subject to the following conditions: | ||
12 | * | ||
13 | * The above copyright notice and this permission notice shall be included in | ||
14 | * all copies or substantial portions of the Software. | ||
15 | * | ||
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
22 | * THE SOFTWARE. | ||
23 | * | ||
24 | */ | ||
25 | |||
26 | // Only compile this code on iOS. These files should NOT be included on your Mac project. | ||
27 | // But in case they are included, it won't be compiled. | ||
28 | #import <Availability.h> | ||
29 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
30 | |||
31 | /* | ||
32 | * This file contains the delegates of the touches | ||
33 | * There are 2 possible delegates: | ||
34 | * - CCStandardTouchHandler: propagates all the events at once | ||
35 | * - CCTargetedTouchHandler: propagates 1 event at the time | ||
36 | */ | ||
37 | |||
38 | #import "CCTouchDelegateProtocol.h" | ||
39 | #import "CCTouchDispatcher.h" | ||
40 | |||
41 | /** | ||
42 | CCTouchHandler | ||
43 | Object than contains the delegate and priority of the event handler. | ||
44 | */ | ||
45 | @interface CCTouchHandler : NSObject { | ||
46 | id delegate; | ||
47 | int priority; | ||
48 | ccTouchSelectorFlag enabledSelectors_; | ||
49 | } | ||
50 | |||
51 | /** delegate */ | ||
52 | @property(nonatomic, readwrite, retain) id delegate; | ||
53 | /** priority */ | ||
54 | @property(nonatomic, readwrite) int priority; // default 0 | ||
55 | /** enabled selectors */ | ||
56 | @property(nonatomic,readwrite) ccTouchSelectorFlag enabledSelectors; | ||
57 | |||
58 | /** allocates a TouchHandler with a delegate and a priority */ | ||
59 | + (id)handlerWithDelegate:(id)aDelegate priority:(int)priority; | ||
60 | /** initializes a TouchHandler with a delegate and a priority */ | ||
61 | - (id)initWithDelegate:(id)aDelegate priority:(int)priority; | ||
62 | @end | ||
63 | |||
64 | /** CCStandardTouchHandler | ||
65 | It forwardes each event to the delegate. | ||
66 | */ | ||
67 | @interface CCStandardTouchHandler : CCTouchHandler | ||
68 | { | ||
69 | } | ||
70 | @end | ||
71 | |||
72 | /** | ||
73 | CCTargetedTouchHandler | ||
74 | Object than contains the claimed touches and if it swallos touches. | ||
75 | Used internally by TouchDispatcher | ||
76 | */ | ||
77 | @interface CCTargetedTouchHandler : CCTouchHandler { | ||
78 | BOOL swallowsTouches; | ||
79 | NSMutableSet *claimedTouches; | ||
80 | } | ||
81 | /** whether or not the touches are swallowed */ | ||
82 | @property(nonatomic, readwrite) BOOL swallowsTouches; // default NO | ||
83 | /** MutableSet that contains the claimed touches */ | ||
84 | @property(nonatomic, readonly) NSMutableSet *claimedTouches; | ||
85 | |||
86 | /** allocates a TargetedTouchHandler with a delegate, a priority and whether or not it swallows touches or not */ | ||
87 | + (id)handlerWithDelegate:(id) aDelegate priority:(int)priority swallowsTouches:(BOOL)swallowsTouches; | ||
88 | /** initializes a TargetedTouchHandler with a delegate, a priority and whether or not it swallows touches or not */ | ||
89 | - (id)initWithDelegate:(id) aDelegate priority:(int)priority swallowsTouches:(BOOL)swallowsTouches; | ||
90 | |||
91 | @end | ||
92 | |||
93 | #endif // __IPHONE_OS_VERSION_MAX_ALLOWED | ||
diff --git a/libs/cocos2d/Platforms/iOS/CCTouchHandler.m b/libs/cocos2d/Platforms/iOS/CCTouchHandler.m new file mode 100755 index 0000000..a52103b --- /dev/null +++ b/libs/cocos2d/Platforms/iOS/CCTouchHandler.m | |||
@@ -0,0 +1,135 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 2009 Valentin Milea | ||
5 | * | ||
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
7 | * of this software and associated documentation files (the "Software"), to deal | ||
8 | * in the Software without restriction, including without limitation the rights | ||
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
10 | * copies of the Software, and to permit persons to whom the Software is | ||
11 | * furnished to do so, subject to the following conditions: | ||
12 | * | ||
13 | * The above copyright notice and this permission notice shall be included in | ||
14 | * all copies or substantial portions of the Software. | ||
15 | * | ||
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
22 | * THE SOFTWARE. | ||
23 | * | ||
24 | */ | ||
25 | |||
26 | |||
27 | // Only compile this code on iOS. These files should NOT be included on your Mac project. | ||
28 | // But in case they are included, it won't be compiled. | ||
29 | #import <Availability.h> | ||
30 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
31 | |||
32 | /* | ||
33 | * This file contains the delegates of the touches | ||
34 | * There are 2 possible delegates: | ||
35 | * - CCStandardTouchHandler: propagates all the events at once | ||
36 | * - CCTargetedTouchHandler: propagates 1 event at the time | ||
37 | */ | ||
38 | |||
39 | #import "CCTouchHandler.h" | ||
40 | #import "../../ccMacros.h" | ||
41 | |||
42 | #pragma mark - | ||
43 | #pragma mark TouchHandler | ||
44 | @implementation CCTouchHandler | ||
45 | |||
46 | @synthesize delegate, priority; | ||
47 | @synthesize enabledSelectors=enabledSelectors_; | ||
48 | |||
49 | + (id)handlerWithDelegate:(id) aDelegate priority:(int)aPriority | ||
50 | { | ||
51 | return [[[self alloc] initWithDelegate:aDelegate priority:aPriority] autorelease]; | ||
52 | } | ||
53 | |||
54 | - (id)initWithDelegate:(id) aDelegate priority:(int)aPriority | ||
55 | { | ||
56 | NSAssert(aDelegate != nil, @"Touch delegate may not be nil"); | ||
57 | |||
58 | if ((self = [super init])) { | ||
59 | self.delegate = aDelegate; | ||
60 | priority = aPriority; | ||
61 | enabledSelectors_ = 0; | ||
62 | } | ||
63 | |||
64 | return self; | ||
65 | } | ||
66 | |||
67 | - (void)dealloc { | ||
68 | CCLOGINFO(@"cocos2d: deallocing %@", self); | ||
69 | [delegate release]; | ||
70 | [super dealloc]; | ||
71 | } | ||
72 | @end | ||
73 | |||
74 | #pragma mark - | ||
75 | #pragma mark StandardTouchHandler | ||
76 | @implementation CCStandardTouchHandler | ||
77 | -(id) initWithDelegate:(id)del priority:(int)pri | ||
78 | { | ||
79 | if( (self=[super initWithDelegate:del priority:pri]) ) { | ||
80 | if( [del respondsToSelector:@selector(ccTouchesBegan:withEvent:)] ) | ||
81 | enabledSelectors_ |= kCCTouchSelectorBeganBit; | ||
82 | if( [del respondsToSelector:@selector(ccTouchesMoved:withEvent:)] ) | ||
83 | enabledSelectors_ |= kCCTouchSelectorMovedBit; | ||
84 | if( [del respondsToSelector:@selector(ccTouchesEnded:withEvent:)] ) | ||
85 | enabledSelectors_ |= kCCTouchSelectorEndedBit; | ||
86 | if( [del respondsToSelector:@selector(ccTouchesCancelled:withEvent:)] ) | ||
87 | enabledSelectors_ |= kCCTouchSelectorCancelledBit; | ||
88 | } | ||
89 | return self; | ||
90 | } | ||
91 | @end | ||
92 | |||
93 | #pragma mark - | ||
94 | #pragma mark TargetedTouchHandler | ||
95 | |||
96 | @interface CCTargetedTouchHandler (private) | ||
97 | -(void) updateKnownTouches:(NSMutableSet *)touches withEvent:(UIEvent *)event selector:(SEL)selector unclaim:(BOOL)doUnclaim; | ||
98 | @end | ||
99 | |||
100 | @implementation CCTargetedTouchHandler | ||
101 | |||
102 | @synthesize swallowsTouches, claimedTouches; | ||
103 | |||
104 | + (id)handlerWithDelegate:(id)aDelegate priority:(int)priority swallowsTouches:(BOOL)swallow | ||
105 | { | ||
106 | return [[[self alloc] initWithDelegate:aDelegate priority:priority swallowsTouches:swallow] autorelease]; | ||
107 | } | ||
108 | |||
109 | - (id)initWithDelegate:(id)aDelegate priority:(int)aPriority swallowsTouches:(BOOL)swallow | ||
110 | { | ||
111 | if ((self = [super initWithDelegate:aDelegate priority:aPriority])) { | ||
112 | claimedTouches = [[NSMutableSet alloc] initWithCapacity:2]; | ||
113 | swallowsTouches = swallow; | ||
114 | |||
115 | if( [aDelegate respondsToSelector:@selector(ccTouchBegan:withEvent:)] ) | ||
116 | enabledSelectors_ |= kCCTouchSelectorBeganBit; | ||
117 | if( [aDelegate respondsToSelector:@selector(ccTouchMoved:withEvent:)] ) | ||
118 | enabledSelectors_ |= kCCTouchSelectorMovedBit; | ||
119 | if( [aDelegate respondsToSelector:@selector(ccTouchEnded:withEvent:)] ) | ||
120 | enabledSelectors_ |= kCCTouchSelectorEndedBit; | ||
121 | if( [aDelegate respondsToSelector:@selector(ccTouchCancelled:withEvent:)] ) | ||
122 | enabledSelectors_ |= kCCTouchSelectorCancelledBit; | ||
123 | } | ||
124 | |||
125 | return self; | ||
126 | } | ||
127 | |||
128 | - (void)dealloc { | ||
129 | [claimedTouches release]; | ||
130 | [super dealloc]; | ||
131 | } | ||
132 | @end | ||
133 | |||
134 | |||
135 | #endif // __IPHONE_OS_VERSION_MAX_ALLOWED \ No newline at end of file | ||
diff --git a/libs/cocos2d/Platforms/iOS/EAGLView.h b/libs/cocos2d/Platforms/iOS/EAGLView.h new file mode 100755 index 0000000..3b6c2f3 --- /dev/null +++ b/libs/cocos2d/Platforms/iOS/EAGLView.h | |||
@@ -0,0 +1,155 @@ | |||
1 | /* | ||
2 | |||
3 | ===== IMPORTANT ===== | ||
4 | |||
5 | This is sample code demonstrating API, technology or techniques in development. | ||
6 | Although this sample code has been reviewed for technical accuracy, it is not | ||
7 | final. Apple is supplying this information to help you plan for the adoption of | ||
8 | the technologies and programming interfaces described herein. This information | ||
9 | is subject to change, and software implemented based on this sample code should | ||
10 | be tested with final operating system software and final documentation. Newer | ||
11 | versions of this sample code may be provided with future seeds of the API or | ||
12 | technology. For information about updates to this and other developer | ||
13 | documentation, view the New & Updated sidebars in subsequent documentation | ||
14 | seeds. | ||
15 | |||
16 | ===================== | ||
17 | |||
18 | File: EAGLView.h | ||
19 | Abstract: Convenience class that wraps the CAEAGLLayer from CoreAnimation into a | ||
20 | UIView subclass. | ||
21 | |||
22 | Version: 1.3 | ||
23 | |||
24 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc. | ||
25 | ("Apple") in consideration of your agreement to the following terms, and your | ||
26 | use, installation, modification or redistribution of this Apple software | ||
27 | constitutes acceptance of these terms. If you do not agree with these terms, | ||
28 | please do not use, install, modify or redistribute this Apple software. | ||
29 | |||
30 | In consideration of your agreement to abide by the following terms, and subject | ||
31 | to these terms, Apple grants you a personal, non-exclusive license, under | ||
32 | Apple's copyrights in this original Apple software (the "Apple Software"), to | ||
33 | use, reproduce, modify and redistribute the Apple Software, with or without | ||
34 | modifications, in source and/or binary forms; provided that if you redistribute | ||
35 | the Apple Software in its entirety and without modifications, you must retain | ||
36 | this notice and the following text and disclaimers in all such redistributions | ||
37 | of the Apple Software. | ||
38 | Neither the name, trademarks, service marks or logos of Apple Inc. may be used | ||
39 | to endorse or promote products derived from the Apple Software without specific | ||
40 | prior written permission from Apple. Except as expressly stated in this notice, | ||
41 | no other rights or licenses, express or implied, are granted by Apple herein, | ||
42 | including but not limited to any patent rights that may be infringed by your | ||
43 | derivative works or by other works in which the Apple Software may be | ||
44 | incorporated. | ||
45 | |||
46 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO | ||
47 | WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED | ||
48 | WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
49 | PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN | ||
50 | COMBINATION WITH YOUR PRODUCTS. | ||
51 | |||
52 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR | ||
53 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | ||
54 | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
55 | ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR | ||
56 | DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF | ||
57 | CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF | ||
58 | APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
59 | |||
60 | Copyright (C) 2008 Apple Inc. All Rights Reserved. | ||
61 | |||
62 | */ | ||
63 | |||
64 | // Only compile this code on iOS. These files should NOT be included on your Mac project. | ||
65 | // But in case they are included, it won't be compiled. | ||
66 | #import <Availability.h> | ||
67 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
68 | |||
69 | #import <UIKit/UIKit.h> | ||
70 | #import <OpenGLES/EAGL.h> | ||
71 | #import <OpenGLES/EAGLDrawable.h> | ||
72 | #import <OpenGLES/ES1/gl.h> | ||
73 | #import <OpenGLES/ES1/glext.h> | ||
74 | |||
75 | #import "ESRenderer.h" | ||
76 | |||
77 | //CLASSES: | ||
78 | |||
79 | @class EAGLView; | ||
80 | @class EAGLSharegroup; | ||
81 | |||
82 | //PROTOCOLS: | ||
83 | |||
84 | @protocol EAGLTouchDelegate <NSObject> | ||
85 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; | ||
86 | - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; | ||
87 | - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; | ||
88 | - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; | ||
89 | @end | ||
90 | |||
91 | //CLASS INTERFACE: | ||
92 | |||
93 | /** EAGLView Class. | ||
94 | * This class wraps the CAEAGLLayer from CoreAnimation into a convenient UIView subclass. | ||
95 | * The view content is basically an EAGL surface you render your OpenGL scene into. | ||
96 | * Note that setting the view non-opaque will only work if the EAGL surface has an alpha channel. | ||
97 | */ | ||
98 | @interface EAGLView : UIView | ||
99 | { | ||
100 | id<ESRenderer> renderer_; | ||
101 | EAGLContext *context_; // weak ref | ||
102 | |||
103 | NSString *pixelformat_; | ||
104 | GLuint depthFormat_; | ||
105 | BOOL preserveBackbuffer_; | ||
106 | |||
107 | CGSize size_; | ||
108 | BOOL discardFramebufferSupported_; | ||
109 | id<EAGLTouchDelegate> touchDelegate_; | ||
110 | |||
111 | //fsaa addition | ||
112 | BOOL multisampling_; | ||
113 | unsigned int requestedSamples_; | ||
114 | } | ||
115 | |||
116 | /** creates an initializes an EAGLView with a frame and 0-bit depth buffer, and a RGB565 color buffer. */ | ||
117 | + (id) viewWithFrame:(CGRect)frame; | ||
118 | /** creates an initializes an EAGLView with a frame, a color buffer format, and 0-bit depth buffer. */ | ||
119 | + (id) viewWithFrame:(CGRect)frame pixelFormat:(NSString*)format; | ||
120 | /** creates an initializes an EAGLView with a frame, a color buffer format, and a depth buffer. */ | ||
121 | + (id) viewWithFrame:(CGRect)frame pixelFormat:(NSString*)format depthFormat:(GLuint)depth; | ||
122 | /** creates an initializes an EAGLView with a frame, a color buffer format, a depth buffer format, a sharegroup, and multisamping */ | ||
123 | + (id) viewWithFrame:(CGRect)frame pixelFormat:(NSString*)format depthFormat:(GLuint)depth preserveBackbuffer:(BOOL)retained sharegroup:(EAGLSharegroup*)sharegroup multiSampling:(BOOL)multisampling numberOfSamples:(unsigned int)samples; | ||
124 | |||
125 | /** Initializes an EAGLView with a frame and 0-bit depth buffer, and a RGB565 color buffer */ | ||
126 | - (id) initWithFrame:(CGRect)frame; //These also set the current context | ||
127 | /** Initializes an EAGLView with a frame, a color buffer format, and 0-bit depth buffer */ | ||
128 | - (id) initWithFrame:(CGRect)frame pixelFormat:(NSString*)format; | ||
129 | /** Initializes an EAGLView with a frame, a color buffer format, a depth buffer format, a sharegroup and multisampling support */ | ||
130 | - (id) initWithFrame:(CGRect)frame pixelFormat:(NSString*)format depthFormat:(GLuint)depth preserveBackbuffer:(BOOL)retained sharegroup:(EAGLSharegroup*)sharegroup multiSampling:(BOOL)sampling numberOfSamples:(unsigned int)nSamples; | ||
131 | |||
132 | /** pixel format: it could be RGBA8 (32-bit) or RGB565 (16-bit) */ | ||
133 | @property(nonatomic,readonly) NSString* pixelFormat; | ||
134 | /** depth format of the render buffer: 0, 16 or 24 bits*/ | ||
135 | @property(nonatomic,readonly) GLuint depthFormat; | ||
136 | |||
137 | /** returns surface size in pixels */ | ||
138 | @property(nonatomic,readonly) CGSize surfaceSize; | ||
139 | |||
140 | /** OpenGL context */ | ||
141 | @property(nonatomic,readonly) EAGLContext *context; | ||
142 | |||
143 | @property(nonatomic,readwrite) BOOL multiSampling; | ||
144 | |||
145 | /** touch delegate */ | ||
146 | @property(nonatomic,readwrite,assign) id<EAGLTouchDelegate> touchDelegate; | ||
147 | |||
148 | /** EAGLView uses double-buffer. This method swaps the buffers */ | ||
149 | -(void) swapBuffers; | ||
150 | |||
151 | - (CGPoint) convertPointFromViewToSurface:(CGPoint)point; | ||
152 | - (CGRect) convertRectFromViewToSurface:(CGRect)rect; | ||
153 | @end | ||
154 | |||
155 | #endif // __IPHONE_OS_VERSION_MAX_ALLOWED | ||
diff --git a/libs/cocos2d/Platforms/iOS/EAGLView.m b/libs/cocos2d/Platforms/iOS/EAGLView.m new file mode 100755 index 0000000..d5ead65 --- /dev/null +++ b/libs/cocos2d/Platforms/iOS/EAGLView.m | |||
@@ -0,0 +1,343 @@ | |||
1 | /* | ||
2 | |||
3 | ===== IMPORTANT ===== | ||
4 | |||
5 | This is sample code demonstrating API, technology or techniques in development. | ||
6 | Although this sample code has been reviewed for technical accuracy, it is not | ||
7 | final. Apple is supplying this information to help you plan for the adoption of | ||
8 | the technologies and programming interfaces described herein. This information | ||
9 | is subject to change, and software implemented based on this sample code should | ||
10 | be tested with final operating system software and final documentation. Newer | ||
11 | versions of this sample code may be provided with future seeds of the API or | ||
12 | technology. For information about updates to this and other developer | ||
13 | documentation, view the New & Updated sidebars in subsequent documentation | ||
14 | seeds. | ||
15 | |||
16 | ===================== | ||
17 | |||
18 | File: EAGLView.m | ||
19 | Abstract: Convenience class that wraps the CAEAGLLayer from CoreAnimation into a | ||
20 | UIView subclass. | ||
21 | |||
22 | Version: 1.3 | ||
23 | |||
24 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc. | ||
25 | ("Apple") in consideration of your agreement to the following terms, and your | ||
26 | use, installation, modification or redistribution of this Apple software | ||
27 | constitutes acceptance of these terms. If you do not agree with these terms, | ||
28 | please do not use, install, modify or redistribute this Apple software. | ||
29 | |||
30 | In consideration of your agreement to abide by the following terms, and subject | ||
31 | to these terms, Apple grants you a personal, non-exclusive license, under | ||
32 | Apple's copyrights in this original Apple software (the "Apple Software"), to | ||
33 | use, reproduce, modify and redistribute the Apple Software, with or without | ||
34 | modifications, in source and/or binary forms; provided that if you redistribute | ||
35 | the Apple Software in its entirety and without modifications, you must retain | ||
36 | this notice and the following text and disclaimers in all such redistributions | ||
37 | of the Apple Software. | ||
38 | Neither the name, trademarks, service marks or logos of Apple Inc. may be used | ||
39 | to endorse or promote products derived from the Apple Software without specific | ||
40 | prior written permission from Apple. Except as expressly stated in this notice, | ||
41 | no other rights or licenses, express or implied, are granted by Apple herein, | ||
42 | including but not limited to any patent rights that may be infringed by your | ||
43 | derivative works or by other works in which the Apple Software may be | ||
44 | incorporated. | ||
45 | |||
46 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO | ||
47 | WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED | ||
48 | WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
49 | PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN | ||
50 | COMBINATION WITH YOUR PRODUCTS. | ||
51 | |||
52 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR | ||
53 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | ||
54 | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
55 | ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR | ||
56 | DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF | ||
57 | CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF | ||
58 | APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
59 | |||
60 | Copyright (C) 2008 Apple Inc. All Rights Reserved. | ||
61 | |||
62 | */ | ||
63 | |||
64 | // Only compile this code on iOS. These files should NOT be included on your Mac project. | ||
65 | // But in case they are included, it won't be compiled. | ||
66 | #import <Availability.h> | ||
67 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
68 | |||
69 | #import <QuartzCore/QuartzCore.h> | ||
70 | |||
71 | #import "EAGLView.h" | ||
72 | #import "ES1Renderer.h" | ||
73 | #import "../../CCDirector.h" | ||
74 | #import "../../ccMacros.h" | ||
75 | #import "../../CCConfiguration.h" | ||
76 | #import "../../Support/OpenGL_Internal.h" | ||
77 | |||
78 | |||
79 | //CLASS IMPLEMENTATIONS: | ||
80 | |||
81 | @interface EAGLView (Private) | ||
82 | - (BOOL) setupSurfaceWithSharegroup:(EAGLSharegroup*)sharegroup; | ||
83 | - (unsigned int) convertPixelFormat:(NSString*) pixelFormat; | ||
84 | @end | ||
85 | |||
86 | @implementation EAGLView | ||
87 | |||
88 | @synthesize surfaceSize=size_; | ||
89 | @synthesize pixelFormat=pixelformat_, depthFormat=depthFormat_; | ||
90 | @synthesize touchDelegate=touchDelegate_; | ||
91 | @synthesize context=context_; | ||
92 | @synthesize multiSampling=multiSampling_; | ||
93 | |||
94 | + (Class) layerClass | ||
95 | { | ||
96 | return [CAEAGLLayer class]; | ||
97 | } | ||
98 | |||
99 | + (id) viewWithFrame:(CGRect)frame | ||
100 | { | ||
101 | return [[[self alloc] initWithFrame:frame] autorelease]; | ||
102 | } | ||
103 | |||
104 | + (id) viewWithFrame:(CGRect)frame pixelFormat:(NSString*)format | ||
105 | { | ||
106 | return [[[self alloc] initWithFrame:frame pixelFormat:format] autorelease]; | ||
107 | } | ||
108 | |||
109 | + (id) viewWithFrame:(CGRect)frame pixelFormat:(NSString*)format depthFormat:(GLuint)depth | ||
110 | { | ||
111 | return [[[self alloc] initWithFrame:frame pixelFormat:format depthFormat:depth preserveBackbuffer:NO sharegroup:nil multiSampling:NO numberOfSamples:0] autorelease]; | ||
112 | } | ||
113 | |||
114 | + (id) viewWithFrame:(CGRect)frame pixelFormat:(NSString*)format depthFormat:(GLuint)depth preserveBackbuffer:(BOOL)retained sharegroup:(EAGLSharegroup*)sharegroup multiSampling:(BOOL)multisampling numberOfSamples:(unsigned int)samples | ||
115 | { | ||
116 | return [[[self alloc] initWithFrame:frame pixelFormat:format depthFormat:depth preserveBackbuffer:retained sharegroup:sharegroup multiSampling:multisampling numberOfSamples:samples] autorelease]; | ||
117 | } | ||
118 | |||
119 | - (id) initWithFrame:(CGRect)frame | ||
120 | { | ||
121 | return [self initWithFrame:frame pixelFormat:kEAGLColorFormatRGB565 depthFormat:0 preserveBackbuffer:NO sharegroup:nil multiSampling:NO numberOfSamples:0]; | ||
122 | } | ||
123 | |||
124 | - (id) initWithFrame:(CGRect)frame pixelFormat:(NSString*)format | ||
125 | { | ||
126 | return [self initWithFrame:frame pixelFormat:format depthFormat:0 preserveBackbuffer:NO sharegroup:nil multiSampling:NO numberOfSamples:0]; | ||
127 | } | ||
128 | |||
129 | - (id) initWithFrame:(CGRect)frame pixelFormat:(NSString*)format depthFormat:(GLuint)depth preserveBackbuffer:(BOOL)retained sharegroup:(EAGLSharegroup*)sharegroup multiSampling:(BOOL)sampling numberOfSamples:(unsigned int)nSamples | ||
130 | { | ||
131 | if((self = [super initWithFrame:frame])) | ||
132 | { | ||
133 | pixelformat_ = format; | ||
134 | depthFormat_ = depth; | ||
135 | multiSampling_ = sampling; | ||
136 | requestedSamples_ = nSamples; | ||
137 | preserveBackbuffer_ = retained; | ||
138 | |||
139 | if( ! [self setupSurfaceWithSharegroup:sharegroup] ) { | ||
140 | [self release]; | ||
141 | return nil; | ||
142 | } | ||
143 | } | ||
144 | |||
145 | return self; | ||
146 | } | ||
147 | |||
148 | -(id) initWithCoder:(NSCoder *)aDecoder | ||
149 | { | ||
150 | if( (self = [super initWithCoder:aDecoder]) ) { | ||
151 | |||
152 | CAEAGLLayer* eaglLayer = (CAEAGLLayer*)[self layer]; | ||
153 | |||
154 | pixelformat_ = kEAGLColorFormatRGB565; | ||
155 | depthFormat_ = 0; // GL_DEPTH_COMPONENT24_OES; | ||
156 | multiSampling_= NO; | ||
157 | requestedSamples_ = 0; | ||
158 | size_ = [eaglLayer bounds].size; | ||
159 | |||
160 | if( ! [self setupSurfaceWithSharegroup:nil] ) { | ||
161 | [self release]; | ||
162 | return nil; | ||
163 | } | ||
164 | } | ||
165 | |||
166 | return self; | ||
167 | } | ||
168 | |||
169 | -(BOOL) setupSurfaceWithSharegroup:(EAGLSharegroup*)sharegroup | ||
170 | { | ||
171 | CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; | ||
172 | |||
173 | eaglLayer.opaque = YES; | ||
174 | eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: | ||
175 | [NSNumber numberWithBool:preserveBackbuffer_], kEAGLDrawablePropertyRetainedBacking, | ||
176 | pixelformat_, kEAGLDrawablePropertyColorFormat, nil]; | ||
177 | |||
178 | |||
179 | renderer_ = [[ES1Renderer alloc] initWithDepthFormat:depthFormat_ | ||
180 | withPixelFormat:[self convertPixelFormat:pixelformat_] | ||
181 | withSharegroup:sharegroup | ||
182 | withMultiSampling:multiSampling_ | ||
183 | withNumberOfSamples:requestedSamples_]; | ||
184 | if (!renderer_) | ||
185 | return NO; | ||
186 | |||
187 | context_ = [renderer_ context]; | ||
188 | [context_ renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:eaglLayer]; | ||
189 | |||
190 | discardFramebufferSupported_ = [[CCConfiguration sharedConfiguration] supportsDiscardFramebuffer]; | ||
191 | |||
192 | return YES; | ||
193 | } | ||
194 | |||
195 | - (void) dealloc | ||
196 | { | ||
197 | CCLOGINFO(@"cocos2d: deallocing %@", self); | ||
198 | |||
199 | |||
200 | [renderer_ release]; | ||
201 | [super dealloc]; | ||
202 | } | ||
203 | |||
204 | - (void) layoutSubviews | ||
205 | { | ||
206 | size_ = [renderer_ backingSize]; | ||
207 | |||
208 | [renderer_ resizeFromLayer:(CAEAGLLayer*)self.layer]; | ||
209 | |||
210 | // Issue #914 #924 | ||
211 | CCDirector *director = [CCDirector sharedDirector]; | ||
212 | [director reshapeProjection:size_]; | ||
213 | |||
214 | // Avoid flicker. Issue #350 | ||
215 | [director performSelectorOnMainThread:@selector(drawScene) withObject:nil waitUntilDone:YES]; | ||
216 | } | ||
217 | |||
218 | - (void) swapBuffers | ||
219 | { | ||
220 | // IMPORTANT: | ||
221 | // - preconditions | ||
222 | // -> context_ MUST be the OpenGL context | ||
223 | // -> renderbuffer_ must be the the RENDER BUFFER | ||
224 | |||
225 | #ifdef __IPHONE_4_0 | ||
226 | |||
227 | if (multiSampling_) | ||
228 | { | ||
229 | /* Resolve from msaaFramebuffer to resolveFramebuffer */ | ||
230 | //glDisable(GL_SCISSOR_TEST); | ||
231 | glBindFramebufferOES(GL_READ_FRAMEBUFFER_APPLE, [renderer_ msaaFrameBuffer]); | ||
232 | glBindFramebufferOES(GL_DRAW_FRAMEBUFFER_APPLE, [renderer_ defaultFrameBuffer]); | ||
233 | glResolveMultisampleFramebufferAPPLE(); | ||
234 | } | ||
235 | |||
236 | if( discardFramebufferSupported_) | ||
237 | { | ||
238 | if (multiSampling_) | ||
239 | { | ||
240 | if (depthFormat_) | ||
241 | { | ||
242 | GLenum attachments[] = {GL_COLOR_ATTACHMENT0_OES, GL_DEPTH_ATTACHMENT_OES}; | ||
243 | glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 2, attachments); | ||
244 | } | ||
245 | else | ||
246 | { | ||
247 | GLenum attachments[] = {GL_COLOR_ATTACHMENT0_OES}; | ||
248 | glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 1, attachments); | ||
249 | } | ||
250 | |||
251 | glBindRenderbufferOES(GL_RENDERBUFFER_OES, [renderer_ colorRenderBuffer]); | ||
252 | |||
253 | } | ||
254 | |||
255 | // not MSAA | ||
256 | else if (depthFormat_ ) { | ||
257 | GLenum attachments[] = { GL_DEPTH_ATTACHMENT_OES}; | ||
258 | glDiscardFramebufferEXT(GL_FRAMEBUFFER_OES, 1, attachments); | ||
259 | } | ||
260 | } | ||
261 | |||
262 | #endif // __IPHONE_4_0 | ||
263 | |||
264 | if(![context_ presentRenderbuffer:GL_RENDERBUFFER_OES]) | ||
265 | CCLOG(@"cocos2d: Failed to swap renderbuffer in %s\n", __FUNCTION__); | ||
266 | |||
267 | #if COCOS2D_DEBUG | ||
268 | CHECK_GL_ERROR(); | ||
269 | #endif | ||
270 | |||
271 | // We can safely re-bind the framebuffer here, since this will be the | ||
272 | // 1st instruction of the new main loop | ||
273 | if( multiSampling_ ) | ||
274 | glBindFramebufferOES(GL_FRAMEBUFFER_OES, [renderer_ msaaFrameBuffer]); | ||
275 | } | ||
276 | |||
277 | - (unsigned int) convertPixelFormat:(NSString*) pixelFormat | ||
278 | { | ||
279 | // define the pixel format | ||
280 | GLenum pFormat; | ||
281 | |||
282 | |||
283 | if([pixelFormat isEqualToString:@"EAGLColorFormat565"]) | ||
284 | pFormat = GL_RGB565_OES; | ||
285 | else | ||
286 | pFormat = GL_RGBA8_OES; | ||
287 | |||
288 | return pFormat; | ||
289 | } | ||
290 | |||
291 | #pragma mark EAGLView - Point conversion | ||
292 | |||
293 | - (CGPoint) convertPointFromViewToSurface:(CGPoint)point | ||
294 | { | ||
295 | CGRect bounds = [self bounds]; | ||
296 | |||
297 | return CGPointMake((point.x - bounds.origin.x) / bounds.size.width * size_.width, (point.y - bounds.origin.y) / bounds.size.height * size_.height); | ||
298 | } | ||
299 | |||
300 | - (CGRect) convertRectFromViewToSurface:(CGRect)rect | ||
301 | { | ||
302 | CGRect bounds = [self bounds]; | ||
303 | |||
304 | return CGRectMake((rect.origin.x - bounds.origin.x) / bounds.size.width * size_.width, (rect.origin.y - bounds.origin.y) / bounds.size.height * size_.height, rect.size.width / bounds.size.width * size_.width, rect.size.height / bounds.size.height * size_.height); | ||
305 | } | ||
306 | |||
307 | // Pass the touches to the superview | ||
308 | #pragma mark EAGLView - Touch Delegate | ||
309 | |||
310 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event | ||
311 | { | ||
312 | if(touchDelegate_) | ||
313 | { | ||
314 | [touchDelegate_ touchesBegan:touches withEvent:event]; | ||
315 | } | ||
316 | } | ||
317 | |||
318 | - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event | ||
319 | { | ||
320 | if(touchDelegate_) | ||
321 | { | ||
322 | [touchDelegate_ touchesMoved:touches withEvent:event]; | ||
323 | } | ||
324 | } | ||
325 | |||
326 | - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event | ||
327 | { | ||
328 | if(touchDelegate_) | ||
329 | { | ||
330 | [touchDelegate_ touchesEnded:touches withEvent:event]; | ||
331 | } | ||
332 | } | ||
333 | - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event | ||
334 | { | ||
335 | if(touchDelegate_) | ||
336 | { | ||
337 | [touchDelegate_ touchesCancelled:touches withEvent:event]; | ||
338 | } | ||
339 | } | ||
340 | |||
341 | @end | ||
342 | |||
343 | #endif // __IPHONE_OS_VERSION_MAX_ALLOWED \ No newline at end of file | ||
diff --git a/libs/cocos2d/Platforms/iOS/ES1Renderer.h b/libs/cocos2d/Platforms/iOS/ES1Renderer.h new file mode 100755 index 0000000..fd946a7 --- /dev/null +++ b/libs/cocos2d/Platforms/iOS/ES1Renderer.h | |||
@@ -0,0 +1,72 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 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 | * File autogenerated with Xcode. Adapted for cocos2d needs. | ||
27 | */ | ||
28 | |||
29 | // Only compile this code on iOS. These files should NOT be included on your Mac project. | ||
30 | // But in case they are included, it won't be compiled. | ||
31 | #import <Availability.h> | ||
32 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
33 | |||
34 | |||
35 | #import "ESRenderer.h" | ||
36 | |||
37 | #import <OpenGLES/ES1/gl.h> | ||
38 | #import <OpenGLES/ES1/glext.h> | ||
39 | |||
40 | @interface ES1Renderer : NSObject <ESRenderer> | ||
41 | { | ||
42 | // The pixel dimensions of the CAEAGLLayer | ||
43 | GLint backingWidth_; | ||
44 | GLint backingHeight_; | ||
45 | |||
46 | unsigned int samplesToUse_; | ||
47 | BOOL multiSampling_; | ||
48 | |||
49 | unsigned int depthFormat_; | ||
50 | unsigned int pixelFormat_; | ||
51 | |||
52 | // The OpenGL ES names for the framebuffer and renderbuffer used to render to this view | ||
53 | GLuint defaultFramebuffer_; | ||
54 | GLuint colorRenderbuffer_; | ||
55 | GLuint depthBuffer_; | ||
56 | |||
57 | |||
58 | //buffers for MSAA | ||
59 | GLuint msaaFramebuffer_; | ||
60 | GLuint msaaColorbuffer_; | ||
61 | |||
62 | EAGLContext *context_; | ||
63 | } | ||
64 | |||
65 | /** EAGLContext */ | ||
66 | @property (nonatomic,readonly) EAGLContext* context; | ||
67 | |||
68 | - (BOOL)resizeFromLayer:(CAEAGLLayer *)layer; | ||
69 | |||
70 | @end | ||
71 | |||
72 | #endif // __IPHONE_OS_VERSION_MAX_ALLOWED | ||
diff --git a/libs/cocos2d/Platforms/iOS/ES1Renderer.m b/libs/cocos2d/Platforms/iOS/ES1Renderer.m new file mode 100755 index 0000000..398d946 --- /dev/null +++ b/libs/cocos2d/Platforms/iOS/ES1Renderer.m | |||
@@ -0,0 +1,259 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 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 | * File autogenerated with Xcode. Adapted for cocos2d needs. | ||
27 | */ | ||
28 | |||
29 | // Only compile this code on iOS. These files should NOT be included on your Mac project. | ||
30 | // But in case they are included, it won't be compiled. | ||
31 | #import <Availability.h> | ||
32 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
33 | |||
34 | #import "ES1Renderer.h" | ||
35 | #import "../../Support/OpenGL_Internal.h" | ||
36 | #import "../../ccMacros.h" | ||
37 | |||
38 | |||
39 | @interface ES1Renderer (private) | ||
40 | |||
41 | - (GLenum) convertPixelFormat:(int) pixelFormat; | ||
42 | |||
43 | @end | ||
44 | |||
45 | |||
46 | @implementation ES1Renderer | ||
47 | |||
48 | @synthesize context=context_; | ||
49 | |||
50 | - (id) initWithDepthFormat:(unsigned int)depthFormat withPixelFormat:(unsigned int)pixelFormat withSharegroup:(EAGLSharegroup*)sharegroup withMultiSampling:(BOOL) multiSampling withNumberOfSamples:(unsigned int) requestedSamples | ||
51 | { | ||
52 | if ((self = [super init])) | ||
53 | { | ||
54 | if ( sharegroup == nil ) | ||
55 | { | ||
56 | context_ = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1]; | ||
57 | } | ||
58 | else | ||
59 | { | ||
60 | context_ = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1 sharegroup:sharegroup]; | ||
61 | } | ||
62 | |||
63 | if (!context_ || ![EAGLContext setCurrentContext:context_]) | ||
64 | { | ||
65 | [self release]; | ||
66 | return nil; | ||
67 | } | ||
68 | |||
69 | // Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer | ||
70 | glGenFramebuffersOES(1, &defaultFramebuffer_); | ||
71 | NSAssert( defaultFramebuffer_, @"Can't create default frame buffer"); | ||
72 | glGenRenderbuffersOES(1, &colorRenderbuffer_); | ||
73 | NSAssert( colorRenderbuffer_, @"Can't create default render buffer"); | ||
74 | |||
75 | glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer_); | ||
76 | glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer_); | ||
77 | glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorRenderbuffer_); | ||
78 | |||
79 | depthFormat_ = depthFormat; | ||
80 | |||
81 | if( depthFormat_ ) { | ||
82 | // glGenRenderbuffersOES(1, &depthBuffer_); | ||
83 | // glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthBuffer_); | ||
84 | // glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthFormat_, 100, 100); | ||
85 | // glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthBuffer_); | ||
86 | |||
87 | // default buffer | ||
88 | // glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer_); | ||
89 | } | ||
90 | |||
91 | pixelFormat_ = pixelFormat; | ||
92 | multiSampling_ = multiSampling; | ||
93 | if (multiSampling_) | ||
94 | { | ||
95 | GLint maxSamplesAllowed; | ||
96 | glGetIntegerv(GL_MAX_SAMPLES_APPLE, &maxSamplesAllowed); | ||
97 | samplesToUse_ = MIN(maxSamplesAllowed,requestedSamples); | ||
98 | |||
99 | /* Create the MSAA framebuffer (offscreen) */ | ||
100 | glGenFramebuffersOES(1, &msaaFramebuffer_); | ||
101 | glBindFramebufferOES(GL_FRAMEBUFFER_OES, msaaFramebuffer_); | ||
102 | |||
103 | } | ||
104 | |||
105 | CHECK_GL_ERROR(); | ||
106 | } | ||
107 | |||
108 | return self; | ||
109 | } | ||
110 | |||
111 | - (BOOL)resizeFromLayer:(CAEAGLLayer *)layer | ||
112 | { | ||
113 | // Allocate color buffer backing based on the current layer size | ||
114 | |||
115 | glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer_); | ||
116 | |||
117 | if (![context_ renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:layer]) | ||
118 | { | ||
119 | CCLOG(@"failed to call context"); | ||
120 | } | ||
121 | |||
122 | glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth_); | ||
123 | glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight_); | ||
124 | |||
125 | CCLOG(@"cocos2d: surface size: %dx%d", (int)backingWidth_, (int)backingHeight_); | ||
126 | |||
127 | if (multiSampling_) | ||
128 | { | ||
129 | |||
130 | if ( msaaColorbuffer_) { | ||
131 | glDeleteRenderbuffersOES(1, &msaaColorbuffer_); | ||
132 | msaaColorbuffer_ = 0; | ||
133 | } | ||
134 | |||
135 | /* Create the offscreen MSAA color buffer. | ||
136 | After rendering, the contents of this will be blitted into ColorRenderbuffer */ | ||
137 | |||
138 | //msaaFrameBuffer needs to be binded | ||
139 | glBindFramebufferOES(GL_FRAMEBUFFER_OES, msaaFramebuffer_); | ||
140 | glGenRenderbuffersOES(1, &msaaColorbuffer_); | ||
141 | glBindRenderbufferOES(GL_RENDERBUFFER_OES, msaaColorbuffer_); | ||
142 | glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER_OES, samplesToUse_,pixelFormat_ , backingWidth_, backingHeight_); | ||
143 | glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, msaaColorbuffer_); | ||
144 | |||
145 | if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) | ||
146 | { | ||
147 | CCLOG(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES)); | ||
148 | return NO; | ||
149 | } | ||
150 | } | ||
151 | |||
152 | if (depthFormat_) | ||
153 | { | ||
154 | if( ! depthBuffer_ ) | ||
155 | glGenRenderbuffersOES(1, &depthBuffer_); | ||
156 | |||
157 | glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthBuffer_); | ||
158 | if( multiSampling_ ) | ||
159 | glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER_OES, samplesToUse_, depthFormat_,backingWidth_, backingHeight_); | ||
160 | else | ||
161 | glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthFormat_, backingWidth_, backingHeight_); | ||
162 | glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthBuffer_); | ||
163 | |||
164 | // bind color buffer | ||
165 | glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer_); | ||
166 | } | ||
167 | |||
168 | glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer_); | ||
169 | |||
170 | if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) | ||
171 | { | ||
172 | CCLOG(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES)); | ||
173 | return NO; | ||
174 | } | ||
175 | |||
176 | CHECK_GL_ERROR(); | ||
177 | |||
178 | return YES; | ||
179 | } | ||
180 | |||
181 | -(CGSize) backingSize | ||
182 | { | ||
183 | return CGSizeMake( backingWidth_, backingHeight_); | ||
184 | } | ||
185 | |||
186 | - (NSString*) description | ||
187 | { | ||
188 | return [NSString stringWithFormat:@"<%@ = %08X | size = %ix%i>", [self class], self, backingWidth_, backingHeight_]; | ||
189 | } | ||
190 | |||
191 | |||
192 | - (void)dealloc | ||
193 | { | ||
194 | CCLOGINFO(@"cocos2d: deallocing %@", self); | ||
195 | |||
196 | // Tear down GL | ||
197 | if(defaultFramebuffer_) | ||
198 | { | ||
199 | glDeleteFramebuffersOES(1, &defaultFramebuffer_); | ||
200 | defaultFramebuffer_ = 0; | ||
201 | } | ||
202 | |||
203 | if(colorRenderbuffer_) | ||
204 | { | ||
205 | glDeleteRenderbuffersOES(1, &colorRenderbuffer_); | ||
206 | colorRenderbuffer_ = 0; | ||
207 | } | ||
208 | |||
209 | if( depthBuffer_ ) | ||
210 | { | ||
211 | glDeleteRenderbuffersOES(1, &depthBuffer_); | ||
212 | depthBuffer_ = 0; | ||
213 | } | ||
214 | |||
215 | if ( msaaColorbuffer_) | ||
216 | { | ||
217 | glDeleteRenderbuffersOES(1, &msaaColorbuffer_); | ||
218 | msaaColorbuffer_ = 0; | ||
219 | } | ||
220 | |||
221 | if ( msaaFramebuffer_) | ||
222 | { | ||
223 | glDeleteRenderbuffersOES(1, &msaaFramebuffer_); | ||
224 | msaaFramebuffer_ = 0; | ||
225 | } | ||
226 | |||
227 | // Tear down context | ||
228 | if ([EAGLContext currentContext] == context_) | ||
229 | [EAGLContext setCurrentContext:nil]; | ||
230 | |||
231 | [context_ release]; | ||
232 | context_ = nil; | ||
233 | |||
234 | [super dealloc]; | ||
235 | } | ||
236 | |||
237 | - (unsigned int) colorRenderBuffer | ||
238 | { | ||
239 | return colorRenderbuffer_; | ||
240 | } | ||
241 | |||
242 | - (unsigned int) defaultFrameBuffer | ||
243 | { | ||
244 | return defaultFramebuffer_; | ||
245 | } | ||
246 | |||
247 | - (unsigned int) msaaFrameBuffer | ||
248 | { | ||
249 | return msaaFramebuffer_; | ||
250 | } | ||
251 | |||
252 | - (unsigned int) msaaColorBuffer | ||
253 | { | ||
254 | return msaaColorbuffer_; | ||
255 | } | ||
256 | |||
257 | @end | ||
258 | |||
259 | #endif // __IPHONE_OS_VERSION_MAX_ALLOWED | ||
diff --git a/libs/cocos2d/Platforms/iOS/ESRenderer.h b/libs/cocos2d/Platforms/iOS/ESRenderer.h new file mode 100755 index 0000000..e612eee --- /dev/null +++ b/libs/cocos2d/Platforms/iOS/ESRenderer.h | |||
@@ -0,0 +1,54 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 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 | * File autogenerated with Xcode. Adapted for cocos2d needs. | ||
27 | */ | ||
28 | |||
29 | // Only compile this code on iOS. These files should NOT be included on your Mac project. | ||
30 | // But in case they are included, it won't be compiled. | ||
31 | #import <Availability.h> | ||
32 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
33 | |||
34 | #import <QuartzCore/QuartzCore.h> | ||
35 | |||
36 | #import <OpenGLES/EAGL.h> | ||
37 | #import <OpenGLES/EAGLDrawable.h> | ||
38 | |||
39 | @protocol ESRenderer <NSObject> | ||
40 | |||
41 | - (id) initWithDepthFormat:(unsigned int)depthFormat withPixelFormat:(unsigned int)pixelFormat withSharegroup:(EAGLSharegroup*)sharegroup withMultiSampling:(BOOL) multiSampling withNumberOfSamples:(unsigned int) requestedSamples; | ||
42 | |||
43 | - (BOOL) resizeFromLayer:(CAEAGLLayer *)layer; | ||
44 | |||
45 | - (EAGLContext*) context; | ||
46 | - (CGSize) backingSize; | ||
47 | |||
48 | - (unsigned int) colorRenderBuffer; | ||
49 | - (unsigned int) defaultFrameBuffer; | ||
50 | - (unsigned int) msaaFrameBuffer; | ||
51 | - (unsigned int) msaaColorBuffer; | ||
52 | @end | ||
53 | |||
54 | #endif // __IPHONE_OS_VERSION_MAX_ALLOWED | ||
diff --git a/libs/cocos2d/Platforms/iOS/glu.c b/libs/cocos2d/Platforms/iOS/glu.c new file mode 100755 index 0000000..2e00d5f --- /dev/null +++ b/libs/cocos2d/Platforms/iOS/glu.c | |||
@@ -0,0 +1,113 @@ | |||
1 | // | ||
2 | // cocos2d (incomplete) GLU implementation | ||
3 | // | ||
4 | // gluLookAt and gluPerspective from: | ||
5 | // http://jet.ro/creations (San Angeles Observation) | ||
6 | // | ||
7 | // | ||
8 | |||
9 | // Only compile this code on iOS. These files should NOT be included on your Mac project. | ||
10 | // But in case they are included, it won't be compiled. | ||
11 | #import <Availability.h> | ||
12 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
13 | |||
14 | #import <OpenGLES/ES1/gl.h> | ||
15 | #import <math.h> | ||
16 | #import "../../Support/OpenGL_Internal.h" | ||
17 | #include "glu.h" | ||
18 | |||
19 | void gluPerspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar) | ||
20 | { | ||
21 | GLfloat xmin, xmax, ymin, ymax; | ||
22 | |||
23 | ymax = zNear * (GLfloat)tanf(fovy * (float)M_PI / 360); | ||
24 | ymin = -ymax; | ||
25 | xmin = ymin * aspect; | ||
26 | xmax = ymax * aspect; | ||
27 | |||
28 | glFrustumf(xmin, xmax, | ||
29 | ymin, ymax, | ||
30 | zNear, zFar); | ||
31 | } | ||
32 | |||
33 | void gluLookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez, | ||
34 | GLfloat centerx, GLfloat centery, GLfloat centerz, | ||
35 | GLfloat upx, GLfloat upy, GLfloat upz) | ||
36 | { | ||
37 | GLfloat m[16]; | ||
38 | GLfloat x[3], y[3], z[3]; | ||
39 | GLfloat mag; | ||
40 | |||
41 | /* Make rotation matrix */ | ||
42 | |||
43 | /* Z vector */ | ||
44 | z[0] = eyex - centerx; | ||
45 | z[1] = eyey - centery; | ||
46 | z[2] = eyez - centerz; | ||
47 | mag = (float)sqrtf(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]); | ||
48 | if (mag) { | ||
49 | z[0] /= mag; | ||
50 | z[1] /= mag; | ||
51 | z[2] /= mag; | ||
52 | } | ||
53 | |||
54 | /* Y vector */ | ||
55 | y[0] = upx; | ||
56 | y[1] = upy; | ||
57 | y[2] = upz; | ||
58 | |||
59 | /* X vector = Y cross Z */ | ||
60 | x[0] = y[1] * z[2] - y[2] * z[1]; | ||
61 | x[1] = -y[0] * z[2] + y[2] * z[0]; | ||
62 | x[2] = y[0] * z[1] - y[1] * z[0]; | ||
63 | |||
64 | /* Recompute Y = Z cross X */ | ||
65 | y[0] = z[1] * x[2] - z[2] * x[1]; | ||
66 | y[1] = -z[0] * x[2] + z[2] * x[0]; | ||
67 | y[2] = z[0] * x[1] - z[1] * x[0]; | ||
68 | |||
69 | /* cross product gives area of parallelogram, which is < 1.0 for | ||
70 | * non-perpendicular unit-length vectors; so normalize x, y here | ||
71 | */ | ||
72 | |||
73 | mag = (float)sqrtf(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]); | ||
74 | if (mag) { | ||
75 | x[0] /= mag; | ||
76 | x[1] /= mag; | ||
77 | x[2] /= mag; | ||
78 | } | ||
79 | |||
80 | mag = (float)sqrtf(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]); | ||
81 | if (mag) { | ||
82 | y[0] /= mag; | ||
83 | y[1] /= mag; | ||
84 | y[2] /= mag; | ||
85 | } | ||
86 | |||
87 | #define M(row,col) m[col*4+row] | ||
88 | M(0, 0) = x[0]; | ||
89 | M(0, 1) = x[1]; | ||
90 | M(0, 2) = x[2]; | ||
91 | M(0, 3) = 0.0f; | ||
92 | M(1, 0) = y[0]; | ||
93 | M(1, 1) = y[1]; | ||
94 | M(1, 2) = y[2]; | ||
95 | M(1, 3) = 0.0f; | ||
96 | M(2, 0) = z[0]; | ||
97 | M(2, 1) = z[1]; | ||
98 | M(2, 2) = z[2]; | ||
99 | M(2, 3) = 0.0f; | ||
100 | M(3, 0) = 0.0f; | ||
101 | M(3, 1) = 0.0f; | ||
102 | M(3, 2) = 0.0f; | ||
103 | M(3, 3) = 1.0f; | ||
104 | #undef M | ||
105 | |||
106 | glMultMatrixf(m); | ||
107 | |||
108 | |||
109 | /* Translate Eye to Origin */ | ||
110 | glTranslatef(-eyex, -eyey, -eyez); | ||
111 | } | ||
112 | |||
113 | #endif // __IPHONE_OS_VERSION_MAX_ALLOWED | ||
diff --git a/libs/cocos2d/Platforms/iOS/glu.h b/libs/cocos2d/Platforms/iOS/glu.h new file mode 100755 index 0000000..86dcac7 --- /dev/null +++ b/libs/cocos2d/Platforms/iOS/glu.h | |||
@@ -0,0 +1,29 @@ | |||
1 | // | ||
2 | // cocos2d GLU implementation | ||
3 | // | ||
4 | // implementation of GLU functions | ||
5 | // | ||
6 | #ifndef __COCOS2D_GLU_H | ||
7 | #define __COCOS2D_GLU_H | ||
8 | |||
9 | // Only compile this code on iOS. These files should NOT be included on your Mac project. | ||
10 | // But in case they are included, it won't be compiled. | ||
11 | #import <Availability.h> | ||
12 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED | ||
13 | |||
14 | #import <OpenGLES/ES1/gl.h> | ||
15 | |||
16 | /** | ||
17 | @file | ||
18 | cocos2d OpenGL GLU implementation | ||
19 | */ | ||
20 | |||
21 | /** OpenGL gluLookAt implementation */ | ||
22 | void gluLookAt(float eyeX, float eyeY, float eyeZ, float lookAtX, float lookAtY, float lookAtZ, float upX, float upY, float upZ); | ||
23 | /** OpenGL gluPerspective implementation */ | ||
24 | void gluPerspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar); | ||
25 | |||
26 | #endif // __IPHONE_OS_VERSION_MAX_ALLOWED | ||
27 | |||
28 | #endif /* __COCOS2D_GLU_H */ | ||
29 | |||