All Data Structures Files Functions Variables Enumerations Enumerator Properties Defines
/Projects/Cogito/src/AppDelegate.m
Go to the documentation of this file.
00001 //
00002 //  AppDelegate.m
00003 //  Author: Thomas Taylor
00004 //
00005 //  13/11/2011: Created class
00006 //
00007 
00008 #import "cocos2d.h"
00009 
00010 #import "AppDelegate.h"
00011 #import "GameConfig.h"
00012 #import "RootViewController.h"
00013 #import "Constants.h"
00014 
00015 @implementation AppDelegate
00016 
00017 @synthesize window;
00018 
00019 - (void) removeStartupFlicker
00020 {
00021  //
00022  // THIS CODE REMOVES THE STARTUP FLICKER
00023  //
00024  // Uncomment the following code if you Application only supports landscape mode
00025  //
00026 #if GAME_AUTOROTATION == kGameAutorotationUIViewController
00027 
00028  CC_ENABLE_DEFAULT_GL_STATES();
00029  CCDirector *director = [CCDirector sharedDirector];
00030  CGSize size = [director winSize];
00031  CCSprite *sprite = [CCSprite spriteWithFile:@"Default.png"];
00032  sprite.position = ccp(size.width/2, size.height/2);
00033  sprite.rotation = -90;
00034  [sprite visit];
00035  [[director openGLView] swapBuffers];
00036  CC_ENABLE_DEFAULT_GL_STATES();
00037  
00038 #endif //GAME_AUTOROTATION == kGameAutorotationUIViewController 
00039 }
00040 - (void) applicationDidFinishLaunching:(UIApplication*)application
00041 {
00042  // Init the window
00043  window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
00044  
00045  // Try to use CADisplayLink director
00046  // if it fails (SDK < 3.1) use the default director
00047  if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
00048   [CCDirector setDirectorType:kCCDirectorTypeDefault];
00049  
00050  
00051  CCDirector *director = [CCDirector sharedDirector];
00052  
00053  // Init the View Controller
00054  viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
00055  viewController.wantsFullScreenLayout = YES;
00056  
00057  //
00058  // Create the EAGLView manually
00059  //  1. Create a RGB565 format. Alternative: RGBA8
00060  // 2. depth format of 0 bit. Use 16 or 24 bit for 3d effects, like CCPageTurnTransition
00061  //
00062  //
00063  EAGLView *glView = // kEAGLColorFormatRGBA8// GL_DEPTH_COMPONENT16_OES[EAGLView viewWithFrame:[window bounds]
00064            pixelFormat:kEAGLColorFormatRGB565 
00065            depthFormat:0      
00066       ];
00067  
00068  // attach the openglView to the director
00069  [director setOpenGLView:glView];
00070  
00071  // Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
00072  if(![director enableRetinaDisplay:YES]) CCLOG(@"Retina Display Not supported");
00073  
00074  //
00075  // VERY IMPORTANT:
00076  // If the rotation is going to be controlled by a UIViewController
00077  // then the device orientation should be "Portrait".
00078  //
00079  // IMPORTANT:
00080  // By default, this template only supports Landscape orientations.
00081  // Edit the RootViewController.m file to edit the supported orientations.
00082  //
00083 #if GAME_AUTOROTATION == kGameAutorotationUIViewController
00084  [director setDeviceOrientation:kCCDeviceOrientationPortrait];
00085 #else
00086  [director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
00087 #endif
00088  
00089  // make the OpenGLView a child of the view controller
00090  [viewController setView:glView];
00091  
00092  // make the View Controller a child of the main window
00093  [window addSubview: viewController.view];
00094  
00095  [window makeKeyAndVisible];
00096  
00097  // Default texture format for PNG/BMP/TIFF/JPEG/GIF images
00098  // It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
00099  // You can change anytime.
00100  [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
00101  
00102  // Removes the startup flicker
00103  [self removeStartupFlicker];
00104  
00105     // load the level data
00106     [[GameManager sharedGameManager] loadLevelData];
00107     
00108  // Start the intro animation
00109     [[GameManager sharedGameManager] runSceneWithID:kStingScene];
00110 }
00111 
00112 
00113 - (void)applicationWillResignActive:(UIApplication *)application {
00114  [[CCDirector sharedDirector] pause];
00115 }
00116 
00117 - (void)applicationDidBecomeActive:(UIApplication *)application 
00118 {
00119     [[NSUserDefaults standardUserDefaults] synchronize];
00120  if(![[GameManager sharedGameManager] gamePaused]) [[CCDirector sharedDirector] resume];
00121 }
00122 
00123 - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
00124  [[CCDirector sharedDirector] purgeCachedData];
00125 }
00126 
00127 -(void) applicationDidEnterBackground:(UIApplication*)application {
00128  [[CCDirector sharedDirector] stopAnimation];
00129 }
00130 
00131 -(void) applicationWillEnterForeground:(UIApplication*)application {
00132  [[CCDirector sharedDirector] startAnimation];
00133 }
00134 
00135 - (void)applicationWillTerminate:(UIApplication *)application {
00136  CCDirector *director = [CCDirector sharedDirector];
00137  
00138  [[director openGLView] removeFromSuperview];
00139  
00140  [viewController release];
00141  
00142  [window release];
00143  
00144  [director end]; 
00145 }
00146 
00147 - (void)applicationSignificantTimeChange:(UIApplication *)application {
00148  [[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
00149 }
00150 
00151 - (void)dealloc {
00152  [[CCDirector sharedDirector] end];
00153  [window release];
00154  [super dealloc];
00155 }
00156 
00157 @end