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.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2014 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 #include "SDL_uikitopenglview.h"
    27 #include "SDL_uikitappdelegate.h"
    28 #include "SDL_uikitmodes.h"
    29 #include "SDL_uikitwindow.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
    53 UIKit_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
    54 {
    55     if (context) {
    56         SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
    57         [data->view setCurrentContext];
    58     }
    59     else {
    60         [EAGLContext setCurrentContext: nil];
    61     }
    62 
    63     return 0;
    64 }
    65 
    66 void UIKit_GL_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h)
    67 {
    68     SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
    69 
    70     if (w) {
    71         *w = data->view.backingWidth;
    72     }
    73     if (h) {
    74         *h = data->view.backingHeight;
    75     }
    76 }
    77 
    78 
    79 int
    80 UIKit_GL_LoadLibrary(_THIS, const char *path)
    81 {
    82     /*
    83         shouldn't be passing a path into this function
    84         why?  Because we've already loaded the library
    85         and because the SDK forbids loading an external SO
    86     */
    87     if (path != NULL) {
    88         return SDL_SetError("iPhone GL Load Library just here for compatibility");
    89     }
    90     return 0;
    91 }
    92 
    93 void UIKit_GL_SwapWindow(_THIS, SDL_Window * window)
    94 {
    95 #if SDL_POWER_UIKIT
    96     /* Check once a frame to see if we should turn off the battery monitor. */
    97     SDL_UIKit_UpdateBatteryMonitoring();
    98 #endif
    99 
   100     SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
   101 
   102     if (nil == data->view) {
   103         return;
   104     }
   105     [data->view swapBuffers];
   106 
   107     /* You need to pump events in order for the OS to make changes visible.
   108        We don't pump events here because we don't want iOS application events
   109        (low memory, terminate, etc.) to happen inside low level rendering.
   110      */
   111 }
   112 
   113 SDL_GLContext
   114 UIKit_GL_CreateContext(_THIS, SDL_Window * window)
   115 {
   116     SDL_uikitopenglview *view;
   117     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   118     UIWindow *uiwindow = data->uiwindow;
   119     CGRect frame = UIKit_ComputeViewFrame(window, uiwindow.screen);
   120     EAGLSharegroup *share_group = nil;
   121     CGFloat scale = 1.0;
   122 
   123     if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
   124         /* Set the scale to the natural scale factor of the screen - the backing
   125            dimensions of the OpenGL view will match the pixel dimensions of the
   126            screen rather than the dimensions in points.
   127          */
   128         scale = uiwindow.screen.scale;
   129     }
   130 
   131     if (_this->gl_config.share_with_current_context) {
   132         SDL_uikitopenglview *view = (SDL_uikitopenglview *) SDL_GL_GetCurrentContext();
   133         share_group = [view.context sharegroup];
   134     }
   135 
   136     /* construct our view, passing in SDL's OpenGL configuration data */
   137     view = [[SDL_uikitopenglview alloc] initWithFrame: frame
   138                                                 scale: scale
   139                                         retainBacking: _this->gl_config.retained_backing
   140                                                 rBits: _this->gl_config.red_size
   141                                                 gBits: _this->gl_config.green_size
   142                                                 bBits: _this->gl_config.blue_size
   143                                                 aBits: _this->gl_config.alpha_size
   144                                             depthBits: _this->gl_config.depth_size
   145                                           stencilBits: _this->gl_config.stencil_size
   146                                                  sRGB: _this->gl_config.framebuffer_srgb_capable
   147                                          majorVersion: _this->gl_config.major_version
   148                                            shareGroup: share_group];
   149     if (!view) {
   150         return NULL;
   151     }
   152 
   153     data->view = view;
   154     view->viewcontroller = data->viewcontroller;
   155     if (view->viewcontroller != nil) {
   156         [view->viewcontroller setView:view];
   157         [view->viewcontroller retain];
   158     }
   159     [uiwindow addSubview: view];
   160 
   161     /* The view controller needs to be the root in order to control rotation on iOS 6.0 */
   162     if (uiwindow.rootViewController == nil) {
   163         uiwindow.rootViewController = view->viewcontroller;
   164     }
   165 
   166     if (UIKit_GL_MakeCurrent(_this, window, view) < 0) {
   167         UIKit_GL_DeleteContext(_this, view);
   168         return NULL;
   169     }
   170 
   171     /* Make this window the current mouse focus for touch input */
   172     if (uiwindow.screen == [UIScreen mainScreen]) {
   173         SDL_SetMouseFocus(window);
   174         SDL_SetKeyboardFocus(window);
   175     }
   176 
   177     return view;
   178 }
   179 
   180 void
   181 UIKit_GL_DeleteContext(_THIS, SDL_GLContext context)
   182 {
   183     /* the delegate has retained the view, this will release him */
   184     SDL_uikitopenglview *view = (SDL_uikitopenglview *)context;
   185     if (view->viewcontroller) {
   186         UIWindow *uiwindow = (UIWindow *)view.superview;
   187         if (uiwindow.rootViewController == view->viewcontroller) {
   188             uiwindow.rootViewController = nil;
   189         }
   190         [view->viewcontroller setView:nil];
   191         [view->viewcontroller release];
   192     }
   193     [view removeFromSuperview];
   194     [view release];
   195 }
   196 
   197 #endif /* SDL_VIDEO_DRIVER_UIKIT */
   198 
   199 /* vi: set ts=4 sw=4 expandtab: */