cocoaTouch/SDLOverrides/SDL_uikitappdelegate.m
changeset 2693 3207e0eacd43
parent 2692 ce9992075118
child 2696 41aa7b56c17b
equal deleted inserted replaced
2692:ce9992075118 2693:3207e0eacd43
    24 #import "SDL_uikitappdelegate.h"
    24 #import "SDL_uikitappdelegate.h"
    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 
    30 
    31 #ifdef main
    31 #ifdef main
    32 #undef main
    32 #undef main
    33 #endif
    33 #endif
    34 
    34 
    53 	[pool release];
    53 	[pool release];
    54 }
    54 }
    55 
    55 
    56 @implementation SDLUIKitDelegate
    56 @implementation SDLUIKitDelegate
    57 
    57 
    58 @synthesize window, windowID, controller;
    58 @synthesize window, windowID, controller, setup;
    59 
    59 
    60 /* convenience method */
    60 /* convenience method */
    61 +(SDLUIKitDelegate *)sharedAppDelegate {
    61 +(SDLUIKitDelegate *)sharedAppDelegate {
    62 	/* the delegate is set in UIApplicationMain(), which is garaunteed to be called before this method */
    62 	/* the delegate is set in UIApplicationMain(), which is garaunteed to be called before this method */
    63 	return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
    63 	return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
    64 }
    64 }
    65 
    65 
    66 void preSDL_main(){
    66 -(void) launchSDL_main{
    67 	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    67 	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    68 
    68 	
       
    69 	// must setup arguments in the same thread
       
    70 	[setup setArgsForLocalPlay];
       
    71 	
       
    72 	// run the user's application, passing argc and argv
    69 	SDL_main(forward_argc, forward_argv);
    73 	SDL_main(forward_argc, forward_argv);
    70 
    74 
    71 	[pool release];
    75 	[pool release];
    72 }
    76 }
    73 
    77 
    74 - (void) startSDLgame {
    78 -(IBAction) startSDLgame {
    75 	pthread_t threadID;
    79 	
    76 
    80 	[setup startThread:@"engineProtocol"];
    77 	if (NO == isServerRunning) {
       
    78 		// don't start another server because the port is already bound
       
    79 		pthread_create (&threadID, NULL, (void *) (*engineProtocolThread), NULL);
       
    80 		pthread_detach (threadID);
       
    81 		isServerRunning = YES;
       
    82 	}
       
    83 
       
    84 	setupArgsForLocalPlay();
       
    85 	
    81 	
    86 	// remove the current view to free resources
    82 	// remove the current view to free resources
    87 	[UIView beginAnimations:nil context:NULL];
    83 	[UIView beginAnimations:nil context:NULL];
    88 	[UIView setAnimationDuration:1.5];
    84 	[UIView setAnimationDuration:1.5];
    89 	controller.view.alpha = 0;
    85 	controller.view.alpha = 0;
    90 	[UIView commitAnimations];
    86 	[UIView commitAnimations];
    91 	[controller.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.5];
    87 	[controller.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.5];
    92 	//[controller.view removeFromSuperview];
    88 	//[controller.view removeFromSuperview];
    93 	
    89 	
    94 	/* run the user's application, passing argc and argv */
       
    95 	NSLog(@"Game is launching...");
    90 	NSLog(@"Game is launching...");
    96 	/*pthread_create (&threadID, NULL, (void *) (*preSDL_main), NULL);
       
    97 	pthread_detach (threadID);*/
       
    98 	int res = SDL_main(forward_argc, forward_argv);
       
    99 
    91 
   100 	// can't reach here yet
    92 	[NSThread detachNewThreadSelector:@selector(launchSDL_main) toTarget:self withObject:nil];
   101 	NSLog(@"Game exited with status %d", res);
    93 	
       
    94 	//SDL_main(forward_argc, forward_argv);
   102 
    95 
   103 	//[self performSelector:@selector(makeNewView) withObject:nil afterDelay:0.0];
    96 
   104 	/* exit, passing the return status from the user's application */
       
   105 	//exit(exit_status);
       
   106 }
    97 }
   107 
    98 
   108 // override the direct execution of SDL_main to allow us to implement the frontend (even using a nib)
    99 // override the direct execution of SDL_main to allow us to implement the frontend (even using a nib)
   109 -(void) applicationDidFinishLaunching:(UIApplication *)application {
   100 -(void) applicationDidFinishLaunching:(UIApplication *)application {
   110 	[application setStatusBarHidden:YES animated:NO];
   101 	[application setStatusBarHidden:YES animated:NO];
   111 
   102 
       
   103 	setup = [[GameSetup alloc] init]; 
   112 	/* Set working directory to resource path */
   104 	/* Set working directory to resource path */
   113 	[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
   105 	[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
   114 //#import "SoundEffect.h"	
   106 //#import "SoundEffect.h"	
   115 //	SoundEffect *erasingSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Erase" ofType:@"caf"]];
   107 //	SoundEffect *erasingSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Erase" ofType:@"caf"]];
   116 //	SoundEffect *selectSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Select" ofType:@"caf"]];
   108 //	SoundEffect *selectSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Select" ofType:@"caf"]];
   147 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
   139 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
   148 	NSLog(@"Rotating...");
   140 	NSLog(@"Rotating...");
   149 	return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
   141 	return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
   150 }
   142 }
   151 */
   143 */
       
   144 +(void) resetFrontend {
       
   145 	[[[SDLUIKitDelegate sharedAppDelegate].window viewWithTag:54867] removeFromSuperview];
       
   146 	[[SDLUIKitDelegate sharedAppDelegate].window addSubview:[SDLUIKitDelegate sharedAppDelegate].controller.view];
       
   147 	
       
   148 	[UIView beginAnimations:nil context:NULL];
       
   149 	[UIView setAnimationDuration:1];
       
   150 	[SDLUIKitDelegate sharedAppDelegate].controller.view.alpha = 1;
       
   151 	[UIView commitAnimations];
       
   152 	
       
   153 	[[SDLUIKitDelegate sharedAppDelegate].window makeKeyAndVisible];
       
   154 }
       
   155 
   152 
   156 
   153 void IPH_returnFrontend (void) {
   157 void IPH_returnFrontend (void) {
   154 	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   158 	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   155 	
   159 
   156 	[[[SDLUIKitDelegate sharedAppDelegate].window viewWithTag:54867] removeFromSuperview];
   160 	[SDLUIKitDelegate resetFrontend];
   157 	[[SDLUIKitDelegate sharedAppDelegate].window addSubview:[SDLUIKitDelegate sharedAppDelegate].controller.view];
       
   158 //	[[SDLUIKitDelegate sharedAppDelegate].window makeKeyAndVisible];
       
   159 	NSLog(@"Game exited...");
   161 	NSLog(@"Game exited...");
   160 //	pthread_exit(NULL);
   162 
   161 	[pool release];
   163 	[pool release];
   162 	exit(0);
   164 	[NSThread exit];
   163 //	while(1);	//prevent exiting	
       
   164 }
   165 }
   165 
   166 
   166 
   167 
   167 -(void) dealloc {
   168 -(void) dealloc {
       
   169 	[setup release];
   168 	[controller release];
   170 	[controller release];
   169 	[window release];
   171 	[window release];
   170 	[super dealloc];
   172 	[super dealloc];
   171 }
   173 }
   172 
   174