AppController.mm 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /****************************************************************************
  2. Copyright (c) 2010-2013 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #import "AppController.h"
  23. #import "cocos2d.h"
  24. #import "AppDelegate.h"
  25. #import "RootViewController.h"
  26. #import "SDKWrapper.h"
  27. #import "platform/ios/CCEAGLView-ios.h"
  28. using namespace cocos2d;
  29. @implementation AppController
  30. Application* app = nullptr;
  31. @synthesize window;
  32. #pragma mark -
  33. #pragma mark Application lifecycle
  34. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  35. [[SDKWrapper getInstance] application:application didFinishLaunchingWithOptions:launchOptions];
  36. // Add the view controller's view to the window and display.
  37. float scale = [[UIScreen mainScreen] scale];
  38. CGRect bounds = [[UIScreen mainScreen] bounds];
  39. window = [[UIWindow alloc] initWithFrame: bounds];
  40. // cocos2d application instance
  41. app = new AppDelegate(bounds.size.width * scale, bounds.size.height * scale);
  42. app->setMultitouch(true);
  43. // Use RootViewController to manage CCEAGLView
  44. _viewController = [[RootViewController alloc]init];
  45. #ifdef NSFoundationVersionNumber_iOS_7_0
  46. _viewController.automaticallyAdjustsScrollViewInsets = NO;
  47. _viewController.extendedLayoutIncludesOpaqueBars = NO;
  48. _viewController.edgesForExtendedLayout = UIRectEdgeAll;
  49. #else
  50. _viewController.wantsFullScreenLayout = YES;
  51. #endif
  52. // Set RootViewController to window
  53. if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
  54. {
  55. // warning: addSubView doesn't work on iOS6
  56. [window addSubview: _viewController.view];
  57. }
  58. else
  59. {
  60. // use this method on ios6
  61. [window setRootViewController:_viewController];
  62. }
  63. [window makeKeyAndVisible];
  64. [[UIApplication sharedApplication] setStatusBarHidden:YES];
  65. //run the cocos2d-x game scene
  66. app->start();
  67. return YES;
  68. }
  69. - (void)applicationWillResignActive:(UIApplication *)application {
  70. /*
  71. 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.
  72. 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.
  73. */
  74. app->onPause();
  75. [[SDKWrapper getInstance] applicationWillResignActive:application];
  76. }
  77. - (void)applicationDidBecomeActive:(UIApplication *)application {
  78. /*
  79. 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.
  80. */
  81. app->onResume();
  82. [[SDKWrapper getInstance] applicationDidBecomeActive:application];
  83. }
  84. - (void)applicationDidEnterBackground:(UIApplication *)application {
  85. /*
  86. 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.
  87. If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
  88. */
  89. [[SDKWrapper getInstance] applicationDidEnterBackground:application];
  90. }
  91. - (void)applicationWillEnterForeground:(UIApplication *)application {
  92. /*
  93. 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.
  94. */
  95. [[SDKWrapper getInstance] applicationWillEnterForeground:application];
  96. }
  97. - (void)applicationWillTerminate:(UIApplication *)application
  98. {
  99. [[SDKWrapper getInstance] applicationWillTerminate:application];
  100. delete app;
  101. app = nil;
  102. }
  103. #pragma mark -
  104. #pragma mark Memory management
  105. - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
  106. /*
  107. Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
  108. */
  109. }
  110. @end