author | smxx |
Wed, 10 Feb 2010 17:53:21 +0000 | |
changeset 2797 | 98b990feec7d |
parent 2772 | 1965eba10be6 |
child 2803 | 1f446fc5c8ec |
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" |
2698 | 30 |
#import "PascalImports.h" |
2772 | 31 |
#import "MainMenuViewController.h" |
2688 | 32 |
|
2696 | 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 |
||
2688 | 38 |
#ifdef main |
39 |
#undef main |
|
40 |
#endif |
|
41 |
||
2702
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
42 |
int main (int argc, char *argv[]) { |
2723 | 43 |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
44 |
int retVal = UIApplicationMain(argc, argv, nil, @"SDLUIKitDelegate"); |
|
45 |
[pool release]; |
|
46 |
return retVal; |
|
2688 | 47 |
} |
48 |
||
49 |
@implementation SDLUIKitDelegate |
|
50 |
||
2738
bfccb2ec4334
new graphics from Tiy and frontend is loaded in landscape mode
koda
parents:
2723
diff
changeset
|
51 |
@synthesize uiwindow, window, viewController; |
2688 | 52 |
|
53 |
/* convenience method */ |
|
54 |
+(SDLUIKitDelegate *)sharedAppDelegate { |
|
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
55 |
/* the delegate is set in UIApplicationMain(), which is guaranteed to be called before this method */ |
2688 | 56 |
return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate]; |
57 |
} |
|
58 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
59 |
-(id) init { |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
60 |
self = [super init]; |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
61 |
self.uiwindow = nil; |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
62 |
self.window = NULL; |
2738
bfccb2ec4334
new graphics from Tiy and frontend is loaded in landscape mode
koda
parents:
2723
diff
changeset
|
63 |
self.viewController = nil; |
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
64 |
return self; |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
65 |
} |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
66 |
|
2696 | 67 |
-(void) dealloc { |
2738
bfccb2ec4334
new graphics from Tiy and frontend is loaded in landscape mode
koda
parents:
2723
diff
changeset
|
68 |
[viewController release]; |
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
69 |
[uiwindow release]; |
2696 | 70 |
[super dealloc]; |
71 |
} |
|
72 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
73 |
#pragma mark - |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
74 |
#pragma mark Custom stuff |
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
|
75 |
-(IBAction) startSDLgame { |
2702
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
76 |
NSAutoreleasePool *internal_pool = [[NSAutoreleasePool alloc] init]; |
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
77 |
|
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
78 |
GameSetup *setup = [[GameSetup 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
|
79 |
[setup startThread:@"engineProtocol"]; |
2696 | 80 |
|
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
81 |
// remove the current view to free resources |
2698 | 82 |
[UIView beginAnimations:@"removing main controller" context:NULL]; |
83 |
[UIView setAnimationDuration:1]; |
|
2743 | 84 |
self.viewController.mainView.alpha = 0; |
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
85 |
[UIView commitAnimations]; |
2698 | 86 |
|
2691 | 87 |
NSLog(@"Game is launching..."); |
2702
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
88 |
const char **gameArgs = [setup getSettings]; |
2772 | 89 |
[setup release]; |
2702
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
90 |
|
2772 | 91 |
// this is the pascal fuction that starts the game |
2702
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
92 |
Game(gameArgs); |
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
93 |
|
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
94 |
free(gameArgs); |
2698 | 95 |
NSLog(@"Game is exting..."); |
96 |
||
2740 | 97 |
[uiwindow addSubview: viewController.view]; |
98 |
[uiwindow makeKeyAndVisible]; |
|
99 |
||
2698 | 100 |
[UIView beginAnimations:@"inserting main controller" context:NULL]; |
101 |
[UIView setAnimationDuration:1]; |
|
2743 | 102 |
self.viewController.mainView.alpha = 1; |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2698
diff
changeset
|
103 |
[UIView commitAnimations]; |
2702
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
104 |
|
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
105 |
[internal_pool release]; |
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
106 |
} |
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
107 |
|
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
108 |
-(BOOL) checkFirstRun { |
2772 | 109 |
//move all this inside the MainMenuViewController |
2702
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
110 |
BOOL isFirstRun = NO; |
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
111 |
|
2723 | 112 |
//use a nssthread a ask the user to wait |
113 |
||
2702
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
114 |
NSString *filePath = [self dataFilePath:@"settings.plist"]; |
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
115 |
if (!([[NSFileManager defaultManager] fileExistsAtPath:filePath])) { |
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
116 |
isFirstRun = YES; |
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
117 |
// file not present, let's create it |
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
118 |
NSMutableDictionary *saveDict = [[NSMutableDictionary alloc] init]; |
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
119 |
|
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
120 |
[saveDict setObject:@"" forKey:@"username"]; |
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
121 |
[saveDict setObject:@"" forKey:@"password"]; |
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
122 |
[saveDict setObject:@"1" forKey:@"music"]; |
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
123 |
[saveDict setObject:@"1" forKey:@"sounds"]; |
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
124 |
[saveDict setObject:@"0" forKey:@"alternate"]; |
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
125 |
|
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
126 |
[saveDict writeToFile:filePath atomically:YES]; |
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
127 |
[saveDict release]; |
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
128 |
} |
48fc46a922fd
rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents:
2701
diff
changeset
|
129 |
return isFirstRun; |
2688 | 130 |
} |
131 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
132 |
-(NSString *)dataFilePath: (NSString *)fileName { |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
133 |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
134 |
NSString *documentsDirectory = [paths objectAtIndex:0]; |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
135 |
return [documentsDirectory stringByAppendingPathComponent:fileName]; |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
136 |
} |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
137 |
|
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
138 |
-(void) applicationDidReceiveMemoryWarning:(UIApplication *)application { |
2723 | 139 |
/* we get too many ones on ipod touch |
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
140 |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Running low on memory" |
2720 | 141 |
message:@"I will try to free some memory but game might slow down a little" |
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
142 |
delegate:nil |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
143 |
cancelButtonTitle:@"Ok" |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
144 |
otherButtonTitles:nil ]; |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
145 |
[alert show]; |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
146 |
[alert release]; |
2723 | 147 |
*/ |
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
148 |
} |
2720 | 149 |
|
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
150 |
#pragma mark - |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
151 |
#pragma mark SDLUIKitDelegate methods |
2688 | 152 |
// override the direct execution of SDL_main to allow us to implement the frontend (even using a nib) |
153 |
-(void) applicationDidFinishLaunching:(UIApplication *)application { |
|
154 |
[application setStatusBarHidden:YES animated:NO]; |
|
2738
bfccb2ec4334
new graphics from Tiy and frontend is loaded in landscape mode
koda
parents:
2723
diff
changeset
|
155 |
[application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]; |
2740 | 156 |
|
157 |
self.uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; |
|
158 |
self.uiwindow.backgroundColor = [UIColor blackColor]; |
|
2738
bfccb2ec4334
new graphics from Tiy and frontend is loaded in landscape mode
koda
parents:
2723
diff
changeset
|
159 |
|
2740 | 160 |
self.viewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController" bundle:nil]; |
2720 | 161 |
|
2688 | 162 |
/* Set working directory to resource path */ |
163 |
[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]]; |
|
2696 | 164 |
|
2738
bfccb2ec4334
new graphics from Tiy and frontend is loaded in landscape mode
koda
parents:
2723
diff
changeset
|
165 |
[uiwindow addSubview:viewController.view]; |
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
166 |
[uiwindow makeKeyAndVisible]; |
2740 | 167 |
[uiwindow layoutSubviews]; |
168 |
[self checkFirstRun]; |
|
2688 | 169 |
} |
170 |
||
171 |
-(void) applicationWillTerminate:(UIApplication *)application { |
|
172 |
SDL_SendQuit(); |
|
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
173 |
/* hack to prevent automatic termination. See SDL_uikitevents.m for details */ |
2688 | 174 |
// have to remove this otherwise game goes on when pushing the home button |
175 |
//longjmp(*(jump_env()), 1); |
|
176 |
} |
|
177 |
||
2698 | 178 |
-(void) applicationWillResignActive:(UIApplication*)application { |
2688 | 179 |
// NSLog(@"%@", NSStringFromSelector(_cmd)); |
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
180 |
SDL_SendWindowEvent(self.window, SDL_WINDOWEVENT_MINIMIZED, 0, 0); |
2688 | 181 |
} |
182 |
||
2698 | 183 |
-(void) applicationDidBecomeActive:(UIApplication*)application { |
2688 | 184 |
// NSLog(@"%@", NSStringFromSelector(_cmd)); |
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
185 |
SDL_SendWindowEvent(self.window, SDL_WINDOWEVENT_RESTORED, 0, 0); |
2688 | 186 |
} |
187 |
||
188 |
@end |