1
0

AppController.mm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /****************************************************************************
  2. Copyright (c) 2010-2013 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #import "AppController.h"
  22. #import "cocos2d.h"
  23. #import "AppDelegate.h"
  24. #import "RootViewController.h"
  25. @implementation AppController
  26. @synthesize window;
  27. #pragma mark -
  28. #pragma mark Application lifecycle
  29. // cocos2d application instance
  30. static AppDelegate s_sharedApplication;
  31. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  32. cocos2d::Application *app = cocos2d::Application::getInstance();
  33. // Initialize the GLView attributes
  34. app->initGLContextAttrs();
  35. cocos2d::GLViewImpl::convertAttrs();
  36. // Override point for customization after application launch.
  37. // Add the view controller's view to the window and display.
  38. window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
  39. // Use RootViewController to manage CCEAGLView
  40. _viewController = [[RootViewController alloc]init];
  41. _viewController.wantsFullScreenLayout = YES;
  42. // Set RootViewController to window
  43. if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
  44. {
  45. // warning: addSubView doesn't work on iOS6
  46. [window addSubview: _viewController.view];
  47. }
  48. else
  49. {
  50. // use this method on ios6
  51. [window setRootViewController:_viewController];
  52. }
  53. [window makeKeyAndVisible];
  54. [[UIApplication sharedApplication] setStatusBarHidden:true];
  55. // IMPORTANT: Setting the GLView should be done after creating the RootViewController
  56. cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView((__bridge void *)_viewController.view);
  57. cocos2d::Director::getInstance()->setOpenGLView(glview);
  58. //run the cocos2d-x game scene
  59. app->run();
  60. return YES;
  61. }
  62. - (void)applicationWillResignActive:(UIApplication *)application {
  63. /*
  64. Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
  65. Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
  66. */
  67. // We don't need to call this method any more. It will interrupt user defined game pause&resume logic
  68. /* cocos2d::Director::getInstance()->pause(); */
  69. }
  70. - (void)applicationDidBecomeActive:(UIApplication *)application {
  71. /*
  72. Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  73. */
  74. // We don't need to call this method any more. It will interrupt user defined game pause&resume logic
  75. /* cocos2d::Director::getInstance()->resume(); */
  76. }
  77. - (void)applicationDidEnterBackground:(UIApplication *)application {
  78. /*
  79. Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
  80. If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
  81. */
  82. cocos2d::Application::getInstance()->applicationDidEnterBackground();
  83. }
  84. - (void)applicationWillEnterForeground:(UIApplication *)application {
  85. /*
  86. Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
  87. */
  88. cocos2d::Application::getInstance()->applicationWillEnterForeground();
  89. }
  90. - (void)applicationWillTerminate:(UIApplication *)application {
  91. /*
  92. Called when the application is about to terminate.
  93. See also applicationDidEnterBackground:.
  94. */
  95. }
  96. #pragma mark -
  97. #pragma mark Memory management
  98. - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
  99. /*
  100. Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
  101. */
  102. }
  103. #if __has_feature(objc_arc)
  104. #else
  105. - (void)dealloc {
  106. [window release];
  107. [_viewController release];
  108. [super dealloc];
  109. }
  110. #endif
  111. @end