comparison src/video/uikit/SDL_uikitopenglview.m @ 5132:5690ebc772c7

Fixed spacing
author Sam Lantinga <slouken@libsdl.org>
date Tue, 01 Feb 2011 08:53:22 -0800
parents 99210400e8b9
children ec13e48ee0d9
comparison
equal deleted inserted replaced
5130:f2c2f0ecba5f 5132:5690ebc772c7
35 @implementation SDL_uikitopenglview 35 @implementation SDL_uikitopenglview
36 36
37 @synthesize context; 37 @synthesize context;
38 38
39 + (Class)layerClass { 39 + (Class)layerClass {
40 return [CAEAGLLayer class]; 40 return [CAEAGLLayer class];
41 } 41 }
42 42
43 - (id)initWithFrame:(CGRect)frame \ 43 - (id)initWithFrame:(CGRect)frame \
44 retainBacking:(BOOL)retained \ 44 retainBacking:(BOOL)retained \
45 rBits:(int)rBits \ 45 rBits:(int)rBits \
46 gBits:(int)gBits \ 46 gBits:(int)gBits \
47 bBits:(int)bBits \ 47 bBits:(int)bBits \
48 aBits:(int)aBits \ 48 aBits:(int)aBits \
49 depthBits:(int)depthBits \ 49 depthBits:(int)depthBits \
50 { 50 {
51 51 NSString *colorFormat=nil;
52 NSString *colorFormat=nil; 52 GLuint depthBufferFormat;
53 GLuint depthBufferFormat; 53 BOOL useDepthBuffer;
54 BOOL useDepthBuffer; 54
55 55 if (rBits == 8 && gBits == 8 && bBits == 8) {
56 if (rBits == 8 && gBits == 8 && bBits == 8) { 56 /* if user specifically requests rbg888 or some color format higher than 16bpp */
57 /* if user specifically requests rbg888 or some color format higher than 16bpp */ 57 colorFormat = kEAGLColorFormatRGBA8;
58 colorFormat = kEAGLColorFormatRGBA8; 58 }
59 } 59 else {
60 else { 60 /* default case (faster) */
61 /* default case (faster) */ 61 colorFormat = kEAGLColorFormatRGB565;
62 colorFormat = kEAGLColorFormatRGB565; 62 }
63 } 63
64 64 if (depthBits == 24) {
65 if (depthBits == 24) { 65 useDepthBuffer = YES;
66 useDepthBuffer = YES; 66 depthBufferFormat = GL_DEPTH_COMPONENT24_OES;
67 depthBufferFormat = GL_DEPTH_COMPONENT24_OES; 67 }
68 } 68 else if (depthBits == 0) {
69 else if (depthBits == 0) { 69 useDepthBuffer = NO;
70 useDepthBuffer = NO; 70 }
71 } 71 else {
72 else { 72 /* default case when depth buffer is not disabled */
73 /* default case when depth buffer is not disabled */ 73 /*
74 /* 74 strange, even when we use this, we seem to get a 24 bit depth buffer on iPhone.
75 strange, even when we use this, we seem to get a 24 bit depth buffer on iPhone. 75 perhaps that's the only depth format iPhone actually supports
76 perhaps that's the only depth format iPhone actually supports 76 */
77 */ 77 useDepthBuffer = YES;
78 useDepthBuffer = YES; 78 depthBufferFormat = GL_DEPTH_COMPONENT16_OES;
79 depthBufferFormat = GL_DEPTH_COMPONENT16_OES; 79 }
80 } 80
81 81 if ((self = [super initWithFrame:frame])) {
82 if ((self = [super initWithFrame:frame])) { 82 // Get the layer
83 // Get the layer 83 CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
84 CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; 84
85 85 eaglLayer.opaque = YES;
86 eaglLayer.opaque = YES; 86 eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
87 eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: 87 [NSNumber numberWithBool: retained], kEAGLDrawablePropertyRetainedBacking, colorFormat, kEAGLDrawablePropertyColorFormat, nil];
88 [NSNumber numberWithBool: retained], kEAGLDrawablePropertyRetainedBacking, colorFormat, kEAGLDrawablePropertyColorFormat, nil]; 88
89 89 context = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES1];
90 context = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES1]; 90
91 91 if (!context || ![EAGLContext setCurrentContext:context]) {
92 if (!context || ![EAGLContext setCurrentContext:context]) { 92 [self release];
93 [self release]; 93 return nil;
94 return nil; 94 }
95 } 95
96 96 /* create the buffers */
97 /* create the buffers */ 97 glGenFramebuffersOES(1, &viewFramebuffer);
98 glGenFramebuffersOES(1, &viewFramebuffer); 98 glGenRenderbuffersOES(1, &viewRenderbuffer);
99 glGenRenderbuffersOES(1, &viewRenderbuffer); 99
100 100 glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
101 glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); 101 glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
102 glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); 102 [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
103 [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer]; 103 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
104 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer); 104
105 105 glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
106 glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth); 106 glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
107 glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight); 107
108 108 if (useDepthBuffer) {
109 if (useDepthBuffer) { 109 glGenRenderbuffersOES(1, &depthRenderbuffer);
110 glGenRenderbuffersOES(1, &depthRenderbuffer); 110 glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
111 glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer); 111 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthBufferFormat, backingWidth, backingHeight);
112 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthBufferFormat, backingWidth, backingHeight); 112 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
113 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer); 113 }
114 } 114
115 115 if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
116 if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) { 116 return NO;
117 return NO; 117 }
118 } 118 /* end create buffers */
119 /* end create buffers */ 119 }
120 } 120 return self;
121 return self;
122 } 121 }
123 122
124 - (void)setCurrentContext { 123 - (void)setCurrentContext {
125 [EAGLContext setCurrentContext:context]; 124 [EAGLContext setCurrentContext:context];
126 } 125 }
127 126
128 127
129 - (void)swapBuffers { 128 - (void)swapBuffers {
130 glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); 129 glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
131 [context presentRenderbuffer:GL_RENDERBUFFER_OES]; 130 [context presentRenderbuffer:GL_RENDERBUFFER_OES];
132 } 131 }
133 132
134 133
135 - (void)layoutSubviews { 134 - (void)layoutSubviews {
136 [EAGLContext setCurrentContext:context]; 135 [EAGLContext setCurrentContext:context];
137 } 136 }
138 137
139 - (void)destroyFramebuffer { 138 - (void)destroyFramebuffer {
140 139 glDeleteFramebuffersOES(1, &viewFramebuffer);
141 glDeleteFramebuffersOES(1, &viewFramebuffer); 140 viewFramebuffer = 0;
142 viewFramebuffer = 0; 141 glDeleteRenderbuffersOES(1, &viewRenderbuffer);
143 glDeleteRenderbuffersOES(1, &viewRenderbuffer); 142 viewRenderbuffer = 0;
144 viewRenderbuffer = 0; 143
145 144 if (depthRenderbuffer) {
146 if (depthRenderbuffer) { 145 glDeleteRenderbuffersOES(1, &depthRenderbuffer);
147 glDeleteRenderbuffersOES(1, &depthRenderbuffer); 146 depthRenderbuffer = 0;
148 depthRenderbuffer = 0; 147 }
149 }
150 } 148 }
151 149
152 150
153 - (void)dealloc { 151 - (void)dealloc {
154 152 [self destroyFramebuffer];
155 [self destroyFramebuffer]; 153 if ([EAGLContext currentContext] == context) {
156 if ([EAGLContext currentContext] == context) { 154 [EAGLContext setCurrentContext:nil];
157 [EAGLContext setCurrentContext:nil]; 155 }
158 } 156 [context release];
159 [context release]; 157 [super dealloc];
160 [super dealloc];
161
162 } 158 }
163 159
164 @end 160 @end