summary refs log tree commit diff stats
path: root/libs/cocos2d/Platforms/iOS/EAGLView.m
diff options
context:
space:
mode:
authorStarla Insigna <starla4444@gmail.com>2011-07-30 11:19:14 -0400
committerStarla Insigna <starla4444@gmail.com>2011-07-30 11:19:14 -0400
commit9cd57b731ab1c666d4a1cb725538fdc137763d12 (patch)
tree5bac45ae5157a1cb10c6e45500cbf72789917980 /libs/cocos2d/Platforms/iOS/EAGLView.m
downloadcartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.tar.gz
cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.tar.bz2
cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.zip
Initial commit (version 0.2.1)
Diffstat (limited to 'libs/cocos2d/Platforms/iOS/EAGLView.m')
-rwxr-xr-xlibs/cocos2d/Platforms/iOS/EAGLView.m343
1 files changed, 343 insertions, 0 deletions
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
5This is sample code demonstrating API, technology or techniques in development.
6Although this sample code has been reviewed for technical accuracy, it is not
7final. Apple is supplying this information to help you plan for the adoption of
8the technologies and programming interfaces described herein. This information
9is subject to change, and software implemented based on this sample code should
10be tested with final operating system software and final documentation. Newer
11versions of this sample code may be provided with future seeds of the API or
12technology. For information about updates to this and other developer
13documentation, view the New & Updated sidebars in subsequent documentation
14seeds.
15
16=====================
17
18File: EAGLView.m
19Abstract: Convenience class that wraps the CAEAGLLayer from CoreAnimation into a
20UIView subclass.
21
22Version: 1.3
23
24Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
25("Apple") in consideration of your agreement to the following terms, and your
26use, installation, modification or redistribution of this Apple software
27constitutes acceptance of these terms. If you do not agree with these terms,
28please do not use, install, modify or redistribute this Apple software.
29
30In consideration of your agreement to abide by the following terms, and subject
31to these terms, Apple grants you a personal, non-exclusive license, under
32Apple's copyrights in this original Apple software (the "Apple Software"), to
33use, reproduce, modify and redistribute the Apple Software, with or without
34modifications, in source and/or binary forms; provided that if you redistribute
35the Apple Software in its entirety and without modifications, you must retain
36this notice and the following text and disclaimers in all such redistributions
37of the Apple Software.
38Neither the name, trademarks, service marks or logos of Apple Inc. may be used
39to endorse or promote products derived from the Apple Software without specific
40prior written permission from Apple. Except as expressly stated in this notice,
41no other rights or licenses, express or implied, are granted by Apple herein,
42including but not limited to any patent rights that may be infringed by your
43derivative works or by other works in which the Apple Software may be
44incorporated.
45
46The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
47WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
48WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
50COMBINATION WITH YOUR PRODUCTS.
51
52IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
53CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
54GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
56DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
57CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
58APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59
60Copyright (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