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