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