author | koda |
Wed, 02 Jun 2010 13:52:23 +0000 | |
changeset 3490 | 016b3172b645 |
parent 3487 | b1d00f1950c8 |
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 |
||
23 |
#import "SDL_uikitappdelegate.h" |
|
24 |
#import "SDL_uikitopenglview.h" |
|
3463 | 25 |
#import "SDL_uikitwindow.h" |
2688 | 26 |
#import "SDL_events_c.h" |
3463 | 27 |
#import "../SDL_sysvideo.h" |
2688 | 28 |
#import "jumphack.h" |
29 |
#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
|
30 |
#import "GameSetup.h" |
2698 | 31 |
#import "PascalImports.h" |
2772 | 32 |
#import "MainMenuViewController.h" |
3116
97dc65a47b15
branch ipad/iphone files to keep compatibility between versions
koda
parents:
3063
diff
changeset
|
33 |
#import "OverlayViewController.h" |
3374
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3365
diff
changeset
|
34 |
#import "CommodityFunctions.h" |
2688 | 35 |
|
36 |
#ifdef main |
|
37 |
#undef main |
|
38 |
#endif |
|
39 |
||
3316 | 40 |
#define VALGRIND "/opt/valgrind/bin/valgrind" |
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[]) { |
3316 | 43 |
#ifdef VALGRIND_REXEC |
44 |
// Using the valgrind build config, rexec ourself in valgrind |
|
45 |
// from http://landonf.bikemonkey.org/code/iphone/iPhone_Simulator_Valgrind.20081224.html |
|
3490 | 46 |
if (argc < 2 || (argc >= 2 && strcmp(argv[1], "-valgrind") != 0)) |
47 |
execl(VALGRIND, VALGRIND, "--leak-check=full", argv[0], "-valgrind", NULL); |
|
3316 | 48 |
#endif |
49 |
||
3027 | 50 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
2723 | 51 |
int retVal = UIApplicationMain(argc, argv, nil, @"SDLUIKitDelegate"); |
52 |
[pool release]; |
|
53 |
return retVal; |
|
2688 | 54 |
} |
55 |
||
56 |
@implementation SDLUIKitDelegate |
|
3487 | 57 |
@synthesize uiwindow, window; |
2688 | 58 |
|
2803
1f446fc5c8ec
allow to compile engine as library with HWLIBRARY symbol
koda
parents:
2772
diff
changeset
|
59 |
// convenience method |
2688 | 60 |
+(SDLUIKitDelegate *)sharedAppDelegate { |
2803
1f446fc5c8ec
allow to compile engine as library with HWLIBRARY symbol
koda
parents:
2772
diff
changeset
|
61 |
// the delegate is set in UIApplicationMain(), which is guaranteed to be called before this method |
2688 | 62 |
return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate]; |
63 |
} |
|
64 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
65 |
-(id) init { |
3122
e005359efc59
hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents:
3116
diff
changeset
|
66 |
if (self = [super init]){ |
3385 | 67 |
mainViewController = nil; |
3122
e005359efc59
hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents:
3116
diff
changeset
|
68 |
isInGame = NO; |
3487 | 69 |
self.uiwindow = nil; |
70 |
self.window = NULL; |
|
3463 | 71 |
} |
72 |
return self; |
|
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
73 |
} |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2702
diff
changeset
|
74 |
|
2696 | 75 |
-(void) dealloc { |
3487 | 76 |
SDL_DestroyWindow(self.window); |
77 |
[uiwindow release]; |
|
3385 | 78 |
[mainViewController release]; |
2696 | 79 |
[super dealloc]; |
80 |
} |
|
81 |
||
3029
67483e87590c
a couple of smaller cleanups that didn't get in previous commit
koda
parents:
3027
diff
changeset
|
82 |
// main routine for calling the actual game engine |
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
|
83 |
-(IBAction) startSDLgame { |
3463 | 84 |
[UIView beginAnimations:@"removing main controller" context:NULL]; |
85 |
[UIView setAnimationDuration:1]; |
|
86 |
mainViewController.view.alpha = 0; |
|
87 |
[UIView commitAnimations]; |
|
3029
67483e87590c
a couple of smaller cleanups that didn't get in previous commit
koda
parents:
3027
diff
changeset
|
88 |
|
67483e87590c
a couple of smaller cleanups that didn't get in previous commit
koda
parents:
3027
diff
changeset
|
89 |
// pull out useful configuration info from various files |
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 |
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
|
91 |
[setup startThread:@"engineProtocol"]; |
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 |
const char **gameArgs = [setup getSettings]; |
2772 | 93 |
[setup release]; |
3463 | 94 |
|
95 |
// since the sdlwindow is not yet created, we add the overlayController with a delay |
|
3487 | 96 |
[self performSelector:@selector(displayOverlayLater) withObject:nil afterDelay:1]; |
3317
198ec44b6d92
minor tweaks, icon for ipad, merged overlayviewcontroller, pop viewcontroller when selected a hat
koda
parents:
3316
diff
changeset
|
97 |
|
3463 | 98 |
// this is the pascal fuction that starts the game (wrapped around isInGame) |
3063 | 99 |
isInGame = YES; |
3463 | 100 |
Game(gameArgs); |
3063 | 101 |
isInGame = NO; |
3006 | 102 |
|
3029
67483e87590c
a couple of smaller cleanups that didn't get in previous commit
koda
parents:
3027
diff
changeset
|
103 |
free(gameArgs); |
3006 | 104 |
|
3463 | 105 |
[UIView beginAnimations:@"inserting main controller" context:NULL]; |
106 |
[UIView setAnimationDuration:1]; |
|
107 |
mainViewController.view.alpha = 1; |
|
108 |
[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
|
109 |
} |
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 |
|
3479 | 111 |
-(void) displayOverlayLater { |
3463 | 112 |
// overlay with controls, become visible after 4 seconds, with a transparency effect |
113 |
OverlayViewController *overlayController = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil]; |
|
114 |
||
115 |
[[[UIApplication sharedApplication] keyWindow] addSubview:overlayController.view]; |
|
116 |
[overlayController release]; |
|
117 |
} |
|
118 |
||
119 |
// override the direct execution of SDL_main to allow us to implement the frontend (or even using a nib) |
|
2688 | 120 |
-(void) applicationDidFinishLaunching:(UIApplication *)application { |
3270 | 121 |
[application setStatusBarHidden:YES]; |
3116
97dc65a47b15
branch ipad/iphone files to keep compatibility between versions
koda
parents:
3063
diff
changeset
|
122 |
[application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]; |
3361 | 123 |
|
3487 | 124 |
uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; |
3463 | 125 |
uiwindow.backgroundColor = [UIColor blackColor]; |
3487 | 126 |
// needed to keep the app running after a game (gets released in sdl_uikitwindow) |
127 |
[uiwindow retain]; |
|
128 |
||
3305 | 129 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) |
3385 | 130 |
mainViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPad" bundle:nil]; |
3305 | 131 |
else |
3385 | 132 |
mainViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPhone" bundle:nil]; |
3463 | 133 |
|
3385 | 134 |
[uiwindow addSubview:mainViewController.view]; |
3463 | 135 |
[uiwindow makeKeyAndVisible]; |
136 |
||
2803
1f446fc5c8ec
allow to compile engine as library with HWLIBRARY symbol
koda
parents:
2772
diff
changeset
|
137 |
// Set working directory to resource path |
3365
37ac593e9027
wow all these files only for land preview and seed generation
koda
parents:
3361
diff
changeset
|
138 |
[[NSFileManager defaultManager] changeCurrentDirectoryPath:[[NSBundle mainBundle] resourcePath]]; |
2688 | 139 |
} |
140 |
||
141 |
-(void) applicationWillTerminate:(UIApplication *)application { |
|
142 |
SDL_SendQuit(); |
|
3063 | 143 |
if (isInGame) { |
144 |
HW_terminate(YES); |
|
3250
d5cd1a617123
intial support for team configuration on the ifrontend
koda
parents:
3199
diff
changeset
|
145 |
// hack to prevent automatic termination. See SDL_uikitevents.m for details |
3063 | 146 |
longjmp(*(jump_env()), 1); |
147 |
} |
|
2688 | 148 |
} |
149 |
||
3029
67483e87590c
a couple of smaller cleanups that didn't get in previous commit
koda
parents:
3027
diff
changeset
|
150 |
-(void) applicationWillResignActive:(UIApplication *)application { |
3335 | 151 |
//NSLog(@"%@", NSStringFromSelector(_cmd)); |
3463 | 152 |
if (isInGame) { |
153 |
HW_pause(); |
|
154 |
||
3487 | 155 |
/* |
3463 | 156 |
// Send every window on every screen a MINIMIZED event. |
157 |
SDL_VideoDevice *_this = SDL_GetVideoDevice(); |
|
158 |
if (!_this) |
|
159 |
return; |
|
160 |
||
161 |
int i; |
|
162 |
for (i = 0; i < _this->num_displays; i++) { |
|
163 |
const SDL_VideoDisplay *display = &_this->displays[i]; |
|
164 |
SDL_Window *window; |
|
165 |
for (window = display->windows; window != nil; window = window->next) |
|
166 |
SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0); |
|
167 |
} |
|
3487 | 168 |
*/ |
3463 | 169 |
} |
2688 | 170 |
} |
171 |
||
3029
67483e87590c
a couple of smaller cleanups that didn't get in previous commit
koda
parents:
3027
diff
changeset
|
172 |
-(void) applicationDidBecomeActive:(UIApplication *)application { |
3335 | 173 |
//NSLog(@"%@", NSStringFromSelector(_cmd)); |
3463 | 174 |
if (isInGame) { |
175 |
HW_pause(); |
|
176 |
||
3487 | 177 |
/* |
3463 | 178 |
// Send every window on every screen a RESTORED event. |
179 |
SDL_VideoDevice *_this = SDL_GetVideoDevice(); |
|
180 |
if (!_this) |
|
181 |
return; |
|
182 |
||
183 |
int i; |
|
184 |
for (i = 0; i < _this->num_displays; i++) { |
|
185 |
const SDL_VideoDisplay *display = &_this->displays[i]; |
|
186 |
SDL_Window *window; |
|
187 |
for (window = display->windows; window != nil; window = window->next) |
|
188 |
SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0); |
|
189 |
} |
|
3487 | 190 |
*/ |
3463 | 191 |
} |
2688 | 192 |
} |
193 |
||
194 |
@end |