project_files/HedgewarsMobile/Classes/SDL_uikitappdelegate.m
changeset 3922 44804043b691
parent 3898 0a9c3735a713
child 3935 5ca27a0e9a63
equal deleted inserted replaced
3921:022dfe1431b7 3922:44804043b691
    37 
    37 
    38 #ifdef main
    38 #ifdef main
    39 #undef main
    39 #undef main
    40 #endif
    40 #endif
    41 
    41 
       
    42 #define BLACKVIEW_TAG 17935
       
    43 #define SECONDBLACKVIEW_TAG 48620
    42 #define VALGRIND "/opt/valgrind/bin/valgrind"
    44 #define VALGRIND "/opt/valgrind/bin/valgrind"
    43 
    45 
    44 int main (int argc, char *argv[]) {
    46 int main (int argc, char *argv[]) {
    45 #ifdef VALGRIND_REXEC
    47 #ifdef VALGRIND_REXEC
    46     // Using the valgrind build config, rexec ourself in valgrind
    48     // Using the valgrind build config, rexec ourself in valgrind
    54     [pool release];
    56     [pool release];
    55     return retVal;
    57     return retVal;
    56 }
    58 }
    57 
    59 
    58 @implementation SDLUIKitDelegate
    60 @implementation SDLUIKitDelegate
    59 @synthesize mainViewController;
    61 @synthesize mainViewController, uiwindow, secondWindow;
    60 
    62 
    61 // convenience method
    63 // convenience method
    62 +(SDLUIKitDelegate *)sharedAppDelegate {
    64 +(SDLUIKitDelegate *)sharedAppDelegate {
    63     // the delegate is set in UIApplicationMain(), which is guaranteed to be called before this method
    65     // the delegate is set in UIApplicationMain(), which is guaranteed to be called before this method
    64     return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
    66     return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
    65 }
    67 }
    66 
    68 
    67 -(id) init {
    69 -(id) init {
    68     if (self = [super init]){
    70     if (self = [super init]){
    69         mainViewController = nil;
    71         mainViewController = nil;
       
    72         uiwindow = nil;
       
    73         secondWindow = nil;
    70         isInGame = NO;
    74         isInGame = NO;
    71     }
    75     }
    72     return self;
    76     return self;
    73 }
    77 }
    74 
    78 
    75 -(void) dealloc {
    79 -(void) dealloc {
    76     [mainViewController release];
    80     [mainViewController release];
       
    81     [uiwindow release];
       
    82     [secondWindow release];
    77     [super dealloc];
    83     [super dealloc];
    78 }
    84 }
    79 
    85 
    80 // main routine for calling the actual game engine
    86 // main routine for calling the actual game engine
    81 -(void) startSDLgame:(NSDictionary *)gameDictionary {
    87 -(void) startSDLgame:(NSDictionary *)gameDictionary {
    82     UIWindow *aWin = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    88     UIWindow *gameWindow;
    83 
    89     if ([[UIScreen screens] count] > 1)
    84     UIView *blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, aWin.frame.size.width, aWin.frame.size.height)];
    90         gameWindow = self.secondWindow;
       
    91     else
       
    92         gameWindow = self.uiwindow;
       
    93     UIView *blackView = [[UIView alloc] initWithFrame:gameWindow.frame];
       
    94     blackView.backgroundColor = [UIColor blackColor];
    85     blackView.opaque = YES;
    95     blackView.opaque = YES;
    86     blackView.backgroundColor = [UIColor blackColor];
    96     blackView.tag = BLACKVIEW_TAG;
    87     [aWin addSubview:blackView];
    97     [gameWindow addSubview:blackView];
       
    98     [blackView release];
       
    99     
       
   100     if ([[UIScreen screens] count] > 1) {
       
   101         UIView *secondBlackView = [[UIView alloc] initWithFrame:self.uiwindow.frame];
       
   102         secondBlackView.backgroundColor = [UIColor blackColor];
       
   103         secondBlackView.opaque = YES;
       
   104         secondBlackView.tag = SECONDBLACKVIEW_TAG;
       
   105         secondBlackView.alpha = 0;
       
   106         [self.uiwindow addSubview:secondBlackView];
       
   107         [UIView beginAnimations:@"fading to game" context:NULL];
       
   108         [UIView setAnimationDuration:1];
       
   109         secondBlackView.alpha = 1;
       
   110         [UIView commitAnimations];
       
   111         [secondBlackView release];
       
   112     }
    88 
   113 
    89     // pull out useful configuration info from various files
   114     // pull out useful configuration info from various files
    90     GameSetup *setup = [[GameSetup alloc] initWithDictionary:gameDictionary];
   115     GameSetup *setup = [[GameSetup alloc] initWithDictionary:gameDictionary];
    91     NSNumber *isNetGameNum = [gameDictionary objectForKey:@"netgame"];
   116     NSNumber *isNetGameNum = [gameDictionary objectForKey:@"netgame"];
    92     
   117     
   102     isInGame = YES;
   127     isInGame = YES;
   103     Game(gameArgs);
   128     Game(gameArgs);
   104     isInGame = NO;
   129     isInGame = NO;
   105     free(gameArgs);
   130     free(gameArgs);
   106 
   131 
   107     [aWin makeKeyAndVisible];
   132     [uiwindow makeKeyAndVisible];
       
   133     
       
   134     UIView *refBlackView = [gameWindow viewWithTag:BLACKVIEW_TAG];
       
   135     UIView *refSecondBlackView = [self.uiwindow viewWithTag:SECONDBLACKVIEW_TAG];
   108     [UIView beginAnimations:@"fading in from ingame" context:NULL];
   136     [UIView beginAnimations:@"fading in from ingame" context:NULL];
   109     [UIView setAnimationDuration:1];
   137     [UIView setAnimationDuration:1];
   110     blackView.alpha = 0;
   138     refBlackView.alpha = 0;
       
   139     refSecondBlackView.alpha = 0;
   111     [UIView commitAnimations];
   140     [UIView commitAnimations];
   112     [blackView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1];
   141     [refBlackView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1];
   113     [blackView performSelector:@selector(release) withObject:nil afterDelay:1]; 
   142     [refSecondBlackView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1];
   114 }
   143 }
   115 
   144 
       
   145 // overlay with controls, become visible later, with a transparency effect
   116 -(void) displayOverlayLater:(NSNumber *)isNetGame {
   146 -(void) displayOverlayLater:(NSNumber *)isNetGame {
   117     // overlay with controls, become visible later, with a transparency effect
       
   118     OverlayViewController *overlayController = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];
   147     OverlayViewController *overlayController = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];
   119 
   148 
   120     // keyWindow is the frontmost window
   149     UIWindow *gameWindow;
   121     [[[UIApplication sharedApplication] keyWindow] addSubview:overlayController.view];
   150     if ([[UIScreen screens] count] > 1)
       
   151         gameWindow = self.uiwindow;
       
   152     else
       
   153         gameWindow = [[UIApplication sharedApplication] keyWindow];
       
   154     [gameWindow addSubview:overlayController.view];
   122     [overlayController release];
   155     [overlayController release];
   123 }
   156 }
   124 
   157 
   125 // override the direct execution of SDL_main to allow us to implement the frontend (or even using a nib)
   158 // override the direct execution of SDL_main to allow us to implement the frontend (or even using a nib)
   126 -(void) applicationDidFinishLaunching:(UIApplication *)application {
   159 -(void) applicationDidFinishLaunching:(UIApplication *)application {
   127     [application setStatusBarHidden:YES];
   160     [application setStatusBarHidden:YES];
   128 
   161 
   129     UIWindow *uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   162     self.uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   130 
   163 
   131     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
   164     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
   132         self.mainViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPad" bundle:nil];
   165         self.mainViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPad" bundle:nil];
   133     else
   166     else
   134         self.mainViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPhone" bundle:nil];
   167         self.mainViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPhone" bundle:nil];
   135 
   168 
   136     [uiwindow addSubview:self.mainViewController.view];
   169     [self.uiwindow addSubview:self.mainViewController.view];
   137     [self.mainViewController release];
   170     [self.mainViewController release];
   138     uiwindow.backgroundColor = [UIColor blackColor];
   171     self.uiwindow.backgroundColor = [UIColor blackColor];
   139     [uiwindow makeKeyAndVisible];
   172     [self.uiwindow makeKeyAndVisible];
   140 
   173 
       
   174     if ([[UIScreen screens]count] > 1) {
       
   175         /*
       
   176         CGSize maxSize = CGSizeZero;
       
   177         UIScreenMode *screenMode = nil;
       
   178         for (UIScreenMode *mode in [[[UIScreen screens] objectAtIndex:1] availableModes]) {
       
   179             if (mode.size.width > maxSize.width) {
       
   180                 maxSize = mode.size;
       
   181                 screenMode = mode;
       
   182             }
       
   183         }
       
   184         */
       
   185         DLog(@"dual head mode ftw");
       
   186         self.secondWindow = [[UIWindow alloc] initWithFrame:[[[UIScreen screens] objectAtIndex:1] bounds]];
       
   187         self.secondWindow.backgroundColor = [UIColor blackColor];
       
   188         self.secondWindow.screen = [[UIScreen screens] objectAtIndex:1];
       
   189         UIImage *titleImage = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"title.png"]];
       
   190         UIImageView *titleView = [[UIImageView alloc] initWithImage:titleImage];
       
   191         titleView.center = self.secondWindow.center;
       
   192         [self.secondWindow addSubview:titleView];
       
   193         [titleView release];
       
   194         [self.secondWindow makeKeyAndVisible];
       
   195     }
       
   196     
   141     // set working directory to resource path
   197     // set working directory to resource path
   142     [[NSFileManager defaultManager] changeCurrentDirectoryPath:[[NSBundle mainBundle] resourcePath]];
   198     [[NSFileManager defaultManager] changeCurrentDirectoryPath:[[NSBundle mainBundle] resourcePath]];
   143 }
   199 }
   144 
   200 
   145 -(void) applicationWillTerminate:(UIApplication *)application {
   201 -(void) applicationWillTerminate:(UIApplication *)application {