src/video/uikit/SDL_uikitvideo.m
changeset 6009 fb5fa3561ca9
parent 6007 0e647f56751d
child 6013 b253cc3c4276
equal deleted inserted replaced
6008:73951a50e6db 6009:fb5fa3561ca9
   116 
   116 
   117   http://iphonedevelopment.blogspot.com/2008/10/starting-in-landscape-mode-without.html
   117   http://iphonedevelopment.blogspot.com/2008/10/starting-in-landscape-mode-without.html
   118 
   118 
   119 */
   119 */
   120 
   120 
   121 static CGSize
       
   122 UIKit_ForcePortrait(const CGSize size)
       
   123 {
       
   124     CGSize retval;
       
   125     if (size.width < size.height) { // portrait
       
   126         retval = size;
       
   127     } else {  // landscape
       
   128         retval.width = size.height;
       
   129         retval.height = size.width;
       
   130     }
       
   131     return retval;
       
   132 }
       
   133 
       
   134 static CGSize
       
   135 UIKit_ForceLandscape(const CGSize size)
       
   136 {
       
   137     CGSize retval;
       
   138     if (size.width > size.height) { // landscape
       
   139         retval = size;
       
   140     } else {  // portrait
       
   141         retval.width = size.height;
       
   142         retval.height = size.width;
       
   143     }
       
   144     return retval;
       
   145 }
       
   146 
       
   147 static void
   121 static void
   148 UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
   122 UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
   149 {
   123 {
   150     UIScreen *uiscreen = (UIScreen *) display->driverdata;
   124     UIScreen *uiscreen = (UIScreen *) display->driverdata;
   151     SDL_DisplayMode mode;
   125     SDL_DisplayMode mode;
   154     // availableModes showed up in 3.2 (the iPad and later). We should only
   128     // availableModes showed up in 3.2 (the iPad and later). We should only
   155     //  land here for at least that version of the OS.
   129     //  land here for at least that version of the OS.
   156     if (!SDL_UIKit_supports_multiple_displays) {
   130     if (!SDL_UIKit_supports_multiple_displays) {
   157         const CGRect rect = [uiscreen bounds];
   131         const CGRect rect = [uiscreen bounds];
   158         mode.format = SDL_PIXELFORMAT_ABGR8888;
   132         mode.format = SDL_PIXELFORMAT_ABGR8888;
       
   133         mode.refresh_rate = 0;
       
   134         mode.driverdata = NULL;
       
   135 
   159         mode.w = (int) rect.size.width;
   136         mode.w = (int) rect.size.width;
   160         mode.h = (int) rect.size.height;
   137         mode.h = (int) rect.size.height;
   161         mode.refresh_rate = 0;
       
   162         mode.driverdata = NULL;
       
   163         SDL_AddDisplayMode(display, &mode);
   138         SDL_AddDisplayMode(display, &mode);
       
   139 
   164         mode.w = (int) rect.size.height;  // swap the orientation, add again.
   140         mode.w = (int) rect.size.height;  // swap the orientation, add again.
   165         mode.h = (int) rect.size.width;
   141         mode.h = (int) rect.size.width;
   166         SDL_AddDisplayMode(display, &mode);
   142         SDL_AddDisplayMode(display, &mode);
   167         return;
   143         return;
   168     }
   144     }
   169 
   145 
   170     const BOOL ismain = (uiscreen == [UIScreen mainScreen]);
       
   171     const NSArray *modes = [uiscreen availableModes];
   146     const NSArray *modes = [uiscreen availableModes];
   172     for (UIScreenMode *uimode in [uiscreen availableModes]) {
   147     for (UIScreenMode *uimode in [uiscreen availableModes]) {
   173         CGSize size = [uimode size];
   148         CGSize size = [uimode size];
   174         mode.format = SDL_PIXELFORMAT_ABGR8888;
   149         mode.format = SDL_PIXELFORMAT_ABGR8888;
   175         mode.refresh_rate = 0;
   150         mode.refresh_rate = 0;
   176         mode.driverdata = uimode;
   151         mode.driverdata = uimode;
   177         mode.w = (int) size.width;
   152         mode.w = (int) size.width;
   178         mode.h = (int) size.height;
   153         mode.h = (int) size.height;
   179         if (SDL_AddDisplayMode(display, &mode))
   154         if (SDL_AddDisplayMode(display, &mode))
   180             [uimode retain];
   155             [uimode retain];        // retain is needed because of mode.driverdata
   181 
   156 
   182         if (ismain) {
   157         if (uiscreen == [UIScreen mainScreen]) {
   183             // Add the mode twice, flipped to portrait and landscape.
   158             // Add the mode with swapped width/height
   184             //  SDL_AddDisplayMode() will ignore duplicates.
   159             mode.w = (int) size.height;
   185             size = UIKit_ForcePortrait([uimode size]);
   160             mode.h = (int) size.width;
   186             mode.w = (int) size.width;
       
   187             mode.h = (int) size.height;
       
   188             if (SDL_AddDisplayMode(display, &mode))
       
   189                 [uimode retain];
       
   190 
       
   191             size = UIKit_ForceLandscape(size);
       
   192             mode.w = (int) size.width;
       
   193             mode.h = (int) size.height;
       
   194             if (SDL_AddDisplayMode(display, &mode))
   161             if (SDL_AddDisplayMode(display, &mode))
   195                 [uimode retain];
   162                 [uimode retain];
   196         }
   163         }
   197     }
   164     }
   198 }
   165 }
   237     //  external displays.
   204     //  external displays.
   238     if (!SDL_UIKit_supports_multiple_displays) {
   205     if (!SDL_UIKit_supports_multiple_displays) {
   239         // Just give 'em the whole main screen.
   206         // Just give 'em the whole main screen.
   240         UIScreen *uiscreen = [UIScreen mainScreen];
   207         UIScreen *uiscreen = [UIScreen mainScreen];
   241         UIScreenMode *uiscreenmode = [uiscreen currentMode];
   208         UIScreenMode *uiscreenmode = [uiscreen currentMode];
   242         const CGRect rect = [uiscreen bounds];
   209         const CGSize size = [uiscreen bounds].size;
   243         UIKit_AddDisplay(uiscreen, uiscreenmode, (int)rect.size.width, (int)rect.size.height);
   210         UIKit_AddDisplay(uiscreen, uiscreenmode, (int)size.width, (int)size.height);
   244     } else {
   211     } else {
   245         for (UIScreen *uiscreen in [UIScreen screens]) {
   212         for (UIScreen *uiscreen in [UIScreen screens]) {
   246             // the main screen is the first element in the array.
   213             // the main screen is the first element in the array.
   247             UIScreenMode *uiscreenmode = [uiscreen currentMode];
   214             UIScreenMode *uiscreenmode = [uiscreen currentMode];
   248             const CGSize size = [[uiscreen currentMode] size];
   215             const CGSize size = [[uiscreen currentMode] size];