src/video/uikit/SDL_uikitvideo.m
author Sam Lantinga
Wed, 02 Feb 2011 14:34:54 -0800
changeset 5154 fb424691cfc7
parent 5131 7b7da52e8775
child 5262 b530ef003506
permissions -rw-r--r--
Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
     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_assert.h"
    39 
    40 #define UIKITVID_DRIVER_NAME "uikit"
    41 
    42 /* Initialization/Query functions */
    43 static int UIKit_VideoInit(_THIS);
    44 static void UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * sdl_display);
    45 static int UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display,
    46                                 SDL_DisplayMode * mode);
    47 static void UIKit_VideoQuit(_THIS);
    48 
    49 BOOL SDL_UIKit_supports_multiple_displays = NO;
    50 
    51 /* DUMMY driver bootstrap functions */
    52 
    53 static int
    54 UIKit_Available(void)
    55 {
    56     return (1);
    57 }
    58 
    59 static void UIKit_DeleteDevice(SDL_VideoDevice * device)
    60 {
    61     SDL_free(device);
    62 }
    63 
    64 static SDL_VideoDevice *
    65 UIKit_CreateDevice(int devindex)
    66 {
    67     SDL_VideoDevice *device;
    68 
    69     /* Initialize all variables that we clean on shutdown */
    70     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    71     if (!device) {
    72         SDL_OutOfMemory();
    73         if (device) {
    74             SDL_free(device);
    75         }
    76         return (0);
    77     }
    78 
    79     /* Set the function pointers */
    80     device->VideoInit = UIKit_VideoInit;
    81     device->VideoQuit = UIKit_VideoQuit;
    82     device->GetDisplayModes = UIKit_GetDisplayModes;
    83     device->SetDisplayMode = UIKit_SetDisplayMode;
    84     device->PumpEvents = UIKit_PumpEvents;
    85     device->CreateWindow = UIKit_CreateWindow;
    86     device->DestroyWindow = UIKit_DestroyWindow;
    87     device->GetWindowWMInfo = UIKit_GetWindowWMInfo;
    88     
    89     
    90     /* OpenGL (ES) functions */
    91     device->GL_MakeCurrent        = UIKit_GL_MakeCurrent;
    92     device->GL_SwapWindow        = UIKit_GL_SwapWindow;
    93     device->GL_CreateContext    = UIKit_GL_CreateContext;
    94     device->GL_DeleteContext    = UIKit_GL_DeleteContext;
    95     device->GL_GetProcAddress   = UIKit_GL_GetProcAddress;
    96     device->GL_LoadLibrary        = UIKit_GL_LoadLibrary;
    97     device->free = UIKit_DeleteDevice;
    98 
    99     device->gl_config.accelerated = 1;
   100 
   101     return device;
   102 }
   103 
   104 VideoBootStrap UIKIT_bootstrap = {
   105     UIKITVID_DRIVER_NAME, "SDL UIKit video driver",
   106     UIKit_Available, UIKit_CreateDevice
   107 };
   108 
   109 
   110 /*
   111 !!! FIXME:
   112 
   113 The main screen should list a AxB mode for portrait orientation, and then
   114  also list BxA for landscape mode. When setting a given resolution, we should
   115  rotate the view's transform appropriately (extra credit if you check the
   116  accelerometer and rotate the display so it's never upside down).
   117 
   118   http://iphonedevelopment.blogspot.com/2008/10/starting-in-landscape-mode-without.html
   119 
   120 */
   121 
   122 static void
   123 UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
   124 {
   125     UIScreen *uiscreen = (UIScreen *) display->driverdata;
   126     SDL_DisplayMode mode;
   127     SDL_zero(mode);
   128 
   129     // availableModes showed up in 3.2 (the iPad and later). We should only
   130     //  land here for at least that version of the OS.
   131     if (!SDL_UIKit_supports_multiple_displays) {
   132         const CGRect rect = [uiscreen bounds];
   133         mode.format = SDL_PIXELFORMAT_ABGR8888;
   134         mode.w = (int) rect.size.width;
   135         mode.h = (int) rect.size.height;
   136         mode.refresh_rate = 0;
   137         mode.driverdata = NULL;
   138         SDL_AddDisplayMode(display, &mode);
   139         return;
   140     }
   141 
   142     const NSArray *modes = [uiscreen availableModes];
   143     const NSUInteger mode_count = [modes count];
   144     NSUInteger i;
   145     for (i = 0; i < mode_count; i++) {
   146         UIScreenMode *uimode = (UIScreenMode *) [modes objectAtIndex:i];
   147         const CGSize size = [uimode size];
   148         mode.format = SDL_PIXELFORMAT_ABGR8888;
   149         mode.w = (int) size.width;
   150         mode.h = (int) size.height;
   151         mode.refresh_rate = 0;
   152         mode.driverdata = uimode;
   153         [uimode retain];
   154         SDL_AddDisplayMode(display, &mode);
   155     }
   156 }
   157 
   158 
   159 static void
   160 UIKit_AddDisplay(UIScreen *uiscreen, int w, int h)
   161 {
   162     SDL_VideoDisplay display;
   163     SDL_DisplayMode mode;
   164     SDL_zero(mode);
   165     mode.format = SDL_PIXELFORMAT_ABGR8888;
   166     mode.w = w;
   167     mode.h = h;
   168     mode.refresh_rate = 0;
   169 
   170     SDL_zero(display);
   171     display.desktop_mode = mode;
   172     display.current_mode = mode;
   173 
   174     [uiscreen retain];
   175     display.driverdata = uiscreen;
   176     SDL_AddVideoDisplay(&display);
   177 }
   178 
   179 
   180 int
   181 UIKit_VideoInit(_THIS)
   182 {
   183     _this->gl_config.driver_loaded = 1;
   184 
   185     NSString *reqSysVer = @"3.2";
   186     NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
   187     if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
   188         SDL_UIKit_supports_multiple_displays = YES;
   189 
   190     // If this is iPhoneOS < 3.2, all devices are one screen, 320x480 pixels.
   191     //  The iPad added both a larger main screen and the ability to use
   192     //  external displays.
   193     if (!SDL_UIKit_supports_multiple_displays) {
   194         // Just give 'em the whole main screen.
   195         UIScreen *uiscreen = [UIScreen mainScreen];
   196         const CGRect rect = [uiscreen bounds];
   197         UIKit_AddDisplay(uiscreen, (int)rect.size.width, (int)rect.size.height);
   198     } else {
   199         const NSArray *screens = [UIScreen screens];
   200         const NSUInteger screen_count = [screens count];
   201         NSUInteger i;
   202         for (i = 0; i < screen_count; i++) {
   203             // the main screen is the first element in the array.
   204             UIScreen *uiscreen = (UIScreen *) [screens objectAtIndex:i];
   205             const CGSize size = [[uiscreen currentMode] size];
   206             UIKit_AddDisplay(uiscreen, (int) size.width, (int) size.height);
   207         }
   208     }
   209 
   210     /* We're done! */
   211     return 0;
   212 }
   213 
   214 static int
   215 UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
   216 {
   217     UIScreen *uiscreen = (UIScreen *) display->driverdata;
   218     if (!SDL_UIKit_supports_multiple_displays) {
   219         // Not on at least iPhoneOS 3.2 (versions prior to iPad).
   220         SDL_assert(mode->driverdata == NULL);
   221     } else {
   222         UIScreenMode *uimode = (UIScreenMode *) mode->driverdata;
   223         [uiscreen setCurrentMode:uimode];
   224     }
   225 
   226     return 0;
   227 }
   228 
   229 void
   230 UIKit_VideoQuit(_THIS)
   231 {
   232     // Release Objective-C objects, so higher level doesn't free() them.
   233     int i, j;
   234     for (i = 0; i < _this->num_displays; i++) {
   235         SDL_VideoDisplay *display = &_this->displays[i];
   236         UIScreen *uiscreen = (UIScreen *) display->driverdata;
   237         [uiscreen release];
   238         display->driverdata = NULL;
   239         for (j = 0; j < display->num_display_modes; j++) {
   240             SDL_DisplayMode *mode = &display->display_modes[j];
   241             UIScreenMode *uimode = (UIScreenMode *) mode->driverdata;
   242             if (uimode) {
   243                 [uimode release];
   244                 mode->driverdata = NULL;
   245             }
   246         }
   247     }
   248 }
   249 
   250 /* vi: set ts=4 sw=4 expandtab: */