cocoaTouch/SDLOverrides/SDL_uikitappdelegate.m
author koda
Mon, 18 Jan 2010 05:36:08 +0000
changeset 2699 249adefa9c1c
parent 2698 90585aba87ad
child 2701 3a8560c00f78
permissions -rw-r--r--
replace initialization/finalization statements with custom init functions
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     1
/*
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     2
 SDL - Simple DirectMedia Layer
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     3
 Copyright (C) 1997-2009 Sam Lantinga
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     4
 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     5
 This library is free software; you can redistribute it and/or
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     6
 modify it under the terms of the GNU Lesser General Public
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     7
 License as published by the Free Software Foundation; either
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     8
 version 2.1 of the License, or (at your option) any later version.
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     9
 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    10
 This library is distributed in the hope that it will be useful,
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    13
 Lesser General Public License for more details.
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    14
 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    15
 You should have received a copy of the GNU Lesser General Public
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    16
 License along with this library; if not, write to the Free Software
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    17
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    18
 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    19
 Sam Lantinga, mods for Hedgewars by Vittorio Giovara
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    20
 slouken@libsdl.org, vittorio.giovara@gmail.com
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    21
*/
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    22
2691
c0da3a98c01c initial support for engine protocol
koda
parents: 2689
diff changeset
    23
#import <pthread.h>
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    24
#import "SDL_uikitappdelegate.h"
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    25
#import "SDL_uikitopenglview.h"
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    26
#import "SDL_events_c.h"
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    27
#import "jumphack.h"
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    28
#import "SDL_video.h"
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents: 2692
diff changeset
    29
#import "GameSetup.h"
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    30
#import "PascalImports.h"
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    31
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    32
//#import "SoundEffect.h"	
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    33
//	SoundEffect *erasingSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Erase" ofType:@"caf"]];
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    34
//	SoundEffect *selectSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Select" ofType:@"caf"]];
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    35
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    36
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    37
#ifdef main
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    38
#undef main
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    39
#endif
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    40
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    41
int main(int argc, char *argv[]) {
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    42
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    43
    int retVal = UIApplicationMain(argc, argv, nil, @"SDLUIKitDelegate");
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    44
    [pool release];
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    45
    return retVal;
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    46
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    47
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    48
@implementation SDLUIKitDelegate
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    49
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents: 2692
diff changeset
    50
@synthesize window, windowID, controller, setup;
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    51
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    52
/* convenience method */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    53
+(SDLUIKitDelegate *)sharedAppDelegate {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    54
	/* the delegate is set in UIApplicationMain(), which is garaunteed to be called before this method */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    55
	return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    56
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    57
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    58
-(void) dealloc {
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    59
	[setup release];
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    60
	[controller release];
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    61
	[window release];
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    62
	[super dealloc];
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    63
}
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    64
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents: 2692
diff changeset
    65
-(IBAction) startSDLgame {
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents: 2692
diff changeset
    66
	
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents: 2692
diff changeset
    67
	[setup startThread:@"engineProtocol"];
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    68
	[setup loadSettingsFromFile:@"settings.plist" forKey:@"systemSettings"];
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
    69
2692
ce9992075118 better network support + initial work for returning to frontend
koda
parents: 2691
diff changeset
    70
	// remove the current view to free resources
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    71
	[UIView beginAnimations:@"removing main controller" context:NULL];
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    72
	[UIView setAnimationDuration:1];
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    73
	controller.view.alpha = 0;
2692
ce9992075118 better network support + initial work for returning to frontend
koda
parents: 2691
diff changeset
    74
	[UIView commitAnimations];
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    75
	[controller.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1];
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    76
2691
c0da3a98c01c initial support for engine protocol
koda
parents: 2689
diff changeset
    77
	NSLog(@"Game is launching...");
2692
ce9992075118 better network support + initial work for returning to frontend
koda
parents: 2691
diff changeset
    78
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    79
	// direct execution or thread? check the one that gives most fps
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    80
	// library or call SDL_main? pascal quits at the end of the main
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    81
	Game();
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents: 2692
diff changeset
    82
	
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    83
	NSLog(@"Game is exting...");
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    84
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    85
	[[window viewWithTag:54867] removeFromSuperview];
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    86
	[setup unloadSettings];
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    87
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    88
	[window addSubview:controller.view];
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    89
	[window makeKeyAndVisible];
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    90
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    91
	[UIView beginAnimations:@"inserting main controller" context:NULL];
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    92
	[UIView setAnimationDuration:1];
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    93
	controller.view.alpha = 1;
2699
249adefa9c1c replace initialization/finalization statements with custom init functions
koda
parents: 2698
diff changeset
    94
	[UIView commitAnimations];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    95
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    96
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    97
// override the direct execution of SDL_main to allow us to implement the frontend (even using a nib)
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    98
-(void) applicationDidFinishLaunching:(UIApplication *)application {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    99
	[application setStatusBarHidden:YES animated:NO];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   100
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
   101
	setup = [[GameSetup alloc] init];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   102
	/* Set working directory to resource path */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   103
	[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
   104
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   105
	[window addSubview:controller.view];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   106
	[window makeKeyAndVisible];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   107
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   108
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   109
-(void) applicationWillTerminate:(UIApplication *)application {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   110
	SDL_SendQuit();
2692
ce9992075118 better network support + initial work for returning to frontend
koda
parents: 2691
diff changeset
   111
	/* hack to prevent automatic termination.  See SDL_uikitevents.m for details */
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   112
	// have to remove this otherwise game goes on when pushing the home button
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   113
	//longjmp(*(jump_env()), 1);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   114
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   115
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   116
-(void) applicationWillResignActive:(UIApplication*)application {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   117
//	NSLog(@"%@", NSStringFromSelector(_cmd));
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   118
	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   119
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   120
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   121
-(void) applicationDidBecomeActive:(UIApplication*)application {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   122
//	NSLog(@"%@", NSStringFromSelector(_cmd));
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   123
	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_RESTORED, 0, 0);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   124
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   125
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   126
/*
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   127
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   128
	NSLog(@"Rotating...");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   129
	return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   130
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   131
*/
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents: 2692
diff changeset
   132
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   133
-(NSString *)dataFilePath: (NSString *)fileName {
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
   134
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
   135
	NSString *documentsDirectory = [paths objectAtIndex:0];
41aa7b56c17b settings are applied to game launch
koda
parents: 2693
diff changeset
   136
	return [documentsDirectory stringByAppendingPathComponent:fileName];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   137
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   138
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   139
@end