src/video/uikit/SDL_uikitopengles.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Oct 2011 05:56:58 -0400
changeset 6044 35448a5ea044
parent 6021 a4bf2a9b0fd0
child 6088 eedb8e10ac22
permissions -rw-r--r--
Lots of fixes importing SDL source wholesale into a new iOS project
slouken@2765
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 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
*/
slouken@6044
    21
#include "SDL_config.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@2765
    28
#include "SDL_uikitwindow.h"
slouken@2765
    29
#include "jumphack.h"
slouken@2765
    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) {
slouken@5131
    74
        SDL_SetError("iPhone GL Load Library just here for compatibility");
slouken@5131
    75
        return -1;
slouken@2765
    76
    }
slouken@2765
    77
    return 0;
slouken@2765
    78
}
slouken@2765
    79
slouken@2765
    80
void UIKit_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@2765
    81
{
kees@6003
    82
#ifdef SDL_POWER_UIKIT
icculus@4444
    83
    // Check once a frame to see if we should turn off the battery monitor.
icculus@4444
    84
    SDL_UIKit_UpdateBatteryMonitoring();
kees@6003
    85
#endif
slouken@2765
    86
slouken@5131
    87
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
kees@6001
    88
slouken@5131
    89
    if (nil == data->view) {
slouken@5131
    90
        return;
slouken@5131
    91
    }
slouken@5131
    92
    [data->view swapBuffers];
slouken@5131
    93
    /* since now we've got something to draw
slouken@5131
    94
       make the window visible */
slouken@5131
    95
    [data->uiwindow makeKeyAndVisible];
slouken@2765
    96
slouken@5131
    97
    /* we need to let the event cycle run, or the OS won't update the OpenGL view! */
slouken@5131
    98
    SDL_PumpEvents();
kees@6001
    99
slouken@2765
   100
}
slouken@2765
   101
slouken@2765
   102
SDL_GLContext UIKit_GL_CreateContext(_THIS, SDL_Window * window)
slouken@2765
   103
{
slouken@5131
   104
    SDL_uikitopenglview *view;
slouken@5131
   105
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5131
   106
    UIWindow *uiwindow = data->uiwindow;
slouken@2765
   107
icculus@4446
   108
    /* construct our view, passing in SDL's OpenGL configuration data */
kees@6003
   109
    view = [[SDL_uikitopenglview alloc] initWithFrame: [uiwindow bounds]
kees@6003
   110
                                    retainBacking: _this->gl_config.retained_backing
kees@6003
   111
                                    rBits: _this->gl_config.red_size
kees@6003
   112
                                    gBits: _this->gl_config.green_size
kees@6003
   113
                                    bBits: _this->gl_config.blue_size
kees@6003
   114
                                    aBits: _this->gl_config.alpha_size
kees@6003
   115
                                    depthBits: _this->gl_config.depth_size
icculus@6021
   116
                                    stencilBits: _this->gl_config.stencil_size
slouken@5209
   117
                                    majorVersion: _this->gl_config.major_version];
kees@6001
   118
slouken@5131
   119
    data->view = view;
icculus@5520
   120
    view->viewcontroller = data->viewcontroller;
icculus@5520
   121
    if (view->viewcontroller != nil) {
icculus@5520
   122
        [view->viewcontroller setView:view];
icculus@5520
   123
        [view->viewcontroller retain];
icculus@5520
   124
    }
icculus@5520
   125
slouken@5131
   126
    /* add the view to our window */
slouken@5131
   127
    [uiwindow addSubview: view ];
icculus@5520
   128
slouken@5131
   129
    if ( UIKit_GL_MakeCurrent(_this, window, view) < 0 ) {
slouken@2765
   130
        UIKit_GL_DeleteContext(_this, view);
slouken@2765
   131
        return NULL;
slouken@2765
   132
    }
slouken@4488
   133
slouken@5131
   134
    /* Make this window the current mouse focus for touch input */
icculus@5527
   135
    /* !!! FIXME: only do this if this is the primary screen. */
slouken@5131
   136
    SDL_SetMouseFocus(window);
slouken@5131
   137
    SDL_SetKeyboardFocus(window);
slouken@4488
   138
slouken@5131
   139
    return view;
slouken@2765
   140
}
slouken@2765
   141
slouken@2765
   142
void UIKit_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@2765
   143
{
slouken@5131
   144
    /* the delegate has retained the view, this will release him */
slouken@5131
   145
    SDL_uikitopenglview *view = (SDL_uikitopenglview *)context;
icculus@5520
   146
    if (view->viewcontroller) {
icculus@5520
   147
        [view->viewcontroller setView:nil];
icculus@5520
   148
        [view->viewcontroller release];
icculus@5520
   149
    }
slouken@5131
   150
    [view removeFromSuperview];
icculus@5520
   151
    [view release];
slouken@2765
   152
}
slouken@2765
   153
slouken@6044
   154
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6044
   155
slouken@5132
   156
/* vi: set ts=4 sw=4 expandtab: */