iOS: Correctly resize renderbuffers when rotating orientation.
authorRyan C. Gordon <icculus@icculus.org>
Sun, 03 Apr 2011 18:33:32 -0500
changeset 55279a03d2300486
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
     1.1 --- a/src/video/uikit/SDL_uikitopengles.m	Sun Apr 03 18:24:27 2011 +0700
     1.2 +++ b/src/video/uikit/SDL_uikitopengles.m	Sun Apr 03 18:33:32 2011 -0500
     1.3 @@ -132,6 +132,7 @@
     1.4      }
     1.5  
     1.6      /* Make this window the current mouse focus for touch input */
     1.7 +    /* !!! FIXME: only do this if this is the primary screen. */
     1.8      SDL_SetMouseFocus(window);
     1.9      SDL_SetKeyboardFocus(window);
    1.10  
     2.1 --- a/src/video/uikit/SDL_uikitopenglview.h	Sun Apr 03 18:24:27 2011 +0700
     2.2 +++ b/src/video/uikit/SDL_uikitopenglview.h	Sun Apr 03 18:33:32 2011 -0500
     2.3 @@ -45,7 +45,9 @@
     2.4      
     2.5      /* OpenGL name for the depth buffer that is attached to viewFramebuffer, if it exists (0 if it does not exist) */
     2.6      GLuint depthRenderbuffer;
     2.7 -    
     2.8 +
     2.9 +    /* format of depthRenderbuffer */
    2.10 +    GLenum depthBufferFormat;
    2.11  }
    2.12  
    2.13  @property (nonatomic, retain, readonly) EAGLContext *context;
    2.14 @@ -62,6 +64,8 @@
    2.15      depthBits:(int)depthBits \
    2.16      majorVersion:(int)majorVersion;
    2.17  
    2.18 +- (void)updateFrame;
    2.19 +
    2.20  @end
    2.21  /* *INDENT-ON* */
    2.22  
     3.1 --- a/src/video/uikit/SDL_uikitopenglview.m	Sun Apr 03 18:24:27 2011 +0700
     3.2 +++ b/src/video/uikit/SDL_uikitopenglview.m	Sun Apr 03 18:33:32 2011 -0500
     3.3 @@ -50,7 +50,6 @@
     3.4        majorVersion:(int)majorVersion \
     3.5  {
     3.6      NSString *colorFormat=nil;
     3.7 -    GLuint depthBufferFormat;
     3.8      BOOL useDepthBuffer;
     3.9      
    3.10      if (rBits == 8 && gBits == 8 && bBits == 8) {
    3.11 @@ -62,6 +61,8 @@
    3.12          colorFormat = kEAGLColorFormatRGB565;
    3.13      }
    3.14      
    3.15 +    depthBufferFormat = 0;
    3.16 +
    3.17      if (depthBits == 24) {
    3.18          useDepthBuffer = YES;
    3.19          depthBufferFormat = GL_DEPTH_COMPONENT24_OES;
    3.20 @@ -108,7 +109,7 @@
    3.21          
    3.22          glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
    3.23          glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
    3.24 -        
    3.25 +
    3.26          if (useDepthBuffer) {
    3.27              glGenRenderbuffersOES(1, &depthRenderbuffer);
    3.28              glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
    3.29 @@ -126,11 +127,36 @@
    3.30          if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    3.31              self.contentScaleFactor = [UIScreen mainScreen].scale;
    3.32  
    3.33 -        self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    3.34 +        self.autoresizingMask = 0;  // don't allow autoresize, since we need to do some magic in -(void)updateFrame.
    3.35      }
    3.36      return self;
    3.37  }
    3.38  
    3.39 +- (void)updateFrame {
    3.40 +    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    3.41 +    glBindRenderbufferOES(GL_RENDERBUFFER_OES, 0);
    3.42 +    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, 0);
    3.43 +    glDeleteRenderbuffersOES(1, &viewRenderbuffer);
    3.44 +
    3.45 +    glGenRenderbuffersOES(1, &viewRenderbuffer);
    3.46 +    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    3.47 +    [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
    3.48 +    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);  
    3.49 +
    3.50 +    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
    3.51 +    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
    3.52 +
    3.53 +    if (depthRenderbuffer != 0) {
    3.54 +        glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
    3.55 +        glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthBufferFormat, backingWidth, backingHeight);
    3.56 +    }
    3.57 +    
    3.58 +    // !!! FIXME: use the screen this is on!
    3.59 +    /* Use the main screen scale (for retina display support) */
    3.60 +    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    3.61 +        self.contentScaleFactor = [UIScreen mainScreen].scale;
    3.62 +}
    3.63 +
    3.64  - (void)setCurrentContext {
    3.65      [EAGLContext setCurrentContext:context];
    3.66  }
     4.1 --- a/src/video/uikit/SDL_uikitview.h	Sun Apr 03 18:24:27 2011 +0700
     4.2 +++ b/src/video/uikit/SDL_uikitview.h	Sun Apr 03 18:33:32 2011 -0500
     4.3 @@ -37,7 +37,7 @@
     4.4  - (id)initWithSDLWindow:(SDL_Window *)_window;
     4.5  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient;
     4.6  - (void)loadView;
     4.7 -- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
     4.8 +- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
     4.9  @end
    4.10  
    4.11  /* *INDENT-OFF* */
     5.1 --- a/src/video/uikit/SDL_uikitwindow.h	Sun Apr 03 18:24:27 2011 +0700
     5.2 +++ b/src/video/uikit/SDL_uikitwindow.h	Sun Apr 03 18:33:32 2011 -0500
     5.3 @@ -43,7 +43,6 @@
     5.4      SDL_uikitviewcontroller *viewcontroller;
     5.5  };
     5.6  
     5.7 -
     5.8  #endif /* _SDL_uikitwindow_h */
     5.9  
    5.10  /* vi: set ts=4 sw=4 expandtab: */
     6.1 --- a/src/video/uikit/SDL_uikitwindow.m	Sun Apr 03 18:24:27 2011 +0700
     6.2 +++ b/src/video/uikit/SDL_uikitwindow.m	Sun Apr 03 18:33:32 2011 -0500
     6.3 @@ -55,7 +55,8 @@
     6.4  }
     6.5  
     6.6  // Send a resized event when the orientation changes.
     6.7 -- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
     6.8 +- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
     6.9 +    const UIInterfaceOrientation toInterfaceOrientation = [self interfaceOrientation];
    6.10      SDL_WindowData *data = self->window->driverdata;
    6.11      UIWindow *uiwindow = data->uiwindow;
    6.12      CGRect frame = [uiwindow frame];
    6.13 @@ -79,8 +80,11 @@
    6.14              SDL_assert(0 && "Unexpected interface orientation!");
    6.15              return;
    6.16      }
    6.17 +
    6.18      frame.size.width = w;
    6.19      frame.size.height = h;
    6.20 +    [uiwindow setFrame:frame];
    6.21 +    [data->view updateFrame];
    6.22      SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
    6.23  }
    6.24