cocoaTouch/SDL_uikitappdelegate.m
changeset 2678 334016e8d895
child 2685 0ba746be5d59
equal deleted inserted replaced
2677:83ad68ceef72 2678:334016e8d895
       
     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 	NSLog(@"Game exited");
       
    82 	
       
    83 	//[self performSelector:@selector(makeNewView) withObject:nil afterDelay:0.0];
       
    84 	/* exit, passing the return status from the user's application */
       
    85 	//exit(exit_status);
       
    86 }
       
    87 
       
    88 // override the direct execution of SDL_main to allow us to implement the frontend (even using a nib)
       
    89 -(void) applicationDidFinishLaunching:(UIApplication *)application {
       
    90 	[application setStatusBarHidden:YES animated:NO];
       
    91 
       
    92 	/* Set working directory to resource path */
       
    93 	[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
       
    94 	
       
    95 	[window addSubview:controller.view];
       
    96 	[window makeKeyAndVisible];
       
    97 }
       
    98 
       
    99 -(void) applicationWillTerminate:(UIApplication *)application {
       
   100 	/* free the memory we used to hold copies of argc and argv */
       
   101 	int i;
       
   102 	for (i=0; i<forward_argc; i++) {
       
   103 		free(forward_argv[i]);
       
   104 	}
       
   105 	free(forward_argv);	
       
   106 	SDL_SendQuit();
       
   107 	 /* hack to prevent automatic termination.  See SDL_uikitevents.m for details */
       
   108 	longjmp(*(jump_env()), 1);
       
   109 }
       
   110 
       
   111 -(void) applicationWillResignActive:(UIApplication*)application
       
   112 {
       
   113 //	NSLog(@"%@", NSStringFromSelector(_cmd));
       
   114 	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
       
   115 }
       
   116 
       
   117 -(void) applicationDidBecomeActive:(UIApplication*)application
       
   118 {
       
   119 //	NSLog(@"%@", NSStringFromSelector(_cmd));
       
   120 	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_RESTORED, 0, 0);
       
   121 }
       
   122 
       
   123 /*
       
   124 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
       
   125 	NSLog(@"Rotating...");
       
   126 	return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
       
   127 }
       
   128 */
       
   129 
       
   130 -(void) dealloc {
       
   131 	[controller release];
       
   132 	[window release];
       
   133 	[super dealloc];
       
   134 }
       
   135 
       
   136 @end