cocoaTouch/SDL_uikitappdelegate.m
changeset 2688 174c94b8ea72
parent 2687 28b8330b8af1
child 2689 dfda97c153a4
equal deleted inserted replaced
2687:28b8330b8af1 2688:174c94b8ea72
     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_events_c.h"
       
    26 #import "jumphack.h"
       
    27 #import "SDL_video.h"
       
    28 
       
    29 #ifdef main
       
    30 #undef main
       
    31 #endif
       
    32 
       
    33 extern int SDL_main(int argc, char *argv[]);
       
    34 static int forward_argc;
       
    35 static char **forward_argv;
       
    36 
       
    37 int main (int argc, char **argv) {
       
    38 	int i;
       
    39 	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
    40 	
       
    41 	/* store arguments */
       
    42 	forward_argc = argc;
       
    43 	forward_argv = (char **)malloc(argc * sizeof(char *));
       
    44 	for (i = 0; i < argc; i++) {
       
    45 		forward_argv[i] = malloc( (strlen(argv[i])+1) * sizeof(char));
       
    46 		strcpy(forward_argv[i], argv[i]);
       
    47 	}
       
    48 
       
    49 	/* Give over control to run loop, SDLUIKitDelegate will handle most things from here */
       
    50 	UIApplicationMain(argc, argv, NULL, @"SDLUIKitDelegate");
       
    51 	
       
    52 	[pool release];
       
    53 }
       
    54 
       
    55 @implementation SDLUIKitDelegate
       
    56 
       
    57 @synthesize window, windowID, controller;
       
    58 
       
    59 /* convenience method */
       
    60 +(SDLUIKitDelegate *)sharedAppDelegate {
       
    61 	/* the delegate is set in UIApplicationMain(), which is garaunteed to be called before this method */
       
    62 	return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
       
    63 }
       
    64 
       
    65 /*- (id)init {
       
    66 	self = [super init];
       
    67 	window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;
       
    68 	windowID = 0;
       
    69 	return self;
       
    70 }*/
       
    71 
       
    72 - (void) startSDLgame {
       
    73 	// HACK: remove the current window and let SDL create a new one
       
    74 	[self.window release];
       
    75 	if (nil != self.window)
       
    76 		self.window = nil;
       
    77 	
       
    78 	/* run the user's application, passing argc and argv */
       
    79 	NSLog(@"Game is launching");
       
    80 	SDL_main(forward_argc, forward_argv);
       
    81 	// can't reach here yet
       
    82 	NSLog(@"Game exited");
       
    83 
       
    84 	//[self performSelector:@selector(makeNewView) withObject:nil afterDelay:0.0];
       
    85 	/* exit, passing the return status from the user's application */
       
    86 	//exit(exit_status);
       
    87 }
       
    88 
       
    89 // override the direct execution of SDL_main to allow us to implement the frontend (even using a nib)
       
    90 -(void) applicationDidFinishLaunching:(UIApplication *)application {
       
    91 	[application setStatusBarHidden:YES animated:NO];
       
    92 
       
    93 	/* Set working directory to resource path */
       
    94 	[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
       
    95 	
       
    96 	[window addSubview:controller.view];
       
    97 	[window makeKeyAndVisible];
       
    98 }
       
    99 
       
   100 -(void) applicationWillTerminate:(UIApplication *)application {
       
   101 	/* free the memory we used to hold copies of argc and argv */
       
   102 	int i;
       
   103 	for (i=0; i < forward_argc; i++) {
       
   104 		free(forward_argv[i]);
       
   105 	}
       
   106 	free(forward_argv);	
       
   107 	SDL_SendQuit();
       
   108 	 /* hack to prevent automatic termination.  See SDL_uikitevents.m for details */
       
   109 	// have to remove this otherwise game goes on when pushing the home button
       
   110 	//longjmp(*(jump_env()), 1);
       
   111 	
       
   112 	NSLog(@"Closing App...");
       
   113 }
       
   114 
       
   115 -(void) applicationWillResignActive:(UIApplication*)application
       
   116 {
       
   117 //	NSLog(@"%@", NSStringFromSelector(_cmd));
       
   118 	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
       
   119 }
       
   120 
       
   121 -(void) applicationDidBecomeActive:(UIApplication*)application
       
   122 {
       
   123 //	NSLog(@"%@", NSStringFromSelector(_cmd));
       
   124 	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_RESTORED, 0, 0);
       
   125 }
       
   126 
       
   127 /*
       
   128 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
       
   129 	NSLog(@"Rotating...");
       
   130 	return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
       
   131 }
       
   132 */
       
   133 
       
   134 -(void) dealloc {
       
   135 	[controller release];
       
   136 	[window release];
       
   137 	[super dealloc];
       
   138 }
       
   139 
       
   140 @end