Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

iOS: Correctly resize renderbuffers when rotating orientation.

Fixes strange rendering after rotating the device.
  • Loading branch information
icculus committed Apr 3, 2011
1 parent e548e78 commit afb40985eb7cf963eae50f9eaa2e6f486c86ae59
@@ -132,6 +132,7 @@ SDL_GLContext UIKit_GL_CreateContext(_THIS, SDL_Window * window)
}

/* 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);

@@ -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* */

@@ -50,7 +50,6 @@ - (id)initWithFrame:(CGRect)frame \
majorVersion:(int)majorVersion \
{
NSString *colorFormat=nil;
GLuint depthBufferFormat;
BOOL useDepthBuffer;

if (rBits == 8 && gBits == 8 && bBits == 8) {
@@ -62,6 +61,8 @@ - (id)initWithFrame:(CGRect)frame \
colorFormat = kEAGLColorFormatRGB565;
}

depthBufferFormat = 0;

if (depthBits == 24) {
useDepthBuffer = YES;
depthBufferFormat = GL_DEPTH_COMPONENT24_OES;
@@ -108,7 +109,7 @@ - (id)initWithFrame:(CGRect)frame \

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 @@ - (id)initWithFrame:(CGRect)frame \
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];
}
@@ -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* */
@@ -43,7 +43,6 @@ struct SDL_WindowData
SDL_uikitviewcontroller *viewcontroller;
};


#endif /* _SDL_uikitwindow_h */

/* vi: set ts=4 sw=4 expandtab: */
@@ -55,7 +55,8 @@ - (void)loadView {
}

// 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 @@ - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrie
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);
}

0 comments on commit afb4098

Please sign in to comment.