project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m
changeset 6321 5a0416e5a6de
parent 6301 1259736c0134
child 6328 d14adf1c7721
child 6337 84e7d1a5e3df
equal deleted inserted replaced
6320:238a6dc0e7ad 6321:5a0416e5a6de
    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;
    30 @synthesize blackView;
    31 
    31 
    32 #pragma mark -
    32 #pragma mark -
    33 #pragma mark Instance methods for engine interaction
    33 #pragma mark Instance methods for engine interaction
    34 // prepares the controllers for hosting a game
    34 // prepares the controllers for hosting a game
    35 -(void) earlyEngineLaunch:(NSString *)pathOrNil withOptions:(NSDictionary *)optionsOrNil {
    35 -(void) earlyEngineLaunch:(NSString *)pathOrNil withOptions:(NSDictionary *)optionsOrNil {
    36     [self retain];
    36     [self retain];
    37     [AudioManagerController stopBackgroundMusic];
    37     [AudioManagerController stopBackgroundMusic];
    38 
    38     [EngineProtocolNetwork spawnThread:pathOrNil withOptions:optionsOrNil];
    39     EngineProtocolNetwork *proto = [[EngineProtocolNetwork alloc] init];
       
    40     self.ipcPort = [proto spawnThread:pathOrNil withOptions:optionsOrNil];
       
    41     [proto release];
       
    42 
    39 
    43     // add a black view hiding the background
    40     // add a black view hiding the background
    44     CGRect theFrame = [[UIScreen mainScreen] bounds];
    41     CGRect theFrame = [[UIScreen mainScreen] bounds];
    45     UIWindow *thisWindow = [[HedgewarsAppDelegate sharedAppDelegate] uiwindow];
    42     UIWindow *thisWindow = [[HedgewarsAppDelegate sharedAppDelegate] uiwindow];
    46     self.blackView = [[UIView alloc] initWithFrame:theFrame];
    43     self.blackView = [[UIView alloc] initWithFrame:theFrame];
    88 
    85 
    89 // main routine for calling the actual game engine
    86 // main routine for calling the actual game engine
    90 -(void) engineLaunch:(NSString *)pathOrNil {
    87 -(void) engineLaunch:(NSString *)pathOrNil {
    91     const char *gameArgs[11];
    88     const char *gameArgs[11];
    92     CGFloat width, height;
    89     CGFloat width, height;
       
    90     NSInteger enginePort = [EngineProtocolNetwork activeEnginePort];
    93     CGFloat screenScale = [[UIScreen mainScreen] safeScale];
    91     CGFloat screenScale = [[UIScreen mainScreen] safeScale];
    94     NSString *ipcString = [[NSString alloc] initWithFormat:@"%d",self.ipcPort];
    92     NSString *ipcString = [[NSString alloc] initWithFormat:@"%d",enginePort];
    95     NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt",[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]];
    93     NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt",[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]];
    96     NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    94     NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    97 
    95 
    98     if (IS_DUALHEAD()) {
    96     if (IS_DUALHEAD()) {
    99         CGRect screenBounds = [[[UIScreen screens] objectAtIndex:1] bounds];
    97         CGRect screenBounds = [[[UIScreen screens] objectAtIndex:1] bounds];
   156     [self lateEngineLaunch];
   154     [self lateEngineLaunch];
   157 }
   155 }
   158 
   156 
   159 #pragma mark -
   157 #pragma mark -
   160 #pragma mark Class methods for setting up the engine from outsite
   158 #pragma mark Class methods for setting up the engine from outsite
   161 // set up variables for a local game
   159 +(void) startGame:(TGameType) type atPath:(NSString *)path withOptions:(NSDictionary *)config {
       
   160     [HWUtils setGameType:type];
       
   161     GameInterfaceBridge *bridge = [[GameInterfaceBridge alloc] init];
       
   162     [bridge earlyEngineLaunch:path withOptions:config];
       
   163     [bridge release];
       
   164 }
       
   165 
   162 +(void) startLocalGame:(NSDictionary *)withOptions {
   166 +(void) startLocalGame:(NSDictionary *)withOptions {
   163     [HWUtils setGameType:gtLocal];
       
   164 
       
   165     NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
   167     NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
   166     [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"];
   168     [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"];
   167     NSString *savePath = [[NSString alloc] initWithFormat:@"%@%@.hws",SAVES_DIRECTORY(),[outputFormatter stringFromDate:[NSDate date]]];
   169     NSString *savePath = [[NSString alloc] initWithFormat:@"%@%@.hws",SAVES_DIRECTORY(),[outputFormatter stringFromDate:[NSDate date]]];
   168     [outputFormatter release];
   170     [outputFormatter release];
   169 
   171 
   170     // in the rare case in which a savefile with the same name exists the older one must be removed (otherwise it gets corrupted)
   172     // in the rare case in which a savefile with the same name exists the older one must be removed (otherwise it gets corrupted)
   171     if ([[NSFileManager defaultManager] fileExistsAtPath:savePath])
   173     if ([[NSFileManager defaultManager] fileExistsAtPath:savePath])
   172         [[NSFileManager defaultManager] removeItemAtPath:savePath error:nil];
   174         [[NSFileManager defaultManager] removeItemAtPath:savePath error:nil];
   173 
   175 
   174     GameInterfaceBridge *bridge = [[GameInterfaceBridge alloc] init];
   176     [self startGame:gtLocal atPath:savePath withOptions:withOptions];
   175     [bridge earlyEngineLaunch:savePath withOptions:withOptions];
       
   176     [bridge release];
       
   177     [savePath release];
   177     [savePath release];
   178 }
   178 }
   179 
   179 
   180 // set up variables for a save game
       
   181 +(void) startSaveGame:(NSString *)atPath {
   180 +(void) startSaveGame:(NSString *)atPath {
   182     [HWUtils setGameType:gtSave];
   181     [self startGame:gtSave atPath:atPath withOptions:nil];
   183     GameInterfaceBridge *bridge = [[GameInterfaceBridge alloc] init];
       
   184     [bridge earlyEngineLaunch:atPath withOptions:nil];
       
   185     [bridge release];
       
   186 }
   182 }
   187 
   183 
   188 +(void) startMissionGame:(NSString *)withScript {
   184 +(void) startMissionGame:(NSString *)withScript {
   189     [HWUtils setGameType:gtMission];
       
   190 
       
   191     NSString *missionPath = [[NSString alloc] initWithFormat:@"escript Missions/Training/%@.lua",withScript];
   185     NSString *missionPath = [[NSString alloc] initWithFormat:@"escript Missions/Training/%@.lua",withScript];
   192     NSDictionary *missionLine = [[NSDictionary alloc] initWithObjectsAndKeys:missionPath,@"mission_command",nil];
   186     NSDictionary *missionLine = [[NSDictionary alloc] initWithObjectsAndKeys:missionPath,@"mission_command",nil];
   193     [missionPath release];
   187     [missionPath release];
   194 
   188 
   195     GameInterfaceBridge *bridge = [[GameInterfaceBridge alloc] init];
   189     [self startGame:gtMission atPath:nil withOptions:missionLine];
   196     [bridge earlyEngineLaunch:nil withOptions:missionLine];
       
   197     [bridge release];
       
   198     [missionLine release];
   190     [missionLine release];
   199 }
   191 }
   200 
   192 
   201 /*
   193 /*
   202 -(void) gameHasEndedWithStats:(NSArray *)stats {
   194 -(void) gameHasEndedWithStats:(NSArray *)stats {