cocoaTouch/SDLOverrides/SDL_uikitappdelegate.m
changeset 2698 90585aba87ad
parent 2697 75880595a9f1
child 2699 249adefa9c1c
equal deleted inserted replaced
2697:75880595a9f1 2698:90585aba87ad
    25 #import "SDL_uikitopenglview.h"
    25 #import "SDL_uikitopenglview.h"
    26 #import "SDL_events_c.h"
    26 #import "SDL_events_c.h"
    27 #import "jumphack.h"
    27 #import "jumphack.h"
    28 #import "SDL_video.h"
    28 #import "SDL_video.h"
    29 #import "GameSetup.h"
    29 #import "GameSetup.h"
       
    30 #import "PascalImports.h"
    30 
    31 
    31 //#import "SoundEffect.h"	
    32 //#import "SoundEffect.h"	
    32 //	SoundEffect *erasingSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Erase" ofType:@"caf"]];
    33 //	SoundEffect *erasingSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Erase" ofType:@"caf"]];
    33 //	SoundEffect *selectSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Select" ofType:@"caf"]];
    34 //	SoundEffect *selectSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Select" ofType:@"caf"]];
    34 
    35 
    35 
    36 
    36 #ifdef main
    37 #ifdef main
    37 #undef main
    38 #undef main
    38 #endif
    39 #endif
    39 
    40 
    40 extern int SDL_main(int argc, char *argv[]);
    41 int main(int argc, char *argv[]) {
    41 int main (int argc, char **argv) {
    42     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    42 	int i;
    43     int retVal = UIApplicationMain(argc, argv, nil, @"SDLUIKitDelegate");
    43 	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    44     [pool release];
    44 	
    45     return retVal;
    45 	/* store arguments */
       
    46 	forward_argc = argc;
       
    47 	forward_argv = (char **)malloc(argc * sizeof(char *));
       
    48 	for (i = 0; i < argc; i++) {
       
    49 		forward_argv[i] = malloc( (strlen(argv[i])+1) * sizeof(char));
       
    50 		strcpy(forward_argv[i], argv[i]);
       
    51 	}
       
    52 
       
    53 	/* Give over control to run loop, SDLUIKitDelegate will handle most things from here */
       
    54 	UIApplicationMain(argc, argv, NULL, @"SDLUIKitDelegate");
       
    55 	
       
    56 	[pool release];
       
    57 }
    46 }
    58 
    47 
    59 @implementation SDLUIKitDelegate
    48 @implementation SDLUIKitDelegate
    60 
    49 
    61 @synthesize window, windowID, controller, setup;
    50 @synthesize window, windowID, controller, setup;
    71 	[controller release];
    60 	[controller release];
    72 	[window release];
    61 	[window release];
    73 	[super dealloc];
    62 	[super dealloc];
    74 }
    63 }
    75 
    64 
    76 -(void) launchSDL_main{
       
    77 	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
    78 	
       
    79 	// must setup arguments in the same thread
       
    80 	[setup setArgsForLocalPlay];
       
    81 	
       
    82 	// run the user's application, passing argc and argv
       
    83 	SDL_main(forward_argc, forward_argv);
       
    84 
       
    85 	[pool release];
       
    86 }
       
    87 
       
    88 -(IBAction) startSDLgame {
    65 -(IBAction) startSDLgame {
    89 	
    66 	
    90 	[setup startThread:@"engineProtocol"];
    67 	[setup startThread:@"engineProtocol"];
    91 	[setup loadSettingsFromFile:@"settings.plist" forKey:@"systemSettings"];
    68 	[setup loadSettingsFromFile:@"settings.plist" forKey:@"systemSettings"];
    92 
    69 
    93 	// remove the current view to free resources
    70 	// remove the current view to free resources
    94 /*	[UIView beginAnimations:nil context:NULL];
    71 	[UIView beginAnimations:@"removing main controller" context:NULL];
    95 	[UIView setAnimationDuration:1.5];
    72 	[UIView setAnimationDuration:1];
    96 	controller.view.alpha = 1;
    73 	controller.view.alpha = 0;
    97 	[UIView commitAnimations];
    74 	[UIView commitAnimations];
    98 	[controller.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.5];
    75 	[controller.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1];
    99 */
    76 
   100 	NSLog(@"Game is launching...");
    77 	NSLog(@"Game is launching...");
   101 
    78 
   102 	[NSThread detachNewThreadSelector:@selector(launchSDL_main) toTarget:self withObject:nil];
    79 	// direct execution or thread? check the one that gives most fps
       
    80 	// library or call SDL_main? pascal quits at the end of the main
       
    81 	Game();
   103 	
    82 	
       
    83 	NSLog(@"Game is exting...");
       
    84 
       
    85 	[[window viewWithTag:54867] removeFromSuperview];
       
    86 	[setup unloadSettings];
       
    87 
       
    88 	[window addSubview:controller.view];
       
    89 	[window makeKeyAndVisible];
       
    90 
       
    91 	[UIView beginAnimations:@"inserting main controller" context:NULL];
       
    92 	[UIView setAnimationDuration:1];
       
    93 	controller.view.alpha = 1;
       
    94 	[UIView commitAnimations];	
   104 }
    95 }
   105 
    96 
   106 // override the direct execution of SDL_main to allow us to implement the frontend (even using a nib)
    97 // override the direct execution of SDL_main to allow us to implement the frontend (even using a nib)
   107 -(void) applicationDidFinishLaunching:(UIApplication *)application {
    98 -(void) applicationDidFinishLaunching:(UIApplication *)application {
   108 	[application setStatusBarHidden:YES animated:NO];
    99 	[application setStatusBarHidden:YES animated:NO];
   114 	[window addSubview:controller.view];
   105 	[window addSubview:controller.view];
   115 	[window makeKeyAndVisible];
   106 	[window makeKeyAndVisible];
   116 }
   107 }
   117 
   108 
   118 -(void) applicationWillTerminate:(UIApplication *)application {
   109 -(void) applicationWillTerminate:(UIApplication *)application {
   119 	/* free the memory we used to hold copies of argc and argv */
       
   120 	int i;
       
   121 	for (i=0; i < forward_argc; i++) {
       
   122 		free(forward_argv[i]);
       
   123 	}
       
   124 	free(forward_argv);	
       
   125 	SDL_SendQuit();
   110 	SDL_SendQuit();
   126 	/* hack to prevent automatic termination.  See SDL_uikitevents.m for details */
   111 	/* hack to prevent automatic termination.  See SDL_uikitevents.m for details */
   127 	// have to remove this otherwise game goes on when pushing the home button
   112 	// have to remove this otherwise game goes on when pushing the home button
   128 	//longjmp(*(jump_env()), 1);
   113 	//longjmp(*(jump_env()), 1);
   129 }
   114 }
   130 
   115 
   131 -(void) applicationWillResignActive:(UIApplication*)application
   116 -(void) applicationWillResignActive:(UIApplication*)application {
   132 {
       
   133 //	NSLog(@"%@", NSStringFromSelector(_cmd));
   117 //	NSLog(@"%@", NSStringFromSelector(_cmd));
   134 	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   118 	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   135 }
   119 }
   136 
   120 
   137 -(void) applicationDidBecomeActive:(UIApplication*)application
   121 -(void) applicationDidBecomeActive:(UIApplication*)application {
   138 {
       
   139 //	NSLog(@"%@", NSStringFromSelector(_cmd));
   122 //	NSLog(@"%@", NSStringFromSelector(_cmd));
   140 	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_RESTORED, 0, 0);
   123 	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_RESTORED, 0, 0);
   141 }
   124 }
   142 
   125 
   143 /*
   126 /*
   144 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
   127 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
   145 	NSLog(@"Rotating...");
   128 	NSLog(@"Rotating...");
   146 	return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
   129 	return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
   147 }
   130 }
   148 */
   131 */
   149 +(void) resetFrontend {
       
   150 	[[[SDLUIKitDelegate sharedAppDelegate].window viewWithTag:54867] removeFromSuperview];
       
   151 	[[SDLUIKitDelegate sharedAppDelegate].window addSubview:[SDLUIKitDelegate sharedAppDelegate].controller.view];
       
   152 	
       
   153 	[UIView beginAnimations:nil context:NULL];
       
   154 	[UIView setAnimationDuration:1];
       
   155 	[SDLUIKitDelegate sharedAppDelegate].controller.view.alpha = 1;
       
   156 	[UIView commitAnimations];
       
   157 	
       
   158 	[[SDLUIKitDelegate sharedAppDelegate].setup unloadSettings];
       
   159 	[[SDLUIKitDelegate sharedAppDelegate].window makeKeyAndVisible];
       
   160 }
       
   161 
   132 
   162 #pragma mark -
   133 -(NSString *)dataFilePath: (NSString *)fileName {
   163 #pragma mark Convenience methods
       
   164 void IPH_returnFrontend (void) {
       
   165 	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
   166 
       
   167 	[SDLUIKitDelegate resetFrontend];
       
   168 	NSLog(@"Game exited...");
       
   169 
       
   170 	[pool release];
       
   171 	[NSThread exit];
       
   172 }
       
   173 
       
   174 
       
   175 +(NSString *)dataFilePath: (NSString *)fileName {
       
   176 	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   134 	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   177 	NSString *documentsDirectory = [paths objectAtIndex:0];
   135 	NSString *documentsDirectory = [paths objectAtIndex:0];
   178 	return [documentsDirectory stringByAppendingPathComponent:fileName];
   136 	return [documentsDirectory stringByAppendingPathComponent:fileName];
   179 }
   137 }
   180 
   138