All Data Structures Files Functions Variables Enumerations Enumerator Properties Defines
/Projects/Cogito/src/RootViewController.m
Go to the documentation of this file.
00001 //
00002 //  RootViewController.m
00003 //  Author: Thomas Taylor
00004 //
00005 //  13/11/2011: Created class
00006 //
00007 
00008 //
00009 // RootViewController + iAd
00010 // If you want to support iAd, use this class as the controller of your iAd
00011 //
00012 
00013 #import "cocos2d.h"
00014 
00015 #import "RootViewController.h"
00016 #import "GameConfig.h"
00017 
00018 @implementation RootViewController
00019 
00020 /*
00021  // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
00022  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
00023  if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
00024  // Custom initialization
00025  }
00026  return self;
00027  }
00028  */
00029 
00030 /*
00031  // Implement loadView to create a view hierarchy programmatically, without using a nib.
00032  - (void)loadView {
00033  }
00034  */
00035 
00036 /*
00037  // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
00038  - (void)viewDidLoad {
00039  [super viewDidLoad];
00040  }
00041  */
00042 
00043 
00044 // Override to allow orientations other than the default portrait orientation.
00045 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
00046  
00047  //
00048  // There are 2 ways to support auto-rotation:
00049  //  - The OpenGL / cocos2d way
00050  //     - Faster, but doesn't rotate the UIKit objects
00051  //  - The ViewController way
00052  //    - A bit slower, but the UiKit objects are placed in the right place
00053  //
00054  
00055 #if GAME_AUTOROTATION==kGameAutorotationNone
00056  //
00057  // EAGLView won't be autorotated.
00058  // Since this method should return YES in at least 1 orientation, 
00059  // we return YES only in the Portrait orientation
00060  //
00061  return ( interfaceOrientation == UIInterfaceOrientationPortrait );
00062  
00063 #elif GAME_AUTOROTATION==kGameAutorotationCCDirector
00064  //
00065  // EAGLView will be rotated by cocos2d
00066  //
00067  // Sample: Autorotate only in landscape mode
00068  //
00069  if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft ) {
00070   [[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeRight];
00071  } else if( interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
00072   [[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeLeft];
00073  }
00074  
00075  // Since this method should return YES in at least 1 orientation, 
00076  // we return YES only in the Portrait orientation
00077  return ( interfaceOrientation == UIInterfaceOrientationPortrait );
00078  
00079 #elif GAME_AUTOROTATION == kGameAutorotationUIViewController
00080  //
00081  // EAGLView will be rotated by the UIViewController
00082  //
00083  // Sample: Autorotate only in landscpe mode
00084  //
00085  // return YES for the supported orientations
00086  
00087  return ( UIInterfaceOrientationIsLandscape( interfaceOrientation ) );
00088  
00089 #else
00090 #error Unknown value in GAME_AUTOROTATION
00091  
00092 #endif // GAME_AUTOROTATION
00093  
00094  
00095  // Shold not happen
00096  return NO;
00097 }
00098 
00099 //
00100 // This callback only will be called when GAME_AUTOROTATION == kGameAutorotationUIViewController
00101 //
00102 #if GAME_AUTOROTATION == kGameAutorotationUIViewController
00103 -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
00104 {
00105  //
00106  // Assuming that the main window has the size of the screen
00107  // BUG: This won't work if the EAGLView is not fullscreen
00109  CGRect screenRect = [[UIScreen mainScreen] bounds];
00110  CGRect rect = CGRectZero;
00111 
00112  
00113  if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)  
00114   rect = screenRect;
00115  
00116  else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
00117   rect.size = CGSizeMake( screenRect.size.height, screenRect.size.width );
00118  
00119  CCDirector *director = [CCDirector sharedDirector];
00120  EAGLView *glView = [director openGLView];
00121  float contentScaleFactor = [director contentScaleFactor];
00122  
00123  if( contentScaleFactor != 1 ) {
00124   rect.size.width *= contentScaleFactor;
00125   rect.size.height *= contentScaleFactor;
00126  }
00127  glView.frame = rect;
00128 }
00129 #endif // GAME_AUTOROTATION == kGameAutorotationUIViewController
00130 
00131 
00132 - (void)didReceiveMemoryWarning {
00133     // Releases the view if it doesn't have a superview.
00134     [super didReceiveMemoryWarning];
00135     
00136     // Release any cached data, images, etc that aren't in use.
00137 }
00138 
00139 - (void)viewDidUnload {
00140     [super viewDidUnload];
00141     // Release any retained subviews of the main view.
00142     // e.g. self.myOutlet = nil;
00143 }
00144 
00145 
00146 - (void)dealloc {
00147     [super dealloc];
00148 }
00149 
00150 
00151 @end