iOS: Correctly resize renderbuffers when rotating orientation.
authorRyan C. Gordon <icculus@icculus.org>
Sun, 03 Apr 2011 18:33:32 -0500
changeset 5527 9a03d2300486
parent 5526 5645b3a5dc03
child 5528 15c9c03a80cc
iOS: Correctly resize renderbuffers when rotating orientation. Fixes strange rendering after rotating the device.
src/video/uikit/SDL_uikitopengles.m
src/video/uikit/SDL_uikitopenglview.h
src/video/uikit/SDL_uikitopenglview.m
src/video/uikit/SDL_uikitview.h
src/video/uikit/SDL_uikitwindow.h
src/video/uikit/SDL_uikitwindow.m
--- a/src/video/uikit/SDL_uikitopengles.m	Sun Apr 03 18:24:27 2011 +0700
+++ b/src/video/uikit/SDL_uikitopengles.m	Sun Apr 03 18:33:32 2011 -0500
@@ -132,6 +132,7 @@
     }
 
     /* Make this window the current mouse focus for touch input */
+    /* !!! FIXME: only do this if this is the primary screen. */
     SDL_SetMouseFocus(window);
     SDL_SetKeyboardFocus(window);
 
--- a/src/video/uikit/SDL_uikitopenglview.h	Sun Apr 03 18:24:27 2011 +0700
+++ b/src/video/uikit/SDL_uikitopenglview.h	Sun Apr 03 18:33:32 2011 -0500
@@ -45,7 +45,9 @@
     
     /* OpenGL name for the depth buffer that is attached to viewFramebuffer, if it exists (0 if it does not exist) */
     GLuint depthRenderbuffer;
-    
+
+    /* format of depthRenderbuffer */
+    GLenum depthBufferFormat;
 }
 
 @property (nonatomic, retain, readonly) EAGLContext *context;
@@ -62,6 +64,8 @@
     depthBits:(int)depthBits \
     majorVersion:(int)majorVersion;
 
+- (void)updateFrame;
+
 @end
 /* *INDENT-ON* */
 
--- a/src/video/uikit/SDL_uikitopenglview.m	Sun Apr 03 18:24:27 2011 +0700
+++ b/src/video/uikit/SDL_uikitopenglview.m	Sun Apr 03 18:33:32 2011 -0500
@@ -50,7 +50,6 @@
       majorVersion:(int)majorVersion \
 {
     NSString *colorFormat=nil;
-    GLuint depthBufferFormat;
     BOOL useDepthBuffer;
     
     if (rBits == 8 && gBits == 8 && bBits == 8) {
@@ -62,6 +61,8 @@
         colorFormat = kEAGLColorFormatRGB565;
     }
     
+    depthBufferFormat = 0;
+
     if (depthBits == 24) {
         useDepthBuffer = YES;
         depthBufferFormat = GL_DEPTH_COMPONENT24_OES;
@@ -108,7 +109,7 @@
         
         glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
         glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
-        
+
         if (useDepthBuffer) {
             glGenRenderbuffersOES(1, &depthRenderbuffer);
             glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
@@ -126,11 +127,36 @@
         if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
             self.contentScaleFactor = [UIScreen mainScreen].scale;
 
-        self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
+        self.autoresizingMask = 0;  // don't allow autoresize, since we need to do some magic in -(void)updateFrame.
     }
     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);
+    }
+    
+    // !!! FIXME: use the screen this is on!
+    /* Use the main screen scale (for retina display support) */
+    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
+        self.contentScaleFactor = [UIScreen mainScreen].scale;
+}
+
 - (void)setCurrentContext {
     [EAGLContext setCurrentContext:context];
 }
--- a/src/video/uikit/SDL_uikitview.h	Sun Apr 03 18:24:27 2011 +0700
+++ b/src/video/uikit/SDL_uikitview.h	Sun Apr 03 18:33:32 2011 -0500
@@ -37,7 +37,7 @@
 - (id)initWithSDLWindow:(SDL_Window *)_window;
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient;
 - (void)loadView;
-- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
+- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
 @end
 
 /* *INDENT-OFF* */
--- a/src/video/uikit/SDL_uikitwindow.h	Sun Apr 03 18:24:27 2011 +0700
+++ b/src/video/uikit/SDL_uikitwindow.h	Sun Apr 03 18:33:32 2011 -0500
@@ -43,7 +43,6 @@
     SDL_uikitviewcontroller *viewcontroller;
 };
 
-
 #endif /* _SDL_uikitwindow_h */
 
 /* vi: set ts=4 sw=4 expandtab: */
--- a/src/video/uikit/SDL_uikitwindow.m	Sun Apr 03 18:24:27 2011 +0700
+++ b/src/video/uikit/SDL_uikitwindow.m	Sun Apr 03 18:33:32 2011 -0500
@@ -55,7 +55,8 @@
 }
 
 // Send a resized event when the orientation changes.
-- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
+- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
+    const UIInterfaceOrientation toInterfaceOrientation = [self interfaceOrientation];
     SDL_WindowData *data = self->window->driverdata;
     UIWindow *uiwindow = data->uiwindow;
     CGRect frame = [uiwindow frame];
@@ -79,8 +80,11 @@
             SDL_assert(0 && "Unexpected interface orientation!");
             return;
     }
+
     frame.size.width = w;
     frame.size.height = h;
+    [uiwindow setFrame:frame];
+    [data->view updateFrame];
     SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
 }