src/video/uikit/SDL_uikitopengles.m
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Feb 2014 00:53:27 -0800
changeset 8149 681eb46b8ac4
parent 8093 b43765095a6f
child 8583 fb2933ca805f
permissions -rw-r--r--
Fixed bug 2374 - Update copyright for 2014...

Is it that time already??
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
*/
slouken@2765
    52
int UIKit_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@2765
    53
{
slouken@5131
    54
    if (context) {
slouken@5131
    55
        SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slouken@5131
    56
        [data->view setCurrentContext];
slouken@5131
    57
    }
slouken@5131
    58
    else {
slouken@5131
    59
        [EAGLContext setCurrentContext: nil];
slouken@5131
    60
    }
kees@6001
    61
slouken@2765
    62
    return 0;
slouken@2765
    63
}
slouken@2765
    64
slouken@2765
    65
int
slouken@2765
    66
UIKit_GL_LoadLibrary(_THIS, const char *path)
slouken@2765
    67
{
kees@6001
    68
    /*
kees@6001
    69
        shouldn't be passing a path into this function
slouken@5131
    70
        why?  Because we've already loaded the library
slouken@5131
    71
        and because the SDK forbids loading an external SO
slouken@5131
    72
    */
slouken@2765
    73
    if (path != NULL) {
icculus@7037
    74
        return SDL_SetError("iPhone GL Load Library just here for compatibility");
slouken@2765
    75
    }
slouken@2765
    76
    return 0;
slouken@2765
    77
}
slouken@2765
    78
slouken@2765
    79
void UIKit_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@2765
    80
{
icculus@6088
    81
#if SDL_POWER_UIKIT
slouken@7191
    82
    /* Check once a frame to see if we should turn off the battery monitor. */
icculus@4444
    83
    SDL_UIKit_UpdateBatteryMonitoring();
kees@6003
    84
#endif
slouken@2765
    85
slouken@5131
    86
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
kees@6001
    87
slouken@5131
    88
    if (nil == data->view) {
slouken@5131
    89
        return;
slouken@5131
    90
    }
slouken@5131
    91
    [data->view swapBuffers];
slouken@2765
    92
slouken@7190
    93
    /* You need to pump events in order for the OS to make changes visible.
slouken@7190
    94
       We don't pump events here because we don't want iOS application events
slouken@7190
    95
       (low memory, terminate, etc.) to happen inside low level rendering.
slouken@7190
    96
     */
slouken@2765
    97
}
slouken@2765
    98
slouken@2765
    99
SDL_GLContext UIKit_GL_CreateContext(_THIS, SDL_Window * window)
slouken@2765
   100
{
slouken@5131
   101
    SDL_uikitopenglview *view;
slouken@5131
   102
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
tim@6267
   103
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
tim@6267
   104
    SDL_DisplayData *displaydata = display->driverdata;
tim@6267
   105
    SDL_DisplayModeData *displaymodedata = display->current_mode.driverdata;
slouken@5131
   106
    UIWindow *uiwindow = data->uiwindow;
icculus@7423
   107
    EAGLSharegroup *share_group = nil;
icculus@7423
   108
icculus@7423
   109
    if (_this->gl_config.share_with_current_context) {
icculus@7425
   110
        SDL_uikitopenglview *view = (SDL_uikitopenglview *) SDL_GL_GetCurrentContext();
icculus@7425
   111
        share_group = [view.context sharegroup];
icculus@7423
   112
    }
slouken@2765
   113
icculus@4446
   114
    /* construct our view, passing in SDL's OpenGL configuration data */
slouken@6439
   115
    CGRect frame;
slouken@6439
   116
    if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
slouken@6439
   117
        frame = [displaydata->uiscreen bounds];
slouken@6439
   118
    } else {
slouken@6439
   119
        frame = [displaydata->uiscreen applicationFrame];
slouken@6439
   120
    }
slouken@6439
   121
    view = [[SDL_uikitopenglview alloc] initWithFrame: frame
tim@6267
   122
                                    scale: displaymodedata->scale
kees@6003
   123
                                    retainBacking: _this->gl_config.retained_backing
kees@6003
   124
                                    rBits: _this->gl_config.red_size
kees@6003
   125
                                    gBits: _this->gl_config.green_size
kees@6003
   126
                                    bBits: _this->gl_config.blue_size
kees@6003
   127
                                    aBits: _this->gl_config.alpha_size
kees@6003
   128
                                    depthBits: _this->gl_config.depth_size
icculus@6021
   129
                                    stencilBits: _this->gl_config.stencil_size
icculus@7423
   130
                                    majorVersion: _this->gl_config.major_version
icculus@7423
   131
                                    shareGroup: share_group];
slouken@6179
   132
    if (!view) {
slouken@6179
   133
        return NULL;
slouken@6179
   134
    }
kees@6001
   135
slouken@5131
   136
    data->view = view;
icculus@5520
   137
    view->viewcontroller = data->viewcontroller;
icculus@5520
   138
    if (view->viewcontroller != nil) {
icculus@5520
   139
        [view->viewcontroller setView:view];
icculus@5520
   140
        [view->viewcontroller retain];
icculus@5520
   141
    }
slouken@6521
   142
    [uiwindow addSubview: view];
slouken@7191
   143
slouken@7191
   144
    /* The view controller needs to be the root in order to control rotation on iOS 6.0 */
slouken@6451
   145
    if (uiwindow.rootViewController == nil) {
slouken@6451
   146
        uiwindow.rootViewController = view->viewcontroller;
slouken@6451
   147
    }
icculus@5520
   148
slouken@6521
   149
    if (UIKit_GL_MakeCurrent(_this, window, view) < 0) {
slouken@2765
   150
        UIKit_GL_DeleteContext(_this, view);
slouken@2765
   151
        return NULL;
slouken@2765
   152
    }
slouken@4488
   153
slouken@5131
   154
    /* Make this window the current mouse focus for touch input */
tim@6267
   155
    if (displaydata->uiscreen == [UIScreen mainScreen]) {
tim@6267
   156
        SDL_SetMouseFocus(window);
tim@6267
   157
        SDL_SetKeyboardFocus(window);
tim@6267
   158
    }
slouken@4488
   159
slouken@5131
   160
    return view;
slouken@2765
   161
}
slouken@2765
   162
slouken@2765
   163
void UIKit_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@2765
   164
{
slouken@5131
   165
    /* the delegate has retained the view, this will release him */
slouken@5131
   166
    SDL_uikitopenglview *view = (SDL_uikitopenglview *)context;
icculus@5520
   167
    if (view->viewcontroller) {
slouken@6521
   168
        UIWindow *uiwindow = (UIWindow *)view.superview;
slouken@6521
   169
        if (uiwindow.rootViewController == view->viewcontroller) {
slouken@6521
   170
            uiwindow.rootViewController = nil;
slouken@6521
   171
        }
icculus@5520
   172
        [view->viewcontroller setView:nil];
icculus@5520
   173
        [view->viewcontroller release];
icculus@5520
   174
    }
slouken@5131
   175
    [view removeFromSuperview];
icculus@5520
   176
    [view release];
slouken@2765
   177
}
slouken@2765
   178
slouken@6044
   179
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6044
   180
slouken@5132
   181
/* vi: set ts=4 sw=4 expandtab: */