app_delegate.mm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*************************************************************************/
  2. /* app_delegate.mm */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #import "app_delegate.h"
  30. #import "gl_view.h"
  31. #include "os_iphone.h"
  32. #include "core/globals.h"
  33. #include "main/main.h"
  34. #ifdef MODULE_FACEBOOKSCORER_IOS_ENABLED
  35. #include "modules/FacebookScorer_ios/FacebookScorer.h"
  36. #endif
  37. #ifdef MODULE_GAME_ANALYTICS_ENABLED
  38. #import "modules/game_analytics/ios/MobileAppTracker.framework/Headers/MobileAppTracker.h"
  39. //#import "modules/game_analytics/ios/MobileAppTracker.h"
  40. #import <AdSupport/AdSupport.h>
  41. #endif
  42. #define kFilteringFactor 0.1
  43. #define kRenderingFrequency 60
  44. #define kAccelerometerFrequency 100.0 // Hz
  45. #ifdef APPIRATER_ENABLED
  46. #import "Appirater.h"
  47. #endif
  48. Error _shell_open(String p_uri) {
  49. NSString* url = [[NSString alloc] initWithUTF8String:p_uri.utf8().get_data()];
  50. if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]])
  51. return ERR_CANT_OPEN;
  52. printf("opening url %ls\n", p_uri.c_str());
  53. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
  54. [url release];
  55. return OK;
  56. };
  57. @implementation AppDelegate
  58. @synthesize window;
  59. extern int gargc;
  60. extern char** gargv;
  61. extern int iphone_main(int, int, int, char**);
  62. extern void iphone_finish();
  63. static ViewController* mainViewController = nil;
  64. + (ViewController*) getViewController
  65. {
  66. return mainViewController;
  67. }
  68. static int frame_count = 0;
  69. - (void)drawView:(GLView*)view; {
  70. switch (frame_count) {
  71. case 0: {
  72. int backingWidth;
  73. int backingHeight;
  74. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
  75. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
  76. OS::VideoMode vm;
  77. vm.fullscreen = true;
  78. vm.width = backingWidth;
  79. vm.height = backingHeight;
  80. vm.resizable = false;
  81. OS::get_singleton()->set_video_mode(vm);
  82. if (!OS::get_singleton()) {
  83. exit(0);
  84. };
  85. ++frame_count;
  86. NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  87. NSString *documentsDirectory = [paths objectAtIndex:0];
  88. //NSString *documentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
  89. OSIPhone::get_singleton()->set_data_dir(String::utf8([documentsDirectory UTF8String]));
  90. NSString *locale_code = [[[NSLocale preferredLanguages] objectAtIndex:0] substringToIndex:2];
  91. OSIPhone::get_singleton()->set_locale(String::utf8([locale_code UTF8String]));
  92. NSString* uuid;
  93. if ([[UIDevice currentDevice]respondsToSelector:@selector(identifierForVendor)]) {
  94. uuid = [UIDevice currentDevice].identifierForVendor.UUIDString;
  95. }else{
  96. // before iOS 6, so just generate an identifier and store it
  97. uuid = [[NSUserDefaults standardUserDefaults] objectForKey:@"identiferForVendor"];
  98. if( !uuid ) {
  99. CFUUIDRef cfuuid = CFUUIDCreate(NULL);
  100. uuid = (__bridge_transfer NSString*)CFUUIDCreateString(NULL, cfuuid);
  101. CFRelease(cfuuid);
  102. [[NSUserDefaults standardUserDefaults] setObject:uuid forKey:@"identifierForVendor"];
  103. }
  104. }
  105. OSIPhone::get_singleton()->set_unique_ID(String::utf8([uuid UTF8String]));
  106. }; break;
  107. /*
  108. case 1: {
  109. ++frame_count;
  110. } break;
  111. */
  112. case 1: {
  113. Main::setup2();
  114. ++frame_count;
  115. } break;
  116. /*
  117. case 3: {
  118. ++frame_count;
  119. } break;
  120. */
  121. case 2: {
  122. Main::start();
  123. ++frame_count;
  124. #ifdef APPIRATER_ENABLED
  125. int aid = Globals::get_singleton()->get("ios/app_id");
  126. [Appirater appLaunched:YES app_id:aid];
  127. #endif
  128. }; break; // no fallthrough
  129. default: {
  130. if (OSIPhone::get_singleton()) {
  131. OSIPhone::get_singleton()->update_accelerometer(accel[0], accel[1], accel[2]);
  132. bool quit_request = OSIPhone::get_singleton()->iterate();
  133. };
  134. };
  135. };
  136. };
  137. - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
  138. printf("****************** did receive memory warning!\n");
  139. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_OS_MEMORY_WARNING);
  140. };
  141. - (void)applicationDidFinishLaunching:(UIApplication*)application {
  142. printf("**************** app delegate init\n");
  143. CGRect rect = [[UIScreen mainScreen] bounds];
  144. [application setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
  145. // disable idle timer
  146. application.idleTimerDisabled = YES;
  147. //Create a full-screen window
  148. window = [[UIWindow alloc] initWithFrame:rect];
  149. //window.autoresizesSubviews = YES;
  150. //[window setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth];
  151. //Create the OpenGL ES view and add it to the window
  152. GLView *glView = [[GLView alloc] initWithFrame:rect];
  153. printf("glview is %p\n", glView);
  154. //[window addSubview:glView];
  155. glView.delegate = self;
  156. //glView.autoresizesSubviews = YES;
  157. //[glView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth];
  158. int backingWidth;
  159. int backingHeight;
  160. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
  161. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
  162. iphone_main(backingWidth, backingHeight, gargc, gargv);
  163. view_controller = [[ViewController alloc] init];
  164. view_controller.view = glView;
  165. window.rootViewController = view_controller;
  166. glView.animationInterval = 1.0 / kRenderingFrequency;
  167. [glView startAnimation];
  168. //Show the window
  169. [window makeKeyAndVisible];
  170. //Configure and start accelerometer
  171. last_accel[0] = 0;
  172. last_accel[1] = 0;
  173. last_accel[2] = 0;
  174. [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
  175. [[UIAccelerometer sharedAccelerometer] setDelegate:self];
  176. //OSIPhone::screen_width = rect.size.width - rect.origin.x;
  177. //OSIPhone::screen_height = rect.size.height - rect.origin.y;
  178. mainViewController = view_controller;
  179. #ifdef MODULE_GAME_ANALYTICS_ENABLED
  180. printf("********************* didFinishLaunchingWithOptions\n");
  181. if(!Globals::get_singleton()->has("mobileapptracker/advertiser_id"))
  182. {
  183. return;
  184. }
  185. if(!Globals::get_singleton()->has("mobileapptracker/conversion_key"))
  186. {
  187. return;
  188. }
  189. String adid = GLOBAL_DEF("mobileapptracker/advertiser_id","");
  190. String convkey = GLOBAL_DEF("mobileapptracker/conversion_key","");
  191. NSString * advertiser_id = [NSString stringWithUTF8String:adid.utf8().get_data()];
  192. NSString * conversion_key = [NSString stringWithUTF8String:convkey.utf8().get_data()];
  193. // Account Configuration info - must be set
  194. [MobileAppTracker initializeWithMATAdvertiserId:advertiser_id
  195. MATConversionKey:conversion_key];
  196. // Used to pass us the IFA, enables highly accurate 1-to-1 attribution.
  197. // Required for many advertising networks.
  198. [MobileAppTracker setAppleAdvertisingIdentifier:[[ASIdentifierManager sharedManager] advertisingIdentifier]
  199. advertisingTrackingEnabled:[[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled]];
  200. #endif
  201. };
  202. - (void)applicationWillTerminate:(UIApplication*)application {
  203. printf("********************* will terminate\n");
  204. iphone_finish();
  205. };
  206. - (void)applicationDidEnterBackground:(UIApplication *)application
  207. {
  208. printf("********************* did enter background\n");
  209. if (OS::get_singleton()->get_main_loop())
  210. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT);
  211. [view_controller.view stopAnimation];
  212. if (OS::get_singleton()->native_video_is_playing()) {
  213. OSIPhone::get_singleton()->native_video_focus_out();
  214. };
  215. }
  216. - (void)applicationWillEnterForeground:(UIApplication *)application
  217. {
  218. printf("********************* did enter foreground\n");
  219. //OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
  220. [view_controller.view startAnimation];
  221. }
  222. - (void) applicationWillResignActive:(UIApplication *)application
  223. {
  224. printf("********************* will resign active\n");
  225. //OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT);
  226. [view_controller.view stopAnimation]; // FIXME: pause seems to be recommended elsewhere
  227. }
  228. - (void) applicationDidBecomeActive:(UIApplication *)application
  229. {
  230. printf("********************* did become active\n");
  231. #ifdef MODULE_GAME_ANALYTICS_ENABLED
  232. printf("********************* mobile app tracker found\n");
  233. [MobileAppTracker measureSession];
  234. #endif
  235. if (OS::get_singleton()->get_main_loop())
  236. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
  237. [view_controller.view startAnimation]; // FIXME: resume seems to be recommended elsewhere
  238. if (OSIPhone::get_singleton()->native_video_is_playing()) {
  239. OSIPhone::get_singleton()->native_video_unpause();
  240. };
  241. }
  242. - (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration {
  243. //Use a basic low-pass filter to only keep the gravity in the accelerometer values
  244. accel[0] = acceleration.x; // * kFilteringFactor + accel[0] * (1.0 - kFilteringFactor);
  245. accel[1] = acceleration.y; // * kFilteringFactor + accel[1] * (1.0 - kFilteringFactor);
  246. accel[2] = acceleration.z; // * kFilteringFactor + accel[2] * (1.0 - kFilteringFactor);
  247. }
  248. - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
  249. #ifdef MODULE_FACEBOOKSCORER_IOS_ENABLED
  250. return [[[FacebookScorer sharedInstance] facebook] handleOpenURL:url];
  251. #else
  252. return false;
  253. #endif
  254. }
  255. // For 4.2+ support
  256. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
  257. #ifdef MODULE_FACEBOOKSCORER_IOS_ENABLED
  258. return [[[FacebookScorer sharedInstance] facebook] handleOpenURL:url];
  259. #else
  260. return false;
  261. #endif
  262. }
  263. - (void)dealloc
  264. {
  265. [window release];
  266. [super dealloc];
  267. }
  268. @end