src/video/uikit/SDL_uikitopenglview.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Oct 2011 05:56:58 -0400
changeset 6044 35448a5ea044
parent 6022 e0327dd7d0c0
child 6085 fcffe5fdf54b
permissions -rw-r--r--
Lots of fixes importing SDL source wholesale into a new iOS project
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 #if SDL_VIDEO_DRIVER_UIKIT
    24 
    25 #import <QuartzCore/QuartzCore.h>
    26 #import <OpenGLES/EAGLDrawable.h>
    27 #import "SDL_uikitopenglview.h"
    28 
    29 
    30 @implementation SDL_uikitopenglview
    31 
    32 @synthesize context;
    33 
    34 + (Class)layerClass
    35 {
    36     return [CAEAGLLayer class];
    37 }
    38 
    39 - (id)initWithFrame:(CGRect)frame
    40       retainBacking:(BOOL)retained
    41       rBits:(int)rBits
    42       gBits:(int)gBits
    43       bBits:(int)bBits
    44       aBits:(int)aBits
    45       depthBits:(int)depthBits
    46       stencilBits:(int)stencilBits
    47       majorVersion:(int)majorVersion
    48 {
    49     depthBufferFormat = 0;
    50 
    51     if ((self = [super initWithFrame:frame])) {
    52         const BOOL useStencilBuffer = (stencilBits != 0);
    53         const BOOL useDepthBuffer = (depthBits != 0);
    54         NSString *colorFormat = nil;
    55 
    56         if (rBits == 8 && gBits == 8 && bBits == 8) {
    57             /* if user specifically requests rbg888 or some color format higher than 16bpp */
    58             colorFormat = kEAGLColorFormatRGBA8;
    59         } else {
    60             /* default case (faster) */
    61             colorFormat = kEAGLColorFormatRGB565;
    62         }
    63 
    64         /* Get the layer */
    65         CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
    66 
    67         eaglLayer.opaque = YES;
    68         eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
    69                                         [NSNumber numberWithBool: retained], kEAGLDrawablePropertyRetainedBacking, colorFormat, kEAGLDrawablePropertyColorFormat, nil];
    70 
    71         if (majorVersion > 1) {
    72             context = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
    73         } else {
    74             context = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES1];
    75         }
    76         if (!context || ![EAGLContext setCurrentContext:context]) {
    77             [self release];
    78             return nil;
    79         }
    80 
    81         /* create the buffers */
    82         glGenFramebuffersOES(1, &viewFramebuffer);
    83         glGenRenderbuffersOES(1, &viewRenderbuffer);
    84 
    85         glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    86         glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    87         [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
    88         glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
    89 
    90         glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
    91         glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
    92 
    93         if ((useDepthBuffer) || (useStencilBuffer)) {
    94             if (useStencilBuffer) {
    95                 /* Apparently you need to pack stencil and depth into one buffer. */
    96                 depthBufferFormat = GL_DEPTH24_STENCIL8_OES;
    97             } else if (useDepthBuffer) {
    98                 /* iOS only has 24-bit depth buffers, even with GL_DEPTH_COMPONENT16_OES */
    99                 depthBufferFormat = GL_DEPTH_COMPONENT24_OES;
   100             }
   101 
   102             glGenRenderbuffersOES(1, &depthRenderbuffer);
   103             glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
   104             glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthBufferFormat, backingWidth, backingHeight);
   105             if (useDepthBuffer) {
   106                 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
   107             }
   108             if (useStencilBuffer) {
   109                 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_STENCIL_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
   110             }
   111         }
   112 
   113         if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
   114             return NO;
   115         }
   116         /* end create buffers */
   117 
   118         // !!! FIXME: use the screen this is on!
   119         /* Use the main screen scale (for retina display support) */
   120         if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
   121             self.contentScaleFactor = [UIScreen mainScreen].scale;
   122 
   123         self.autoresizingMask = 0;  // don't allow autoresize, since we need to do some magic in -(void)updateFrame.
   124     }
   125     return self;
   126 }
   127 
   128 - (void)updateFrame
   129 {
   130     glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
   131     glBindRenderbufferOES(GL_RENDERBUFFER_OES, 0);
   132     glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, 0);
   133     glDeleteRenderbuffersOES(1, &viewRenderbuffer);
   134 
   135     glGenRenderbuffersOES(1, &viewRenderbuffer);
   136     glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
   137     [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
   138     glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
   139 
   140     glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
   141     glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
   142 
   143     if (depthRenderbuffer != 0) {
   144         glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
   145         glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthBufferFormat, backingWidth, backingHeight);
   146     }
   147 
   148     // !!! FIXME: use the screen this is on!
   149     /* Use the main screen scale (for retina display support) */
   150     if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
   151         self.contentScaleFactor = [UIScreen mainScreen].scale;
   152 }
   153 
   154 - (void)setCurrentContext
   155 {
   156     [EAGLContext setCurrentContext:context];
   157 }
   158 
   159 
   160 - (void)swapBuffers
   161 {
   162     glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
   163     [context presentRenderbuffer:GL_RENDERBUFFER_OES];
   164 }
   165 
   166 
   167 - (void)layoutSubviews
   168 {
   169     [EAGLContext setCurrentContext:context];
   170 }
   171 
   172 - (void)destroyFramebuffer
   173 {
   174     glDeleteFramebuffersOES(1, &viewFramebuffer);
   175     viewFramebuffer = 0;
   176     glDeleteRenderbuffersOES(1, &viewRenderbuffer);
   177     viewRenderbuffer = 0;
   178 
   179     if (depthRenderbuffer) {
   180         glDeleteRenderbuffersOES(1, &depthRenderbuffer);
   181         depthRenderbuffer = 0;
   182     }
   183 }
   184 
   185 
   186 - (void)dealloc
   187 {
   188     [self destroyFramebuffer];
   189     if ([EAGLContext currentContext] == context) {
   190         [EAGLContext setCurrentContext:nil];
   191     }
   192     [context release];
   193     [super dealloc];
   194 }
   195 
   196 @end
   197 
   198 #endif /* SDL_VIDEO_DRIVER_UIKIT */
   199 
   200 /* vi: set ts=4 sw=4 expandtab: */