author | koda |
Sat, 16 Jan 2010 06:48:56 +0000 | |
changeset 2697 | 75880595a9f1 |
parent 2696 | 41aa7b56c17b |
child 2698 | 90585aba87ad |
permissions | -rw-r--r-- |
2688 | 1 |
/* |
2 |
SDL - Simple DirectMedia Layer |
|
3 |
Copyright (C) 1997-2009 Sam Lantinga |
|
4 |
||
5 |
This library is free software; you can redistribute it and/or |
|
6 |
modify it under the terms of the GNU Lesser General Public |
|
7 |
License as published by the Free Software Foundation; either |
|
8 |
version 2.1 of the License, or (at your option) any later version. |
|
9 |
||
10 |
This library is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
Lesser General Public License for more details. |
|
14 |
||
15 |
You should have received a copy of the GNU Lesser General Public |
|
16 |
License along with this library; if not, write to the Free Software |
|
17 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
18 |
||
19 |
Sam Lantinga, mods for Hedgewars by Vittorio Giovara |
|
20 |
slouken@libsdl.org, vittorio.giovara@gmail.com |
|
21 |
*/ |
|
22 |
||
2691 | 23 |
#import <pthread.h> |
2688 | 24 |
#import "SDL_uikitappdelegate.h" |
25 |
#import "SDL_uikitopenglview.h" |
|
26 |
#import "SDL_events_c.h" |
|
27 |
#import "jumphack.h" |
|
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" |
2688 | 30 |
|
2696 | 31 |
//#import "SoundEffect.h" |
32 |
// SoundEffect *erasingSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Erase" ofType:@"caf"]]; |
|
33 |
// SoundEffect *selectSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Select" ofType:@"caf"]]; |
|
34 |
||
35 |
||
2688 | 36 |
#ifdef main |
37 |
#undef main |
|
38 |
#endif |
|
39 |
||
40 |
extern int SDL_main(int argc, char *argv[]); |
|
41 |
int main (int argc, char **argv) { |
|
42 |
int i; |
|
43 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|
44 |
||
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 |
} |
|
58 |
||
59 |
@implementation SDLUIKitDelegate |
|
60 |
||
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
|
61 |
@synthesize window, windowID, controller, setup; |
2688 | 62 |
|
63 |
/* convenience method */ |
|
64 |
+(SDLUIKitDelegate *)sharedAppDelegate { |
|
65 |
/* the delegate is set in UIApplicationMain(), which is garaunteed to be called before this method */ |
|
66 |
return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate]; |
|
67 |
} |
|
68 |
||
2696 | 69 |
-(void) dealloc { |
70 |
[setup release]; |
|
71 |
[controller release]; |
|
72 |
[window release]; |
|
73 |
[super dealloc]; |
|
74 |
} |
|
75 |
||
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
|
76 |
-(void) launchSDL_main{ |
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
77 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
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
|
78 |
|
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
|
79 |
// must setup arguments in the same thread |
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
|
80 |
[setup setArgsForLocalPlay]; |
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
|
81 |
|
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 |
// run the user's application, passing argc and argv |
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
83 |
SDL_main(forward_argc, forward_argv); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
84 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
85 |
[pool release]; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
86 |
} |
2691 | 87 |
|
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
|
88 |
-(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
|
89 |
|
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
|
90 |
[setup startThread:@"engineProtocol"]; |
2696 | 91 |
[setup loadSettingsFromFile:@"settings.plist" forKey:@"systemSettings"]; |
92 |
||
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
93 |
// remove the current view to free resources |
2697 | 94 |
/* [UIView beginAnimations:nil context:NULL]; |
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
95 |
[UIView setAnimationDuration:1.5]; |
2697 | 96 |
controller.view.alpha = 1; |
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
97 |
[UIView commitAnimations]; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
98 |
[controller.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.5]; |
2697 | 99 |
*/ |
2691 | 100 |
NSLog(@"Game is launching..."); |
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
101 |
|
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
|
102 |
[NSThread detachNewThreadSelector:@selector(launchSDL_main) toTarget:self withObject:nil]; |
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
|
103 |
|
2688 | 104 |
} |
105 |
||
106 |
// override the direct execution of SDL_main to allow us to implement the frontend (even using a nib) |
|
107 |
-(void) applicationDidFinishLaunching:(UIApplication *)application { |
|
108 |
[application setStatusBarHidden:YES animated:NO]; |
|
109 |
||
2696 | 110 |
setup = [[GameSetup alloc] init]; |
2688 | 111 |
/* Set working directory to resource path */ |
112 |
[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]]; |
|
2696 | 113 |
|
2688 | 114 |
[window addSubview:controller.view]; |
115 |
[window makeKeyAndVisible]; |
|
116 |
} |
|
117 |
||
118 |
-(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(); |
|
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
126 |
/* hack to prevent automatic termination. See SDL_uikitevents.m for details */ |
2688 | 127 |
// have to remove this otherwise game goes on when pushing the home button |
128 |
//longjmp(*(jump_env()), 1); |
|
129 |
} |
|
130 |
||
131 |
-(void) applicationWillResignActive:(UIApplication*)application |
|
132 |
{ |
|
133 |
// NSLog(@"%@", NSStringFromSelector(_cmd)); |
|
134 |
SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_MINIMIZED, 0, 0); |
|
135 |
} |
|
136 |
||
137 |
-(void) applicationDidBecomeActive:(UIApplication*)application |
|
138 |
{ |
|
139 |
// NSLog(@"%@", NSStringFromSelector(_cmd)); |
|
140 |
SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_RESTORED, 0, 0); |
|
141 |
} |
|
142 |
||
143 |
/* |
|
144 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
145 |
NSLog(@"Rotating..."); |
|
146 |
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); |
|
147 |
} |
|
148 |
*/ |
|
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
|
149 |
+(void) resetFrontend { |
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
|
150 |
[[[SDLUIKitDelegate sharedAppDelegate].window viewWithTag:54867] removeFromSuperview]; |
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
|
151 |
[[SDLUIKitDelegate sharedAppDelegate].window addSubview:[SDLUIKitDelegate sharedAppDelegate].controller.view]; |
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
|
152 |
|
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
|
153 |
[UIView beginAnimations:nil context:NULL]; |
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
|
154 |
[UIView setAnimationDuration:1]; |
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
|
155 |
[SDLUIKitDelegate sharedAppDelegate].controller.view.alpha = 1; |
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
|
156 |
[UIView commitAnimations]; |
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
|
157 |
|
2696 | 158 |
[[SDLUIKitDelegate sharedAppDelegate].setup unloadSettings]; |
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
|
159 |
[[SDLUIKitDelegate sharedAppDelegate].window makeKeyAndVisible]; |
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
|
160 |
} |
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
|
161 |
|
2696 | 162 |
#pragma mark - |
163 |
#pragma mark Convenience methods |
|
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
164 |
void IPH_returnFrontend (void) { |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
165 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
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
|
166 |
|
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
|
167 |
[SDLUIKitDelegate resetFrontend]; |
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
168 |
NSLog(@"Game exited..."); |
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
|
169 |
|
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
170 |
[pool release]; |
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
|
171 |
[NSThread exit]; |
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
172 |
} |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
173 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
174 |
|
2696 | 175 |
+(NSString *)dataFilePath: (NSString *)fileName { |
176 |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
|
177 |
NSString *documentsDirectory = [paths objectAtIndex:0]; |
|
178 |
return [documentsDirectory stringByAppendingPathComponent:fileName]; |
|
2688 | 179 |
} |
180 |
||
181 |
@end |