author | smxx |
Wed, 17 Mar 2010 22:56:23 +0000 | |
changeset 3013 | dfc1f16aeb53 |
parent 3006 | da6023c2745b |
child 3025 | 01682ec58eb0 |
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" |
3006 | 32 |
#import "overlayViewController.h" |
2688 | 33 |
|
34 |
#ifdef main |
|
35 |
#undef main |
|
36 |
#endif |
|
37 |
||
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
|
38 |
int main (int argc, char *argv[]) { |
2723 | 39 |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
40 |
int retVal = UIApplicationMain(argc, argv, nil, @"SDLUIKitDelegate"); |
|
41 |
[pool release]; |
|
42 |
return retVal; |
|
2688 | 43 |
} |
44 |
||
45 |
@implementation SDLUIKitDelegate |
|
46 |
||
3006 | 47 |
@synthesize uiwindow, window, viewController, overlayController; |
2688 | 48 |
|
2803
1f446fc5c8ec
allow to compile engine as library with HWLIBRARY symbol
koda
parents:
2772
diff
changeset
|
49 |
// convenience method |
2688 | 50 |
+(SDLUIKitDelegate *)sharedAppDelegate { |
2803
1f446fc5c8ec
allow to compile engine as library with HWLIBRARY symbol
koda
parents:
2772
diff
changeset
|
51 |
// the delegate is set in UIApplicationMain(), which is guaranteed to be called before this method |
2688 | 52 |
return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate]; |
53 |
} |
|
54 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
55 |
-(id) init { |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
56 |
self = [super init]; |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
57 |
self.uiwindow = nil; |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
58 |
self.window = NULL; |
2738
bfccb2ec4334
new graphics from Tiy and frontend is loaded in landscape mode
koda
parents:
2723
diff
changeset
|
59 |
self.viewController = nil; |
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
60 |
return self; |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
61 |
} |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
62 |
|
2696 | 63 |
-(void) dealloc { |
2738
bfccb2ec4334
new graphics from Tiy and frontend is loaded in landscape mode
koda
parents:
2723
diff
changeset
|
64 |
[viewController release]; |
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
65 |
[uiwindow release]; |
2696 | 66 |
[super dealloc]; |
67 |
} |
|
68 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
69 |
#pragma mark - |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
70 |
#pragma mark Custom stuff |
3006 | 71 |
-(void) showMenuAfterwards { |
72 |
// draws the controller overlay after the sdl window has taken control |
|
73 |
[uiwindow bringSubviewToFront:overlayController.view]; |
|
74 |
||
75 |
[UIView beginAnimations:@"showing overlay" context:NULL]; |
|
76 |
[UIView setAnimationDuration:1]; |
|
77 |
self. overlayController.view.alpha = 1; |
|
78 |
[UIView commitAnimations]; |
|
79 |
} |
|
80 |
||
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
|
81 |
-(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
|
82 |
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
|
83 |
|
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
|
84 |
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
|
85 |
[setup startThread:@"engineProtocol"]; |
2696 | 86 |
|
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
87 |
// remove the current view to free resources |
2698 | 88 |
[UIView beginAnimations:@"removing main controller" context:NULL]; |
89 |
[UIView setAnimationDuration:1]; |
|
2743 | 90 |
self.viewController.mainView.alpha = 0; |
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
91 |
[UIView commitAnimations]; |
2698 | 92 |
|
3006 | 93 |
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
|
94 |
const char **gameArgs = [setup getSettings]; |
2772 | 95 |
[setup release]; |
3006 | 96 |
|
97 |
// overlay with controls |
|
98 |
overlayController = [[overlayViewController alloc] initWithNibName:@"overlayViewController" bundle:nil]; |
|
99 |
overlayController.view.alpha = 0; |
|
100 |
[uiwindow addSubview: overlayController.view]; |
|
101 |
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showMenuAfterwards) userInfo:nil repeats:NO]; |
|
102 |
||
103 |
Game(gameArgs); // this is the pascal fuction that starts the game |
|
104 |
||
105 |
// let's clean memory |
|
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
|
106 |
free(gameArgs); |
3006 | 107 |
[overlayController.view removeFromSuperview]; |
108 |
[overlayController release]; |
|
109 |
||
110 |
NSLog(@"...Game is exting..."); |
|
2698 | 111 |
|
2740 | 112 |
[uiwindow addSubview: viewController.view]; |
113 |
[uiwindow makeKeyAndVisible]; |
|
114 |
||
2698 | 115 |
[UIView beginAnimations:@"inserting main controller" context:NULL]; |
116 |
[UIView setAnimationDuration:1]; |
|
2743 | 117 |
self.viewController.mainView.alpha = 1; |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2698
diff
changeset
|
118 |
[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
|
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 |
[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
|
121 |
} |
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 |
|
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
123 |
-(NSString *)dataFilePath: (NSString *)fileName { |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
124 |
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
|
125 |
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
|
126 |
return [documentsDirectory stringByAppendingPathComponent:fileName]; |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
127 |
} |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
128 |
|
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
129 |
#pragma mark - |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
130 |
#pragma mark SDLUIKitDelegate methods |
2688 | 131 |
// override the direct execution of SDL_main to allow us to implement the frontend (even using a nib) |
132 |
-(void) applicationDidFinishLaunching:(UIApplication *)application { |
|
133 |
[application setStatusBarHidden:YES animated:NO]; |
|
2738
bfccb2ec4334
new graphics from Tiy and frontend is loaded in landscape mode
koda
parents:
2723
diff
changeset
|
134 |
[application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]; |
2740 | 135 |
|
136 |
self.uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; |
|
137 |
self.uiwindow.backgroundColor = [UIColor blackColor]; |
|
2738
bfccb2ec4334
new graphics from Tiy and frontend is loaded in landscape mode
koda
parents:
2723
diff
changeset
|
138 |
|
2740 | 139 |
self.viewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController" bundle:nil]; |
2720 | 140 |
|
2803
1f446fc5c8ec
allow to compile engine as library with HWLIBRARY symbol
koda
parents:
2772
diff
changeset
|
141 |
// Set working directory to resource path |
2688 | 142 |
[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]]; |
2696 | 143 |
|
2738
bfccb2ec4334
new graphics from Tiy and frontend is loaded in landscape mode
koda
parents:
2723
diff
changeset
|
144 |
[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
|
145 |
[uiwindow makeKeyAndVisible]; |
2740 | 146 |
[uiwindow layoutSubviews]; |
2688 | 147 |
} |
148 |
||
149 |
-(void) applicationWillTerminate:(UIApplication *)application { |
|
150 |
SDL_SendQuit(); |
|
2803
1f446fc5c8ec
allow to compile engine as library with HWLIBRARY symbol
koda
parents:
2772
diff
changeset
|
151 |
// hack to prevent automatic termination. See SDL_uikitevents.m for details |
2688 | 152 |
// have to remove this otherwise game goes on when pushing the home button |
153 |
//longjmp(*(jump_env()), 1); |
|
154 |
} |
|
155 |
||
2698 | 156 |
-(void) applicationWillResignActive:(UIApplication*)application { |
2803
1f446fc5c8ec
allow to compile engine as library with HWLIBRARY symbol
koda
parents:
2772
diff
changeset
|
157 |
//NSLog(@"%@", NSStringFromSelector(_cmd)); |
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
158 |
SDL_SendWindowEvent(self.window, SDL_WINDOWEVENT_MINIMIZED, 0, 0); |
2688 | 159 |
} |
160 |
||
2698 | 161 |
-(void) applicationDidBecomeActive:(UIApplication*)application { |
2803
1f446fc5c8ec
allow to compile engine as library with HWLIBRARY symbol
koda
parents:
2772
diff
changeset
|
162 |
//NSLog(@"%@", NSStringFromSelector(_cmd)); |
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
163 |
SDL_SendWindowEvent(self.window, SDL_WINDOWEVENT_RESTORED, 0, 0); |
2688 | 164 |
} |
165 |
||
166 |
@end |