src/video/uikit/SDL_uikitopengles.m
author Sam Lantinga <slouken@libsdl.org>
Tue, 26 May 2015 06:27:46 -0700
changeset 9619 b94b6d0bff0f
parent 9540 32ddc92d78cf
child 9727 b845f659e015
permissions -rw-r--r--
Updated the copyright year to 2015
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2015 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_internal.h"
    22 
    23 #if SDL_VIDEO_DRIVER_UIKIT
    24 
    25 #include "SDL_uikitopengles.h"
    26 #import "SDL_uikitopenglview.h"
    27 #include "SDL_uikitmodes.h"
    28 #include "SDL_uikitwindow.h"
    29 #include "SDL_uikitevents.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 iOS Simulator.
    44      * -We don't know that the path won't change in the future. */
    45     return dlsym(RTLD_DEFAULT, proc);
    46 }
    47 
    48 /*
    49   note that SDL_GL_DeleteContext makes it current without passing the window
    50 */
    51 int
    52 UIKit_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
    53 {
    54     @autoreleasepool {
    55         SDLEAGLContext *eaglcontext = (__bridge SDLEAGLContext *) context;
    56 
    57         if (![EAGLContext setCurrentContext:eaglcontext]) {
    58             return SDL_SetError("Could not make EAGL context current");
    59         }
    60 
    61         if (eaglcontext) {
    62             [eaglcontext.sdlView setSDLWindow:window];
    63         }
    64     }
    65 
    66     return 0;
    67 }
    68 
    69 void
    70 UIKit_GL_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h)
    71 {
    72     @autoreleasepool {
    73         SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
    74         UIView *view = data.viewcontroller.view;
    75         if ([view isKindOfClass:[SDL_uikitopenglview class]]) {
    76             SDL_uikitopenglview *glview = (SDL_uikitopenglview *) view;
    77             if (w) {
    78                 *w = glview.backingWidth;
    79             }
    80             if (h) {
    81                 *h = glview.backingHeight;
    82             }
    83         }
    84     }
    85 }
    86 
    87 int
    88 UIKit_GL_LoadLibrary(_THIS, const char *path)
    89 {
    90     /* We shouldn't pass a path to this function, since we've already loaded the
    91      * library. */
    92     if (path != NULL) {
    93         return SDL_SetError("iOS GL Load Library just here for compatibility");
    94     }
    95     return 0;
    96 }
    97 
    98 void UIKit_GL_SwapWindow(_THIS, SDL_Window * window)
    99 {
   100     @autoreleasepool {
   101         SDLEAGLContext *context = (__bridge SDLEAGLContext *) SDL_GL_GetCurrentContext();
   102 
   103 #if SDL_POWER_UIKIT
   104         /* Check once a frame to see if we should turn off the battery monitor. */
   105         SDL_UIKit_UpdateBatteryMonitoring();
   106 #endif
   107 
   108         [context.sdlView swapBuffers];
   109 
   110         /* You need to pump events in order for the OS to make changes visible.
   111          * We don't pump events here because we don't want iOS application events
   112          * (low memory, terminate, etc.) to happen inside low level rendering. */
   113     }
   114 }
   115 
   116 SDL_GLContext
   117 UIKit_GL_CreateContext(_THIS, SDL_Window * window)
   118 {
   119     @autoreleasepool {
   120         SDL_uikitopenglview *view;
   121         SDL_WindowData *data = (__bridge SDL_WindowData *) window->driverdata;
   122         CGRect frame = UIKit_ComputeViewFrame(window, data.uiwindow.screen);
   123         EAGLSharegroup *sharegroup = nil;
   124         CGFloat scale = 1.0;
   125 
   126         if (_this->gl_config.share_with_current_context) {
   127             EAGLContext *context = (__bridge EAGLContext *) SDL_GL_GetCurrentContext();
   128             sharegroup = context.sharegroup;
   129         }
   130 
   131         if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
   132             /* Set the scale to the natural scale factor of the screen - the
   133              * backing dimensions of the OpenGL view will match the pixel
   134              * dimensions of the screen rather than the dimensions in points. */
   135 #ifdef __IPHONE_8_0
   136             if ([data.uiwindow.screen respondsToSelector:@selector(nativeScale)]) {
   137                 scale = data.uiwindow.screen.nativeScale;
   138             } else
   139 #endif
   140             {
   141                 scale = data.uiwindow.screen.scale;
   142             }
   143         }
   144 
   145         /* construct our view, passing in SDL's OpenGL configuration data */
   146         view = [[SDL_uikitopenglview alloc] initWithFrame:frame
   147                                                     scale:scale
   148                                             retainBacking:_this->gl_config.retained_backing
   149                                                     rBits:_this->gl_config.red_size
   150                                                     gBits:_this->gl_config.green_size
   151                                                     bBits:_this->gl_config.blue_size
   152                                                     aBits:_this->gl_config.alpha_size
   153                                                 depthBits:_this->gl_config.depth_size
   154                                               stencilBits:_this->gl_config.stencil_size
   155                                                      sRGB:_this->gl_config.framebuffer_srgb_capable
   156                                              majorVersion:_this->gl_config.major_version
   157                                                shareGroup:sharegroup];
   158         if (!view) {
   159             return NULL;
   160         }
   161 
   162         SDLEAGLContext *context = view.context;
   163         if (UIKit_GL_MakeCurrent(_this, window, (__bridge SDL_GLContext) context) < 0) {
   164             UIKit_GL_DeleteContext(_this, (SDL_GLContext) CFBridgingRetain(context));
   165             return NULL;
   166         }
   167 
   168         /* We return a +1'd context. The window's driverdata owns the view (via
   169          * MakeCurrent.) */
   170         return (SDL_GLContext) CFBridgingRetain(context);
   171     }
   172 }
   173 
   174 void
   175 UIKit_GL_DeleteContext(_THIS, SDL_GLContext context)
   176 {
   177     @autoreleasepool {
   178         /* Transfer ownership the +1'd context to ARC. */
   179         SDLEAGLContext *eaglcontext = (SDLEAGLContext *) CFBridgingRelease(context);
   180 
   181         /* Detach the context's view from its window. */
   182         [eaglcontext.sdlView setSDLWindow:NULL];
   183     }
   184 }
   185 
   186 #endif /* SDL_VIDEO_DRIVER_UIKIT */
   187 
   188 /* vi: set ts=4 sw=4 expandtab: */