project_files/HedgewarsMobile/Classes/SDL_uikitappdelegate.m
changeset 3598 a8aa06bae895
parent 3548 4d220ee7c75f
child 3616 85d69ddb41b6
equal deleted inserted replaced
3597:978c30ef50fc 3598:a8aa06bae895
    52     [pool release];
    52     [pool release];
    53     return retVal;
    53     return retVal;
    54 }
    54 }
    55 
    55 
    56 @implementation SDLUIKitDelegate
    56 @implementation SDLUIKitDelegate
    57 @synthesize uiwindow, window;
       
    58 
    57 
    59 // convenience method
    58 // convenience method
    60 +(SDLUIKitDelegate *)sharedAppDelegate {
    59 +(SDLUIKitDelegate *)sharedAppDelegate {
    61     // the delegate is set in UIApplicationMain(), which is guaranteed to be called before this method
    60     // the delegate is set in UIApplicationMain(), which is guaranteed to be called before this method
    62     return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
    61     return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
    64 
    63 
    65 -(id) init {
    64 -(id) init {
    66     if (self = [super init]){
    65     if (self = [super init]){
    67         mainViewController = nil;
    66         mainViewController = nil;
    68         isInGame = NO;
    67         isInGame = NO;
    69         self.uiwindow = nil;     
       
    70         self.window = NULL;
       
    71     }
    68     }
    72     return self;
    69     return self;
    73 }
    70 }
    74 
    71 
    75 -(void) dealloc {
    72 -(void) dealloc {
    76     SDL_DestroyWindow(self.window);
       
    77      [uiwindow release];
       
    78     [mainViewController release];
    73     [mainViewController release];
    79     [super dealloc];
    74     [super dealloc];
    80 }
    75 }
    81 
    76 
    82 // main routine for calling the actual game engine
    77 // main routine for calling the actual game engine
    91     [setup startThread:@"engineProtocol"];
    86     [setup startThread:@"engineProtocol"];
    92     const char **gameArgs = [setup getSettings];
    87     const char **gameArgs = [setup getSettings];
    93     [setup release];
    88     [setup release];
    94 
    89 
    95     // since the sdlwindow is not yet created, we add the overlayController with a delay
    90     // since the sdlwindow is not yet created, we add the overlayController with a delay
    96     [self performSelector:@selector(displayOverlayLater) withObject:nil afterDelay:0.5];
    91     [self performSelector:@selector(displayOverlayLater) withObject:nil afterDelay:0.3];
    97     
    92     
    98     // this is the pascal fuction that starts the game (wrapped around isInGame)
    93     // this is the pascal fuction that starts the game (wrapped around isInGame)
    99     isInGame = YES;
    94     isInGame = YES;
   100     Game(gameArgs);
    95     Game(gameArgs);
   101     isInGame = NO;
    96     isInGame = NO;
       
    97     free(gameArgs);
   102     
    98     
   103     free(gameArgs);
    99     UIWindow *aWin = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
       
   100     [aWin makeKeyAndVisible];
       
   101     aWin =  [[[UIApplication sharedApplication] windows] lastObject];
       
   102     [aWin removeFromSuperview];
       
   103     
       
   104     DLog(@"%@",[[UIApplication sharedApplication] windows]);
   104     
   105     
   105     [UIView beginAnimations:@"inserting main controller" context:NULL];
   106     [UIView beginAnimations:@"inserting main controller" context:NULL];
   106     [UIView setAnimationDuration:1];
   107     [UIView setAnimationDuration:1];
   107     mainViewController.view.alpha = 1;
   108     mainViewController.view.alpha = 1;
   108     [UIView commitAnimations];
   109     [UIView commitAnimations];
   109 }
   110 }
   110 
   111 
   111 -(void) displayOverlayLater {
   112 -(void) displayOverlayLater {
   112     // overlay with controls, become visible after 4 seconds, with a transparency effect
   113     // overlay with controls, become visible later, with a transparency effect
   113     OverlayViewController *overlayController = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];
   114     OverlayViewController *overlayController = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];
   114     
   115 
       
   116     // keyWindow is the frontmost window
   115     [[[UIApplication sharedApplication] keyWindow] addSubview:overlayController.view];
   117     [[[UIApplication sharedApplication] keyWindow] addSubview:overlayController.view];
   116     [overlayController release];
   118     [overlayController release];
   117 }
   119 }
   118 
   120 
   119 // override the direct execution of SDL_main to allow us to implement the frontend (or even using a nib)
   121 // override the direct execution of SDL_main to allow us to implement the frontend (or even using a nib)
   120 -(void) applicationDidFinishLaunching:(UIApplication *)application {
   122 -(void) applicationDidFinishLaunching:(UIApplication *)application {
   121     [application setStatusBarHidden:YES];
   123     [application setStatusBarHidden:YES];
   122     [application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];  
   124     [application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];  
   123     
   125     
   124     uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   126     UIWindow *uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   125     uiwindow.backgroundColor = [UIColor blackColor];
   127     uiwindow.backgroundColor = [UIColor blackColor];
   126     // needed to keep the app running after a game (gets released in sdl_uikitwindow)
       
   127     [uiwindow retain];
       
   128     
   128     
   129     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
   129     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
   130         mainViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPad" bundle:nil];
   130         mainViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPad" bundle:nil];
   131     else
   131     else
   132         mainViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPhone" bundle:nil];
   132         mainViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPhone" bundle:nil];
   146         longjmp(*(jump_env()), 1);
   146         longjmp(*(jump_env()), 1);
   147     }
   147     }
   148 }
   148 }
   149 
   149 
   150 -(void) applicationDidReceiveMemoryWarning:(UIApplication *)application {
   150 -(void) applicationDidReceiveMemoryWarning:(UIApplication *)application {
       
   151     if (mainViewController.view.superview == nil)
       
   152         mainViewController = nil;
       
   153     MSG_MEMCLEAN();
   151     print_free_memory();
   154     print_free_memory();
   152 }
   155 }
   153 
   156 
   154 -(void) applicationWillResignActive:(UIApplication *)application {
   157 -(void) applicationWillResignActive:(UIApplication *)application {
   155     //NSLog(@"%@", NSStringFromSelector(_cmd));
       
   156     if (isInGame) {
   158     if (isInGame) {
   157         HW_pause();
   159         HW_pause();
   158         
   160         
   159         /*
       
   160         // Send every window on every screen a MINIMIZED event.
   161         // Send every window on every screen a MINIMIZED event.
   161         SDL_VideoDevice *_this = SDL_GetVideoDevice();
   162         SDL_VideoDevice *_this = SDL_GetVideoDevice();
   162         if (!_this)
   163         if (!_this)
   163             return;
   164             return;
   164 
   165 
   167             const SDL_VideoDisplay *display = &_this->displays[i];
   168             const SDL_VideoDisplay *display = &_this->displays[i];
   168             SDL_Window *window;
   169             SDL_Window *window;
   169             for (window = display->windows; window != nil; window = window->next)
   170             for (window = display->windows; window != nil; window = window->next)
   170                 SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   171                 SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   171         }
   172         }
   172         */
       
   173     }
   173     }
   174 }
   174 }
   175 
   175 
   176 -(void) applicationDidBecomeActive:(UIApplication *)application {
   176 -(void) applicationDidBecomeActive:(UIApplication *)application {
   177     //NSLog(@"%@", NSStringFromSelector(_cmd));
   177     //NSLog(@"%@", NSStringFromSelector(_cmd));
   178     if (isInGame) {
   178     if (isInGame) {
   179         HW_pause();
   179         HW_pause();
   180 
   180 
   181         /*
       
   182         // Send every window on every screen a RESTORED event.
   181         // Send every window on every screen a RESTORED event.
   183         SDL_VideoDevice *_this = SDL_GetVideoDevice();
   182         SDL_VideoDevice *_this = SDL_GetVideoDevice();
   184         if (!_this)
   183         if (!_this)
   185             return;
   184             return;
   186 
   185 
   189             const SDL_VideoDisplay *display = &_this->displays[i];
   188             const SDL_VideoDisplay *display = &_this->displays[i];
   190             SDL_Window *window;
   189             SDL_Window *window;
   191             for (window = display->windows; window != nil; window = window->next)
   190             for (window = display->windows; window != nil; window = window->next)
   192                 SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
   191                 SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
   193         }
   192         }
   194         */
       
   195     }
   193     }
   196 }
   194 }
   197 
   195 
   198 @end
   196 @end