src/video/uikit/SDL_uikitvideo.m
author Sam Lantinga
Thu, 20 Jan 2011 16:05:59 -0800
changeset 5056 8b7988f42fcb
parent 4446 8b03a20b320f
child 5131 7b7da52e8775
permissions -rw-r--r--
Added the ability to get the UIKit window through the SDL API.
You can also do this through the native API:
UIWindow *window = [[UIApplication sharedApplication] keyWindow];

Also needed to name the union for events and window info.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #import <UIKit/UIKit.h>
    24 
    25 #include "SDL_config.h"
    26 
    27 #include "SDL_video.h"
    28 #include "SDL_mouse.h"
    29 #include "../SDL_sysvideo.h"
    30 #include "../SDL_pixels_c.h"
    31 #include "../../events/SDL_events_c.h"
    32 
    33 #include "SDL_uikitvideo.h"
    34 #include "SDL_uikitevents.h"
    35 #include "SDL_uikitwindow.h"
    36 #include "SDL_uikitopengles.h"
    37 
    38 #include "SDL_renderer_sw.h"
    39 #include "SDL_renderer_gles.h"
    40 
    41 #include "SDL_assert.h"
    42 
    43 #define UIKITVID_DRIVER_NAME "uikit"
    44 
    45 /* Initialization/Query functions */
    46 static int UIKit_VideoInit(_THIS);
    47 static void UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * sdl_display);
    48 static int UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display,
    49                                 SDL_DisplayMode * mode);
    50 static void UIKit_VideoQuit(_THIS);
    51 
    52 BOOL SDL_UIKit_supports_multiple_displays = NO;
    53 
    54 /* DUMMY driver bootstrap functions */
    55 
    56 static int
    57 UIKit_Available(void)
    58 {
    59 	return (1);
    60 }
    61 
    62 static void UIKit_DeleteDevice(SDL_VideoDevice * device)
    63 {
    64     SDL_free(device);
    65 }
    66 
    67 static SDL_VideoDevice *
    68 UIKit_CreateDevice(int devindex)
    69 {
    70     SDL_VideoDevice *device;
    71 
    72     /* Initialize all variables that we clean on shutdown */
    73     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    74     if (!device) {
    75         SDL_OutOfMemory();
    76         if (device) {
    77             SDL_free(device);
    78         }
    79         return (0);
    80     }
    81 
    82     /* Set the function pointers */
    83     device->VideoInit = UIKit_VideoInit;
    84     device->VideoQuit = UIKit_VideoQuit;
    85     device->GetDisplayModes = UIKit_GetDisplayModes;
    86     device->SetDisplayMode = UIKit_SetDisplayMode;
    87     device->PumpEvents = UIKit_PumpEvents;
    88 	device->CreateWindow = UIKit_CreateWindow;
    89 	device->DestroyWindow = UIKit_DestroyWindow;
    90     device->GetWindowWMInfo = UIKit_GetWindowWMInfo;
    91 	
    92 	
    93 	/* OpenGL (ES) functions */
    94 	device->GL_MakeCurrent		= UIKit_GL_MakeCurrent;
    95 	device->GL_SwapWindow		= UIKit_GL_SwapWindow;
    96 	device->GL_CreateContext	= UIKit_GL_CreateContext;
    97 	device->GL_DeleteContext    = UIKit_GL_DeleteContext;
    98 	device->GL_GetProcAddress   = UIKit_GL_GetProcAddress;
    99 	device->GL_LoadLibrary	    = UIKit_GL_LoadLibrary;
   100 	device->free = UIKit_DeleteDevice;
   101 
   102 	device->gl_config.accelerated = 1;
   103 	
   104     return device;
   105 }
   106 
   107 VideoBootStrap UIKIT_bootstrap = {
   108     UIKITVID_DRIVER_NAME, "SDL UIKit video driver",
   109     UIKit_Available, UIKit_CreateDevice
   110 };
   111 
   112 
   113 /*
   114 !!! FIXME:
   115 
   116 The main screen should list a AxB mode for portrait orientation, and then
   117  also list BxA for landscape mode. When setting a given resolution, we should
   118  rotate the view's transform appropriately (extra credit if you check the
   119  accelerometer and rotate the display so it's never upside down).
   120 
   121   http://iphonedevelopment.blogspot.com/2008/10/starting-in-landscape-mode-without.html
   122 
   123 */
   124 
   125 static void
   126 UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
   127 {
   128     UIScreen *uiscreen = (UIScreen *) display->driverdata;
   129     SDL_DisplayMode mode;
   130     SDL_zero(mode);
   131 
   132     // availableModes showed up in 3.2 (the iPad and later). We should only
   133     //  land here for at least that version of the OS.
   134     if (!SDL_UIKit_supports_multiple_displays) {
   135         const CGRect rect = [uiscreen bounds];
   136         mode.format = SDL_PIXELFORMAT_ABGR8888;
   137         mode.w = (int) rect.size.width;
   138         mode.h = (int) rect.size.height;
   139         mode.refresh_rate = 0;
   140         mode.driverdata = NULL;
   141         SDL_AddDisplayMode(display, &mode);
   142         return;
   143     }
   144 
   145     const NSArray *modes = [uiscreen availableModes];
   146     const NSUInteger mode_count = [modes count];
   147     NSUInteger i;
   148     for (i = 0; i < mode_count; i++) {
   149         UIScreenMode *uimode = (UIScreenMode *) [modes objectAtIndex:i];
   150         const CGSize size = [uimode size];
   151         mode.format = SDL_PIXELFORMAT_ABGR8888;
   152         mode.w = (int) size.width;
   153         mode.h = (int) size.height;
   154         mode.refresh_rate = 0;
   155         mode.driverdata = uimode;
   156         [uimode retain];
   157         SDL_AddDisplayMode(display, &mode);
   158     }
   159 }
   160 
   161 
   162 static void
   163 UIKit_AddDisplay(UIScreen *uiscreen, int w, int h)
   164 {
   165     SDL_VideoDisplay display;
   166     SDL_DisplayMode mode;
   167     SDL_zero(mode);
   168     mode.format = SDL_PIXELFORMAT_ABGR8888;
   169     mode.w = w;
   170     mode.h = h;
   171     mode.refresh_rate = 0;
   172 
   173     SDL_zero(display);
   174     display.desktop_mode = mode;
   175     display.current_mode = mode;
   176 
   177     [uiscreen retain];
   178     display.driverdata = uiscreen;
   179     SDL_AddVideoDisplay(&display);
   180 }
   181 
   182 
   183 int
   184 UIKit_VideoInit(_THIS)
   185 {
   186     _this->gl_config.driver_loaded = 1;
   187 
   188     NSString *reqSysVer = @"3.2";
   189     NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
   190     if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
   191         SDL_UIKit_supports_multiple_displays = YES;
   192 
   193     // If this is iPhoneOS < 3.2, all devices are one screen, 320x480 pixels.
   194     //  The iPad added both a larger main screen and the ability to use
   195     //  external displays.
   196     if (!SDL_UIKit_supports_multiple_displays) {
   197         // Just give 'em the whole main screen.
   198         UIScreen *uiscreen = [UIScreen mainScreen];
   199         const CGRect rect = [uiscreen bounds];
   200         UIKit_AddDisplay(uiscreen, (int)rect.size.width, (int)rect.size.height);
   201     } else {
   202         const NSArray *screens = [UIScreen screens];
   203         const NSUInteger screen_count = [screens count];
   204         NSUInteger i;
   205         for (i = 0; i < screen_count; i++) {
   206             // the main screen is the first element in the array.
   207             UIScreen *uiscreen = (UIScreen *) [screens objectAtIndex:i];
   208             const CGSize size = [[uiscreen currentMode] size];
   209             UIKit_AddDisplay(uiscreen, (int) size.width, (int) size.height);
   210         }
   211     }
   212 
   213     /* We're done! */
   214     return 0;
   215 }
   216 
   217 static int
   218 UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
   219 {
   220     UIScreen *uiscreen = (UIScreen *) display->driverdata;
   221     if (!SDL_UIKit_supports_multiple_displays) {
   222         // Not on at least iPhoneOS 3.2 (versions prior to iPad).
   223         SDL_assert(mode->driverdata == NULL);
   224     } else {
   225         UIScreenMode *uimode = (UIScreenMode *) mode->driverdata;
   226         [uiscreen setCurrentMode:uimode];
   227     }
   228 
   229     return 0;
   230 }
   231 
   232 void
   233 UIKit_VideoQuit(_THIS)
   234 {
   235     // Release Objective-C objects, so higher level doesn't free() them.
   236     int i, j;
   237     for (i = 0; i < _this->num_displays; i++) {
   238         SDL_VideoDisplay *display = &_this->displays[i];
   239         UIScreen *uiscreen = (UIScreen *) display->driverdata;
   240         [uiscreen release];
   241         display->driverdata = NULL;
   242         for (j = 0; j < display->num_display_modes; j++) {
   243             SDL_DisplayMode *mode = &display->display_modes[j];
   244             UIScreenMode *uimode = (UIScreenMode *) mode->driverdata;
   245             if (uimode) {
   246                 [uimode release];
   247                 mode->driverdata = NULL;
   248             }
   249         }
   250     }
   251 }
   252 
   253 /* vi: set ts=4 sw=4 expandtab: */