Mercurial > sdl-ios-xcode
comparison src/video/uikit/SDL_uikitopenglview.m @ 2351:d2a519d2cc57 gsoc2008_iphone
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.
author | Holmes Futrell <hfutrell@umail.ucsb.edu> |
---|---|
date | Thu, 17 Jul 2008 22:35:59 +0000 |
parents | |
children | e103b316a4ef |
comparison
equal
deleted
inserted
replaced
2350:eb828d6c3efb | 2351:d2a519d2cc57 |
---|---|
1 // | |
2 // EAGLView.m | |
3 // test2 | |
4 // | |
5 // Created by Holmes Futrell on 7/11/08. | |
6 // Copyright __MyCompanyName__ 2008. All rights reserved. | |
7 // | |
8 | |
9 | |
10 | |
11 #import <QuartzCore/QuartzCore.h> | |
12 #import <OpenGLES/EAGLDrawable.h> | |
13 | |
14 #import "SDL_uikitopenglview.h" | |
15 | |
16 // A class extension to declare private methods | |
17 @interface SDL_uikitopenglview (privateMethods) | |
18 | |
19 - (BOOL) createFramebuffer; | |
20 - (void) destroyFramebuffer; | |
21 | |
22 @end | |
23 | |
24 | |
25 @implementation SDL_uikitopenglview | |
26 | |
27 @synthesize context; | |
28 // You must implement this | |
29 + (Class)layerClass { | |
30 return [CAEAGLLayer class]; | |
31 } | |
32 | |
33 /* | |
34 stencilBits ignored. | |
35 Right now iPhone stencil buffer doesn't appear supported. Maybe it will be in the future ... who knows. | |
36 */ | |
37 - (id)initWithFrame:(CGRect)frame \ | |
38 retainBacking:(BOOL)retained \ | |
39 rBits:(int)rBits \ | |
40 gBits:(int)gBits \ | |
41 bBits:(int)bBits \ | |
42 aBits:(int)aBits \ | |
43 depthBits:(int)depthBits \ | |
44 { | |
45 | |
46 NSString *colorFormat=nil; | |
47 GLuint depthBufferFormat; | |
48 BOOL useDepthBuffer; | |
49 | |
50 if (rBits == 8 && gBits == 8 && bBits == 8) { | |
51 /* if user specifically requests rbg888 or some color format higher than 16bpp */ | |
52 colorFormat = kEAGLColorFormatRGBA8; | |
53 } | |
54 else { | |
55 /* default case (faster) */ | |
56 colorFormat = kEAGLColorFormatRGB565; | |
57 } | |
58 | |
59 if (depthBits == 24) { | |
60 useDepthBuffer = YES; | |
61 depthBufferFormat = GL_DEPTH_COMPONENT24_OES; | |
62 } | |
63 else if (depthBits == 0) { | |
64 useDepthBuffer = NO; | |
65 } | |
66 else { | |
67 /* default case when depth buffer is not disabled */ | |
68 /* | |
69 strange, even when we use this, we seem to get a 24 bit depth buffer on iPhone. | |
70 perhaps that's the only depth format iPhone actually supports | |
71 */ | |
72 useDepthBuffer = YES; | |
73 depthBufferFormat = GL_DEPTH_COMPONENT16_OES; | |
74 } | |
75 | |
76 if ((self = [super initWithFrame:frame])) { | |
77 // Get the layer | |
78 CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; | |
79 | |
80 eaglLayer.opaque = YES; | |
81 eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: | |
82 [NSNumber numberWithBool: retained], kEAGLDrawablePropertyRetainedBacking, colorFormat, kEAGLDrawablePropertyColorFormat, nil]; | |
83 | |
84 context = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES1]; | |
85 | |
86 if (!context || ![EAGLContext setCurrentContext:context]) { | |
87 [self release]; | |
88 return nil; | |
89 } | |
90 | |
91 /* create the buffers */ | |
92 glGenFramebuffersOES(1, &viewFramebuffer); | |
93 glGenRenderbuffersOES(1, &viewRenderbuffer); | |
94 | |
95 glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); | |
96 glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); | |
97 [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer]; | |
98 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer); | |
99 | |
100 glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth); | |
101 glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight); | |
102 | |
103 if (useDepthBuffer) { | |
104 glGenRenderbuffersOES(1, &depthRenderbuffer); | |
105 glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer); | |
106 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthBufferFormat, backingWidth, backingHeight); | |
107 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer); | |
108 } | |
109 | |
110 if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) { | |
111 NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES)); | |
112 return NO; | |
113 } | |
114 /* end create buffers */ | |
115 | |
116 NSLog(@"Done initializing ..."); | |
117 | |
118 } | |
119 return self; | |
120 } | |
121 | |
122 - (void)setCurrentContext { | |
123 [EAGLContext setCurrentContext:context]; | |
124 } | |
125 | |
126 | |
127 - (void)swapBuffers { | |
128 | |
129 | |
130 glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); | |
131 if (![context presentRenderbuffer:GL_RENDERBUFFER_OES]) { | |
132 NSLog(@"Could not swap buffers"); | |
133 } | |
134 } | |
135 | |
136 | |
137 - (void)layoutSubviews { | |
138 [EAGLContext setCurrentContext:context]; | |
139 } | |
140 | |
141 - (void)destroyFramebuffer { | |
142 | |
143 glDeleteFramebuffersOES(1, &viewFramebuffer); | |
144 viewFramebuffer = 0; | |
145 glDeleteRenderbuffersOES(1, &viewRenderbuffer); | |
146 viewRenderbuffer = 0; | |
147 | |
148 if (depthRenderbuffer) { | |
149 glDeleteRenderbuffersOES(1, &depthRenderbuffer); | |
150 depthRenderbuffer = 0; | |
151 } | |
152 } | |
153 | |
154 | |
155 - (void)dealloc { | |
156 | |
157 [self destroyFramebuffer]; | |
158 if ([EAGLContext currentContext] == context) { | |
159 [EAGLContext setCurrentContext:nil]; | |
160 } | |
161 [context release]; | |
162 [super dealloc]; | |
163 | |
164 } | |
165 | |
166 @end |