Skip to content

Latest commit

 

History

History
269 lines (215 loc) · 8.86 KB

SDL_uikitopenglview.m

File metadata and controls

269 lines (215 loc) · 8.86 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
Simple DirectMedia Layer
Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#if SDL_VIDEO_DRIVER_UIKIT
#include <QuartzCore/QuartzCore.h>
#include <OpenGLES/EAGLDrawable.h>
#include "SDL_uikitopenglview.h"
#include "SDL_uikitmessagebox.h"
@implementation SDL_uikitopenglview
@synthesize context;
Jul 15, 2014
Jul 15, 2014
35
36
37
@synthesize backingWidth = backingWidth;
@synthesize backingHeight = backingHeight;
38
39
40
41
42
43
+ (Class)layerClass
{
return [CAEAGLLayer class];
}
- (id)initWithFrame:(CGRect)frame
Jul 22, 2014
Jul 22, 2014
44
scale:(CGFloat)scale
45
retainBacking:(BOOL)retained
Jul 22, 2014
Jul 22, 2014
46
47
48
49
50
51
52
53
54
rBits:(int)rBits
gBits:(int)gBits
bBits:(int)bBits
aBits:(int)aBits
depthBits:(int)depthBits
stencilBits:(int)stencilBits
sRGB:(BOOL)sRGB
majorVersion:(int)majorVersion
shareGroup:(EAGLSharegroup*)shareGroup
55
56
57
58
59
60
61
62
{
depthBufferFormat = 0;
if ((self = [super initWithFrame:frame])) {
const BOOL useStencilBuffer = (stencilBits != 0);
const BOOL useDepthBuffer = (depthBits != 0);
NSString *colorFormat = nil;
Jul 22, 2014
Jul 22, 2014
63
64
65
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.autoresizesSubviews = YES;
66
67
68
69
70
/* The EAGLRenderingAPI enum values currently map 1:1 to major GLES
versions, and this allows us to handle future OpenGL ES versions.
*/
EAGLRenderingAPI api = majorVersion;
Jul 22, 2014
Jul 22, 2014
71
72
73
74
75
76
77
78
79
context = [[EAGLContext alloc] initWithAPI:api sharegroup:shareGroup];
if (!context || ![EAGLContext setCurrentContext:context]) {
[self release];
SDL_SetError("OpenGL ES %d not supported", majorVersion);
return nil;
}
BOOL hasiOS7 = [[UIDevice currentDevice].systemVersion compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending;
if (sRGB && hasiOS7) {
Jul 23, 2014
Jul 23, 2014
80
/* sRGB EAGL drawable support was added in iOS 7 */
Jul 22, 2014
Jul 22, 2014
81
colorFormat = kEAGLColorFormatSRGBA8;
Jul 23, 2014
Jul 23, 2014
82
} else if (rBits >= 8 && gBits >= 8 && bBits >= 8) {
83
84
85
86
87
88
89
90
91
92
93
/* if user specifically requests rbg888 or some color format higher than 16bpp */
colorFormat = kEAGLColorFormatRGBA8;
} else {
/* default case (faster) */
colorFormat = kEAGLColorFormatRGB565;
}
/* Get the layer */
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = YES;
Jul 23, 2014
Jul 23, 2014
94
95
96
97
eaglLayer.drawableProperties = @{
kEAGLDrawablePropertyRetainedBacking: @(retained),
kEAGLDrawablePropertyColorFormat: colorFormat
};
98
99
/* Set the appropriate scale (for retina display support) */
Jun 21, 2014
Jun 21, 2014
100
self.contentScaleFactor = scale;
Jul 22, 2014
Jul 22, 2014
102
/* Create the color Renderbuffer Object */
103
glGenRenderbuffersOES(1, &viewRenderbuffer);
Jul 22, 2014
Jul 22, 2014
104
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
Jul 22, 2014
Jul 22, 2014
106
107
108
109
110
111
112
113
if (![context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:eaglLayer]) {
[self release];
SDL_SetError("Failed creating OpenGL ES drawable");
return nil;
}
/* Create the Framebuffer Object */
glGenFramebuffersOES(1, &viewFramebuffer);
114
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
Jul 22, 2014
Jul 22, 2014
115
116
/* attach the color renderbuffer to the FBO */
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
if ((useDepthBuffer) || (useStencilBuffer)) {
if (useStencilBuffer) {
/* Apparently you need to pack stencil and depth into one buffer. */
depthBufferFormat = GL_DEPTH24_STENCIL8_OES;
} else if (useDepthBuffer) {
/* iOS only has 24-bit depth buffers, even with GL_DEPTH_COMPONENT16_OES */
depthBufferFormat = GL_DEPTH_COMPONENT24_OES;
}
glGenRenderbuffersOES(1, &depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthBufferFormat, backingWidth, backingHeight);
if (useDepthBuffer) {
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
}
if (useStencilBuffer) {
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_STENCIL_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
}
}
if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
Jul 22, 2014
Jul 22, 2014
143
144
145
[self release];
SDL_SetError("Failed creating OpenGL ES framebuffer");
return nil;
146
147
148
149
}
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
}
Jul 22, 2014
Jul 22, 2014
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
return self;
}
- (void)updateFrame
{
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, 0);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, 0);
glDeleteRenderbuffersOES(1, &viewRenderbuffer);
glGenRenderbuffersOES(1, &viewRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
if (depthRenderbuffer != 0) {
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthBufferFormat, backingWidth, backingHeight);
}
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
}
- (void)setAnimationCallback:(int)interval
callback:(void (*)(void*))callback
callbackParam:(void*)callbackParam
{
[self stopAnimation];
animationInterval = interval;
animationCallback = callback;
animationCallbackParam = callbackParam;
Jul 16, 2014
Jul 16, 2014
187
if (animationCallback) {
188
[self startAnimation];
Jul 16, 2014
Jul 16, 2014
189
}
190
191
192
193
}
- (void)startAnimation
{
Jun 21, 2014
Jun 21, 2014
194
displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(doLoop:)];
195
196
197
198
199
200
201
202
203
204
[displayLink setFrameInterval:animationInterval];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)stopAnimation
{
[displayLink invalidate];
displayLink = nil;
}
Jun 21, 2014
Jun 21, 2014
205
- (void)doLoop:(CADisplayLink*)sender
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
{
/* Don't run the game loop while a messagebox is up */
if (!UIKit_ShowingMessageBox()) {
animationCallback(animationCallbackParam);
}
}
- (void)setCurrentContext
{
[EAGLContext setCurrentContext:context];
}
- (void)swapBuffers
{
/* viewRenderbuffer should always be bound here. Code that binds something
else is responsible for rebinding viewRenderbuffer, to reduce
duplicate state changes. */
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
- (void)layoutSubviews
{
Jul 21, 2014
Jul 21, 2014
230
231
[super layoutSubviews];
232
233
234
235
236
237
[EAGLContext setCurrentContext:context];
[self updateFrame];
}
- (void)destroyFramebuffer
{
Jul 22, 2014
Jul 22, 2014
238
239
240
241
242
243
244
245
246
if (viewFramebuffer != 0) {
glDeleteFramebuffersOES(1, &viewFramebuffer);
viewFramebuffer = 0;
}
if (viewRenderbuffer != 0) {
glDeleteRenderbuffersOES(1, &viewRenderbuffer);
viewRenderbuffer = 0;
}
Jul 22, 2014
Jul 22, 2014
248
if (depthRenderbuffer != 0) {
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
glDeleteRenderbuffersOES(1, &depthRenderbuffer);
depthRenderbuffer = 0;
}
}
- (void)dealloc
{
[self destroyFramebuffer];
if ([EAGLContext currentContext] == context) {
[EAGLContext setCurrentContext:nil];
}
[context release];
[super dealloc];
}
@end
#endif /* SDL_VIDEO_DRIVER_UIKIT */
/* vi: set ts=4 sw=4 expandtab: */