cocoaTouch/SDLOverrides/SDL_uikitappdelegate.m
changeset 3463 23c50be687a9
parent 3385 361bd29293f4
child 3479 972ae3ec178a
equal deleted inserted replaced
3462:4b36933dce1d 3463:23c50be687a9
    18  
    18  
    19  Sam Lantinga, mods for Hedgewars by Vittorio Giovara
    19  Sam Lantinga, mods for Hedgewars by Vittorio Giovara
    20  slouken@libsdl.org, vittorio.giovara@gmail.com
    20  slouken@libsdl.org, vittorio.giovara@gmail.com
    21 */
    21 */
    22 
    22 
    23 #import <pthread.h>
       
    24 #import "SDL_uikitappdelegate.h"
    23 #import "SDL_uikitappdelegate.h"
    25 #import "SDL_uikitopenglview.h"
    24 #import "SDL_uikitopenglview.h"
       
    25 #import "SDL_uikitwindow.h"
    26 #import "SDL_events_c.h"
    26 #import "SDL_events_c.h"
       
    27 #import "../SDL_sysvideo.h"
    27 #import "jumphack.h"
    28 #import "jumphack.h"
    28 #import "SDL_video.h"
    29 #import "SDL_video.h"
    29 #import "GameSetup.h"
    30 #import "GameSetup.h"
    30 #import "PascalImports.h"
    31 #import "PascalImports.h"
    31 #import "MainMenuViewController.h"
    32 #import "MainMenuViewController.h"
    52 	[pool release];
    53 	[pool release];
    53 	return retVal;
    54 	return retVal;
    54 }
    55 }
    55 
    56 
    56 @implementation SDLUIKitDelegate
    57 @implementation SDLUIKitDelegate
    57 @synthesize uiwindow, window;
       
    58 
    58 
    59 // convenience method
    59 // convenience method
    60 +(SDLUIKitDelegate *)sharedAppDelegate {
    60 +(SDLUIKitDelegate *)sharedAppDelegate {
    61 	// the delegate is set in UIApplicationMain(), which is guaranteed to be called before this method
    61 	// the delegate is set in UIApplicationMain(), which is guaranteed to be called before this method
    62 	return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
    62 	return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
    63 }
    63 }
    64 
    64 
    65 -(id) init {
    65 -(id) init {
    66 	if (self = [super init]){
    66 	if (self = [super init]){
    67         self.uiwindow = nil;
       
    68         self.window = NULL;
       
    69         mainViewController = nil;
    67         mainViewController = nil;
    70         isInGame = NO;
    68         isInGame = NO;
    71         return self;
    69     }
    72     } else 
    70     return self;
    73         return nil;
       
    74 }
    71 }
    75 
    72 
    76 -(void) dealloc {
    73 -(void) dealloc {
    77     SDL_DestroyWindow(self.window);
       
    78     [mainViewController release];
    74     [mainViewController release];
    79 	[uiwindow release];
       
    80 	[super dealloc];
    75 	[super dealloc];
    81 }
    76 }
    82 
    77 
    83 // main routine for calling the actual game engine
    78 // main routine for calling the actual game engine
    84 -(IBAction) startSDLgame {
    79 -(IBAction) startSDLgame {
    85     [mainViewController disappear];
    80     [UIView beginAnimations:@"removing main controller" context:NULL];
       
    81 	[UIView setAnimationDuration:1];
       
    82 	mainViewController.view.alpha = 0;
       
    83 	[UIView commitAnimations];
    86 
    84 
    87     // pull out useful configuration info from various files
    85     // pull out useful configuration info from various files
    88 	GameSetup *setup = [[GameSetup alloc] init];
    86 	GameSetup *setup = [[GameSetup alloc] init];
    89 	[setup startThread:@"engineProtocol"];
    87 	[setup startThread:@"engineProtocol"];
    90 	const char **gameArgs = [setup getSettings];
    88 	const char **gameArgs = [setup getSettings];
    91 	[setup release];
    89 	[setup release];
       
    90 
       
    91     //NSLog(@"%@",[[[UIApplication sharedApplication] windows]);
       
    92     // since the sdlwindow is not yet created, we add the overlayController with a delay
       
    93     [self performSelector:@selector(later) withObject:nil afterDelay:4];
    92     
    94     
    93     OverlayViewController *overlayController;
    95     // this is the pascal fuction that starts the game (wrapped around isInGame)
    94     // overlay with controls, become visible after 2 seconds
       
    95     overlayController = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];
       
    96     
       
    97     [uiwindow addSubview:overlayController.view];
       
    98     [overlayController release];
       
    99 
       
   100     isInGame = YES;
    96     isInGame = YES;
   101 	Game(gameArgs); // this is the pascal fuction that starts the game
    97 	Game(gameArgs);
   102     isInGame = NO;
    98     isInGame = NO;
   103     
    99     
   104     free(gameArgs);
   100     free(gameArgs);
   105     [overlayController.view removeFromSuperview];
   101     //[overlayController.view removeFromSuperview];
   106     
   102     
   107     [mainViewController appear];
   103     [UIView beginAnimations:@"inserting main controller" context:NULL];
       
   104 	[UIView setAnimationDuration:1];
       
   105 	mainViewController.view.alpha = 1;
       
   106 	[UIView commitAnimations];
   108 }
   107 }
   109 
   108 
   110 // override the direct execution of SDL_main to allow us to implement the frontend (even using a nib)
   109 -(void) later {
       
   110     // overlay with controls, become visible after 4 seconds, with a transparency effect
       
   111     OverlayViewController *overlayController = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];
       
   112     
       
   113     [[[UIApplication sharedApplication] keyWindow] addSubview:overlayController.view];
       
   114     [overlayController release];
       
   115 }
       
   116 
       
   117 // override the direct execution of SDL_main to allow us to implement the frontend (or even using a nib)
   111 -(void) applicationDidFinishLaunching:(UIApplication *)application {
   118 -(void) applicationDidFinishLaunching:(UIApplication *)application {
   112 	//[application setStatusBarHidden:YES animated:NO];
       
   113     //[application setStatusBarHidden:YES withAnimation:NO];
       
   114     [application setStatusBarHidden:YES];
   119     [application setStatusBarHidden:YES];
   115     [application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];  
   120     [application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];  
   116     
   121     
   117 	self.uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   122     UIWindow *uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   118 	self.uiwindow.backgroundColor = [UIColor blackColor];
   123 	uiwindow.backgroundColor = [UIColor blackColor];
   119 	
   124 	
   120     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
   125     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
   121         mainViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPad" bundle:nil];
   126         mainViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPad" bundle:nil];
   122     else
   127     else
   123         mainViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPhone" bundle:nil];
   128         mainViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPhone" bundle:nil];
       
   129 
   124 	[uiwindow addSubview:mainViewController.view];
   130 	[uiwindow addSubview:mainViewController.view];
   125     
   131 	[uiwindow makeKeyAndVisible];
       
   132 
   126 	// Set working directory to resource path
   133 	// Set working directory to resource path
   127 	[[NSFileManager defaultManager] changeCurrentDirectoryPath:[[NSBundle mainBundle] resourcePath]];
   134 	[[NSFileManager defaultManager] changeCurrentDirectoryPath:[[NSBundle mainBundle] resourcePath]];
   128 
       
   129 	[uiwindow makeKeyAndVisible];
       
   130 	[uiwindow layoutSubviews];
       
   131 }
   135 }
   132 
   136 
   133 -(void) applicationWillTerminate:(UIApplication *)application {
   137 -(void) applicationWillTerminate:(UIApplication *)application {
   134 	SDL_SendQuit();
   138 	SDL_SendQuit();
   135     if (isInGame) {
   139     if (isInGame) {
   139     }
   143     }
   140 }
   144 }
   141 
   145 
   142 -(void) applicationWillResignActive:(UIApplication *)application {
   146 -(void) applicationWillResignActive:(UIApplication *)application {
   143 	//NSLog(@"%@", NSStringFromSelector(_cmd));
   147 	//NSLog(@"%@", NSStringFromSelector(_cmd));
   144     if (isInGame) HW_pause();
   148     if (isInGame) {
   145 	//SDL_SendWindowEvent(self.window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   149         HW_pause();
       
   150         
       
   151         // Send every window on every screen a MINIMIZED event.
       
   152         SDL_VideoDevice *_this = SDL_GetVideoDevice();
       
   153         if (!_this)
       
   154             return;
       
   155 
       
   156         int i;
       
   157         for (i = 0; i < _this->num_displays; i++) {
       
   158             const SDL_VideoDisplay *display = &_this->displays[i];
       
   159             SDL_Window *window;
       
   160             for (window = display->windows; window != nil; window = window->next)
       
   161                 SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
       
   162         }
       
   163     }
   146 }
   164 }
   147 
   165 
   148 -(void) applicationDidBecomeActive:(UIApplication *)application {
   166 -(void) applicationDidBecomeActive:(UIApplication *)application {
   149 	//NSLog(@"%@", NSStringFromSelector(_cmd));
   167 	//NSLog(@"%@", NSStringFromSelector(_cmd));
   150     if (isInGame) HW_pause();
   168     if (isInGame) {
   151 	//SDL_SendWindowEvent(self.window, SDL_WINDOWEVENT_RESTORED, 0, 0);
   169         HW_pause();
       
   170 
       
   171         // Send every window on every screen a RESTORED event.
       
   172         SDL_VideoDevice *_this = SDL_GetVideoDevice();
       
   173         if (!_this)
       
   174             return;
       
   175 
       
   176         int i;
       
   177         for (i = 0; i < _this->num_displays; i++) {
       
   178             const SDL_VideoDisplay *display = &_this->displays[i];
       
   179             SDL_Window *window;
       
   180             for (window = display->windows; window != nil; window = window->next)
       
   181                 SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
       
   182         }
       
   183     }
   152 }
   184 }
   153 
   185 
   154 @end
   186 @end