author | koda |
Tue, 12 Jan 2010 07:32:15 +0000 | |
changeset 2692 | ce9992075118 |
parent 2691 | c0da3a98c01c |
child 2693 | 3207e0eacd43 |
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" |
|
2691 | 29 |
#import "gameSetup.h" |
2688 | 30 |
|
31 |
#ifdef main |
|
32 |
#undef main |
|
33 |
#endif |
|
34 |
||
35 |
extern int SDL_main(int argc, char *argv[]); |
|
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
36 |
BOOL isServerRunning = NO; |
2688 | 37 |
|
38 |
int main (int argc, char **argv) { |
|
39 |
int i; |
|
40 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|
41 |
||
42 |
/* store arguments */ |
|
43 |
forward_argc = argc; |
|
44 |
forward_argv = (char **)malloc(argc * sizeof(char *)); |
|
45 |
for (i = 0; i < argc; i++) { |
|
46 |
forward_argv[i] = malloc( (strlen(argv[i])+1) * sizeof(char)); |
|
47 |
strcpy(forward_argv[i], argv[i]); |
|
48 |
} |
|
49 |
||
50 |
/* Give over control to run loop, SDLUIKitDelegate will handle most things from here */ |
|
51 |
UIApplicationMain(argc, argv, NULL, @"SDLUIKitDelegate"); |
|
52 |
||
53 |
[pool release]; |
|
54 |
} |
|
55 |
||
56 |
@implementation SDLUIKitDelegate |
|
57 |
||
58 |
@synthesize window, windowID, controller; |
|
59 |
||
60 |
/* convenience method */ |
|
61 |
+(SDLUIKitDelegate *)sharedAppDelegate { |
|
62 |
/* the delegate is set in UIApplicationMain(), which is garaunteed to be called before this method */ |
|
63 |
return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate]; |
|
64 |
} |
|
65 |
||
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
66 |
void preSDL_main(){ |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
67 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
68 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
69 |
SDL_main(forward_argc, forward_argv); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
70 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
71 |
[pool release]; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
72 |
} |
2691 | 73 |
|
2688 | 74 |
- (void) startSDLgame { |
2691 | 75 |
pthread_t threadID; |
2688 | 76 |
|
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
77 |
if (NO == isServerRunning) { |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
78 |
// don't start another server because the port is already bound |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
79 |
pthread_create (&threadID, NULL, (void *) (*engineProtocolThread), NULL); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
80 |
pthread_detach (threadID); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
81 |
isServerRunning = YES; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
82 |
} |
2691 | 83 |
|
84 |
setupArgsForLocalPlay(); |
|
85 |
||
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
86 |
// remove the current view to free resources |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
87 |
[UIView beginAnimations:nil context:NULL]; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
88 |
[UIView setAnimationDuration:1.5]; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
89 |
controller.view.alpha = 0; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
90 |
[UIView commitAnimations]; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
91 |
[controller.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.5]; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
92 |
//[controller.view removeFromSuperview]; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
93 |
|
2688 | 94 |
/* run the user's application, passing argc and argv */ |
2691 | 95 |
NSLog(@"Game is launching..."); |
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
96 |
/*pthread_create (&threadID, NULL, (void *) (*preSDL_main), NULL); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
97 |
pthread_detach (threadID);*/ |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
98 |
int res = SDL_main(forward_argc, forward_argv); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
99 |
|
2688 | 100 |
// can't reach here yet |
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
101 |
NSLog(@"Game exited with status %d", res); |
2688 | 102 |
|
103 |
//[self performSelector:@selector(makeNewView) withObject:nil afterDelay:0.0]; |
|
104 |
/* exit, passing the return status from the user's application */ |
|
105 |
//exit(exit_status); |
|
106 |
} |
|
107 |
||
108 |
// override the direct execution of SDL_main to allow us to implement the frontend (even using a nib) |
|
109 |
-(void) applicationDidFinishLaunching:(UIApplication *)application { |
|
110 |
[application setStatusBarHidden:YES animated:NO]; |
|
111 |
||
112 |
/* Set working directory to resource path */ |
|
113 |
[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]]; |
|
2689 | 114 |
//#import "SoundEffect.h" |
115 |
// SoundEffect *erasingSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Erase" ofType:@"caf"]]; |
|
116 |
// SoundEffect *selectSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Select" ofType:@"caf"]]; |
|
2688 | 117 |
[window addSubview:controller.view]; |
118 |
[window makeKeyAndVisible]; |
|
119 |
} |
|
120 |
||
121 |
-(void) applicationWillTerminate:(UIApplication *)application { |
|
122 |
/* free the memory we used to hold copies of argc and argv */ |
|
123 |
int i; |
|
124 |
for (i=0; i < forward_argc; i++) { |
|
125 |
free(forward_argv[i]); |
|
126 |
} |
|
127 |
free(forward_argv); |
|
128 |
SDL_SendQuit(); |
|
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
129 |
/* hack to prevent automatic termination. See SDL_uikitevents.m for details */ |
2688 | 130 |
// have to remove this otherwise game goes on when pushing the home button |
131 |
//longjmp(*(jump_env()), 1); |
|
132 |
} |
|
133 |
||
134 |
-(void) applicationWillResignActive:(UIApplication*)application |
|
135 |
{ |
|
136 |
// NSLog(@"%@", NSStringFromSelector(_cmd)); |
|
137 |
SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_MINIMIZED, 0, 0); |
|
138 |
} |
|
139 |
||
140 |
-(void) applicationDidBecomeActive:(UIApplication*)application |
|
141 |
{ |
|
142 |
// NSLog(@"%@", NSStringFromSelector(_cmd)); |
|
143 |
SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_RESTORED, 0, 0); |
|
144 |
} |
|
145 |
||
146 |
/* |
|
147 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
148 |
NSLog(@"Rotating..."); |
|
149 |
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); |
|
150 |
} |
|
151 |
*/ |
|
152 |
||
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
153 |
void IPH_returnFrontend (void) { |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
154 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
155 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
156 |
[[[SDLUIKitDelegate sharedAppDelegate].window viewWithTag:54867] removeFromSuperview]; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
157 |
[[SDLUIKitDelegate sharedAppDelegate].window addSubview:[SDLUIKitDelegate sharedAppDelegate].controller.view]; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
158 |
// [[SDLUIKitDelegate sharedAppDelegate].window makeKeyAndVisible]; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
159 |
NSLog(@"Game exited..."); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
160 |
// pthread_exit(NULL); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
161 |
[pool release]; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
162 |
exit(0); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
163 |
// while(1); //prevent exiting |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
164 |
} |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
165 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
166 |
|
2688 | 167 |
-(void) dealloc { |
168 |
[controller release]; |
|
169 |
[window release]; |
|
170 |
[super dealloc]; |
|
171 |
} |
|
172 |
||
173 |
@end |