src/video/uikit/SDL_uikitopenglview.m
author Ryan C. Gordon <icculus@icculus.org>
Fri, 12 Jul 2013 23:30:26 -0400
changeset 7423 dfe7ad02f008
parent 7191 75360622e65f
child 7784 70b305e09b4e
permissions -rw-r--r--
Implement SDL_GL_SHARE_WITH_CURRENT_CONTEXT for iOS. Fixes Bugzilla #1947.
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
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6686
diff changeset
     3
  Copyright (C) 1997-2013 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
*/
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 6022
diff changeset
    21
#include "SDL_config.h"
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 6022
diff changeset
    22
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 6022
diff changeset
    23
#if SDL_VIDEO_DRIVER_UIKIT
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
6686
45014250760c Don't run the game loop while a messagebox is up
Sam Lantinga <slouken@libsdl.org>
parents: 6435
diff changeset
    25
#include <QuartzCore/QuartzCore.h>
45014250760c Don't run the game loop while a messagebox is up
Sam Lantinga <slouken@libsdl.org>
parents: 6435
diff changeset
    26
#include <OpenGLES/EAGLDrawable.h>
45014250760c Don't run the game loop while a messagebox is up
Sam Lantinga <slouken@libsdl.org>
parents: 6435
diff changeset
    27
#include "SDL_uikitopenglview.h"
45014250760c Don't run the game loop while a messagebox is up
Sam Lantinga <slouken@libsdl.org>
parents: 6435
diff changeset
    28
#include "SDL_uikitmessagebox.h"
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
@implementation SDL_uikitopenglview
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
@synthesize context;
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
    35
+ (Class)layerClass
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
    36
{
5129
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
    37
    return [CAEAGLLayer class];
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
    40
- (id)initWithFrame:(CGRect)frame
6267
ed2cf4669e55 * On iOS, fix support for the case where [UIScreen scale] is not 1.0 (retina)
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 6179
diff changeset
    41
      scale:(CGFloat)scale
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
    42
      retainBacking:(BOOL)retained
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
    43
      rBits:(int)rBits
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
    44
      gBits:(int)gBits
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
    45
      bBits:(int)bBits
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
    46
      aBits:(int)aBits
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
    47
      depthBits:(int)depthBits
6021
a4bf2a9b0fd0 Added stencil buffer support on iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 6008
diff changeset
    48
      stencilBits:(int)stencilBits
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
    49
      majorVersion:(int)majorVersion
7423
dfe7ad02f008 Implement SDL_GL_SHARE_WITH_CURRENT_CONTEXT for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
    50
      shareGroup:(EAGLSharegroup*)shareGroup
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
{
5527
9a03d2300486 iOS: Correctly resize renderbuffers when rotating orientation.
Ryan C. Gordon <icculus@icculus.org>
parents: 5520
diff changeset
    52
    depthBufferFormat = 0;
9a03d2300486 iOS: Correctly resize renderbuffers when rotating orientation.
Ryan C. Gordon <icculus@icculus.org>
parents: 5520
diff changeset
    53
6022
e0327dd7d0c0 More cleanup in SDL_uikitopenglview.m
Ryan C. Gordon <icculus@icculus.org>
parents: 6021
diff changeset
    54
    if ((self = [super initWithFrame:frame])) {
e0327dd7d0c0 More cleanup in SDL_uikitopenglview.m
Ryan C. Gordon <icculus@icculus.org>
parents: 6021
diff changeset
    55
        const BOOL useStencilBuffer = (stencilBits != 0);
e0327dd7d0c0 More cleanup in SDL_uikitopenglview.m
Ryan C. Gordon <icculus@icculus.org>
parents: 6021
diff changeset
    56
        const BOOL useDepthBuffer = (depthBits != 0);
e0327dd7d0c0 More cleanup in SDL_uikitopenglview.m
Ryan C. Gordon <icculus@icculus.org>
parents: 6021
diff changeset
    57
        NSString *colorFormat = nil;
e0327dd7d0c0 More cleanup in SDL_uikitopenglview.m
Ryan C. Gordon <icculus@icculus.org>
parents: 6021
diff changeset
    58
e0327dd7d0c0 More cleanup in SDL_uikitopenglview.m
Ryan C. Gordon <icculus@icculus.org>
parents: 6021
diff changeset
    59
        if (rBits == 8 && gBits == 8 && bBits == 8) {
e0327dd7d0c0 More cleanup in SDL_uikitopenglview.m
Ryan C. Gordon <icculus@icculus.org>
parents: 6021
diff changeset
    60
            /* 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
    61
            colorFormat = kEAGLColorFormatRGBA8;
e0327dd7d0c0 More cleanup in SDL_uikitopenglview.m
Ryan C. Gordon <icculus@icculus.org>
parents: 6021
diff changeset
    62
        } else {
e0327dd7d0c0 More cleanup in SDL_uikitopenglview.m
Ryan C. Gordon <icculus@icculus.org>
parents: 6021
diff changeset
    63
            /* default case (faster) */
e0327dd7d0c0 More cleanup in SDL_uikitopenglview.m
Ryan C. Gordon <icculus@icculus.org>
parents: 6021
diff changeset
    64
            colorFormat = kEAGLColorFormatRGB565;
6021
a4bf2a9b0fd0 Added stencil buffer support on iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 6008
diff changeset
    65
        }
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
    66
6022
e0327dd7d0c0 More cleanup in SDL_uikitopenglview.m
Ryan C. Gordon <icculus@icculus.org>
parents: 6021
diff changeset
    67
        /* Get the layer */
5129
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
    68
        CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
    69
5129
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
    70
        eaglLayer.opaque = YES;
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
    71
        eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
    72
                                        [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
    73
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
    74
        if (majorVersion > 1) {
7423
dfe7ad02f008 Implement SDL_GL_SHARE_WITH_CURRENT_CONTEXT for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
    75
            context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2 sharegroup:shareGroup];
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
    76
        } else {
7423
dfe7ad02f008 Implement SDL_GL_SHARE_WITH_CURRENT_CONTEXT for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
    77
            context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1 sharegroup:shareGroup];
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
    78
        }
5129
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
    79
        if (!context || ![EAGLContext setCurrentContext:context]) {
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
    80
            [self release];
6179
a83eb92704f3 Fixed bug 1342 - SDL_CreateRenderer creates OpenGL ES 2.0 renderer in iPhone 3G using default index (-1)
Sam Lantinga <slouken@libsdl.org>
parents: 6164
diff changeset
    81
            SDL_SetError("OpenGL ES %d not supported", majorVersion);
5129
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
    82
            return nil;
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
    83
        }
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
    84
6267
ed2cf4669e55 * On iOS, fix support for the case where [UIScreen scale] is not 1.0 (retina)
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 6179
diff changeset
    85
        /* Set the appropriate scale (for retina display support) */
6164
67f122d8284f Fixed bug 1239 - Crash in iPad with iOS 3.2 because of missing contentScaleFactor support
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    86
        if ([self respondsToSelector:@selector(contentScaleFactor)])
6267
ed2cf4669e55 * On iOS, fix support for the case where [UIScreen scale] is not 1.0 (retina)
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 6179
diff changeset
    87
            self.contentScaleFactor = scale;
6085
fcffe5fdf54b Fixed where we set the content scale - it's used by the renderbufferStorage:GL_RENDERBUFFER_OES call.
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
    88
5129
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
    89
        /* create the buffers */
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
    90
        glGenFramebuffersOES(1, &viewFramebuffer);
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
    91
        glGenRenderbuffersOES(1, &viewRenderbuffer);
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
    92
5129
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
    93
        glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
    94
        glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
    95
        [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
    96
        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
    97
5129
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
    98
        glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
    99
        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
   100
6021
a4bf2a9b0fd0 Added stencil buffer support on iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 6008
diff changeset
   101
        if ((useDepthBuffer) || (useStencilBuffer)) {
a4bf2a9b0fd0 Added stencil buffer support on iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 6008
diff changeset
   102
            if (useStencilBuffer) {
6022
e0327dd7d0c0 More cleanup in SDL_uikitopenglview.m
Ryan C. Gordon <icculus@icculus.org>
parents: 6021
diff changeset
   103
                /* 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
   104
                depthBufferFormat = GL_DEPTH24_STENCIL8_OES;
6022
e0327dd7d0c0 More cleanup in SDL_uikitopenglview.m
Ryan C. Gordon <icculus@icculus.org>
parents: 6021
diff changeset
   105
            } else if (useDepthBuffer) {
e0327dd7d0c0 More cleanup in SDL_uikitopenglview.m
Ryan C. Gordon <icculus@icculus.org>
parents: 6021
diff changeset
   106
                /* 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
   107
                depthBufferFormat = GL_DEPTH_COMPONENT24_OES;
6021
a4bf2a9b0fd0 Added stencil buffer support on iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 6008
diff changeset
   108
            }
6022
e0327dd7d0c0 More cleanup in SDL_uikitopenglview.m
Ryan C. Gordon <icculus@icculus.org>
parents: 6021
diff changeset
   109
5129
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   110
            glGenRenderbuffersOES(1, &depthRenderbuffer);
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   111
            glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   112
            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
   113
            if (useDepthBuffer) {
a4bf2a9b0fd0 Added stencil buffer support on iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 6008
diff changeset
   114
                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
   115
            }
a4bf2a9b0fd0 Added stencil buffer support on iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 6008
diff changeset
   116
            if (useStencilBuffer) {
a4bf2a9b0fd0 Added stencil buffer support on iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 6008
diff changeset
   117
                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
   118
            }
5129
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   119
        }
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
   120
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   121
        if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
5129
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   122
            return NO;
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   123
        }
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   124
        /* end create buffers */
5130
ec13e48ee0d9 Fixed bug #1112 (retina display support)
Sam Lantinga <slouken@libsdl.org>
parents: 5129
diff changeset
   125
6435
6172658e3ce9 We can update the OpenGL layer binding when we're laying out the views
Sam Lantinga <slouken@libsdl.org>
parents: 6342
diff changeset
   126
        self.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
6172658e3ce9 We can update the OpenGL layer binding when we're laying out the views
Sam Lantinga <slouken@libsdl.org>
parents: 6342
diff changeset
   127
        self.autoresizesSubviews = YES;
5129
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   128
    }
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   129
    return self;
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   130
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   131
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   132
- (void)updateFrame
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   133
{
5527
9a03d2300486 iOS: Correctly resize renderbuffers when rotating orientation.
Ryan C. Gordon <icculus@icculus.org>
parents: 5520
diff changeset
   134
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
9a03d2300486 iOS: Correctly resize renderbuffers when rotating orientation.
Ryan C. Gordon <icculus@icculus.org>
parents: 5520
diff changeset
   135
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, 0);
9a03d2300486 iOS: Correctly resize renderbuffers when rotating orientation.
Ryan C. Gordon <icculus@icculus.org>
parents: 5520
diff changeset
   136
    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
   137
    glDeleteRenderbuffersOES(1, &viewRenderbuffer);
9a03d2300486 iOS: Correctly resize renderbuffers when rotating orientation.
Ryan C. Gordon <icculus@icculus.org>
parents: 5520
diff changeset
   138
9a03d2300486 iOS: Correctly resize renderbuffers when rotating orientation.
Ryan C. Gordon <icculus@icculus.org>
parents: 5520
diff changeset
   139
    glGenRenderbuffersOES(1, &viewRenderbuffer);
9a03d2300486 iOS: Correctly resize renderbuffers when rotating orientation.
Ryan C. Gordon <icculus@icculus.org>
parents: 5520
diff changeset
   140
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
9a03d2300486 iOS: Correctly resize renderbuffers when rotating orientation.
Ryan C. Gordon <icculus@icculus.org>
parents: 5520
diff changeset
   141
    [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
   142
    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
   143
9a03d2300486 iOS: Correctly resize renderbuffers when rotating orientation.
Ryan C. Gordon <icculus@icculus.org>
parents: 5520
diff changeset
   144
    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
   145
    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
   146
9a03d2300486 iOS: Correctly resize renderbuffers when rotating orientation.
Ryan C. Gordon <icculus@icculus.org>
parents: 5520
diff changeset
   147
    if (depthRenderbuffer != 0) {
9a03d2300486 iOS: Correctly resize renderbuffers when rotating orientation.
Ryan C. Gordon <icculus@icculus.org>
parents: 5520
diff changeset
   148
        glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
9a03d2300486 iOS: Correctly resize renderbuffers when rotating orientation.
Ryan C. Gordon <icculus@icculus.org>
parents: 5520
diff changeset
   149
        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
   150
    }
9a03d2300486 iOS: Correctly resize renderbuffers when rotating orientation.
Ryan C. Gordon <icculus@icculus.org>
parents: 5520
diff changeset
   151
}
9a03d2300486 iOS: Correctly resize renderbuffers when rotating orientation.
Ryan C. Gordon <icculus@icculus.org>
parents: 5520
diff changeset
   152
6342
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   153
- (void)setAnimationCallback:(int)interval
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   154
    callback:(void (*)(void*))callback
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   155
    callbackParam:(void*)callbackParam
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   156
{
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   157
    [self stopAnimation];
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   158
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   159
    animationInterval = interval;
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   160
    animationCallback = callback;
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   161
    animationCallbackParam = callbackParam;
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   162
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   163
    if (animationCallback)
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   164
        [self startAnimation];
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   165
}
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   166
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   167
- (void)startAnimation
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   168
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   169
    /* CADisplayLink is API new to iPhone SDK 3.1.
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   170
     * Compiling against earlier versions will result in a warning, but can be dismissed
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   171
     * if the system version runtime check for CADisplayLink exists in -initWithCoder:.
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   172
     */
6342
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   173
    displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doLoop:)];
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   174
    [displayLink setFrameInterval:animationInterval];
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   175
    [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   176
}
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   177
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   178
- (void)stopAnimation
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   179
{
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   180
    [displayLink invalidate];
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   181
    displayLink = nil;
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   182
}
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   183
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   184
- (void)doLoop:(id)sender
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   185
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   186
    /* Don't run the game loop while a messagebox is up */
6686
45014250760c Don't run the game loop while a messagebox is up
Sam Lantinga <slouken@libsdl.org>
parents: 6435
diff changeset
   187
    if (!UIKit_ShowingMessageBox()) {
45014250760c Don't run the game loop while a messagebox is up
Sam Lantinga <slouken@libsdl.org>
parents: 6435
diff changeset
   188
        animationCallback(animationCallbackParam);
45014250760c Don't run the game loop while a messagebox is up
Sam Lantinga <slouken@libsdl.org>
parents: 6435
diff changeset
   189
    }
6342
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   190
}
ac83b73f0edd Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback()
Sam Lantinga <slouken@libsdl.org>
parents: 6267
diff changeset
   191
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   192
- (void)setCurrentContext
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   193
{
5129
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   194
    [EAGLContext setCurrentContext:context];
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   195
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   196
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   197
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   198
- (void)swapBuffers
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   199
{
5129
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   200
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   201
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   202
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   203
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   204
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   205
- (void)layoutSubviews
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   206
{
5129
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   207
    [EAGLContext setCurrentContext:context];
6435
6172658e3ce9 We can update the OpenGL layer binding when we're laying out the views
Sam Lantinga <slouken@libsdl.org>
parents: 6342
diff changeset
   208
    [self updateFrame];
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   209
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   210
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   211
- (void)destroyFramebuffer
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   212
{
5129
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   213
    glDeleteFramebuffersOES(1, &viewFramebuffer);
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   214
    viewFramebuffer = 0;
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   215
    glDeleteRenderbuffersOES(1, &viewRenderbuffer);
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   216
    viewRenderbuffer = 0;
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
   217
5129
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   218
    if (depthRenderbuffer) {
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   219
        glDeleteRenderbuffersOES(1, &depthRenderbuffer);
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   220
        depthRenderbuffer = 0;
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   221
    }
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   222
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   223
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   224
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   225
- (void)dealloc
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   226
{
5129
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   227
    [self destroyFramebuffer];
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   228
    if ([EAGLContext currentContext] == context) {
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   229
        [EAGLContext setCurrentContext:nil];
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   230
    }
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
   231
    [context release];
5129
5690ebc772c7 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   232
    [super dealloc];
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   233
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   234
2859
99210400e8b9 Updated copyright date
Sam Lantinga <slouken@libsdl.org>
parents: 2765
diff changeset
   235
@end
5132
5f09cb749d75 Added vim spacing hints
Sam Lantinga <slouken@libsdl.org>
parents: 5130
diff changeset
   236
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 6022
diff changeset
   237
#endif /* SDL_VIDEO_DRIVER_UIKIT */
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 6022
diff changeset
   238
5132
5f09cb749d75 Added vim spacing hints
Sam Lantinga <slouken@libsdl.org>
parents: 5130
diff changeset
   239
/* vi: set ts=4 sw=4 expandtab: */