project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m
branchhedgeroid
changeset 6224 42b256eca362
parent 6029 5073285b6599
parent 6209 074ab6ebcb3e
child 6328 d14adf1c7721
equal deleted inserted replaced
6055:88cfcd9161d3 6224:42b256eca362
    18  * File created on 18/04/2011.
    18  * File created on 18/04/2011.
    19  */
    19  */
    20 
    20 
    21 
    21 
    22 #import "GameInterfaceBridge.h"
    22 #import "GameInterfaceBridge.h"
    23 #import "PascalImports.h"
    23 #import "ServerSetup.h"
    24 #import "EngineProtocolNetwork.h"
    24 #import "EngineProtocolNetwork.h"
    25 #import "OverlayViewController.h"
    25 #import "OverlayViewController.h"
    26 #import "StatsPageViewController.h"
    26 #import "StatsPageViewController.h"
    27 #import "AudioManagerController.h"
    27 #import "AudioManagerController.h"
    28 #import "ObjcExports.h"
    28 #import "ObjcExports.h"
    30 @implementation GameInterfaceBridge
    30 @implementation GameInterfaceBridge
    31 @synthesize parentController, savePath, overlayController, engineProtocol, ipcPort, gameType;
    31 @synthesize parentController, savePath, overlayController, engineProtocol, ipcPort, gameType;
    32 
    32 
    33 -(id) initWithController:(id) viewController {
    33 -(id) initWithController:(id) viewController {
    34     if (self = [super init]) {
    34     if (self = [super init]) {
    35         self.ipcPort = randomPort();
    35         self.ipcPort = [ServerSetup randomPort];
    36         self.gameType = gtNone;
    36         self.gameType = gtNone;
    37         self.savePath = nil;
    37         self.savePath = nil;
    38 
    38 
    39         self.parentController = (UIViewController *)viewController;
    39         self.parentController = (UIViewController *)viewController;
    40         self.engineProtocol = [[EngineProtocolNetwork alloc] initOnPort:self.ipcPort];
    40         self.engineProtocol = [[EngineProtocolNetwork alloc] initOnPort:self.ipcPort];
    60     UIView *injected = (IS_DUALHEAD() ? self.parentController.view : UIVIEW_HW_SDLVIEW);
    60     UIView *injected = (IS_DUALHEAD() ? self.parentController.view : UIVIEW_HW_SDLVIEW);
    61     [injected addSubview:self.overlayController.view];
    61     [injected addSubview:self.overlayController.view];
    62 }
    62 }
    63 
    63 
    64 // main routine for calling the actual game engine
    64 // main routine for calling the actual game engine
    65 -(void) startGameEngine {
    65 -(void) engineLaunch {
    66     const char *gameArgs[11];
    66     const char *gameArgs[11];
    67     NSInteger width, height;
    67     CGFloat width, height;
       
    68     CGFloat screenScale = [[UIScreen mainScreen] safeScale];
    68     NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", self.ipcPort];
    69     NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", self.ipcPort];
    69     NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]];
    70     NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]];
    70     NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    71     NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    71 
    72 
    72     if (IS_DUALHEAD()) {
    73     if (IS_DUALHEAD()) {
    73         CGRect screenBounds = [[[UIScreen screens] objectAtIndex:1] bounds];
    74         CGRect screenBounds = [[[UIScreen screens] objectAtIndex:1] bounds];
    74         width = (int) screenBounds.size.width;
    75         width = screenBounds.size.width;
    75         height = (int) screenBounds.size.height;
    76         height = screenBounds.size.height;
    76     } else {
    77     } else {
    77         CGRect screenBounds = [[UIScreen mainScreen] bounds];
    78         CGRect screenBounds = [[UIScreen mainScreen] bounds];
    78         width = (int) screenBounds.size.height;
    79         width = screenBounds.size.height;
    79         height = (int) screenBounds.size.width;
    80         height = screenBounds.size.width;
    80     }
    81     }
    81 
    82 
    82     NSString *horizontalSize = [[NSString alloc] initWithFormat:@"%d", width * (int)getScreenScale()];
    83     NSString *horizontalSize = [[NSString alloc] initWithFormat:@"%d", (int)(width * screenScale)];
    83     NSString *verticalSize = [[NSString alloc] initWithFormat:@"%d", height * (int)getScreenScale()];
    84     NSString *verticalSize = [[NSString alloc] initWithFormat:@"%d", (int)(height * screenScale)];
    84 
    85 
    85     NSString *modelId = getModelType();
    86     NSString *modelId = [HWUtils modelType];
    86     NSInteger tmpQuality;
    87     NSInteger tmpQuality;
    87     if ([modelId hasPrefix:@"iPhone1"] || [modelId hasPrefix:@"iPod1,1"] || [modelId hasPrefix:@"iPod2,1"])     // = iPhone and iPhone 3G or iPod Touch or iPod Touch 2G
    88     if ([modelId hasPrefix:@"iPhone1"] || [modelId hasPrefix:@"iPod1,1"] || [modelId hasPrefix:@"iPod2,1"])     // = iPhone and iPhone 3G or iPod Touch or iPod Touch 2G
    88         tmpQuality = 0x00000001 | 0x00000002 | 0x00000008 | 0x00000040;                 // rqLowRes | rqBlurryLand | rqSimpleRope | rqKillFlakes
    89         tmpQuality = 0x00000001 | 0x00000002 | 0x00000008 | 0x00000040;                 // rqLowRes | rqBlurryLand | rqSimpleRope | rqKillFlakes
    89     else if ([modelId hasPrefix:@"iPhone2"] || [modelId hasPrefix:@"iPod3"])                                    // = iPhone 3GS or iPod Touch 3G
    90     else if ([modelId hasPrefix:@"iPhone2"] || [modelId hasPrefix:@"iPod3"])                                    // = iPhone 3GS or iPod Touch 3G
    90         tmpQuality = 0x00000002 | 0x00000040;                                           // rqBlurryLand | rqKillFlakes
    91         tmpQuality = 0x00000002 | 0x00000040;                                           // rqBlurryLand | rqKillFlakes
   117     [verticalSize release];
   118     [verticalSize release];
   118     [horizontalSize release];
   119     [horizontalSize release];
   119     [localeString release];
   120     [localeString release];
   120     [ipcString release];
   121     [ipcString release];
   121 
   122 
   122     objcExportsInit();
   123     [ObjcExports initialize];
   123 
   124 
   124     // this is the pascal fuction that starts the game, wrapped around isInGame
   125     // this is the pascal fuction that starts the game, wrapped around isInGame
   125     [HedgewarsAppDelegate sharedAppDelegate].isInGame = YES;
   126     [HedgewarsAppDelegate sharedAppDelegate].isInGame = YES;
   126     Game(gameArgs);
   127     Game(gameArgs);
   127     [HedgewarsAppDelegate sharedAppDelegate].isInGame = NO;
   128     [HedgewarsAppDelegate sharedAppDelegate].isInGame = NO;
   154     [userDefaults synchronize];
   155     [userDefaults synchronize];
   155 
   156 
   156     [AudioManagerController pauseBackgroundMusic];
   157     [AudioManagerController pauseBackgroundMusic];
   157 
   158 
   158     // SYSTEMS ARE GO!!
   159     // SYSTEMS ARE GO!!
   159     [self startGameEngine];
   160     [self engineLaunch];
   160     
   161     
   161     // remove completed games notification
   162     // remove completed games notification
   162     [userDefaults setObject:@"" forKey:@"savedGamePath"];
   163     [userDefaults setObject:@"" forKey:@"savedGamePath"];
   163     [userDefaults synchronize];
   164     [userDefaults synchronize];
   164 
   165 
   180 
   181 
   181     [AudioManagerController playBackgroundMusic];
   182     [AudioManagerController playBackgroundMusic];
   182 }
   183 }
   183 
   184 
   184 // set up variables for a local game
   185 // set up variables for a local game
   185 -(void) startLocalGame:(NSDictionary *)withDictionary {
   186 -(void) startLocalGame:(NSDictionary *)withOptions {
   186     self.gameType = gtLocal;
   187     self.gameType = gtLocal;
   187 
   188 
   188     NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
   189     NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
   189     [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"];
   190     [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"];
   190     NSString *path = [[NSString alloc] initWithFormat:@"%@%@.hws",SAVES_DIRECTORY(),[outputFormatter stringFromDate:[NSDate date]]];
   191     NSString *path = [[NSString alloc] initWithFormat:@"%@%@.hws",SAVES_DIRECTORY(),[outputFormatter stringFromDate:[NSDate date]]];
   194 
   195 
   195     // in the rare case in which a savefile with the same name exists the older one must be removed (or it gets corrupted)
   196     // in the rare case in which a savefile with the same name exists the older one must be removed (or it gets corrupted)
   196     if ([[NSFileManager defaultManager] fileExistsAtPath:self.savePath])
   197     if ([[NSFileManager defaultManager] fileExistsAtPath:self.savePath])
   197         [[NSFileManager defaultManager] removeItemAtPath:self.savePath error:nil];
   198         [[NSFileManager defaultManager] removeItemAtPath:self.savePath error:nil];
   198 
   199 
   199     [self.engineProtocol spawnThread:self.savePath withOptions:withDictionary];
   200     [self.engineProtocol spawnThread:self.savePath withOptions:withOptions];
   200     [self prepareEngineLaunch];
   201     [self prepareEngineLaunch];
   201 }
   202 }
   202 
   203 
   203 // set up variables for a save game
   204 // set up variables for a save game
   204 -(void) startSaveGame:(NSString *)atPath {
   205 -(void) startSaveGame:(NSString *)atPath {
   205     self.gameType = gtSave;
   206     self.gameType = gtSave;
   206     self.savePath = atPath;
   207     self.savePath = atPath;
   207 
   208 
   208     [self.engineProtocol spawnThread:self.savePath];
   209     [self.engineProtocol spawnThread:self.savePath];
       
   210     [self prepareEngineLaunch];
       
   211 }
       
   212 
       
   213 -(void) startMissionGame:(NSString *)withScript {
       
   214     self.gameType = gtMission;
       
   215     self.savePath = nil;
       
   216 
       
   217     NSString *missionPath = [[NSString alloc] initWithFormat:@"escript Missions/Training/%@.lua",withScript];
       
   218     NSDictionary *config = [NSDictionary dictionaryWithObject:missionPath forKey:@"mission_command"];
       
   219     [missionPath release];
       
   220     [self.engineProtocol spawnThread:nil withOptions:config];
   209     [self prepareEngineLaunch];
   221     [self prepareEngineLaunch];
   210 }
   222 }
   211 
   223 
   212 -(void) gameHasEndedWithStats:(NSArray *)stats {
   224 -(void) gameHasEndedWithStats:(NSArray *)stats {
   213     // wrap this around a retain/realse to prevent being deallocated too soon
   225     // wrap this around a retain/realse to prevent being deallocated too soon