23 #if SDL_VIDEO_DRIVER_UIKIT
25 #include <QuartzCore/QuartzCore.h>
26 #include <OpenGLES/EAGLDrawable.h>
37 return [CAEAGLLayer class];
40 - (
id)initWithFrame:(CGRect)frame
42 retainBacking:(
BOOL)retained
47 depthBits:(
int)depthBits
48 stencilBits:(
int)stencilBits
49 majorVersion:(
int)majorVersion
50 shareGroup:(EAGLSharegroup*)shareGroup
52 depthBufferFormat = 0;
54 if ((
self = [super initWithFrame:frame])) {
55 const BOOL useStencilBuffer = (stencilBits != 0);
56 const BOOL useDepthBuffer = (depthBits != 0);
59 if (rBits == 8 && gBits == 8 && bBits == 8) {
61 colorFormat = kEAGLColorFormatRGBA8;
64 colorFormat = kEAGLColorFormatRGB565;
68 CAEAGLLayer *eaglLayer = (CAEAGLLayer *)
self.
layer;
70 eaglLayer.opaque = YES;
71 eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
72 [NSNumber numberWithBool: retained], kEAGLDrawablePropertyRetainedBacking, colorFormat, kEAGLDrawablePropertyColorFormat, nil];
74 if (majorVersion > 1) {
75 context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2 sharegroup:shareGroup];
77 context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1 sharegroup:shareGroup];
81 SDL_SetError(
"OpenGL ES %d not supported", majorVersion);
86 if ([
self respondsToSelector:
@selector(contentScaleFactor)])
87 self.contentScaleFactor =
scale;
90 glGenFramebuffersOES(1, &viewFramebuffer);
91 glGenRenderbuffersOES(1, &viewRenderbuffer);
93 glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
94 glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
95 [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
96 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
98 glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
99 glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
101 if ((useDepthBuffer) || (useStencilBuffer)) {
102 if (useStencilBuffer) {
105 }
else if (useDepthBuffer) {
110 glGenRenderbuffersOES(1, &depthRenderbuffer);
111 glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
112 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthBufferFormat, backingWidth, backingHeight);
113 if (useDepthBuffer) {
114 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
116 if (useStencilBuffer) {
117 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_STENCIL_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
121 if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
126 self.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
127 self.autoresizesSubviews = YES;
134 glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
135 glBindRenderbufferOES(GL_RENDERBUFFER_OES, 0);
136 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, 0);
137 glDeleteRenderbuffersOES(1, &viewRenderbuffer);
139 glGenRenderbuffersOES(1, &viewRenderbuffer);
140 glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
141 [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
142 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
144 glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
145 glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
147 if (depthRenderbuffer != 0) {
148 glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
149 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthBufferFormat, backingWidth, backingHeight);
153 - (
void)setAnimationCallback:(
int)interval
154 callback:(
void (*)(
void*))callback
155 callbackParam:(
void*)callbackParam
157 [
self stopAnimation];
159 animationInterval = interval;
160 animationCallback = callback;
161 animationCallbackParam = callbackParam;
163 if (animationCallback)
164 [
self startAnimation];
173 displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doLoop:)];
174 [displayLink setFrameInterval:animationInterval];
175 [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
180 [displayLink invalidate];
184 - (
void)doLoop:(
id)sender
187 if (!UIKit_ShowingMessageBox()) {
188 animationCallback(animationCallbackParam);
194 [EAGLContext setCurrentContext:context];
200 glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
201 [context presentRenderbuffer:GL_RENDERBUFFER_OES];
205 - (
void)layoutSubviews
207 [EAGLContext setCurrentContext:context];
211 - (
void)destroyFramebuffer
213 glDeleteFramebuffersOES(1, &viewFramebuffer);
215 glDeleteRenderbuffersOES(1, &viewRenderbuffer);
216 viewRenderbuffer = 0;
218 if (depthRenderbuffer) {
219 glDeleteRenderbuffersOES(1, &depthRenderbuffer);
220 depthRenderbuffer = 0;
227 [
self destroyFramebuffer];
228 if ([EAGLContext currentContext] == context) {
229 [EAGLContext setCurrentContext:nil];
#define GL_DEPTH_COMPONENT24_OES
GLvoid **typedef void(GLAPIENTRY *PFNGLGETVERTEXATTRIBDVPROC)(GLuint
GLint GLboolean GLint layer
GLenum GLenum colorFormat
DECLSPEC int SDLCALL SDL_SetError(const char *fmt,...)
GLenum GLenum GLenum GLenum GLenum scale
#define GL_DEPTH24_STENCIL8_OES
typedef BOOL(WINAPI *PFNWGLSETSTEREOEMITTERSTATE3DLPROC)(HDC hDC