SDL_uikitopenglview is an OpenGL ES View class based on the one found in Apple's OpenGL ES based application template. It's created from SDL_uikitopengles.m, normally. gsoc2008_iphone
authorHolmes Futrell <hfutrell@umail.ucsb.edu>
Thu, 17 Jul 2008 22:35:59 +0000
branchgsoc2008_iphone
changeset 2351d2a519d2cc57
parent 2350 eb828d6c3efb
child 2352 1ecbeff9eb4c
SDL_uikitopenglview is an OpenGL ES View class based on the one found in Apple's OpenGL ES based application template. It's created from SDL_uikitopengles.m, normally.
src/video/uikit/SDL_uikitopenglview.h
src/video/uikit/SDL_uikitopenglview.m
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/uikit/SDL_uikitopenglview.h	Thu Jul 17 22:35:59 2008 +0000
     1.3 @@ -0,0 +1,50 @@
     1.4 +//
     1.5 +//  EAGLView.h
     1.6 +//  test2
     1.7 +//
     1.8 +//  Created by Holmes Futrell on 7/11/08.
     1.9 +//  Copyright __MyCompanyName__ 2008. All rights reserved.
    1.10 +//
    1.11 +
    1.12 +
    1.13 +#import <UIKit/UIKit.h>
    1.14 +#import <OpenGLES/EAGL.h>
    1.15 +#import <OpenGLES/ES1/gl.h>
    1.16 +#import <OpenGLES/ES1/glext.h>
    1.17 +#import "SDL_uikitview.h"
    1.18 +/*
    1.19 + This class wraps the CAEAGLLayer from CoreAnimation into a convenient UIView subclass.
    1.20 + The view content is basically an EAGL surface you render your OpenGL scene into.
    1.21 + Note that setting the view non-opaque will only work if the EAGL surface has an alpha channel.
    1.22 + */
    1.23 +@interface SDL_uikitopenglview : SDL_uikitview {
    1.24 +	
    1.25 +@private
    1.26 +	/* The pixel dimensions of the backbuffer */
    1.27 +	GLint backingWidth;
    1.28 +	GLint backingHeight;
    1.29 +	
    1.30 +	EAGLContext *context;
    1.31 +	
    1.32 +	/* OpenGL names for the renderbuffer and framebuffers used to render to this view */
    1.33 +	GLuint viewRenderbuffer, viewFramebuffer;
    1.34 +	
    1.35 +	/* OpenGL name for the depth buffer that is attached to viewFramebuffer, if it exists (0 if it does not exist) */
    1.36 +	GLuint depthRenderbuffer;
    1.37 +	
    1.38 +}
    1.39 +
    1.40 +@property (nonatomic, retain, readonly) EAGLContext *context;
    1.41 +
    1.42 +- (void)swapBuffers;
    1.43 +- (void)setCurrentContext;
    1.44 +
    1.45 +- (id)initWithFrame:(CGRect)frame
    1.46 +	retainBacking:(BOOL)retained \
    1.47 +	rBits:(int)rBits \
    1.48 +	gBits:(int)gBits \
    1.49 +	bBits:(int)bBits \
    1.50 +	aBits:(int)aBits \
    1.51 +	depthBits:(int)depthBits;
    1.52 +
    1.53 +@end
    1.54 \ No newline at end of file
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/video/uikit/SDL_uikitopenglview.m	Thu Jul 17 22:35:59 2008 +0000
     2.3 @@ -0,0 +1,166 @@
     2.4 +//
     2.5 +//  EAGLView.m
     2.6 +//  test2
     2.7 +//
     2.8 +//  Created by Holmes Futrell on 7/11/08.
     2.9 +//  Copyright __MyCompanyName__ 2008. All rights reserved.
    2.10 +//
    2.11 +
    2.12 +
    2.13 +
    2.14 +#import <QuartzCore/QuartzCore.h>
    2.15 +#import <OpenGLES/EAGLDrawable.h>
    2.16 +
    2.17 +#import "SDL_uikitopenglview.h"
    2.18 +
    2.19 +// A class extension to declare private methods
    2.20 +@interface SDL_uikitopenglview (privateMethods)
    2.21 +
    2.22 +- (BOOL) createFramebuffer;
    2.23 +- (void) destroyFramebuffer;
    2.24 +
    2.25 +@end
    2.26 +
    2.27 +
    2.28 +@implementation SDL_uikitopenglview
    2.29 +
    2.30 +@synthesize context;
    2.31 +// You must implement this
    2.32 ++ (Class)layerClass {
    2.33 +	return [CAEAGLLayer class];
    2.34 +}
    2.35 +
    2.36 +/*
    2.37 +	stencilBits ignored.
    2.38 +	Right now iPhone stencil buffer doesn't appear supported.  Maybe it will be in the future ... who knows.
    2.39 +*/
    2.40 +- (id)initWithFrame:(CGRect)frame \
    2.41 +	  retainBacking:(BOOL)retained \
    2.42 +	  rBits:(int)rBits \
    2.43 +	  gBits:(int)gBits \
    2.44 +	  bBits:(int)bBits \
    2.45 +	  aBits:(int)aBits \
    2.46 +	  depthBits:(int)depthBits \
    2.47 +{
    2.48 +	
    2.49 +	NSString *colorFormat=nil;
    2.50 +	GLuint depthBufferFormat;
    2.51 +	BOOL useDepthBuffer;
    2.52 +	
    2.53 +	if (rBits == 8 && gBits == 8 && bBits == 8) {
    2.54 +		/* if user specifically requests rbg888 or some color format higher than 16bpp */
    2.55 +		colorFormat = kEAGLColorFormatRGBA8;
    2.56 +	}
    2.57 +	else {
    2.58 +		/* default case (faster) */
    2.59 +		colorFormat = kEAGLColorFormatRGB565;
    2.60 +	}
    2.61 +	
    2.62 +	if (depthBits == 24) {
    2.63 +		useDepthBuffer = YES;
    2.64 +		depthBufferFormat = GL_DEPTH_COMPONENT24_OES;
    2.65 +	}
    2.66 +	else if (depthBits == 0) {
    2.67 +		useDepthBuffer = NO;
    2.68 +	}
    2.69 +	else {
    2.70 +		/* default case when depth buffer is not disabled */
    2.71 +		/* 
    2.72 +		   strange, even when we use this, we seem to get a 24 bit depth buffer on iPhone.
    2.73 +		   perhaps that's the only depth format iPhone actually supports
    2.74 +		*/
    2.75 +		useDepthBuffer = YES;
    2.76 +		depthBufferFormat = GL_DEPTH_COMPONENT16_OES;
    2.77 +	}
    2.78 +	
    2.79 +	if ((self = [super initWithFrame:frame])) {
    2.80 +		// Get the layer
    2.81 +		CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
    2.82 +		
    2.83 +		eaglLayer.opaque = YES;
    2.84 +		eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
    2.85 +										[NSNumber numberWithBool: retained], kEAGLDrawablePropertyRetainedBacking, colorFormat, kEAGLDrawablePropertyColorFormat, nil];
    2.86 +		
    2.87 +		context = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES1];
    2.88 +		
    2.89 +		if (!context || ![EAGLContext setCurrentContext:context]) {
    2.90 +			[self release];
    2.91 +			return nil;
    2.92 +		}
    2.93 +		
    2.94 +		/* create the buffers */
    2.95 +		glGenFramebuffersOES(1, &viewFramebuffer);
    2.96 +		glGenRenderbuffersOES(1, &viewRenderbuffer);
    2.97 +		
    2.98 +		glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    2.99 +		glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
   2.100 +		[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
   2.101 +		glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
   2.102 +		
   2.103 +		glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
   2.104 +		glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
   2.105 +		
   2.106 +		if (useDepthBuffer) {
   2.107 +			glGenRenderbuffersOES(1, &depthRenderbuffer);
   2.108 +			glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
   2.109 +			glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthBufferFormat, backingWidth, backingHeight);
   2.110 +			glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
   2.111 +		}
   2.112 +			
   2.113 +		if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
   2.114 +			NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
   2.115 +			return NO;
   2.116 +		}
   2.117 +		/* end create buffers */
   2.118 +		
   2.119 +		NSLog(@"Done initializing ...");
   2.120 +		
   2.121 +	}
   2.122 +	return self;
   2.123 +}
   2.124 +
   2.125 +- (void)setCurrentContext {
   2.126 +	[EAGLContext setCurrentContext:context];
   2.127 +}
   2.128 +
   2.129 +
   2.130 +- (void)swapBuffers {
   2.131 +	
   2.132 +	
   2.133 +	glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
   2.134 +	if (![context presentRenderbuffer:GL_RENDERBUFFER_OES]) {
   2.135 +		NSLog(@"Could not swap buffers");
   2.136 +	}
   2.137 +}
   2.138 +
   2.139 +
   2.140 +- (void)layoutSubviews {
   2.141 +	[EAGLContext setCurrentContext:context];
   2.142 +}
   2.143 +
   2.144 +- (void)destroyFramebuffer {
   2.145 +	
   2.146 +	glDeleteFramebuffersOES(1, &viewFramebuffer);
   2.147 +	viewFramebuffer = 0;
   2.148 +	glDeleteRenderbuffersOES(1, &viewRenderbuffer);
   2.149 +	viewRenderbuffer = 0;
   2.150 +	
   2.151 +	if (depthRenderbuffer) {
   2.152 +		glDeleteRenderbuffersOES(1, &depthRenderbuffer);
   2.153 +		depthRenderbuffer = 0;
   2.154 +	}
   2.155 +}
   2.156 +
   2.157 +
   2.158 +- (void)dealloc {
   2.159 +		
   2.160 +	[self destroyFramebuffer];
   2.161 +	if ([EAGLContext currentContext] == context) {
   2.162 +		[EAGLContext setCurrentContext:nil];
   2.163 +	}
   2.164 +	[context release];	
   2.165 +	[super dealloc];
   2.166 +	
   2.167 +}
   2.168 +
   2.169 +@end
   2.170 \ No newline at end of file