|
1 /* |
|
2 * Hedgewars-iOS, a Hedgewars port for iOS devices |
|
3 * Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com> |
|
4 * |
|
5 * This program is free software; you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation; version 2 of the License |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program; if not, write to the Free Software |
|
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
17 * |
|
18 * File created on 18/04/2011. |
|
19 */ |
|
20 |
|
21 |
|
22 #import "GameInterfaceBridge.h" |
|
23 #import "PascalImports.h" |
|
24 #import "EngineProtocolNetwork.h" |
|
25 #import "OverlayViewController.h" |
|
26 |
|
27 #define BUFFER_SIZE 255 // like in original frontend |
|
28 |
|
29 @implementation GameInterfaceBridge |
|
30 @synthesize parentController, systemSettings, savePath, overlayController, ipcPort, gameType, engineProtocol; |
|
31 |
|
32 -(id) initWithController:(id) viewController { |
|
33 if (self = [super init]) { |
|
34 self.parentController = (UIViewController *)viewController; |
|
35 self.engineProtocol = [[EngineProtocolNetwork alloc] init]; |
|
36 ; |
|
37 self.savePath = nil; |
|
38 |
|
39 self.systemSettings = [NSDictionary dictionaryWithContentsOfFile:SETTINGS_FILE()]; |
|
40 self.overlayController = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil]; |
|
41 self.ipcPort = randomPort(); |
|
42 |
|
43 self.gameType = gtNone; |
|
44 } |
|
45 return self; |
|
46 } |
|
47 |
|
48 -(void) dealloc { |
|
49 releaseAndNil(parentController); |
|
50 releaseAndNil(engineProtocol); |
|
51 releaseAndNil(systemSettings); |
|
52 releaseAndNil(savePath); |
|
53 releaseAndNil(overlayController); |
|
54 [super dealloc]; |
|
55 } |
|
56 |
|
57 #pragma mark - |
|
58 // overlay with controls, become visible later, with a transparency effect since the sdlwindow is not yet created |
|
59 -(void) displayOverlayLater:(id) object { |
|
60 NSDictionary *dict = (NSDictionary *)object; |
|
61 |
|
62 [self.overlayController setUseClassicMenu:[[dict objectForKey:@"menu"] boolValue]]; |
|
63 [self.overlayController setInitialOrientation:[[dict objectForKey:@"orientation"] intValue]]; |
|
64 |
|
65 UIWindow *gameWindow = (IS_DUALHEAD() ? [HedgewarsAppDelegate sharedAppDelegate].uiwindow : [[UIApplication sharedApplication] keyWindow]); |
|
66 [gameWindow addSubview:self.overlayController.view]; |
|
67 } |
|
68 |
|
69 // main routine for calling the actual game engine |
|
70 -(void) startGameEngine { |
|
71 self.parentController.view.opaque = YES; |
|
72 self.parentController.view.backgroundColor = [UIColor blackColor]; |
|
73 self.parentController.view.alpha = 0; |
|
74 |
|
75 [UIView beginAnimations:@"fade out to black" context:NULL]; |
|
76 [UIView setAnimationDuration:1]; |
|
77 self.parentController.view.alpha = 1; |
|
78 [UIView commitAnimations]; |
|
79 |
|
80 self.engineProtocol.savePath = self.savePath; |
|
81 [self.engineProtocol spawnThreadOnPort:self.ipcPort]; |
|
82 |
|
83 NSDictionary *overlayOptions = [[NSDictionary alloc] initWithObjectsAndKeys: |
|
84 [NSNumber numberWithInt:self.parentController.interfaceOrientation],@"orientation", |
|
85 [self.systemSettings objectForKey:@"menu"],@"menu", |
|
86 nil]; |
|
87 [self performSelector:@selector(displayOverlayLater:) withObject:overlayOptions afterDelay:1]; |
|
88 [overlayOptions release]; |
|
89 |
|
90 // this is the pascal fuction that starts the game, wrapped around isInGame |
|
91 [HedgewarsAppDelegate sharedAppDelegate].isInGame = YES; |
|
92 Game([self gatherGameSettings]); |
|
93 [HedgewarsAppDelegate sharedAppDelegate].isInGame = NO; |
|
94 |
|
95 [UIView beginAnimations:@"fade in" context:NULL]; |
|
96 [UIView setAnimationDuration:1]; |
|
97 self.parentController.view.alpha = 0; |
|
98 [UIView commitAnimations]; |
|
99 } |
|
100 |
|
101 -(void) startLocalGame:(NSDictionary *)withDictionary { |
|
102 self.gameType = gtLocal; |
|
103 [self.engineProtocol setGameConfig:withDictionary]; |
|
104 |
|
105 NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; |
|
106 [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"]; |
|
107 NSString *newDateString = [outputFormatter stringFromDate:[NSDate date]]; |
|
108 self.savePath = [SAVES_DIRECTORY() stringByAppendingFormat:@"%@.hws", newDateString]; |
|
109 [outputFormatter release]; |
|
110 |
|
111 [self startGameEngine]; |
|
112 } |
|
113 |
|
114 -(void) startSaveGame:(NSString *)atPath { |
|
115 self.gameType = gtSave; |
|
116 self.savePath = atPath; |
|
117 [self.engineProtocol setGameConfig:nil]; |
|
118 |
|
119 [self startGameEngine]; |
|
120 } |
|
121 |
|
122 #pragma mark - |
|
123 -(const char **)gatherGameSettings { |
|
124 const char *gameArgs[10]; |
|
125 NSInteger width, height, orientation; |
|
126 NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", self.ipcPort]; |
|
127 NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]]; |
|
128 |
|
129 if (IS_DUALHEAD()) { |
|
130 CGRect screenBounds = [[[UIScreen screens] objectAtIndex:1] bounds]; |
|
131 width = (int) screenBounds.size.width; |
|
132 height = (int) screenBounds.size.height; |
|
133 orientation = 0; |
|
134 } else { |
|
135 CGRect screenBounds = [[UIScreen mainScreen] bounds]; |
|
136 width = (int) screenBounds.size.height; |
|
137 height = (int) screenBounds.size.width; |
|
138 orientation = (self.parentController.interfaceOrientation == UIDeviceOrientationLandscapeLeft) ? -90 : 90; |
|
139 } |
|
140 |
|
141 NSString *horizontalSize = [[NSString alloc] initWithFormat:@"%d", width]; |
|
142 NSString *verticalSize = [[NSString alloc] initWithFormat:@"%d", height]; |
|
143 NSString *rotation = [[NSString alloc] initWithFormat:@"%d", orientation]; |
|
144 BOOL enhanced = [[self.systemSettings objectForKey:@"enhanced"] boolValue]; |
|
145 |
|
146 NSString *modelId = modelType(); |
|
147 NSInteger tmpQuality; |
|
148 if ([modelId hasPrefix:@"iPhone1"] || [modelId hasPrefix:@"iPod1,1"] || [modelId hasPrefix:@"iPod2,1"]) // = iPhone and iPhone 3G or iPod Touch or iPod Touch 2G |
|
149 tmpQuality = 0x00000001 | 0x00000002 | 0x00000008 | 0x00000040; // rqLowRes | rqBlurryLand | rqSimpleRope | rqKillFlakes |
|
150 else if ([modelId hasPrefix:@"iPhone2"] || [modelId hasPrefix:@"iPod3"]) // = iPhone 3GS or iPod Touch 3G |
|
151 tmpQuality = 0x00000002 | 0x00000040; // rqBlurryLand | rqKillFlakes |
|
152 else if ([modelId hasPrefix:@"iPad1"] || [modelId hasPrefix:@"iPod4"] || enhanced == NO) // = iPad 1G or iPod Touch 4G or not enhanced mode |
|
153 tmpQuality = 0x00000002; // rqBlurryLand |
|
154 else // = everything else |
|
155 tmpQuality = 0; // full quality |
|
156 |
|
157 // disable tooltips on iPhone |
|
158 if (IS_IPAD() == NO) |
|
159 tmpQuality = tmpQuality | 0x00000400; |
|
160 |
|
161 // prevents using an empty nickname |
|
162 NSString *username = [self.systemSettings objectForKey:@"username"]; |
|
163 if ([username length] == 0) |
|
164 username = [NSString stringWithFormat:@"MobileUser-%@",ipcString]; |
|
165 |
|
166 gameArgs[ 0] = [ipcString UTF8String]; //ipcPort |
|
167 gameArgs[ 1] = [horizontalSize UTF8String]; //cScreenWidth |
|
168 gameArgs[ 2] = [verticalSize UTF8String]; //cScreenHeight |
|
169 gameArgs[ 3] = [[NSString stringWithFormat:@"%d",tmpQuality] UTF8String]; //quality |
|
170 gameArgs[ 4] = "en.txt";//[localeString UTF8String]; //cLocaleFName |
|
171 gameArgs[ 5] = [username UTF8String]; //UserNick |
|
172 gameArgs[ 6] = [[[self.systemSettings objectForKey:@"sound"] stringValue] UTF8String]; //isSoundEnabled |
|
173 gameArgs[ 7] = [[[self.systemSettings objectForKey:@"music"] stringValue] UTF8String]; //isMusicEnabled |
|
174 gameArgs[ 8] = [[[self.systemSettings objectForKey:@"alternate"] stringValue] UTF8String]; //cAltDamage |
|
175 gameArgs[ 9] = [rotation UTF8String]; //rotateQt |
|
176 gameArgs[10] = (self.gameType == gtSave) ? [self.savePath UTF8String] : NULL; //recordFileName |
|
177 |
|
178 [verticalSize release]; |
|
179 [horizontalSize release]; |
|
180 [rotation release]; |
|
181 [localeString release]; |
|
182 [ipcString release]; |
|
183 return gameArgs; |
|
184 } |
|
185 |
|
186 |
|
187 @end |