123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- /*************************************************************************/
- /* app_delegate.mm */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* http://www.godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /*************************************************************************/
- #import "app_delegate.h"
- #import "gl_view.h"
- #include "os_iphone.h"
- #include "core/globals.h"
- #include "main/main.h"
- #ifdef MODULE_FACEBOOKSCORER_IOS_ENABLED
- #include "modules/FacebookScorer_ios/FacebookScorer.h"
- #endif
- #ifdef MODULE_GAME_ANALYTICS_ENABLED
- #import "modules/game_analytics/ios/MobileAppTracker.framework/Headers/MobileAppTracker.h"
- //#import "modules/game_analytics/ios/MobileAppTracker.h"
- #import <AdSupport/AdSupport.h>
- #endif
- #define kFilteringFactor 0.1
- #define kRenderingFrequency 60
- #define kAccelerometerFrequency 100.0 // Hz
- #ifdef APPIRATER_ENABLED
- #import "Appirater.h"
- #endif
- Error _shell_open(String p_uri) {
- NSString* url = [[NSString alloc] initWithUTF8String:p_uri.utf8().get_data()];
- if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]])
- return ERR_CANT_OPEN;
- printf("opening url %ls\n", p_uri.c_str());
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
- [url release];
- return OK;
- };
- @implementation AppDelegate
- @synthesize window;
- extern int gargc;
- extern char** gargv;
- extern int iphone_main(int, int, int, char**);
- extern void iphone_finish();
- static ViewController* mainViewController = nil;
- + (ViewController*) getViewController
- {
- return mainViewController;
- }
- static int frame_count = 0;
- - (void)drawView:(GLView*)view; {
- switch (frame_count) {
- case 0: {
- int backingWidth;
- int backingHeight;
- glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
- glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
-
- OS::VideoMode vm;
- vm.fullscreen = true;
- vm.width = backingWidth;
- vm.height = backingHeight;
- vm.resizable = false;
- OS::get_singleton()->set_video_mode(vm);
- if (!OS::get_singleton()) {
- exit(0);
- };
- ++frame_count;
- NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- //NSString *documentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
- OSIPhone::get_singleton()->set_data_dir(String::utf8([documentsDirectory UTF8String]));
-
- NSString *locale_code = [[[NSLocale preferredLanguages] objectAtIndex:0] substringToIndex:2];
- OSIPhone::get_singleton()->set_locale(String::utf8([locale_code UTF8String]));
- NSString* uuid;
- if ([[UIDevice currentDevice]respondsToSelector:@selector(identifierForVendor)]) {
- uuid = [UIDevice currentDevice].identifierForVendor.UUIDString;
- }else{
- // before iOS 6, so just generate an identifier and store it
- uuid = [[NSUserDefaults standardUserDefaults] objectForKey:@"identiferForVendor"];
- if( !uuid ) {
- CFUUIDRef cfuuid = CFUUIDCreate(NULL);
- uuid = (__bridge_transfer NSString*)CFUUIDCreateString(NULL, cfuuid);
- CFRelease(cfuuid);
- [[NSUserDefaults standardUserDefaults] setObject:uuid forKey:@"identifierForVendor"];
- }
- }
- OSIPhone::get_singleton()->set_unique_ID(String::utf8([uuid UTF8String]));
- }; break;
- /*
- case 1: {
- ++frame_count;
- } break;
- */
- case 1: {
- Main::setup2();
- ++frame_count;
- } break;
- /*
- case 3: {
- ++frame_count;
- } break;
- */
- case 2: {
- Main::start();
- ++frame_count;
- #ifdef APPIRATER_ENABLED
- int aid = Globals::get_singleton()->get("ios/app_id");
- [Appirater appLaunched:YES app_id:aid];
- #endif
- }; break; // no fallthrough
- default: {
- if (OSIPhone::get_singleton()) {
- OSIPhone::get_singleton()->update_accelerometer(accel[0], accel[1], accel[2]);
- bool quit_request = OSIPhone::get_singleton()->iterate();
- };
- };
- };
- };
- - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
- printf("****************** did receive memory warning!\n");
- OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_OS_MEMORY_WARNING);
- };
- - (void)applicationDidFinishLaunching:(UIApplication*)application {
- printf("**************** app delegate init\n");
- CGRect rect = [[UIScreen mainScreen] bounds];
- [application setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
- // disable idle timer
- application.idleTimerDisabled = YES;
- //Create a full-screen window
- window = [[UIWindow alloc] initWithFrame:rect];
- //window.autoresizesSubviews = YES;
- //[window setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth];
- //Create the OpenGL ES view and add it to the window
- GLView *glView = [[GLView alloc] initWithFrame:rect];
- printf("glview is %p\n", glView);
- //[window addSubview:glView];
- glView.delegate = self;
- //glView.autoresizesSubviews = YES;
- //[glView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth];
- int backingWidth;
- int backingHeight;
- glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
- glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
-
- iphone_main(backingWidth, backingHeight, gargc, gargv);
-
- view_controller = [[ViewController alloc] init];
- view_controller.view = glView;
- window.rootViewController = view_controller;
- glView.animationInterval = 1.0 / kRenderingFrequency;
- [glView startAnimation];
-
- //Show the window
- [window makeKeyAndVisible];
- //Configure and start accelerometer
- last_accel[0] = 0;
- last_accel[1] = 0;
- last_accel[2] = 0;
- [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
- [[UIAccelerometer sharedAccelerometer] setDelegate:self];
- //OSIPhone::screen_width = rect.size.width - rect.origin.x;
- //OSIPhone::screen_height = rect.size.height - rect.origin.y;
-
- mainViewController = view_controller;
-
- #ifdef MODULE_GAME_ANALYTICS_ENABLED
- printf("********************* didFinishLaunchingWithOptions\n");
- if(!Globals::get_singleton()->has("mobileapptracker/advertiser_id"))
- {
- return;
- }
- if(!Globals::get_singleton()->has("mobileapptracker/conversion_key"))
- {
- return;
- }
-
- String adid = GLOBAL_DEF("mobileapptracker/advertiser_id","");
- String convkey = GLOBAL_DEF("mobileapptracker/conversion_key","");
-
- NSString * advertiser_id = [NSString stringWithUTF8String:adid.utf8().get_data()];
- NSString * conversion_key = [NSString stringWithUTF8String:convkey.utf8().get_data()];
-
- // Account Configuration info - must be set
- [MobileAppTracker initializeWithMATAdvertiserId:advertiser_id
- MATConversionKey:conversion_key];
-
- // Used to pass us the IFA, enables highly accurate 1-to-1 attribution.
- // Required for many advertising networks.
- [MobileAppTracker setAppleAdvertisingIdentifier:[[ASIdentifierManager sharedManager] advertisingIdentifier]
- advertisingTrackingEnabled:[[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled]];
-
- #endif
-
- };
- - (void)applicationWillTerminate:(UIApplication*)application {
- printf("********************* will terminate\n");
- iphone_finish();
- };
- - (void)applicationDidEnterBackground:(UIApplication *)application
- {
- printf("********************* did enter background\n");
- if (OS::get_singleton()->get_main_loop())
- OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT);
- [view_controller.view stopAnimation];
- if (OS::get_singleton()->native_video_is_playing()) {
- OSIPhone::get_singleton()->native_video_focus_out();
- };
- }
- - (void)applicationWillEnterForeground:(UIApplication *)application
- {
- printf("********************* did enter foreground\n");
- //OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
- [view_controller.view startAnimation];
- }
- - (void) applicationWillResignActive:(UIApplication *)application
- {
- printf("********************* will resign active\n");
- //OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT);
- [view_controller.view stopAnimation]; // FIXME: pause seems to be recommended elsewhere
- }
- - (void) applicationDidBecomeActive:(UIApplication *)application
- {
- printf("********************* did become active\n");
- #ifdef MODULE_GAME_ANALYTICS_ENABLED
- printf("********************* mobile app tracker found\n");
- [MobileAppTracker measureSession];
- #endif
- if (OS::get_singleton()->get_main_loop())
- OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
- [view_controller.view startAnimation]; // FIXME: resume seems to be recommended elsewhere
- if (OSIPhone::get_singleton()->native_video_is_playing()) {
- OSIPhone::get_singleton()->native_video_unpause();
- };
- }
- - (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration {
- //Use a basic low-pass filter to only keep the gravity in the accelerometer values
- accel[0] = acceleration.x; // * kFilteringFactor + accel[0] * (1.0 - kFilteringFactor);
- accel[1] = acceleration.y; // * kFilteringFactor + accel[1] * (1.0 - kFilteringFactor);
- accel[2] = acceleration.z; // * kFilteringFactor + accel[2] * (1.0 - kFilteringFactor);
- }
- - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
- #ifdef MODULE_FACEBOOKSCORER_IOS_ENABLED
- return [[[FacebookScorer sharedInstance] facebook] handleOpenURL:url];
- #else
- return false;
- #endif
- }
- // For 4.2+ support
- - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
- #ifdef MODULE_FACEBOOKSCORER_IOS_ENABLED
- return [[[FacebookScorer sharedInstance] facebook] handleOpenURL:url];
- #else
- return false;
- #endif
- }
- - (void)dealloc
- {
- [window release];
- [super dealloc];
- }
- @end
|