summary refs log tree commit diff stats
path: root/libs/cocos2d/Platforms/Mac/MacGLView.m
diff options
context:
space:
mode:
Diffstat (limited to 'libs/cocos2d/Platforms/Mac/MacGLView.m')
-rwxr-xr-xlibs/cocos2d/Platforms/Mac/MacGLView.m242
1 files changed, 242 insertions, 0 deletions
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