cocoaTouch/SDLOverrides/SDL_uikitopenglview.m
changeset 2723 eaa6ac1e95ea
equal deleted inserted replaced
2722:d891285e500f 2723:eaa6ac1e95ea
       
     1 /*
       
     2  SDL - Simple DirectMedia Layer
       
     3  Copyright (C) 1997-2009 Sam Lantinga
       
     4  
       
     5  This library is free software; you can redistribute it and/or
       
     6  modify it under the terms of the GNU Lesser General Public
       
     7  License as published by the Free Software Foundation; either
       
     8  version 2.1 of the License, or (at your option) any later version.
       
     9  
       
    10  This library is distributed in the hope that it will be useful,
       
    11  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13  Lesser General Public License for more details.
       
    14  
       
    15  You should have received a copy of the GNU Lesser General Public
       
    16  License along with this library; if not, write to the Free Software
       
    17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    18  
       
    19  Sam Lantinga
       
    20  slouken@libsdl.org
       
    21  */
       
    22 
       
    23 #import <QuartzCore/QuartzCore.h>
       
    24 #import <OpenGLES/EAGLDrawable.h>
       
    25 #import "SDL_uikitopenglview.h"
       
    26 
       
    27 @interface SDL_uikitopenglview (privateMethods)
       
    28 
       
    29 - (BOOL) createFramebuffer;
       
    30 - (void) destroyFramebuffer;
       
    31 
       
    32 @end
       
    33 
       
    34 
       
    35 @implementation SDL_uikitopenglview
       
    36 
       
    37 @synthesize context;
       
    38 
       
    39 + (Class)layerClass {
       
    40 	return [CAEAGLLayer class];
       
    41 }
       
    42 
       
    43 - (id)initWithFrame:(CGRect)frame \
       
    44 retainBacking:(BOOL)retained \
       
    45 rBits:(int)rBits \
       
    46 gBits:(int)gBits \
       
    47 bBits:(int)bBits \
       
    48 aBits:(int)aBits \
       
    49 depthBits:(int)depthBits \
       
    50 {
       
    51 	
       
    52 	NSString *colorFormat=nil;
       
    53 	GLuint depthBufferFormat;
       
    54 	BOOL useDepthBuffer;
       
    55 	
       
    56 	if (rBits == 8 && gBits == 8 && bBits == 8) {
       
    57 		/* if user specifically requests rbg888 or some color format higher than 16bpp */
       
    58 		colorFormat = kEAGLColorFormatRGBA8;
       
    59 	}
       
    60 	else {
       
    61 		/* default case (faster) */
       
    62 		colorFormat = kEAGLColorFormatRGB565;
       
    63 	}
       
    64 	
       
    65 	if (depthBits == 24) {
       
    66 		useDepthBuffer = YES;
       
    67 		depthBufferFormat = GL_DEPTH_COMPONENT24_OES;
       
    68 	}
       
    69 	else if (depthBits == 0) {
       
    70 		useDepthBuffer = NO;
       
    71 	}
       
    72 	else {
       
    73 		/* default case when depth buffer is not disabled */
       
    74 		/* 
       
    75 		 strange, even when we use this, we seem to get a 24 bit depth buffer on iPhone.
       
    76 		 perhaps that's the only depth format iPhone actually supports
       
    77 		 */
       
    78 		useDepthBuffer = YES;
       
    79 		depthBufferFormat = GL_DEPTH_COMPONENT16_OES;
       
    80 	}
       
    81 	
       
    82 	
       
    83 	// we invert heigh and size to get q landscape mode up
       
    84 	CGRect rect = [[UIScreen mainScreen] bounds];
       
    85 	float swapTemp = rect.size.height;
       
    86 	rect.size.width = rect.size.height;
       
    87 	rect.size.height = swapTemp;
       
    88 	
       
    89 	if ((self = [super initWithFrame:rect])) {
       
    90 		// Get the layer
       
    91 		CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
       
    92 		
       
    93 		eaglLayer.opaque = YES;
       
    94 		eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
       
    95 						[NSNumber numberWithBool: retained], kEAGLDrawablePropertyRetainedBacking, colorFormat, kEAGLDrawablePropertyColorFormat, nil];
       
    96 		
       
    97 		context = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES1];
       
    98 		
       
    99 		if (!context || ![EAGLContext setCurrentContext:context]) {
       
   100 			[self release];
       
   101 			return nil;
       
   102 		}
       
   103 		
       
   104 		/* create the buffers */
       
   105 		glGenFramebuffersOES(1, &viewFramebuffer);
       
   106 		glGenRenderbuffersOES(1, &viewRenderbuffer);
       
   107 		
       
   108 		glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
       
   109 		glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
       
   110 		[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
       
   111 		glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
       
   112 		
       
   113 		glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
       
   114 		glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
       
   115 		
       
   116 		if (useDepthBuffer) {
       
   117 			glGenRenderbuffersOES(1, &depthRenderbuffer);
       
   118 			glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
       
   119 			glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthBufferFormat, backingWidth, backingHeight);
       
   120 			glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
       
   121 		}
       
   122 		
       
   123 		if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
       
   124 			return NO;
       
   125 		}
       
   126 		/* end create buffers */
       
   127 	}
       
   128 	return self;
       
   129 }
       
   130 
       
   131 - (void)setCurrentContext {
       
   132 	[EAGLContext setCurrentContext:context];
       
   133 }
       
   134 
       
   135 
       
   136 - (void)swapBuffers {
       
   137 	glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
       
   138 	[context presentRenderbuffer:GL_RENDERBUFFER_OES];
       
   139 }
       
   140 
       
   141 
       
   142 - (void)layoutSubviews {
       
   143 	[EAGLContext setCurrentContext:context];
       
   144 }
       
   145 
       
   146 - (void)destroyFramebuffer {
       
   147 	
       
   148 	glDeleteFramebuffersOES(1, &viewFramebuffer);
       
   149 	viewFramebuffer = 0;
       
   150 	glDeleteRenderbuffersOES(1, &viewRenderbuffer);
       
   151 	viewRenderbuffer = 0;
       
   152 	
       
   153 	if (depthRenderbuffer) {
       
   154 		glDeleteRenderbuffersOES(1, &depthRenderbuffer);
       
   155 		depthRenderbuffer = 0;
       
   156 	}
       
   157 }
       
   158 
       
   159 
       
   160 - (void)dealloc {
       
   161 	
       
   162 	[self destroyFramebuffer];
       
   163 	if ([EAGLContext currentContext] == context) {
       
   164 		[EAGLContext setCurrentContext:nil];
       
   165 	}
       
   166 	[context release];	
       
   167 	[super dealloc];
       
   168 	
       
   169 }
       
   170 
       
   171 @end