cocoaTouch/SDLOverrides/SDL_uikitappdelegate.m
changeset 2803 1f446fc5c8ec
parent 2772 1965eba10be6
child 3006 da6023c2745b
equal deleted inserted replaced
2802:28cd5b87bce9 2803:1f446fc5c8ec
    28 #import "SDL_video.h"
    28 #import "SDL_video.h"
    29 #import "GameSetup.h"
    29 #import "GameSetup.h"
    30 #import "PascalImports.h"
    30 #import "PascalImports.h"
    31 #import "MainMenuViewController.h"
    31 #import "MainMenuViewController.h"
    32 
    32 
    33 //#import "SoundEffect.h"	
       
    34 //	SoundEffect *erasingSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Erase" ofType:@"caf"]];
       
    35 //	SoundEffect *selectSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Select" ofType:@"caf"]];
       
    36 
       
    37 
       
    38 #ifdef main
    33 #ifdef main
    39 #undef main
    34 #undef main
    40 #endif
    35 #endif
    41 
    36 
    42 int main (int argc, char *argv[]) {
    37 int main (int argc, char *argv[]) {
    48 
    43 
    49 @implementation SDLUIKitDelegate
    44 @implementation SDLUIKitDelegate
    50 
    45 
    51 @synthesize uiwindow, window, viewController;
    46 @synthesize uiwindow, window, viewController;
    52 
    47 
    53 /* convenience method */
    48 // convenience method
    54 +(SDLUIKitDelegate *)sharedAppDelegate {
    49 +(SDLUIKitDelegate *)sharedAppDelegate {
    55 	/* the delegate is set in UIApplicationMain(), which is guaranteed to be called before this method */
    50 	// the delegate is set in UIApplicationMain(), which is guaranteed to be called before this method
    56 	return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
    51 	return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
    57 }
    52 }
    58 
    53 
    59 -(id) init {
    54 -(id) init {
    60 	self = [super init];
    55 	self = [super init];
   103 	[UIView commitAnimations];
    98 	[UIView commitAnimations];
   104 	
    99 	
   105 	[internal_pool release];
   100 	[internal_pool release];
   106 }
   101 }
   107 
   102 
   108 -(BOOL) checkFirstRun {
       
   109 	//move all this inside the MainMenuViewController
       
   110 	BOOL isFirstRun = NO;
       
   111 	
       
   112 	//use a nssthread a ask the user to wait
       
   113 	
       
   114 	NSString *filePath = [self dataFilePath:@"settings.plist"];
       
   115 	if (!([[NSFileManager defaultManager] fileExistsAtPath:filePath])) {
       
   116 		isFirstRun = YES;
       
   117 		// file not present, let's create it
       
   118 		NSMutableDictionary *saveDict = [[NSMutableDictionary alloc] init];
       
   119 	
       
   120 		[saveDict setObject:@"" forKey:@"username"];
       
   121 		[saveDict setObject:@"" forKey:@"password"];
       
   122 		[saveDict setObject:@"1" forKey:@"music"];
       
   123 		[saveDict setObject:@"1" forKey:@"sounds"];
       
   124 		[saveDict setObject:@"0" forKey:@"alternate"];
       
   125 	
       
   126 		[saveDict writeToFile:filePath atomically:YES];
       
   127 		[saveDict release];
       
   128 	}
       
   129 	return isFirstRun;
       
   130 }
       
   131 
       
   132 -(NSString *)dataFilePath: (NSString *)fileName {
   103 -(NSString *)dataFilePath: (NSString *)fileName {
   133 	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   104 	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   134 	NSString *documentsDirectory = [paths objectAtIndex:0];
   105 	NSString *documentsDirectory = [paths objectAtIndex:0];
   135 	return [documentsDirectory stringByAppendingPathComponent:fileName];
   106 	return [documentsDirectory stringByAppendingPathComponent:fileName];
   136 }
       
   137 
       
   138 -(void) applicationDidReceiveMemoryWarning:(UIApplication *)application {
       
   139 	/* we get too many ones on ipod touch
       
   140 	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Running low on memory"
       
   141 							message:@"I will try to free some memory but game might slow down a little"
       
   142 						       delegate:nil
       
   143 					      cancelButtonTitle:@"Ok"
       
   144 					      otherButtonTitles:nil ];
       
   145 	[alert show];
       
   146 	[alert release];
       
   147 	*/
       
   148 }
   107 }
   149 
   108 
   150 #pragma mark -
   109 #pragma mark -
   151 #pragma mark SDLUIKitDelegate methods
   110 #pragma mark SDLUIKitDelegate methods
   152 // override the direct execution of SDL_main to allow us to implement the frontend (even using a nib)
   111 // override the direct execution of SDL_main to allow us to implement the frontend (even using a nib)
   157 	self.uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   116 	self.uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   158 	self.uiwindow.backgroundColor = [UIColor blackColor];
   117 	self.uiwindow.backgroundColor = [UIColor blackColor];
   159 	
   118 	
   160 	self.viewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController" bundle:nil];
   119 	self.viewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController" bundle:nil];
   161 	
   120 	
   162 	/* Set working directory to resource path */
   121 	// Set working directory to resource path
   163 	[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
   122 	[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
   164 
   123 
   165 	[uiwindow addSubview:viewController.view];
   124 	[uiwindow addSubview:viewController.view];
   166 	[uiwindow makeKeyAndVisible];
   125 	[uiwindow makeKeyAndVisible];
   167 	[uiwindow layoutSubviews];
   126 	[uiwindow layoutSubviews];
   168 	[self checkFirstRun];
       
   169 }
   127 }
   170 
   128 
   171 -(void) applicationWillTerminate:(UIApplication *)application {
   129 -(void) applicationWillTerminate:(UIApplication *)application {
   172 	SDL_SendQuit();
   130 	SDL_SendQuit();
   173 	/* hack to prevent automatic termination.  See SDL_uikitevents.m for details */
   131 	// hack to prevent automatic termination.  See SDL_uikitevents.m for details
   174 	// have to remove this otherwise game goes on when pushing the home button
   132 	// have to remove this otherwise game goes on when pushing the home button
   175 	//longjmp(*(jump_env()), 1);
   133 	//longjmp(*(jump_env()), 1);
   176 }
   134 }
   177 
   135 
   178 -(void) applicationWillResignActive:(UIApplication*)application {
   136 -(void) applicationWillResignActive:(UIApplication*)application {
   179 //	NSLog(@"%@", NSStringFromSelector(_cmd));
   137 	//NSLog(@"%@", NSStringFromSelector(_cmd));
   180 	SDL_SendWindowEvent(self.window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   138 	SDL_SendWindowEvent(self.window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   181 }
   139 }
   182 
   140 
   183 -(void) applicationDidBecomeActive:(UIApplication*)application {
   141 -(void) applicationDidBecomeActive:(UIApplication*)application {
   184 //	NSLog(@"%@", NSStringFromSelector(_cmd));
   142 	//NSLog(@"%@", NSStringFromSelector(_cmd));
   185 	SDL_SendWindowEvent(self.window, SDL_WINDOWEVENT_RESTORED, 0, 0);
   143 	SDL_SendWindowEvent(self.window, SDL_WINDOWEVENT_RESTORED, 0, 0);
   186 }
   144 }
   187 
   145 
   188 @end
   146 @end