src/video/uikit/SDL_uikitappdelegate.m
author Sam Lantinga <slouken@libsdl.org>
Sat, 17 Oct 2009 07:36:45 +0000
changeset 3400 4ec48602f1db
parent 2859 99210400e8b9
child 3422 ec4db979dddb
permissions -rw-r--r--
iPhone interruption patch / SDL 1.3

Eric Wing to Sam

I've been sitting on this too long. I need to push.
It's untested because of the unrelated crashing bug I've been experiencing.
Also have a fix for SIZEOF_VOIDP in the config for both iPhone and Mac.
     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
    20  slouken@libsdl.org
    21 */
    22 
    23 #import "SDL_uikitappdelegate.h"
    24 #import "SDL_uikitopenglview.h"
    25 #import "SDL_events_c.h"
    26 #import "jumphack.h"
    27 
    28 #ifdef main
    29 #undef main
    30 #endif
    31 
    32 extern int SDL_main(int argc, char *argv[]);
    33 static int forward_argc;
    34 static char **forward_argv;
    35 
    36 int main(int argc, char **argv) {
    37 
    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 
    56 @implementation SDLUIKitDelegate
    57 
    58 @synthesize window;
    59 @synthesize windowID;
    60 
    61 /* convenience method */
    62 +(SDLUIKitDelegate *)sharedAppDelegate {
    63 	/* the delegate is set in UIApplicationMain(), which is garaunteed to be called before this method */
    64 	return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
    65 }
    66 
    67 - (id)init {
    68 	self = [super init];
    69 	window = nil;
    70 	windowID = 0;
    71 	return self;
    72 }
    73 
    74 - (void)applicationDidFinishLaunching:(UIApplication *)application {
    75 			
    76 	/* Set working directory to resource path */
    77 	[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
    78 	
    79 	/* run the user's application, passing argc and argv */
    80 	int exit_status = SDL_main(forward_argc, forward_argv);
    81 	
    82 	/* free the memory we used to hold copies of argc and argv */
    83 	int i;
    84 	for (i=0; i<forward_argc; i++) {
    85 		free(forward_argv[i]);
    86 	}
    87 	free(forward_argv);	
    88 		
    89 	/* exit, passing the return status from the user's application */
    90 	exit(exit_status);
    91 		
    92 }
    93 
    94 - (void)applicationWillTerminate:(UIApplication *)application {
    95 	
    96 	SDL_SendQuit();
    97 	 /* hack to prevent automatic termination.  See SDL_uikitevents.m for details */
    98 	longjmp(*(jump_env()), 1);
    99 	
   100 }
   101 
   102 - (void) applicationWillResignActive:(UIApplication*)application
   103 {
   104 //	NSLog(@"%@", NSStringFromSelector(_cmd));
   105 	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   106 }
   107 
   108 - (void) applicationDidBecomeActive:(UIApplication*)application
   109 {
   110 //	NSLog(@"%@", NSStringFromSelector(_cmd));
   111 	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_RESTORED, 0, 0);
   112 }
   113 
   114 
   115 
   116 -(void)dealloc {
   117 	[window release];
   118 	[super dealloc];
   119 }
   120 
   121 @end