cocoaTouch/SDLOverrides/SDL_uikitappdelegate.m
changeset 2714 c85ffe57d971
parent 2702 48fc46a922fd
child 2720 a5111ec4d25f
equal deleted inserted replaced
2713:71250942e95b 2714:c85ffe57d971
    45     return retVal;
    45     return retVal;
    46 }
    46 }
    47 
    47 
    48 @implementation SDLUIKitDelegate
    48 @implementation SDLUIKitDelegate
    49 
    49 
    50 @synthesize window, windowID, controller;
    50 @synthesize uiwindow, window, controller;
    51 
    51 
    52 /* convenience method */
    52 /* convenience method */
    53 +(SDLUIKitDelegate *)sharedAppDelegate {
    53 +(SDLUIKitDelegate *)sharedAppDelegate {
    54 	/* the delegate is set in UIApplicationMain(), which is garaunteed to be called before this method */
    54 	/* the delegate is set in UIApplicationMain(), which is guaranteed to be called before this method */
    55 	return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
    55 	return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
       
    56 }
       
    57 
       
    58 -(id) init {
       
    59 	self = [super init];
       
    60 	self.uiwindow = nil;
       
    61 	self.window = NULL;
       
    62 	self.controller = nil;
       
    63 	return self;
    56 }
    64 }
    57 
    65 
    58 -(void) dealloc {
    66 -(void) dealloc {
    59 	[controller release];
    67 	[controller release];
    60 	[window release];
    68 	[uiwindow release];
    61 	[super dealloc];
    69 	[super dealloc];
    62 }
    70 }
    63 
    71 
       
    72 #pragma mark -
       
    73 #pragma mark Custom stuff
    64 -(IBAction) startSDLgame {
    74 -(IBAction) startSDLgame {
    65 	NSAutoreleasePool *internal_pool = [[NSAutoreleasePool alloc] init];
    75 	NSAutoreleasePool *internal_pool = [[NSAutoreleasePool alloc] init];
    66 
    76 
    67 	GameSetup *setup = [[GameSetup alloc] init];
    77 	GameSetup *setup = [[GameSetup alloc] init];
    68 	[setup startThread:@"engineProtocol"];
    78 	[setup startThread:@"engineProtocol"];
    82 	Game(gameArgs);
    92 	Game(gameArgs);
    83 	
    93 	
    84 	free(gameArgs);
    94 	free(gameArgs);
    85 	NSLog(@"Game is exting...");
    95 	NSLog(@"Game is exting...");
    86 
    96 
    87 	[[window viewWithTag:54867] removeFromSuperview];
       
    88 	[setup release];
    97 	[setup release];
    89 
       
    90 	[window addSubview:controller.view];
       
    91 	[window makeKeyAndVisible];
       
    92 
    98 
    93 	[UIView beginAnimations:@"inserting main controller" context:NULL];
    99 	[UIView beginAnimations:@"inserting main controller" context:NULL];
    94 	[UIView setAnimationDuration:1];
   100 	[UIView setAnimationDuration:1];
    95 	controller.view.alpha = 1;
   101 	controller.view.alpha = 1;
    96 	[UIView commitAnimations];
   102 	[UIView commitAnimations];
       
   103 	
       
   104 	[uiwindow addSubview:controller.view];
       
   105 	[uiwindow makeKeyAndVisible];
    97 	
   106 	
    98 	[internal_pool release];
   107 	[internal_pool release];
    99 }
   108 }
   100 
   109 
   101 -(BOOL) checkFirstRun {
   110 -(BOOL) checkFirstRun {
   118 		[saveDict release];
   127 		[saveDict release];
   119 	}
   128 	}
   120 	return isFirstRun;
   129 	return isFirstRun;
   121 }
   130 }
   122 
   131 
       
   132 -(NSString *)dataFilePath: (NSString *)fileName {
       
   133 	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       
   134 	NSString *documentsDirectory = [paths objectAtIndex:0];
       
   135 	return [documentsDirectory stringByAppendingPathComponent:fileName];
       
   136 }
       
   137 
       
   138 -(void) applicationDidReceiveMemoryWarning:(UIApplication *)application {
       
   139 	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Running low on memory"
       
   140 							message:@"Will try to free some memory but app may crash"
       
   141 						       delegate:nil
       
   142 					      cancelButtonTitle:@"Ok"
       
   143 					      otherButtonTitles:nil ];
       
   144 	[alert show];
       
   145 	[alert release];
       
   146 }
       
   147 #pragma mark -
       
   148 #pragma mark SDLUIKitDelegate methods
   123 // override the direct execution of SDL_main to allow us to implement the frontend (even using a nib)
   149 // override the direct execution of SDL_main to allow us to implement the frontend (even using a nib)
   124 -(void) applicationDidFinishLaunching:(UIApplication *)application {
   150 -(void) applicationDidFinishLaunching:(UIApplication *)application {
   125 	[application setStatusBarHidden:YES animated:NO];
   151 	[application setStatusBarHidden:YES animated:NO];
   126 
   152 
   127 	[self checkFirstRun];
   153 	[self checkFirstRun];
   128 	/* Set working directory to resource path */
   154 	/* Set working directory to resource path */
   129 	[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
   155 	[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
   130 
   156 
   131 	[window addSubview:controller.view];
   157 	[uiwindow addSubview:controller.view];
   132 	[window makeKeyAndVisible];
   158 	[uiwindow makeKeyAndVisible];
   133 }
   159 }
   134 
   160 
   135 -(void) applicationWillTerminate:(UIApplication *)application {
   161 -(void) applicationWillTerminate:(UIApplication *)application {
   136 	SDL_SendQuit();
   162 	SDL_SendQuit();
   137 	/* hack to prevent automatic termination.  See SDL_uikitevents.m for details */
   163 	/* hack to prevent automatic termination.  See SDL_uikitevents.m for details */
   139 	//longjmp(*(jump_env()), 1);
   165 	//longjmp(*(jump_env()), 1);
   140 }
   166 }
   141 
   167 
   142 -(void) applicationWillResignActive:(UIApplication*)application {
   168 -(void) applicationWillResignActive:(UIApplication*)application {
   143 //	NSLog(@"%@", NSStringFromSelector(_cmd));
   169 //	NSLog(@"%@", NSStringFromSelector(_cmd));
   144 	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   170 	SDL_SendWindowEvent(self.window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   145 }
   171 }
   146 
   172 
   147 -(void) applicationDidBecomeActive:(UIApplication*)application {
   173 -(void) applicationDidBecomeActive:(UIApplication*)application {
   148 //	NSLog(@"%@", NSStringFromSelector(_cmd));
   174 //	NSLog(@"%@", NSStringFromSelector(_cmd));
   149 	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_RESTORED, 0, 0);
   175 	SDL_SendWindowEvent(self.window, SDL_WINDOWEVENT_RESTORED, 0, 0);
   150 }
   176 }
   151 
   177 
   152 /*
   178 /*
   153 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
   179 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
   154 	NSLog(@"Rotating...");
   180 	NSLog(@"Rotating...");
   155 	return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
   181 	return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
   156 }
   182 }
   157 */
   183 */
   158 
   184 
   159 -(NSString *)dataFilePath: (NSString *)fileName {
       
   160 	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       
   161 	NSString *documentsDirectory = [paths objectAtIndex:0];
       
   162 	return [documentsDirectory stringByAppendingPathComponent:fileName];
       
   163 }
       
   164 
       
   165 @end
   185 @end