src/video/uikit/SDL_uikitopengles.m
author Alex Szpakowski <slime73@gmail.com>
Wed, 23 Jul 2014 21:55:42 -0300
branchiOS-improvements
changeset 9502 933ed557b7c1
parent 9498 d02c27e3f698
child 9506 18e3f94bd860
permissions -rw-r--r--
Fixed SDL_SetWindowFullscreen on iOS for the last time, hopefully.

Fixed iOS version checking code.
slouken@2765
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@5262
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@5262
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@5262
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@5262
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@6044
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_UIKIT
slouken@2765
    24
slouken@2765
    25
#include "SDL_uikitopengles.h"
slouken@2765
    26
#include "SDL_uikitopenglview.h"
slouken@2765
    27
#include "SDL_uikitappdelegate.h"
slouken@6518
    28
#include "SDL_uikitmodes.h"
slouken@2765
    29
#include "SDL_uikitwindow.h"
slouken@6321
    30
#include "../SDL_sysvideo.h"
slouken@4490
    31
#include "../../events/SDL_keyboard_c.h"
slouken@4490
    32
#include "../../events/SDL_mouse_c.h"
slouken@6044
    33
#include "../../power/uikit/SDL_syspower.h"
slouken@2765
    34
#include "SDL_loadso.h"
slouken@2765
    35
#include <dlfcn.h>
slouken@2765
    36
slouken@2765
    37
static int UIKit_GL_Initialize(_THIS);
slouken@2765
    38
slouken@2765
    39
void *
slouken@2765
    40
UIKit_GL_GetProcAddress(_THIS, const char *proc)
kees@6001
    41
{
slouken@5131
    42
    /* Look through all SO's for the proc symbol.  Here's why:
slouken@5131
    43
       -Looking for the path to the OpenGL Library seems not to work in the iPhone Simulator.
slouken@5131
    44
       -We don't know that the path won't change in the future.
slouken@5131
    45
    */
slouken@5363
    46
    return dlsym(RTLD_DEFAULT, proc);
slouken@2765
    47
}
slouken@2765
    48
slouken@2765
    49
/*
slouken@5131
    50
    note that SDL_GL_Delete context makes it current without passing the window
slouken@2765
    51
*/
slime73@9488
    52
int
slime73@9488
    53
UIKit_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@2765
    54
{
slouken@5131
    55
    if (context) {
slouken@5131
    56
        SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slouken@5131
    57
        [data->view setCurrentContext];
slouken@5131
    58
    }
slouken@5131
    59
    else {
slouken@5131
    60
        [EAGLContext setCurrentContext: nil];
slouken@5131
    61
    }
kees@6001
    62
slouken@2765
    63
    return 0;
slouken@2765
    64
}
slouken@2765
    65
slime73@9488
    66
void UIKit_GL_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h)
slime73@9488
    67
{
slime73@9488
    68
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slime73@9488
    69
slime73@9488
    70
    if (w) {
slime73@9488
    71
        *w = data->view.backingWidth;
slime73@9488
    72
    }
slime73@9488
    73
    if (h) {
slime73@9488
    74
        *h = data->view.backingHeight;
slime73@9488
    75
    }
slime73@9488
    76
}
slime73@9488
    77
slime73@9488
    78
slouken@2765
    79
int
slouken@2765
    80
UIKit_GL_LoadLibrary(_THIS, const char *path)
slouken@2765
    81
{
kees@6001
    82
    /*
kees@6001
    83
        shouldn't be passing a path into this function
slouken@5131
    84
        why?  Because we've already loaded the library
slouken@5131
    85
        and because the SDK forbids loading an external SO
slouken@5131
    86
    */
slouken@2765
    87
    if (path != NULL) {
icculus@7037
    88
        return SDL_SetError("iPhone GL Load Library just here for compatibility");
slouken@2765
    89
    }
slouken@2765
    90
    return 0;
slouken@2765
    91
}
slouken@2765
    92
slouken@2765
    93
void UIKit_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@2765
    94
{
icculus@6088
    95
#if SDL_POWER_UIKIT
slouken@7191
    96
    /* Check once a frame to see if we should turn off the battery monitor. */
icculus@4444
    97
    SDL_UIKit_UpdateBatteryMonitoring();
kees@6003
    98
#endif
slouken@2765
    99
slouken@5131
   100
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
kees@6001
   101
slouken@5131
   102
    if (nil == data->view) {
slouken@5131
   103
        return;
slouken@5131
   104
    }
slouken@5131
   105
    [data->view swapBuffers];
slouken@2765
   106
slouken@7190
   107
    /* You need to pump events in order for the OS to make changes visible.
slouken@7190
   108
       We don't pump events here because we don't want iOS application events
slouken@7190
   109
       (low memory, terminate, etc.) to happen inside low level rendering.
slouken@7190
   110
     */
slouken@2765
   111
}
slouken@2765
   112
slime73@9488
   113
SDL_GLContext
slime73@9488
   114
UIKit_GL_CreateContext(_THIS, SDL_Window * window)
slouken@2765
   115
{
slouken@5131
   116
    SDL_uikitopenglview *view;
slouken@5131
   117
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5131
   118
    UIWindow *uiwindow = data->uiwindow;
slime73@9502
   119
    CGRect frame = UIKit_ComputeViewFrame(window, uiwindow.screen);
icculus@7423
   120
    EAGLSharegroup *share_group = nil;
slime73@9488
   121
    CGFloat scale = 1.0;
slime73@9488
   122
slime73@9488
   123
    if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
slime73@9488
   124
        /* Set the scale to the natural scale factor of the screen - the backing
slime73@9488
   125
           dimensions of the OpenGL view will match the pixel dimensions of the
slime73@9488
   126
           screen rather than the dimensions in points.
slime73@9488
   127
         */
slime73@9502
   128
        scale = uiwindow.screen.scale;
slime73@9488
   129
    }
icculus@7423
   130
icculus@7423
   131
    if (_this->gl_config.share_with_current_context) {
icculus@7425
   132
        SDL_uikitopenglview *view = (SDL_uikitopenglview *) SDL_GL_GetCurrentContext();
icculus@7425
   133
        share_group = [view.context sharegroup];
icculus@7423
   134
    }
slouken@2765
   135
slime73@9498
   136
    /* construct our view, passing in SDL's OpenGL configuration data */
slouken@6439
   137
    view = [[SDL_uikitopenglview alloc] initWithFrame: frame
slime73@9498
   138
                                                scale: scale
slime73@9498
   139
                                        retainBacking: _this->gl_config.retained_backing
slime73@9498
   140
                                                rBits: _this->gl_config.red_size
slime73@9498
   141
                                                gBits: _this->gl_config.green_size
slime73@9498
   142
                                                bBits: _this->gl_config.blue_size
slime73@9498
   143
                                                aBits: _this->gl_config.alpha_size
slime73@9498
   144
                                            depthBits: _this->gl_config.depth_size
slime73@9498
   145
                                          stencilBits: _this->gl_config.stencil_size
slime73@9498
   146
                                                 sRGB: _this->gl_config.framebuffer_srgb_capable
slime73@9498
   147
                                         majorVersion: _this->gl_config.major_version
slime73@9498
   148
                                           shareGroup: share_group];
slouken@6179
   149
    if (!view) {
slouken@6179
   150
        return NULL;
slouken@6179
   151
    }
kees@6001
   152
slouken@5131
   153
    data->view = view;
icculus@5520
   154
    view->viewcontroller = data->viewcontroller;
icculus@5520
   155
    if (view->viewcontroller != nil) {
icculus@5520
   156
        [view->viewcontroller setView:view];
icculus@5520
   157
        [view->viewcontroller retain];
icculus@5520
   158
    }
slouken@6521
   159
    [uiwindow addSubview: view];
slouken@7191
   160
slouken@7191
   161
    /* The view controller needs to be the root in order to control rotation on iOS 6.0 */
slouken@6451
   162
    if (uiwindow.rootViewController == nil) {
slouken@6451
   163
        uiwindow.rootViewController = view->viewcontroller;
slouken@6451
   164
    }
icculus@5520
   165
slouken@6521
   166
    if (UIKit_GL_MakeCurrent(_this, window, view) < 0) {
slouken@2765
   167
        UIKit_GL_DeleteContext(_this, view);
slouken@2765
   168
        return NULL;
slouken@2765
   169
    }
slouken@4488
   170
slouken@5131
   171
    /* Make this window the current mouse focus for touch input */
slime73@9502
   172
    if (uiwindow.screen == [UIScreen mainScreen]) {
tim@6267
   173
        SDL_SetMouseFocus(window);
tim@6267
   174
        SDL_SetKeyboardFocus(window);
tim@6267
   175
    }
slouken@4488
   176
slouken@5131
   177
    return view;
slouken@2765
   178
}
slouken@2765
   179
slime73@9488
   180
void
slime73@9488
   181
UIKit_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@2765
   182
{
slouken@5131
   183
    /* the delegate has retained the view, this will release him */
slouken@5131
   184
    SDL_uikitopenglview *view = (SDL_uikitopenglview *)context;
icculus@5520
   185
    if (view->viewcontroller) {
slouken@6521
   186
        UIWindow *uiwindow = (UIWindow *)view.superview;
slouken@6521
   187
        if (uiwindow.rootViewController == view->viewcontroller) {
slouken@6521
   188
            uiwindow.rootViewController = nil;
slouken@6521
   189
        }
icculus@5520
   190
        [view->viewcontroller setView:nil];
icculus@5520
   191
        [view->viewcontroller release];
icculus@5520
   192
    }
slouken@5131
   193
    [view removeFromSuperview];
icculus@5520
   194
    [view release];
slouken@2765
   195
}
slouken@2765
   196
slouken@6044
   197
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6044
   198
slouken@5132
   199
/* vi: set ts=4 sw=4 expandtab: */