25 #import "StatsPageViewController.h" |
25 #import "StatsPageViewController.h" |
26 #import "AudioManagerController.h" |
26 #import "AudioManagerController.h" |
27 #import "ObjcExports.h" |
27 #import "ObjcExports.h" |
28 |
28 |
29 @implementation GameInterfaceBridge |
29 @implementation GameInterfaceBridge |
|
30 @synthesize ipcPort, blackView; |
|
31 |
|
32 #pragma mark - |
|
33 #pragma mark Instance methods for engine interaction |
|
34 // prepares the controllers for hosting a game |
|
35 -(void) earlyEngineLaunch:(NSString *)pathOrNil withOptions:(NSDictionary *)optionsOrNil { |
|
36 [self retain]; |
|
37 [AudioManagerController stopBackgroundMusic]; |
|
38 |
|
39 EngineProtocolNetwork *proto = [[EngineProtocolNetwork alloc] init]; |
|
40 self.ipcPort = [proto spawnThread:pathOrNil withOptions:optionsOrNil]; |
|
41 [proto release]; |
|
42 |
|
43 // add a black view hiding the background |
|
44 CGRect theFrame = [[UIScreen mainScreen] bounds]; |
|
45 UIWindow *thisWindow = [[HedgewarsAppDelegate sharedAppDelegate] uiwindow]; |
|
46 self.blackView = [[UIView alloc] initWithFrame:theFrame]; |
|
47 self.blackView.opaque = YES; |
|
48 self.blackView.backgroundColor = [UIColor blackColor]; |
|
49 self.blackView.alpha = 0; |
|
50 [UIView beginAnimations:@"fade out" context:NULL]; |
|
51 [UIView setAnimationDuration:1]; |
|
52 self.blackView.alpha = 1; |
|
53 [UIView commitAnimations]; |
|
54 [thisWindow addSubview:self.blackView]; |
|
55 [self.blackView release]; |
|
56 |
|
57 // keep track of uncompleted games |
|
58 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; |
|
59 [userDefaults setObject:pathOrNil forKey:@"savedGamePath"]; |
|
60 [userDefaults synchronize]; |
|
61 |
|
62 // let's launch the engine using this -perfomSelector so that the runloop can deal with queued messages first |
|
63 [self performSelector:@selector(engineLaunch:) withObject:pathOrNil afterDelay:0.1f]; |
|
64 } |
|
65 |
|
66 // cleans up everything |
|
67 -(void) lateEngineLaunch { |
|
68 // remove completed games notification |
|
69 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; |
|
70 [userDefaults setObject:@"" forKey:@"savedGamePath"]; |
|
71 [userDefaults synchronize]; |
|
72 |
|
73 // remove the cover view with a transition |
|
74 self.blackView.alpha = 1; |
|
75 [UIView beginAnimations:@"fade in" context:NULL]; |
|
76 [UIView setAnimationDuration:1]; |
|
77 self.blackView.alpha = 0; |
|
78 [UIView commitAnimations]; |
|
79 [self.blackView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1]; |
|
80 |
|
81 // the overlay is not needed any more and can be removed |
|
82 [[OverlayViewController mainOverlay] removeOverlay]; |
|
83 |
|
84 // restart music and we're done |
|
85 [AudioManagerController playBackgroundMusic]; |
|
86 [self release]; |
|
87 } |
30 |
88 |
31 // main routine for calling the actual game engine |
89 // main routine for calling the actual game engine |
32 +(void) engineLaunchOn:(NSInteger) ipcPort withArgument:(NSString *)path { |
90 -(void) engineLaunch:(NSString *)pathOrNil { |
33 const char *gameArgs[11]; |
91 const char *gameArgs[11]; |
34 CGFloat width, height; |
92 CGFloat width, height; |
35 CGFloat screenScale = [[UIScreen mainScreen] safeScale]; |
93 CGFloat screenScale = [[UIScreen mainScreen] safeScale]; |
36 NSString *ipcString = [[NSString alloc] initWithFormat:@"%d",ipcPort]; |
94 NSString *ipcString = [[NSString alloc] initWithFormat:@"%d",self.ipcPort]; |
37 NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]]; |
95 NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt",[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]]; |
38 NSUserDefaults *settings = [NSUserDefaults standardUserDefaults]; |
96 NSUserDefaults *settings = [NSUserDefaults standardUserDefaults]; |
39 |
97 |
40 if (IS_DUALHEAD()) { |
98 if (IS_DUALHEAD()) { |
41 CGRect screenBounds = [[[UIScreen screens] objectAtIndex:1] bounds]; |
99 CGRect screenBounds = [[[UIScreen screens] objectAtIndex:1] bounds]; |
42 width = screenBounds.size.width; |
100 width = screenBounds.size.width; |
81 gameArgs[ 5] = [username UTF8String]; //UserNick |
139 gameArgs[ 5] = [username UTF8String]; //UserNick |
82 gameArgs[ 6] = [[[settings objectForKey:@"sound"] stringValue] UTF8String]; //isSoundEnabled |
140 gameArgs[ 6] = [[[settings objectForKey:@"sound"] stringValue] UTF8String]; //isSoundEnabled |
83 gameArgs[ 7] = [[[settings objectForKey:@"music"] stringValue] UTF8String]; //isMusicEnabled |
141 gameArgs[ 7] = [[[settings objectForKey:@"music"] stringValue] UTF8String]; //isMusicEnabled |
84 gameArgs[ 8] = [[[settings objectForKey:@"alternate"] stringValue] UTF8String]; //cAltDamage |
142 gameArgs[ 8] = [[[settings objectForKey:@"alternate"] stringValue] UTF8String]; //cAltDamage |
85 gameArgs[ 9] = [rotation UTF8String]; //rotateQt |
143 gameArgs[ 9] = [rotation UTF8String]; //rotateQt |
86 gameArgs[10] = ([HWUtils gameType] == gtSave) ? [path UTF8String] : NULL; //recordFileName |
144 gameArgs[10] = ([HWUtils gameType] == gtSave) ? [pathOrNil UTF8String] : NULL; //recordFileName |
87 |
145 |
88 [verticalSize release]; |
146 [verticalSize release]; |
89 [horizontalSize release]; |
147 [horizontalSize release]; |
90 [rotation release]; |
148 [rotation release]; |
91 [localeString release]; |
149 [localeString release]; |
93 |
151 |
94 [HWUtils setGameStatus:gsLoading]; |
152 [HWUtils setGameStatus:gsLoading]; |
95 |
153 |
96 // this is the pascal function that starts the game |
154 // this is the pascal function that starts the game |
97 Game(gameArgs); |
155 Game(gameArgs); |
98 } |
156 [self lateEngineLaunch]; |
99 |
157 } |
100 // prepares the controllers for hosting a game |
158 |
101 +(void) prepareEngineOn:(NSString *)pathOrNil withOptions:(NSDictionary *)optionsOrNil { |
159 #pragma mark - |
102 EngineProtocolNetwork *proto = [[EngineProtocolNetwork alloc] init]; |
160 #pragma mark Class methods for setting up the engine from outsite |
103 NSInteger ipcPort = [proto spawnThread:pathOrNil withOptions:optionsOrNil]; |
|
104 |
|
105 CGRect theFrame = [[UIScreen mainScreen] bounds]; |
|
106 UIWindow *thisWindow = [[HedgewarsAppDelegate sharedAppDelegate] uiwindow]; |
|
107 // we add a black view hiding the background |
|
108 UIView *blackView = [[UIView alloc] initWithFrame:theFrame]; |
|
109 [thisWindow addSubview:blackView]; |
|
110 blackView.opaque = YES; |
|
111 blackView.backgroundColor = [UIColor blackColor]; |
|
112 blackView.alpha = 0; |
|
113 |
|
114 // when dual screen we apply a little transition |
|
115 if (IS_DUALHEAD()) { |
|
116 [UIView beginAnimations:@"fade out" context:NULL]; |
|
117 [UIView setAnimationDuration:1]; |
|
118 blackView.alpha = 1; |
|
119 [UIView commitAnimations]; |
|
120 } else |
|
121 blackView.alpha = 1; |
|
122 |
|
123 // keep track of uncompleted games |
|
124 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; |
|
125 [userDefaults setObject:pathOrNil forKey:@"savedGamePath"]; |
|
126 [userDefaults synchronize]; |
|
127 |
|
128 [AudioManagerController pauseBackgroundMusic]; |
|
129 |
|
130 // SYSTEMS ARE GO!! |
|
131 [self engineLaunchOn:ipcPort withArgument:pathOrNil]; |
|
132 |
|
133 // remove completed games notification |
|
134 [userDefaults setObject:@"" forKey:@"savedGamePath"]; |
|
135 [userDefaults synchronize]; |
|
136 |
|
137 // now we can remove the cover with a transition |
|
138 blackView.frame = theFrame; |
|
139 blackView.alpha = 1; |
|
140 [UIView beginAnimations:@"fade in" context:NULL]; |
|
141 [UIView setAnimationDuration:1]; |
|
142 blackView.alpha = 0; |
|
143 [UIView commitAnimations]; |
|
144 [blackView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1]; |
|
145 [blackView release]; |
|
146 |
|
147 // the overlay is not needed any more and can be removed |
|
148 [[OverlayViewController mainOverlay] removeOverlay]; |
|
149 |
|
150 [AudioManagerController playBackgroundMusic]; |
|
151 } |
|
152 |
|
153 // set up variables for a local game |
161 // set up variables for a local game |
154 +(void) startLocalGame:(NSDictionary *)withOptions { |
162 +(void) startLocalGame:(NSDictionary *)withOptions { |
155 [HWUtils setGameType:gtLocal]; |
163 [HWUtils setGameType:gtLocal]; |
156 |
164 |
157 NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; |
165 NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; |
158 [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"]; |
166 [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"]; |
159 NSString *savePath = [[NSString alloc] initWithFormat:@"%@%@.hws",SAVES_DIRECTORY(),[outputFormatter stringFromDate:[NSDate date]]]; |
167 NSString *savePath = [[NSString alloc] initWithFormat:@"%@%@.hws",SAVES_DIRECTORY(),[outputFormatter stringFromDate:[NSDate date]]]; |
160 [outputFormatter release]; |
168 [outputFormatter release]; |
161 |
169 |
162 // in the rare case in which a savefile with the same name exists the older one must be removed (or it gets corrupted) |
170 // in the rare case in which a savefile with the same name exists the older one must be removed (otherwise it gets corrupted) |
163 if ([[NSFileManager defaultManager] fileExistsAtPath:savePath]) |
171 if ([[NSFileManager defaultManager] fileExistsAtPath:savePath]) |
164 [[NSFileManager defaultManager] removeItemAtPath:savePath error:nil]; |
172 [[NSFileManager defaultManager] removeItemAtPath:savePath error:nil]; |
165 |
173 |
166 [GameInterfaceBridge prepareEngineOn:savePath withOptions:withOptions]; |
174 GameInterfaceBridge *bridge = [[GameInterfaceBridge alloc] init]; |
|
175 [bridge earlyEngineLaunch:savePath withOptions:withOptions]; |
|
176 [bridge release]; |
167 [savePath release]; |
177 [savePath release]; |
168 } |
178 } |
169 |
179 |
170 // set up variables for a save game |
180 // set up variables for a save game |
171 +(void) startSaveGame:(NSString *)atPath { |
181 +(void) startSaveGame:(NSString *)atPath { |
172 [HWUtils setGameType:gtSave]; |
182 [HWUtils setGameType:gtSave]; |
173 [GameInterfaceBridge prepareEngineOn:atPath withOptions:nil]; |
183 GameInterfaceBridge *bridge = [[GameInterfaceBridge alloc] init]; |
|
184 [bridge earlyEngineLaunch:atPath withOptions:nil]; |
|
185 [bridge release]; |
174 } |
186 } |
175 |
187 |
176 +(void) startMissionGame:(NSString *)withScript { |
188 +(void) startMissionGame:(NSString *)withScript { |
177 [HWUtils setGameType:gtMission]; |
189 [HWUtils setGameType:gtMission]; |
178 |
190 |
179 NSString *missionPath = [[NSString alloc] initWithFormat:@"escript Missions/Training/%@.lua",withScript]; |
191 NSString *missionPath = [[NSString alloc] initWithFormat:@"escript Missions/Training/%@.lua",withScript]; |
180 NSDictionary *missionLine = [[NSDictionary alloc] initWithObjectsAndKeys:missionPath,@"mission_command",nil]; |
192 NSDictionary *missionLine = [[NSDictionary alloc] initWithObjectsAndKeys:missionPath,@"mission_command",nil]; |
181 [missionPath release]; |
193 [missionPath release]; |
182 |
194 |
183 [GameInterfaceBridge prepareEngineOn:nil withOptions:missionLine]; |
195 GameInterfaceBridge *bridge = [[GameInterfaceBridge alloc] init]; |
|
196 [bridge earlyEngineLaunch:nil withOptions:missionLine]; |
|
197 [bridge release]; |
184 [missionLine release]; |
198 [missionLine release]; |
185 } |
199 } |
186 |
200 |
187 /* |
201 /* |
188 -(void) gameHasEndedWithStats:(NSArray *)stats { |
202 -(void) gameHasEndedWithStats:(NSArray *)stats { |