?? eaglview.m
字號:
/*===== IMPORTANT =====This is sample code demonstrating API, technology or techniques in development.Although this sample code has been reviewed for technical accuracy, it is notfinal. Apple is supplying this information to help you plan for the adoption ofthe technologies and programming interfaces described herein. This informationis subject to change, and software implemented based on this sample code shouldbe tested with final operating system software and final documentation. Newerversions of this sample code may be provided with future seeds of the API ortechnology. For information about updates to this and other developerdocumentation, view the New & Updated sidebars in subsequent documentationseeds.=====================File: EAGLView.mAbstract: Convenience class that wraps the CAEAGLLayer from CoreAnimation into aUIView subclass.Version: 1.3Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.("Apple") in consideration of your agreement to the following terms, and youruse, installation, modification or redistribution of this Apple softwareconstitutes acceptance of these terms. If you do not agree with these terms,please do not use, install, modify or redistribute this Apple software.In consideration of your agreement to abide by the following terms, and subjectto these terms, Apple grants you a personal, non-exclusive license, underApple's copyrights in this original Apple software (the "Apple Software"), touse, reproduce, modify and redistribute the Apple Software, with or withoutmodifications, in source and/or binary forms; provided that if you redistributethe Apple Software in its entirety and without modifications, you must retainthis notice and the following text and disclaimers in all such redistributionsof the Apple Software.Neither the name, trademarks, service marks or logos of Apple Inc. may be usedto endorse or promote products derived from the Apple Software without specificprior written permission from Apple. Except as expressly stated in this notice,no other rights or licenses, express or implied, are granted by Apple herein,including but not limited to any patent rights that may be infringed by yourderivative works or by other works in which the Apple Software may beincorporated.The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NOWARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIEDWARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULARPURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR INCOMBINATION WITH YOUR PRODUCTS.IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL ORCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTEGOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/ORDISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OFCONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IFAPPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.Copyright (C) 2008 Apple Inc. All Rights Reserved.*/#import <QuartzCore/QuartzCore.h>#import "EAGLView.h"#import "OpenGL_Internal.h"//CLASS IMPLEMENTATIONS:@implementation EAGLView@synthesize delegate=_delegate, autoresizesSurface=_autoresize, surfaceSize=_size, framebuffer = _framebuffer, pixelFormat = _format, depthFormat = _depthFormat, context = _context, touchDelegate;+ (Class) layerClass{ return [CAEAGLLayer class];}- (BOOL) _createSurface{ CAEAGLLayer* eaglLayer = (CAEAGLLayer*)[self layer]; CGSize newSize; GLuint oldRenderbuffer; GLuint oldFramebuffer; if(![EAGLContext setCurrentContext:_context]) { return NO; } newSize = [eaglLayer bounds].size; newSize.width = roundf(newSize.width); newSize.height = roundf(newSize.height); glGetIntegerv(GL_RENDERBUFFER_BINDING_OES, (GLint *) &oldRenderbuffer); glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, (GLint *) &oldFramebuffer); glGenRenderbuffersOES(1, &_renderbuffer); glBindRenderbufferOES(GL_RENDERBUFFER_OES, _renderbuffer); if(![_context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:eaglLayer]) { glDeleteRenderbuffersOES(1, &_renderbuffer); glBindRenderbufferOES(GL_RENDERBUFFER_BINDING_OES, oldRenderbuffer); return NO; } glGenFramebuffersOES(1, &_framebuffer); glBindFramebufferOES(GL_FRAMEBUFFER_OES, _framebuffer); glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, _renderbuffer); if (_depthFormat) { glGenRenderbuffersOES(1, &_depthBuffer); glBindRenderbufferOES(GL_RENDERBUFFER_OES, _depthBuffer); glRenderbufferStorageOES(GL_RENDERBUFFER_OES, _depthFormat, newSize.width, newSize.height); glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, _depthBuffer); } _size = newSize; if(!_hasBeenCurrent) { glViewport(0, 0, newSize.width, newSize.height); glScissor(0, 0, newSize.width, newSize.height); _hasBeenCurrent = YES; } else { glBindFramebufferOES(GL_FRAMEBUFFER_OES, oldFramebuffer); } glBindRenderbufferOES(GL_RENDERBUFFER_OES, oldRenderbuffer); CHECK_GL_ERROR(); [_delegate didResizeEAGLSurfaceForView:self]; return YES;}- (void) _destroySurface{ EAGLContext *oldContext = [EAGLContext currentContext]; if (oldContext != _context) [EAGLContext setCurrentContext:_context]; if(_depthFormat) { glDeleteRenderbuffersOES(1, &_depthBuffer); _depthBuffer = 0; } glDeleteRenderbuffersOES(1, &_renderbuffer); _renderbuffer = 0; glDeleteFramebuffersOES(1, &_framebuffer); _framebuffer = 0; if (oldContext != _context) [EAGLContext setCurrentContext:oldContext]; else [EAGLContext setCurrentContext:nil];}- (id) initWithFrame:(CGRect)frame{ return [self initWithFrame:frame pixelFormat:kEAGLColorFormatRGB565 depthFormat:0 preserveBackbuffer:NO];}- (id) initWithFrame:(CGRect)frame pixelFormat:(NSString*)format { return [self initWithFrame:frame pixelFormat:format depthFormat:0 preserveBackbuffer:NO];}- (id) initWithFrame:(CGRect)frame pixelFormat:(NSString*)format depthFormat:(GLuint)depth preserveBackbuffer:(BOOL)retained{ if((self = [super initWithFrame:frame])) { [self setOpaque:YES]; CAEAGLLayer* eaglLayer = (CAEAGLLayer*)[self layer]; [eaglLayer setDrawableProperties:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:retained], kEAGLDrawablePropertyRetainedBacking, format, kEAGLDrawablePropertyColorFormat, nil]]; _format = format; _depthFormat = depth; _context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1]; if(_context == nil) { [self release]; return nil; } if(![self _createSurface]) { [self release]; return nil; } } return self;}- (void) dealloc{ [self _destroySurface]; [_context release]; _context = nil; [super dealloc];}- (void) layoutSubviews{ CGRect bounds = [self bounds]; if(_autoresize && ((roundf(bounds.size.width) != _size.width) || (roundf(bounds.size.height) != _size.height))) { [self _destroySurface];#if __DEBUG__ REPORT_ERROR(@"Resizing surface from %fx%f to %fx%f", _size.width, _size.height, roundf(bounds.size.width), roundf(bounds.size.height));#endif [self _createSurface]; }}- (void) setAutoresizesEAGLSurface:(BOOL)autoresizesEAGLSurface;{ _autoresize = autoresizesEAGLSurface; if(_autoresize) [self layoutSubviews];}- (void) setCurrentContext{ if(![EAGLContext setCurrentContext:_context]) { printf("Failed to set current context %p in %s\n", _context, __FUNCTION__); }}- (BOOL) isCurrentContext{ return ([EAGLContext currentContext] == _context ? YES : NO);}- (void) clearCurrentContext{ if(![EAGLContext setCurrentContext:nil]) printf("Failed to clear current context in %s\n", __FUNCTION__);}- (void) swapBuffers{ EAGLContext *oldContext = [EAGLContext currentContext]; //GLuint oldRenderbuffer; if(oldContext != _context) [EAGLContext setCurrentContext:_context];#if DEBUG CHECK_GL_ERROR();#endif //glGetIntegerv(GL_RENDERBUFFER_BINDING_OES, (GLint *) &oldRenderbuffer); glBindRenderbufferOES(GL_RENDERBUFFER_OES, _renderbuffer); //glBindRenderbufferOES(GL_RENDERBUFFER_OES, _framebuffer); if(![_context presentRenderbuffer:GL_RENDERBUFFER_OES]) printf("Failed to swap renderbuffer in %s\n", __FUNCTION__); if(oldContext != _context) [EAGLContext setCurrentContext:oldContext];}- (CGPoint) convertPointFromViewToSurface:(CGPoint)point{ CGRect bounds = [self bounds]; return CGPointMake((point.x - bounds.origin.x) / bounds.size.width * _size.width, (point.y - bounds.origin.y) / bounds.size.height * _size.height);}- (CGRect) convertRectFromViewToSurface:(CGRect)rect{ CGRect bounds = [self bounds]; 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);}// Pass the touches to the superview- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ if(touchDelegate) { [touchDelegate touchesBegan:touches withEvent:event]; }}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ if(touchDelegate) { [touchDelegate touchesMoved:touches withEvent:event]; }}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ if(touchDelegate) { [touchDelegate touchesEnded:touches withEvent:event]; }}- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ if(touchDelegate) { [touchDelegate touchesCancelled:touches withEvent:event]; }}@end
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -