| 1 |
/* |
| 2 |
Copyright 2013-2017 Jan Adamec, Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in the "Castle Game Engine" distribution, |
| 8 |
for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
*/ |
| 16 |
|
| 17 |
#import "AppDelegate.h" |
| 18 |
#import "OpenGLController.h" |
| 19 |
#import "ServiceAbstract.h" |
| 20 |
#import "MiscellaneousService.h" |
| 21 |
#import "CgeUtils.h" |
| 22 |
|
| 23 |
// import services |
| 24 |
/* IOS-SERVICES-IMPORT */ |
| 25 |
|
| 26 |
AppDelegate* appDelegateSingleton; |
| 27 |
|
| 28 |
void receiveMessageFromPascal(const char* message) |
| 29 |
{ |
| 30 |
if (appDelegateSingleton == nil) { |
| 31 |
NSLog(@"Objective-C received message from Pascal, but appDelegateSingleton is not assigned, this should not happen."); |
| 32 |
return; |
| 33 |
} |
| 34 |
[appDelegateSingleton messageReceived:message]; |
| 35 |
} |
| 36 |
|
| 37 |
AppDelegate* getAppDelegate() |
| 38 |
{ |
| 39 |
return appDelegateSingleton; |
| 40 |
} |
| 41 |
|
| 42 |
@implementation AppDelegate |
| 43 |
|
| 44 |
- (void)initializeServices:(OpenGLController* )viewController |
| 45 |
{ |
| 46 |
services = [[NSMutableArray alloc] init]; |
| 47 |
|
| 48 |
// create MiscellaneousService, added by default |
| 49 |
{ |
| 50 |
MiscellaneousService* serviceInstance; |
| 51 |
serviceInstance = [[MiscellaneousService alloc] init]; |
| 52 |
serviceInstance.mainController = viewController; |
| 53 |
serviceInstance.window = self.window; |
| 54 |
[services addObject: serviceInstance]; |
| 55 |
} |
| 56 |
|
| 57 |
// create services |
| 58 |
/* IOS-SERVICES-CREATE */ |
| 59 |
|
| 60 |
// initialize messaging with CastleMessaging unit |
| 61 |
appDelegateSingleton = self; |
| 62 |
CGEApp_SetReceiveMessageFromPascalCallback(receiveMessageFromPascal); |
| 63 |
} |
| 64 |
|
| 65 |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
| 66 |
{ |
| 67 |
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; |
| 68 |
|
| 69 |
GLKView *view = [[GLKView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; |
| 70 |
|
| 71 |
OpenGLController * viewController = [[OpenGLController alloc] initWithNibName:nil bundle:nil]; |
| 72 |
viewController.view = view; |
| 73 |
viewController.preferredFramesPerSecond = 60; |
| 74 |
|
| 75 |
self.window.rootViewController = viewController; |
| 76 |
|
| 77 |
// initialize services once window and viewController are initialized, |
| 78 |
// but before doing [viewController viewDidLoad] which performs OpenGL initialization |
| 79 |
// including calling Application.OnInitialize (that may want to already use services). |
| 80 |
[self initializeServices: viewController]; |
| 81 |
|
| 82 |
// call application:didFinishLaunchingWithOptions on all services |
| 83 |
for (int i = 0; i < [services count]; i++) { |
| 84 |
ServiceAbstract* service = [services objectAtIndex: i]; |
| 85 |
[service application: application didFinishLaunchingWithOptions: launchOptions]; |
| 86 |
} |
| 87 |
|
| 88 |
[viewController viewDidLoad]; |
| 89 |
|
| 90 |
// CGE have already drawn tiLoading frame, no need for a background color. |
| 91 |
//self.window.backgroundColor = [UIColor whiteColor]; |
| 92 |
|
| 93 |
// Although the window seems already visible, but makeKeyAndVisible call is still |
| 94 |
// needed to keep visible after application:didFinishLaunchingWithOptions call. |
| 95 |
[self.window makeKeyAndVisible]; |
| 96 |
|
| 97 |
return YES; |
| 98 |
} |
| 99 |
|
| 100 |
- (void)applicationWillResignActive:(UIApplication *)application |
| 101 |
{ |
| 102 |
// 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. |
| 103 |
// 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. |
| 104 |
} |
| 105 |
|
| 106 |
- (void)applicationDidEnterBackground:(UIApplication *)application |
| 107 |
{ |
| 108 |
// 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. |
| 109 |
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. |
| 110 |
|
| 111 |
// call applicationDidEnterBackground on all services |
| 112 |
for (int i = 0; i < [services count]; i++) { |
| 113 |
ServiceAbstract* service = [services objectAtIndex: i]; |
| 114 |
[service applicationDidEnterBackground]; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
- (void)applicationWillEnterForeground:(UIApplication *)application |
| 119 |
{ |
| 120 |
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. |
| 121 |
} |
| 122 |
|
| 123 |
- (void)applicationDidBecomeActive:(UIApplication *)application |
| 124 |
{ |
| 125 |
// 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. |
| 126 |
|
| 127 |
// call applicationDidBecomeActive on all services |
| 128 |
for (int i = 0; i < [services count]; i++) { |
| 129 |
ServiceAbstract* service = [services objectAtIndex: i]; |
| 130 |
[service applicationDidBecomeActive: application]; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
- (void)applicationWillTerminate:(UIApplication *)application |
| 135 |
{ |
| 136 |
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. |
| 137 |
} |
| 138 |
|
| 139 |
- (BOOL)application:(UIApplication *)application |
| 140 |
openURL:(NSURL *)url |
| 141 |
options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options |
| 142 |
{ |
| 143 |
// call application:openURL:options on all services |
| 144 |
for (int i = 0; i < [services count]; i++) { |
| 145 |
ServiceAbstract* service = [services objectAtIndex: i]; |
| 146 |
BOOL result = [service application: application openURL: url options: options]; |
| 147 |
if (result) { |
| 148 |
return result; |
| 149 |
} |
| 150 |
} |
| 151 |
if ([url.scheme isEqualToString:@"file"]) |
| 152 |
return CGEApp_HandleOpenUrl(url.fileSystemRepresentation); |
| 153 |
return NO; |
| 154 |
} |
| 155 |
|
| 156 |
- (void)messageReceived:(const char *)message |
| 157 |
{ |
| 158 |
NSString* messageStr = @(message); |
| 159 |
NSArray* messageAsList = [messageStr componentsSeparatedByString:@"\1"]; |
| 160 |
|
| 161 |
// call receiveMessageFromPascal on all services |
| 162 |
bool handled = false; |
| 163 |
for (int i = 0; i < [services count]; i++) { |
| 164 |
ServiceAbstract* service = [services objectAtIndex: i]; |
| 165 |
bool serviceHandled = [service messageReceived: messageAsList]; |
| 166 |
handled = handled || serviceHandled; |
| 167 |
} |
| 168 |
|
| 169 |
if (!handled) { |
| 170 |
NSString* messageMultiline = [messageAsList componentsJoinedByString:@"\n"]; |
| 171 |
NSLog(@"Message received by Objective-C but not handled by any service: %@", messageMultiline); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
- (void)onPurchase:(AvailableProduct*) product |
| 176 |
withTransaction:(SKPaymentTransaction*) transaction |
| 177 |
{ |
| 178 |
for (int i = 0; i < [services count]; i++) { |
| 179 |
ServiceAbstract* service = [services objectAtIndex: i]; |
| 180 |
[service onPurchase: product withTransaction: transaction]; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
@end |