project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m
changeset 5155 f2165724605c
parent 5154 851f36579ed4
child 5156 641abe679bf0
equal deleted inserted replaced
5154:851f36579ed4 5155:f2165724605c
    22 #import "GameInterfaceBridge.h"
    22 #import "GameInterfaceBridge.h"
    23 #import "PascalImports.h"
    23 #import "PascalImports.h"
    24 #import "EngineProtocolNetwork.h"
    24 #import "EngineProtocolNetwork.h"
    25 #import "OverlayViewController.h"
    25 #import "OverlayViewController.h"
    26 
    26 
    27 #define BUFFER_SIZE 255     // like in original frontend
       
    28 
       
    29 @implementation GameInterfaceBridge
    27 @implementation GameInterfaceBridge
    30 @synthesize parentController, systemSettings, savePath, overlayController, ipcPort, gameType, engineProtocol;
    28 @synthesize parentController, systemSettings, savePath, overlayController, ipcPort, gameType, engineProtocol;
    31 
    29 
    32 -(id) initWithController:(id) viewController {
    30 -(id) initWithController:(id) viewController {
    33     if (self = [super init]) {
    31     if (self = [super init]) {
       
    32         self.ipcPort = randomPort();
       
    33         self.gameType = gtNone;
       
    34         self.savePath = nil;
       
    35 
    34         self.parentController = (UIViewController *)viewController;
    36         self.parentController = (UIViewController *)viewController;
    35         self.engineProtocol = [[EngineProtocolNetwork alloc] init];
    37         self.engineProtocol = [[EngineProtocolNetwork alloc] initOnPort:self.ipcPort];
    36 ;
    38         self.engineProtocol.delegate = self;
    37         self.savePath = nil;
       
    38 
    39 
    39         self.systemSettings = [NSDictionary dictionaryWithContentsOfFile:SETTINGS_FILE()];
    40         self.systemSettings = [NSDictionary dictionaryWithContentsOfFile:SETTINGS_FILE()];
    40         self.overlayController = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];
    41         self.overlayController = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];
    41         self.ipcPort = randomPort();
       
    42 
       
    43         self.gameType = gtNone;
       
    44     }
    42     }
    45     return self;
    43     return self;
    46 }
    44 }
    47 
    45 
    48 -(void) dealloc {
    46 -(void) dealloc {
    66     [gameWindow addSubview:self.overlayController.view];
    64     [gameWindow addSubview:self.overlayController.view];
    67 }
    65 }
    68 
    66 
    69 // main routine for calling the actual game engine
    67 // main routine for calling the actual game engine
    70 -(void) startGameEngine {
    68 -(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];
    69     const char *gameArgs[10];
   125     NSInteger width, height, orientation;
    70     NSInteger width, height, orientation;
   126     NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", self.ipcPort];
    71     NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", self.ipcPort];
   127     NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]];
    72     NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]];
   128 
    73 
   178     [verticalSize release];
   123     [verticalSize release];
   179     [horizontalSize release];
   124     [horizontalSize release];
   180     [rotation release];
   125     [rotation release];
   181     [localeString release];
   126     [localeString release];
   182     [ipcString release];
   127     [ipcString release];
   183     return gameArgs;
   128 
       
   129     // this is the pascal fuction that starts the game, wrapped around isInGame
       
   130     [HedgewarsAppDelegate sharedAppDelegate].isInGame = YES;
       
   131     Game(gameArgs);
       
   132     [HedgewarsAppDelegate sharedAppDelegate].isInGame = NO;
   184 }
   133 }
   185 
   134 
       
   135 // prepares the controllers for hosting a game
       
   136 -(void) prepareEngineLaunch {
       
   137     self.parentController.view.opaque = YES;
       
   138     self.parentController.view.backgroundColor = [UIColor blackColor];
       
   139     self.parentController.view.alpha = 0;
       
   140 
       
   141     [UIView beginAnimations:@"fade out to black" context:NULL];
       
   142     [UIView setAnimationDuration:1];
       
   143     self.parentController.view.alpha = 1;
       
   144     [UIView commitAnimations];
       
   145 
       
   146     NSDictionary *overlayOptions = [[NSDictionary alloc] initWithObjectsAndKeys:
       
   147                                     [NSNumber numberWithInt:self.parentController.interfaceOrientation],@"orientation",
       
   148                                     [self.systemSettings objectForKey:@"menu"],@"menu",
       
   149                                     nil];
       
   150     [self performSelector:@selector(displayOverlayLater:) withObject:overlayOptions afterDelay:1];
       
   151     [overlayOptions release];
       
   152 
       
   153     [self startGameEngine];
       
   154 
       
   155     [UIView beginAnimations:@"fade in" context:NULL];
       
   156     [UIView setAnimationDuration:1];
       
   157     self.parentController.view.alpha = 0;
       
   158     [UIView commitAnimations];
       
   159 }
       
   160 
       
   161 // set up variables for a local game
       
   162 -(void) startLocalGame:(NSDictionary *)withDictionary {
       
   163     self.gameType = gtLocal;
       
   164 
       
   165     NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
       
   166     [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"];
       
   167     NSString *newDateString = [outputFormatter stringFromDate:[NSDate date]];
       
   168     self.savePath = [SAVES_DIRECTORY() stringByAppendingFormat:@"%@.hws", newDateString];
       
   169     [outputFormatter release];
       
   170 
       
   171     [self.engineProtocol spawnThread:self.savePath withOptions:withDictionary];
       
   172     [self prepareEngineLaunch];
       
   173 }
       
   174 
       
   175 // set up variables for a save game
       
   176 -(void) startSaveGame:(NSString *)atPath {
       
   177     self.gameType = gtSave;
       
   178     self.savePath = atPath;
       
   179 
       
   180     [self.engineProtocol spawnThread:self.savePath];
       
   181     [self prepareEngineLaunch];
       
   182 }
       
   183 
       
   184 -(void) gameHasEndedWithStats:(NSArray *)stats {
       
   185     DLog(@"%@",stats);
       
   186 
       
   187     [self.overlayController removeOverlay];
       
   188     // can remove the file if the replay has ended
       
   189     if (self.gameType == gtSave)
       
   190         [[NSFileManager defaultManager] removeItemAtPath:self.savePath error:NULL];
       
   191 }
   186 
   192 
   187 @end
   193 @end