author | koda |
Wed, 30 Jun 2010 22:42:55 +0200 | |
changeset 3598 | a8aa06bae895 |
parent 3548 | 4d220ee7c75f |
child 3616 | 85d69ddb41b6 |
permissions | -rw-r--r-- |
3547 | 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" |
|
25 |
#import "SDL_uikitwindow.h" |
|
26 |
#import "SDL_events_c.h" |
|
27 |
#import "../SDL_sysvideo.h" |
|
28 |
#import "jumphack.h" |
|
29 |
#import "SDL_video.h" |
|
30 |
#import "GameSetup.h" |
|
31 |
#import "PascalImports.h" |
|
32 |
#import "MainMenuViewController.h" |
|
33 |
#import "OverlayViewController.h" |
|
34 |
#import "CommodityFunctions.h" |
|
35 |
||
36 |
#ifdef main |
|
37 |
#undef main |
|
38 |
#endif |
|
39 |
||
40 |
#define VALGRIND "/opt/valgrind/bin/valgrind" |
|
41 |
||
42 |
int main (int argc, char *argv[]) { |
|
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 |
|
46 |
if (argc < 2 || (argc >= 2 && strcmp(argv[1], "-valgrind") != 0)) |
|
47 |
execl(VALGRIND, VALGRIND, "--leak-check=full", argv[0], "-valgrind", NULL); |
|
48 |
#endif |
|
49 |
||
50 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|
51 |
int retVal = UIApplicationMain(argc, argv, nil, @"SDLUIKitDelegate"); |
|
52 |
[pool release]; |
|
53 |
return retVal; |
|
54 |
} |
|
55 |
||
56 |
@implementation SDLUIKitDelegate |
|
57 |
||
58 |
// convenience method |
|
59 |
+(SDLUIKitDelegate *)sharedAppDelegate { |
|
60 |
// the delegate is set in UIApplicationMain(), which is guaranteed to be called before this method |
|
61 |
return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate]; |
|
62 |
} |
|
63 |
||
64 |
-(id) init { |
|
65 |
if (self = [super init]){ |
|
66 |
mainViewController = nil; |
|
67 |
isInGame = NO; |
|
68 |
} |
|
69 |
return self; |
|
70 |
} |
|
71 |
||
72 |
-(void) dealloc { |
|
73 |
[mainViewController release]; |
|
74 |
[super dealloc]; |
|
75 |
} |
|
76 |
||
77 |
// main routine for calling the actual game engine |
|
78 |
-(IBAction) startSDLgame { |
|
79 |
[UIView beginAnimations:@"removing main controller" context:NULL]; |
|
80 |
[UIView setAnimationDuration:1]; |
|
81 |
mainViewController.view.alpha = 0; |
|
82 |
[UIView commitAnimations]; |
|
83 |
||
84 |
// pull out useful configuration info from various files |
|
85 |
GameSetup *setup = [[GameSetup alloc] init]; |
|
86 |
[setup startThread:@"engineProtocol"]; |
|
87 |
const char **gameArgs = [setup getSettings]; |
|
88 |
[setup release]; |
|
89 |
||
90 |
// since the sdlwindow is not yet created, we add the overlayController with a delay |
|
3598 | 91 |
[self performSelector:@selector(displayOverlayLater) withObject:nil afterDelay:0.3]; |
3547 | 92 |
|
93 |
// this is the pascal fuction that starts the game (wrapped around isInGame) |
|
94 |
isInGame = YES; |
|
95 |
Game(gameArgs); |
|
96 |
isInGame = NO; |
|
3598 | 97 |
free(gameArgs); |
3547 | 98 |
|
3598 | 99 |
UIWindow *aWin = [[[UIApplication sharedApplication] windows] objectAtIndex:0]; |
100 |
[aWin makeKeyAndVisible]; |
|
101 |
aWin = [[[UIApplication sharedApplication] windows] lastObject]; |
|
102 |
[aWin removeFromSuperview]; |
|
103 |
||
104 |
DLog(@"%@",[[UIApplication sharedApplication] windows]); |
|
3547 | 105 |
|
106 |
[UIView beginAnimations:@"inserting main controller" context:NULL]; |
|
107 |
[UIView setAnimationDuration:1]; |
|
108 |
mainViewController.view.alpha = 1; |
|
109 |
[UIView commitAnimations]; |
|
110 |
} |
|
111 |
||
112 |
-(void) displayOverlayLater { |
|
3598 | 113 |
// overlay with controls, become visible later, with a transparency effect |
3547 | 114 |
OverlayViewController *overlayController = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil]; |
3598 | 115 |
|
116 |
// keyWindow is the frontmost window |
|
3547 | 117 |
[[[UIApplication sharedApplication] keyWindow] addSubview:overlayController.view]; |
118 |
[overlayController release]; |
|
119 |
} |
|
120 |
||
121 |
// override the direct execution of SDL_main to allow us to implement the frontend (or even using a nib) |
|
122 |
-(void) applicationDidFinishLaunching:(UIApplication *)application { |
|
123 |
[application setStatusBarHidden:YES]; |
|
124 |
[application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]; |
|
125 |
||
3598 | 126 |
UIWindow *uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; |
3547 | 127 |
uiwindow.backgroundColor = [UIColor blackColor]; |
128 |
||
129 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) |
|
130 |
mainViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPad" bundle:nil]; |
|
131 |
else |
|
132 |
mainViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPhone" bundle:nil]; |
|
133 |
||
134 |
[uiwindow addSubview:mainViewController.view]; |
|
135 |
[uiwindow makeKeyAndVisible]; |
|
136 |
||
137 |
// Set working directory to resource path |
|
138 |
[[NSFileManager defaultManager] changeCurrentDirectoryPath:[[NSBundle mainBundle] resourcePath]]; |
|
139 |
} |
|
140 |
||
141 |
-(void) applicationWillTerminate:(UIApplication *)application { |
|
142 |
SDL_SendQuit(); |
|
143 |
if (isInGame) { |
|
144 |
HW_terminate(YES); |
|
145 |
// hack to prevent automatic termination. See SDL_uikitevents.m for details |
|
146 |
longjmp(*(jump_env()), 1); |
|
147 |
} |
|
148 |
} |
|
149 |
||
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
150 |
-(void) applicationDidReceiveMemoryWarning:(UIApplication *)application { |
3598 | 151 |
if (mainViewController.view.superview == nil) |
152 |
mainViewController = nil; |
|
153 |
MSG_MEMCLEAN(); |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
154 |
print_free_memory(); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
155 |
} |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
156 |
|
3547 | 157 |
-(void) applicationWillResignActive:(UIApplication *)application { |
158 |
if (isInGame) { |
|
159 |
HW_pause(); |
|
160 |
||
161 |
// Send every window on every screen a MINIMIZED event. |
|
162 |
SDL_VideoDevice *_this = SDL_GetVideoDevice(); |
|
163 |
if (!_this) |
|
164 |
return; |
|
165 |
||
166 |
int i; |
|
167 |
for (i = 0; i < _this->num_displays; i++) { |
|
168 |
const SDL_VideoDisplay *display = &_this->displays[i]; |
|
169 |
SDL_Window *window; |
|
170 |
for (window = display->windows; window != nil; window = window->next) |
|
171 |
SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0); |
|
172 |
} |
|
173 |
} |
|
174 |
} |
|
175 |
||
176 |
-(void) applicationDidBecomeActive:(UIApplication *)application { |
|
177 |
//NSLog(@"%@", NSStringFromSelector(_cmd)); |
|
178 |
if (isInGame) { |
|
179 |
HW_pause(); |
|
180 |
||
181 |
// Send every window on every screen a RESTORED event. |
|
182 |
SDL_VideoDevice *_this = SDL_GetVideoDevice(); |
|
183 |
if (!_this) |
|
184 |
return; |
|
185 |
||
186 |
int i; |
|
187 |
for (i = 0; i < _this->num_displays; i++) { |
|
188 |
const SDL_VideoDisplay *display = &_this->displays[i]; |
|
189 |
SDL_Window *window; |
|
190 |
for (window = display->windows; window != nil; window = window->next) |
|
191 |
SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0); |
|
192 |
} |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
@end |