cocoaTouch/SDLOverrides/SDL_uikitappdelegate.m
changeset 2688 174c94b8ea72
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 - (void) startSDLgame {
       
    66 
       
    67 	/* run the user's application, passing argc and argv */
       
    68 	NSLog(@"Game is launching");
       
    69 	SDL_main(forward_argc, forward_argv);
       
    70 	// can't reach here yet
       
    71 	NSLog(@"Game exited");
       
    72 
       
    73 	//[self performSelector:@selector(makeNewView) withObject:nil afterDelay:0.0];
       
    74 	/* exit, passing the return status from the user's application */
       
    75 	//exit(exit_status);
       
    76 }
       
    77 
       
    78 // override the direct execution of SDL_main to allow us to implement the frontend (even using a nib)
       
    79 -(void) applicationDidFinishLaunching:(UIApplication *)application {
       
    80 	[application setStatusBarHidden:YES animated:NO];
       
    81 
       
    82 	/* Set working directory to resource path */
       
    83 	[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
       
    84 	
       
    85 	[window addSubview:controller.view];
       
    86 	[window makeKeyAndVisible];
       
    87 }
       
    88 
       
    89 -(void) applicationWillTerminate:(UIApplication *)application {
       
    90 	/* free the memory we used to hold copies of argc and argv */
       
    91 	int i;
       
    92 	for (i=0; i < forward_argc; i++) {
       
    93 		free(forward_argv[i]);
       
    94 	}
       
    95 	free(forward_argv);	
       
    96 	SDL_SendQuit();
       
    97 	 /* hack to prevent automatic termination.  See SDL_uikitevents.m for details */
       
    98 	// have to remove this otherwise game goes on when pushing the home button
       
    99 	//longjmp(*(jump_env()), 1);
       
   100 	
       
   101 	NSLog(@"Closing App...");
       
   102 }
       
   103 
       
   104 -(void) applicationWillResignActive:(UIApplication*)application
       
   105 {
       
   106 //	NSLog(@"%@", NSStringFromSelector(_cmd));
       
   107 	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
       
   108 }
       
   109 
       
   110 -(void) applicationDidBecomeActive:(UIApplication*)application
       
   111 {
       
   112 //	NSLog(@"%@", NSStringFromSelector(_cmd));
       
   113 	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_RESTORED, 0, 0);
       
   114 }
       
   115 
       
   116 /*
       
   117 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
       
   118 	NSLog(@"Rotating...");
       
   119 	return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
       
   120 }
       
   121 */
       
   122 
       
   123 -(void) dealloc {
       
   124 	[controller release];
       
   125 	[window release];
       
   126 	[super dealloc];
       
   127 }
       
   128 
       
   129 @end