2
0
Эх сурвалжийг харах

Adds basic support for Portrait mode to iOS

NOTE: At this time should should only select Portrait orientations OR landscape orientations in your info.plist file
You can select both portrait orientations or both landscape or one portrait or one landscape, but NOT one portrait and one landscape
The app will run but the graphics will become squished.
Brandon Slack 13 жил өмнө
parent
commit
7dda125e33

+ 51 - 9
gameplay/src/PlatformiOS.mm

@@ -15,6 +15,13 @@
 #import <OpenGLES/ES2/glext.h>
 #import <mach/mach_time.h>
 
+#define UIInterfaceOrientationEnum(x) ([x isEqualToString:@"UIInterfaceOrientationPortrait"]?UIInterfaceOrientationPortrait:                        \
+                                      ([x isEqualToString:@"UIInterfaceOrientationPortraitUpsideDown"]?UIInterfaceOrientationPortraitUpsideDown:    \
+                                      ([x isEqualToString:@"UIInterfaceOrientationLandscapeLeft"]?UIInterfaceOrientationLandscapeLeft:              \
+                                        UIInterfaceOrientationLandscapeRight)))
+#define DeviceOrientedSize(o)         ((o == UIInterfaceOrientationPortrait || o == UIInterfaceOrientationPortraitUpsideDown)?                      \
+                                            CGSizeMake([[UIScreen mainScreen] bounds].size.width * [[UIScreen mainScreen] scale], [[UIScreen mainScreen] bounds].size.height * [[UIScreen mainScreen] scale]):  \
+                                            CGSizeMake([[UIScreen mainScreen] bounds].size.height * [[UIScreen mainScreen] scale], [[UIScreen mainScreen] bounds].size.width * [[UIScreen mainScreen] scale]))
 
 using namespace std;
 using namespace gameplay;
@@ -58,6 +65,7 @@ int getKey(unichar keyCode);
 @property (readonly, nonatomic, getter=isUpdating) BOOL updating;
 @property (readonly, nonatomic, getter=getContext) EAGLContext* context;
 
+- (void)startGame;
 - (void)startUpdating;
 - (void)stopUpdating;
 - (void)update:(id)sender;
@@ -144,13 +152,11 @@ int getKey(unichar keyCode);
         NSString* bundlePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/"];
         FileSystem::setResourcePath([bundlePath fileSystemRepresentation]); 
         
-        _game = Game::getInstance();
-        __timeStart = getMachTimeInMilliseconds();
-        _game->run();
     }
     return self;
 }
 
+
 - (void) dealloc
 {
     _game->exit();
@@ -253,6 +259,13 @@ int getKey(unichar keyCode);
     }
 }
 
+- (void)startGame 
+{
+    _game = Game::getInstance();
+    __timeStart = getMachTimeInMilliseconds();
+    _game->run();  
+}
+
 - (void)startUpdating
 {
     if (!updating)
@@ -408,12 +421,36 @@ int getKey(unichar keyCode);
     {
         __view = (View*)self.view;
     }
+    // Start the game after assigning __view so Platform object knows about it on game start
+    [(View*)self.view startGame];
 }
 
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
-    // Return YES for supported orientation, currently support landscape only?
-    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
+    // Fetch the supported orientations array
+    NSArray *supportedOrientations = NULL;
+    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) 
+    {
+        supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations~ipad"];    
+    }
+    else if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
+    { 
+        supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations~iphone"];
+    }
+    
+    if(supportedOrientations == NULL) 
+    {
+       supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations"]; 
+    }
+
+    // If no supported orientations default to v1.0 handling (landscape only)
+    if(supportedOrientations == nil) {
+        return UIInterfaceOrientationIsLandscape(interfaceOrientation);
+    }
+    for(NSString *s in supportedOrientations) {
+        if(interfaceOrientation == UIInterfaceOrientationEnum(s)) return YES;
+    }    
+    return NO;
 }
 
 - (void)startUpdating 
@@ -435,17 +472,20 @@ int getKey(unichar keyCode);
     ViewController* viewController;
     CMMotionManager *motionManager;
 }
+@property (nonatomic, retain) ViewController *viewController;
 @end
 
 
 @implementation AppDelegate
 
+@synthesize viewController;
+
 - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
 {
     __appDelegate = self;
     [UIApplication sharedApplication].statusBarHidden = YES;
+    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
 
-    
     motionManager = [[CMMotionManager alloc] init];
     if([motionManager isAccelerometerAvailable] == YES) 
     {
@@ -519,7 +559,7 @@ int getKey(unichar keyCode);
 }
 
 - (void)applicationDidBecomeActive:(UIApplication*)application 
-{
+{    
     [viewController startUpdating];
 }
 
@@ -818,12 +858,14 @@ void Platform::signalShutdown()
     
 unsigned int Platform::getDisplayWidth()
 {
-    return WINDOW_WIDTH;
+    CGSize size = DeviceOrientedSize([__appDelegate.viewController interfaceOrientation]);
+    return size.width;
 }
 
 unsigned int Platform::getDisplayHeight()
 {
-    return WINDOW_HEIGHT;
+    CGSize size = DeviceOrientedSize([__appDelegate.viewController interfaceOrientation]);
+    return size.height;
 }
 
 long Platform::getAbsoluteTime()

+ 4 - 1
gameplay/src/gameplay-main-ios.mm

@@ -1,5 +1,6 @@
 #ifdef __APPLE__
 
+#import <Foundation/Foundation.h>
 #include "gameplay.h"
 
 using namespace gameplay;
@@ -8,12 +9,14 @@ using namespace gameplay;
  * Main entry point.
  */
 int main(int argc, char** argv)
-{    
+{   
+    NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
     Game* game = Game::getInstance();
     Platform* platform = Platform::create(game);
     GP_ASSERT(platform);
     int result = platform->enterMessagePump();
 	delete platform;
+    [p release];
     return result;
 }