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
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 #if SDL_VIDEO_DRIVER_UIKIT
    24 
    25 #include "SDL_uikitopengles.h"
    26 #include "SDL_uikitopenglview.h"
    27 #include "SDL_uikitappdelegate.h"
    28 #include "SDL_uikitwindow.h"
    29 #include "jumphack.h"
    30 #include "SDL_sysvideo.h"
    31 #include "../../events/SDL_keyboard_c.h"
    32 #include "../../events/SDL_mouse_c.h"
    33 #include "../../power/uikit/SDL_syspower.h"
    34 #include "SDL_loadso.h"
    35 #include <dlfcn.h>
    36 
    37 static int UIKit_GL_Initialize(_THIS);
    38 
    39 void *
    40 UIKit_GL_GetProcAddress(_THIS, const char *proc)
    41 {
    42     /* Look through all SO's for the proc symbol.  Here's why:
    43        -Looking for the path to the OpenGL Library seems not to work in the iPhone Simulator.
    44        -We don't know that the path won't change in the future.
    45     */
    46     return dlsym(RTLD_DEFAULT, proc);
    47 }
    48 
    49 /*
    50     note that SDL_GL_Delete context makes it current without passing the window
    51 */
    52 int UIKit_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
    53 {
    54     if (context) {
    55         SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
    56         [data->view setCurrentContext];
    57     }
    58     else {
    59         [EAGLContext setCurrentContext: nil];
    60     }
    61 
    62     return 0;
    63 }
    64 
    65 int
    66 UIKit_GL_LoadLibrary(_THIS, const char *path)
    67 {
    68     /*
    69         shouldn't be passing a path into this function
    70         why?  Because we've already loaded the library
    71         and because the SDK forbids loading an external SO
    72     */
    73     if (path != NULL) {
    74         SDL_SetError("iPhone GL Load Library just here for compatibility");
    75         return -1;
    76     }
    77     return 0;
    78 }
    79 
    80 void UIKit_GL_SwapWindow(_THIS, SDL_Window * window)
    81 {
    82 #ifdef SDL_POWER_UIKIT
    83     // Check once a frame to see if we should turn off the battery monitor.
    84     SDL_UIKit_UpdateBatteryMonitoring();
    85 #endif
    86 
    87     SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
    88 
    89     if (nil == data->view) {
    90         return;
    91     }
    92     [data->view swapBuffers];
    93     /* since now we've got something to draw
    94        make the window visible */
    95     [data->uiwindow makeKeyAndVisible];
    96 
    97     /* we need to let the event cycle run, or the OS won't update the OpenGL view! */
    98     SDL_PumpEvents();
    99 
   100 }
   101 
   102 SDL_GLContext UIKit_GL_CreateContext(_THIS, SDL_Window * window)
   103 {
   104     SDL_uikitopenglview *view;
   105     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   106     UIWindow *uiwindow = data->uiwindow;
   107 
   108     /* construct our view, passing in SDL's OpenGL configuration data */
   109     view = [[SDL_uikitopenglview alloc] initWithFrame: [uiwindow bounds]
   110                                     retainBacking: _this->gl_config.retained_backing
   111                                     rBits: _this->gl_config.red_size
   112                                     gBits: _this->gl_config.green_size
   113                                     bBits: _this->gl_config.blue_size
   114                                     aBits: _this->gl_config.alpha_size
   115                                     depthBits: _this->gl_config.depth_size
   116                                     stencilBits: _this->gl_config.stencil_size
   117                                     majorVersion: _this->gl_config.major_version];
   118 
   119     data->view = view;
   120     view->viewcontroller = data->viewcontroller;
   121     if (view->viewcontroller != nil) {
   122         [view->viewcontroller setView:view];
   123         [view->viewcontroller retain];
   124     }
   125 
   126     /* add the view to our window */
   127     [uiwindow addSubview: view ];
   128 
   129     if ( UIKit_GL_MakeCurrent(_this, window, view) < 0 ) {
   130         UIKit_GL_DeleteContext(_this, view);
   131         return NULL;
   132     }
   133 
   134     /* Make this window the current mouse focus for touch input */
   135     /* !!! FIXME: only do this if this is the primary screen. */
   136     SDL_SetMouseFocus(window);
   137     SDL_SetKeyboardFocus(window);
   138 
   139     return view;
   140 }
   141 
   142 void UIKit_GL_DeleteContext(_THIS, SDL_GLContext context)
   143 {
   144     /* the delegate has retained the view, this will release him */
   145     SDL_uikitopenglview *view = (SDL_uikitopenglview *)context;
   146     if (view->viewcontroller) {
   147         [view->viewcontroller setView:nil];
   148         [view->viewcontroller release];
   149     }
   150     [view removeFromSuperview];
   151     [view release];
   152 }
   153 
   154 #endif /* SDL_VIDEO_DRIVER_UIKIT */
   155 
   156 /* vi: set ts=4 sw=4 expandtab: */