瀏覽代碼

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 年之前
父節點
當前提交
7dda125e33
共有 2 個文件被更改,包括 55 次插入10 次删除
  1. 51 9
      gameplay/src/PlatformiOS.mm
  2. 4 1
      gameplay/src/gameplay-main-ios.mm

+ 51 - 9
gameplay/src/PlatformiOS.mm

@@ -15,6 +15,13 @@
 #import <OpenGLES/ES2/glext.h>
 #import <OpenGLES/ES2/glext.h>
 #import <mach/mach_time.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 std;
 using namespace gameplay;
 using namespace gameplay;
@@ -58,6 +65,7 @@ int getKey(unichar keyCode);
 @property (readonly, nonatomic, getter=isUpdating) BOOL updating;
 @property (readonly, nonatomic, getter=isUpdating) BOOL updating;
 @property (readonly, nonatomic, getter=getContext) EAGLContext* context;
 @property (readonly, nonatomic, getter=getContext) EAGLContext* context;
 
 
+- (void)startGame;
 - (void)startUpdating;
 - (void)startUpdating;
 - (void)stopUpdating;
 - (void)stopUpdating;
 - (void)update:(id)sender;
 - (void)update:(id)sender;
@@ -144,13 +152,11 @@ int getKey(unichar keyCode);
         NSString* bundlePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/"];
         NSString* bundlePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/"];
         FileSystem::setResourcePath([bundlePath fileSystemRepresentation]); 
         FileSystem::setResourcePath([bundlePath fileSystemRepresentation]); 
         
         
-        _game = Game::getInstance();
-        __timeStart = getMachTimeInMilliseconds();
-        _game->run();
     }
     }
     return self;
     return self;
 }
 }
 
 
+
 - (void) dealloc
 - (void) dealloc
 {
 {
     _game->exit();
     _game->exit();
@@ -253,6 +259,13 @@ int getKey(unichar keyCode);
     }
     }
 }
 }
 
 
+- (void)startGame 
+{
+    _game = Game::getInstance();
+    __timeStart = getMachTimeInMilliseconds();
+    _game->run();  
+}
+
 - (void)startUpdating
 - (void)startUpdating
 {
 {
     if (!updating)
     if (!updating)
@@ -408,12 +421,36 @@ int getKey(unichar keyCode);
     {
     {
         __view = (View*)self.view;
         __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
 - (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 
 - (void)startUpdating 
@@ -435,17 +472,20 @@ int getKey(unichar keyCode);
     ViewController* viewController;
     ViewController* viewController;
     CMMotionManager *motionManager;
     CMMotionManager *motionManager;
 }
 }
+@property (nonatomic, retain) ViewController *viewController;
 @end
 @end
 
 
 
 
 @implementation AppDelegate
 @implementation AppDelegate
 
 
+@synthesize viewController;
+
 - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
 - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
 {
 {
     __appDelegate = self;
     __appDelegate = self;
     [UIApplication sharedApplication].statusBarHidden = YES;
     [UIApplication sharedApplication].statusBarHidden = YES;
+    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
 
 
-    
     motionManager = [[CMMotionManager alloc] init];
     motionManager = [[CMMotionManager alloc] init];
     if([motionManager isAccelerometerAvailable] == YES) 
     if([motionManager isAccelerometerAvailable] == YES) 
     {
     {
@@ -519,7 +559,7 @@ int getKey(unichar keyCode);
 }
 }
 
 
 - (void)applicationDidBecomeActive:(UIApplication*)application 
 - (void)applicationDidBecomeActive:(UIApplication*)application 
-{
+{    
     [viewController startUpdating];
     [viewController startUpdating];
 }
 }
 
 
@@ -818,12 +858,14 @@ void Platform::signalShutdown()
     
     
 unsigned int Platform::getDisplayWidth()
 unsigned int Platform::getDisplayWidth()
 {
 {
-    return WINDOW_WIDTH;
+    CGSize size = DeviceOrientedSize([__appDelegate.viewController interfaceOrientation]);
+    return size.width;
 }
 }
 
 
 unsigned int Platform::getDisplayHeight()
 unsigned int Platform::getDisplayHeight()
 {
 {
-    return WINDOW_HEIGHT;
+    CGSize size = DeviceOrientedSize([__appDelegate.viewController interfaceOrientation]);
+    return size.height;
 }
 }
 
 
 long Platform::getAbsoluteTime()
 long Platform::getAbsoluteTime()

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

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