project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m
changeset 6353 d8f62c805619
parent 6337 84e7d1a5e3df
child 6365 c992df555afb
equal deleted inserted replaced
6352:2448f5390bd5 6353:d8f62c805619
    24 #import "OverlayViewController.h"
    24 #import "OverlayViewController.h"
    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 static UIViewController *callingController;
       
    30 
    29 @implementation GameInterfaceBridge
    31 @implementation GameInterfaceBridge
    30 @synthesize blackView;
    32 @synthesize blackView, savePath, proto;
    31 
    33 
    32 #pragma mark -
    34 #pragma mark -
    33 #pragma mark Instance methods for engine interaction
    35 #pragma mark Instance methods for engine interaction
    34 // prepares the controllers for hosting a game
    36 // prepares the controllers for hosting a game
    35 -(void) earlyEngineLaunch:(NSString *)pathOrNil withOptions:(NSDictionary *)optionsOrNil {
    37 -(void) earlyEngineLaunch:(NSDictionary *)optionsOrNil {
    36     [self retain];
    38     [self retain];
    37     [AudioManagerController stopBackgroundMusic];
    39     [AudioManagerController stopBackgroundMusic];
    38     [EngineProtocolNetwork spawnThread:pathOrNil withOptions:optionsOrNil];
    40 
       
    41     EngineProtocolNetwork *engineProtocol = [[EngineProtocolNetwork alloc] init];
       
    42     self.proto = engineProtocol;
       
    43     [engineProtocol release];
       
    44     [self.proto spawnThread:self.savePath withOptions:optionsOrNil];
    39 
    45 
    40     // add a black view hiding the background
    46     // add a black view hiding the background
    41     CGRect theFrame = [[UIScreen mainScreen] bounds];
    47     CGRect theFrame = [[UIScreen mainScreen] bounds];
    42     UIWindow *thisWindow = [[HedgewarsAppDelegate sharedAppDelegate] uiwindow];
    48     UIWindow *thisWindow = [[HedgewarsAppDelegate sharedAppDelegate] uiwindow];
    43     self.blackView = [[UIView alloc] initWithFrame:theFrame];
    49     self.blackView = [[UIView alloc] initWithFrame:theFrame];
    51     [thisWindow addSubview:self.blackView];
    57     [thisWindow addSubview:self.blackView];
    52     [self.blackView release];
    58     [self.blackView release];
    53 
    59 
    54     // keep track of uncompleted games
    60     // keep track of uncompleted games
    55     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    61     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    56     [userDefaults setObject:pathOrNil forKey:@"savedGamePath"];
    62     [userDefaults setObject:self.savePath forKey:@"savedGamePath"];
    57     [userDefaults synchronize];
    63     [userDefaults synchronize];
    58 
    64 
    59     // let's launch the engine using this -perfomSelector so that the runloop can deal with queued messages first
    65     // let's launch the engine using this -perfomSelector so that the runloop can deal with queued messages first
    60     [self performSelector:@selector(engineLaunch:) withObject:pathOrNil afterDelay:0.1f];
    66     [self performSelector:@selector(engineLaunch) withObject:nil afterDelay:0.1f];
    61 }
    67 }
    62 
    68 
    63 // cleans up everything
    69 // cleans up everything
    64 -(void) lateEngineLaunch {
    70 -(void) lateEngineLaunch {
       
    71     // notify views below that they are getting the spotlight again
       
    72     [[[HedgewarsAppDelegate sharedAppDelegate] uiwindow] makeKeyAndVisible];
       
    73     [callingController viewWillAppear:YES];
       
    74 
    65     // remove completed games notification
    75     // remove completed games notification
    66     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    76     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    67     [userDefaults setObject:@"" forKey:@"savedGamePath"];
    77     [userDefaults setObject:@"" forKey:@"savedGamePath"];
    68     [userDefaults synchronize];
    78     [userDefaults synchronize];
    69 
    79 
    76     [self.blackView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1];
    86     [self.blackView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1];
    77 
    87 
    78     // the overlay is not needed any more and can be removed
    88     // the overlay is not needed any more and can be removed
    79     [[OverlayViewController mainOverlay] removeOverlay];
    89     [[OverlayViewController mainOverlay] removeOverlay];
    80 
    90 
       
    91     // engine thread *should* be done by now
       
    92     NSArray *stats = self.proto.statsArray;
       
    93     if (stats != nil) {
       
    94         StatsPageViewController *statsPage = [[StatsPageViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
    95         statsPage.statsArray = stats;
       
    96         statsPage.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
       
    97         if ([statsPage respondsToSelector:@selector(setModalPresentationStyle:)])
       
    98             statsPage.modalPresentationStyle = UIModalPresentationPageSheet;
       
    99 
       
   100         [callingController presentModalViewController:statsPage animated:YES];
       
   101         [statsPage release];
       
   102     }
       
   103     [stats release];    // we retained the array before
       
   104 
       
   105     // can remove the savefile if the replay has ended
       
   106     if ([HWUtils gameType] == gtSave)
       
   107         [[NSFileManager defaultManager] removeItemAtPath:self.savePath error:nil];
       
   108 
    81     // restart music and we're done
   109     // restart music and we're done
    82     [AudioManagerController playBackgroundMusic];
   110     [AudioManagerController playBackgroundMusic];
       
   111     [HWUtils setGameStatus:gsNone];
       
   112     [HWUtils setGameType:gtNone];
    83     [self release];
   113     [self release];
    84 }
   114 }
    85 
   115 
    86 // main routine for calling the actual game engine
   116 // main routine for calling the actual game engine
    87 -(void) engineLaunch:(NSString *)pathOrNil {
   117 -(void) engineLaunch {
    88     const char *gameArgs[11];
   118     const char *gameArgs[11];
    89     CGFloat width, height;
   119     CGFloat width, height;
    90     NSInteger enginePort = [EngineProtocolNetwork activeEnginePort];
   120     NSInteger enginePort = self.proto.enginePort;
    91     CGFloat screenScale = [[UIScreen mainScreen] safeScale];
   121     CGFloat screenScale = [[UIScreen mainScreen] safeScale];
    92     NSString *ipcString = [[NSString alloc] initWithFormat:@"%d",enginePort];
   122     NSString *ipcString = [[NSString alloc] initWithFormat:@"%d",enginePort];
    93     NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt",[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]];
   123     NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt",[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]];
    94     NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
   124     NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    95 
   125 
   137     gameArgs[ 5] = [username UTF8String];                                                       //UserNick
   167     gameArgs[ 5] = [username UTF8String];                                                       //UserNick
   138     gameArgs[ 6] = [[[settings objectForKey:@"sound"] stringValue] UTF8String];                 //isSoundEnabled
   168     gameArgs[ 6] = [[[settings objectForKey:@"sound"] stringValue] UTF8String];                 //isSoundEnabled
   139     gameArgs[ 7] = [[[settings objectForKey:@"music"] stringValue] UTF8String];                 //isMusicEnabled
   169     gameArgs[ 7] = [[[settings objectForKey:@"music"] stringValue] UTF8String];                 //isMusicEnabled
   140     gameArgs[ 8] = [[[settings objectForKey:@"alternate"] stringValue] UTF8String];             //cAltDamage
   170     gameArgs[ 8] = [[[settings objectForKey:@"alternate"] stringValue] UTF8String];             //cAltDamage
   141     gameArgs[ 9] = [rotation UTF8String];                                                       //rotateQt
   171     gameArgs[ 9] = [rotation UTF8String];                                                       //rotateQt
   142     gameArgs[10] = ([HWUtils gameType] == gtSave) ? [pathOrNil UTF8String] : NULL;              //recordFileName
   172     gameArgs[10] = ([HWUtils gameType] == gtSave) ? [self.savePath UTF8String] : NULL;          //recordFileName
   143 
   173 
   144     [verticalSize release];
   174     [verticalSize release];
   145     [horizontalSize release];
   175     [horizontalSize release];
   146     [rotation release];
   176     [rotation release];
   147     [localeString release];
   177     [localeString release];
   152     // this is the pascal function that starts the game
   182     // this is the pascal function that starts the game
   153     Game(gameArgs);
   183     Game(gameArgs);
   154     [self lateEngineLaunch];
   184     [self lateEngineLaunch];
   155 }
   185 }
   156 
   186 
       
   187 -(void) dealloc {
       
   188     releaseAndNil(blackView);
       
   189     releaseAndNil(savePath);
       
   190     releaseAndNil(proto);
       
   191     [super dealloc];
       
   192 }
       
   193 
   157 #pragma mark -
   194 #pragma mark -
   158 #pragma mark Class methods for setting up the engine from outsite
   195 #pragma mark Class methods for setting up the engine from outsite
       
   196 +(void) registerCallingController:(UIViewController *)controller {
       
   197     callingController = controller;
       
   198 }
       
   199 
   159 +(void) startGame:(TGameType) type atPath:(NSString *)path withOptions:(NSDictionary *)config {
   200 +(void) startGame:(TGameType) type atPath:(NSString *)path withOptions:(NSDictionary *)config {
   160     [HWUtils setGameType:type];
   201     [HWUtils setGameType:type];
   161     id bridge = [[self alloc] init];
   202     id bridge = [[self alloc] init];
   162     [bridge earlyEngineLaunch:path withOptions:config];
   203     [bridge setSavePath:path];
       
   204     [bridge earlyEngineLaunch:config];
   163     [bridge release];
   205     [bridge release];
   164 }
   206 }
   165 
   207 
   166 +(void) startLocalGame:(NSDictionary *)withOptions {
   208 +(void) startLocalGame:(NSDictionary *)withOptions {
   167     NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
   209     NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
   188 
   230 
   189     [self startGame:gtMission atPath:nil withOptions:missionLine];
   231     [self startGame:gtMission atPath:nil withOptions:missionLine];
   190     [missionLine release];
   232     [missionLine release];
   191 }
   233 }
   192 
   234 
   193 /*
       
   194 -(void) gameHasEndedWithStats:(NSArray *)stats {
       
   195     // wrap this around a retain/realse to prevent being deallocated too soon
       
   196     [self retain];
       
   197     // display stats page if there is something to display
       
   198     if (stats != nil) {
       
   199         StatsPageViewController *statsPage = [[StatsPageViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
   200         statsPage.statsArray = stats;
       
   201         statsPage.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
       
   202         if ([statsPage respondsToSelector:@selector(setModalPresentationStyle:)])
       
   203             statsPage.modalPresentationStyle = UIModalPresentationPageSheet;
       
   204 
       
   205         [self.parentController presentModalViewController:statsPage animated:YES];
       
   206         [statsPage release];
       
   207     }
       
   208 
       
   209     // can remove the savefile if the replay has ended
       
   210     if ([HWUtils gameType] == gtSave)
       
   211         [[NSFileManager defaultManager] removeItemAtPath:self.savePath error:nil];
       
   212     [self release];
       
   213 }
       
   214 */
       
   215 
   235 
   216 @end
   236 @end