project_files/HedgewarsMobile/Classes/otherSrc/SDL_uikitappdelegate.m
changeset 3546 ccf4854df294
parent 3545 b07ee704f35d
child 3547 02875b1145b7
equal deleted inserted replaced
3545:b07ee704f35d 3546:ccf4854df294
     1 /*
       
     2  SDL - Simple DirectMedia Layer
       
     3  Copyright (C) 1997-2009 Sam Lantinga
       
     4  
       
     5  This library is free software; you can redistribute it and/or
       
     6  modify it under the terms of the GNU Lesser General Public
       
     7  License as published by the Free Software Foundation; either
       
     8  version 2.1 of the License, or (at your option) any later version.
       
     9  
       
    10  This library is distributed in the hope that it will be useful,
       
    11  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13  Lesser General Public License for more details.
       
    14  
       
    15  You should have received a copy of the GNU Lesser General Public
       
    16  License along with this library; if not, write to the Free Software
       
    17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    18  
       
    19  Sam Lantinga, mods for Hedgewars by Vittorio Giovara
       
    20  slouken@libsdl.org, vittorio.giovara@gmail.com
       
    21 */
       
    22 
       
    23 #import "SDL_uikitappdelegate.h"
       
    24 #import "SDL_uikitopenglview.h"
       
    25 #import "SDL_uikitwindow.h"
       
    26 #import "SDL_events_c.h"
       
    27 #import "../SDL_sysvideo.h"
       
    28 #import "jumphack.h"
       
    29 #import "SDL_video.h"
       
    30 #import "GameSetup.h"
       
    31 #import "PascalImports.h"
       
    32 #import "MainMenuViewController.h"
       
    33 #import "OverlayViewController.h"
       
    34 #import "CommodityFunctions.h"
       
    35 
       
    36 #ifdef main
       
    37 #undef main
       
    38 #endif
       
    39 
       
    40 #define VALGRIND "/opt/valgrind/bin/valgrind"
       
    41 
       
    42 int main (int argc, char *argv[]) {
       
    43 #ifdef VALGRIND_REXEC
       
    44     // Using the valgrind build config, rexec ourself in valgrind
       
    45     // from http://landonf.bikemonkey.org/code/iphone/iPhone_Simulator_Valgrind.20081224.html
       
    46     if (argc < 2 || (argc >= 2 && strcmp(argv[1], "-valgrind") != 0))
       
    47         execl(VALGRIND, VALGRIND, "--leak-check=full", argv[0], "-valgrind", NULL);
       
    48 #endif
       
    49 
       
    50 	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
    51 	int retVal = UIApplicationMain(argc, argv, nil, @"SDLUIKitDelegate");
       
    52 	[pool release];
       
    53 	return retVal;
       
    54 }
       
    55 
       
    56 @implementation SDLUIKitDelegate
       
    57 @synthesize uiwindow, window;
       
    58 
       
    59 // convenience method
       
    60 +(SDLUIKitDelegate *)sharedAppDelegate {
       
    61 	// the delegate is set in UIApplicationMain(), which is guaranteed to be called before this method
       
    62 	return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
       
    63 }
       
    64 
       
    65 -(id) init {
       
    66 	if (self = [super init]){
       
    67         mainViewController = nil;
       
    68         isInGame = NO;
       
    69         self.uiwindow = nil; 	 
       
    70         self.window = NULL;
       
    71     }
       
    72     return self;
       
    73 }
       
    74 
       
    75 -(void) dealloc {
       
    76     SDL_DestroyWindow(self.window);
       
    77      [uiwindow release];
       
    78     [mainViewController release];
       
    79 	[super dealloc];
       
    80 }
       
    81 
       
    82 // main routine for calling the actual game engine
       
    83 -(IBAction) startSDLgame {
       
    84     [UIView beginAnimations:@"removing main controller" context:NULL];
       
    85 	[UIView setAnimationDuration:1];
       
    86 	mainViewController.view.alpha = 0;
       
    87 	[UIView commitAnimations];
       
    88 
       
    89     // pull out useful configuration info from various files
       
    90 	GameSetup *setup = [[GameSetup alloc] init];
       
    91 	[setup startThread:@"engineProtocol"];
       
    92 	const char **gameArgs = [setup getSettings];
       
    93 	[setup release];
       
    94 
       
    95     // since the sdlwindow is not yet created, we add the overlayController with a delay
       
    96     [self performSelector:@selector(displayOverlayLater) withObject:nil afterDelay:0.5];
       
    97     
       
    98     // this is the pascal fuction that starts the game (wrapped around isInGame)
       
    99     isInGame = YES;
       
   100 	Game(gameArgs);
       
   101     isInGame = NO;
       
   102     
       
   103     free(gameArgs);
       
   104     
       
   105     [UIView beginAnimations:@"inserting main controller" context:NULL];
       
   106 	[UIView setAnimationDuration:1];
       
   107 	mainViewController.view.alpha = 1;
       
   108 	[UIView commitAnimations];
       
   109 }
       
   110 
       
   111 -(void) displayOverlayLater {
       
   112     // overlay with controls, become visible after 4 seconds, with a transparency effect
       
   113     OverlayViewController *overlayController = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];
       
   114     
       
   115     [[[UIApplication sharedApplication] keyWindow] addSubview:overlayController.view];
       
   116     [overlayController release];
       
   117 }
       
   118 
       
   119 // override the direct execution of SDL_main to allow us to implement the frontend (or even using a nib)
       
   120 -(void) applicationDidFinishLaunching:(UIApplication *)application {
       
   121     [application setStatusBarHidden:YES];
       
   122     [application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];  
       
   123     
       
   124     uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
       
   125 	uiwindow.backgroundColor = [UIColor blackColor];
       
   126     // needed to keep the app running after a game (gets released in sdl_uikitwindow)
       
   127 	[uiwindow retain];
       
   128     
       
   129     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
       
   130         mainViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPad" bundle:nil];
       
   131     else
       
   132         mainViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPhone" bundle:nil];
       
   133 
       
   134 	[uiwindow addSubview:mainViewController.view];
       
   135 	[uiwindow makeKeyAndVisible];
       
   136 
       
   137 	// Set working directory to resource path
       
   138 	[[NSFileManager defaultManager] changeCurrentDirectoryPath:[[NSBundle mainBundle] resourcePath]];
       
   139 }
       
   140 
       
   141 -(void) applicationWillTerminate:(UIApplication *)application {
       
   142 	SDL_SendQuit();
       
   143     if (isInGame) {
       
   144         HW_terminate(YES);
       
   145         // hack to prevent automatic termination. See SDL_uikitevents.m for details
       
   146         longjmp(*(jump_env()), 1);
       
   147     }
       
   148 }
       
   149 
       
   150 -(void) applicationWillResignActive:(UIApplication *)application {
       
   151 	//NSLog(@"%@", NSStringFromSelector(_cmd));
       
   152     if (isInGame) {
       
   153         HW_pause();
       
   154         
       
   155         /*
       
   156         // Send every window on every screen a MINIMIZED event.
       
   157         SDL_VideoDevice *_this = SDL_GetVideoDevice();
       
   158         if (!_this)
       
   159             return;
       
   160 
       
   161         int i;
       
   162         for (i = 0; i < _this->num_displays; i++) {
       
   163             const SDL_VideoDisplay *display = &_this->displays[i];
       
   164             SDL_Window *window;
       
   165             for (window = display->windows; window != nil; window = window->next)
       
   166                 SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
       
   167         }
       
   168         */
       
   169     }
       
   170 }
       
   171 
       
   172 -(void) applicationDidBecomeActive:(UIApplication *)application {
       
   173 	//NSLog(@"%@", NSStringFromSelector(_cmd));
       
   174     if (isInGame) {
       
   175         HW_pause();
       
   176 
       
   177         /*
       
   178         // Send every window on every screen a RESTORED event.
       
   179         SDL_VideoDevice *_this = SDL_GetVideoDevice();
       
   180         if (!_this)
       
   181             return;
       
   182 
       
   183         int i;
       
   184         for (i = 0; i < _this->num_displays; i++) {
       
   185             const SDL_VideoDisplay *display = &_this->displays[i];
       
   186             SDL_Window *window;
       
   187             for (window = display->windows; window != nil; window = window->next)
       
   188                 SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
       
   189         }
       
   190         */
       
   191     }
       
   192 }
       
   193 
       
   194 @end