src/video/uikit/SDL_uikitappdelegate.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 02 Nov 2009 07:55:42 +0000
changeset 3422 ec4db979dddb
parent 3400 4ec48602f1db
child 3685 64ce267332c6
permissions -rw-r--r--
Fixed bug #847

Roger Willcocks 2009-10-25 08:45:37 PDT

appDidFinishLaunching is triggered before all the setup's complete. The easiest
fix is to arrange for another event to be sent when it's really ready, and run
SDL_main from there. Ref.
http://blog.rightsprite.com/2008/11/iphone-applicationdidfinishlaunching.html

SDL-1.3.0-4563/src/video/uikit/SDL_uikitappdelegate.m:



- (void)postFinishLaunch {

/* run the user's application, passing argc and argv */
int exit_status = SDL_main(forward_argc, forward_argv);

/* free the memory we used to hold copies of argc and argv */
int i;
for (i=0; i<forward_argc; i++) {
free(forward_argv[i]);
}
free(forward_argv);

/* exit, passing the return status from the user's application */
exit(exit_status);

}

- (void)applicationDidFinishLaunching:(UIApplication *)application {

/* Set working directory to resource path */
[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle
mainBundle] resourcePath]];

[self performSelector:@selector(postFinishLaunch) withObject:nil
afterDelay:0.0];
slouken@2765
     1
/*
slouken@2765
     2
 SDL - Simple DirectMedia Layer
slouken@2859
     3
 Copyright (C) 1997-2009 Sam Lantinga
slouken@2765
     4
 
slouken@2765
     5
 This library is free software; you can redistribute it and/or
slouken@2765
     6
 modify it under the terms of the GNU Lesser General Public
slouken@2765
     7
 License as published by the Free Software Foundation; either
slouken@2765
     8
 version 2.1 of the License, or (at your option) any later version.
slouken@2765
     9
 
slouken@2765
    10
 This library is distributed in the hope that it will be useful,
slouken@2765
    11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@2765
    12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@2765
    13
 Lesser General Public License for more details.
slouken@2765
    14
 
slouken@2765
    15
 You should have received a copy of the GNU Lesser General Public
slouken@2765
    16
 License along with this library; if not, write to the Free Software
slouken@2765
    17
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@2765
    18
 
slouken@2765
    19
 Sam Lantinga
slouken@2765
    20
 slouken@libsdl.org
slouken@2765
    21
*/
slouken@2765
    22
slouken@2765
    23
#import "SDL_uikitappdelegate.h"
slouken@2765
    24
#import "SDL_uikitopenglview.h"
slouken@2765
    25
#import "SDL_events_c.h"
slouken@2765
    26
#import "jumphack.h"
slouken@2765
    27
slouken@2765
    28
#ifdef main
slouken@2765
    29
#undef main
slouken@2765
    30
#endif
slouken@2765
    31
slouken@2765
    32
extern int SDL_main(int argc, char *argv[]);
slouken@2765
    33
static int forward_argc;
slouken@2765
    34
static char **forward_argv;
slouken@2765
    35
slouken@2765
    36
int main(int argc, char **argv) {
slouken@2765
    37
slouken@2765
    38
	int i;
slouken@2765
    39
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@2765
    40
	
slouken@2765
    41
	/* store arguments */
slouken@2765
    42
	forward_argc = argc;
slouken@2765
    43
	forward_argv = (char **)malloc(argc * sizeof(char *));
slouken@2765
    44
	for (i=0; i<argc; i++) {
slouken@2765
    45
		forward_argv[i] = malloc( (strlen(argv[i])+1) * sizeof(char));
slouken@2765
    46
		strcpy(forward_argv[i], argv[i]);
slouken@2765
    47
	}
slouken@2765
    48
slouken@2765
    49
	/* Give over control to run loop, SDLUIKitDelegate will handle most things from here */
slouken@2765
    50
	UIApplicationMain(argc, argv, NULL, @"SDLUIKitDelegate");
slouken@2765
    51
	
slouken@2765
    52
	[pool release];
slouken@2765
    53
	
slouken@2765
    54
}
slouken@2765
    55
slouken@2765
    56
@implementation SDLUIKitDelegate
slouken@2765
    57
slouken@2765
    58
@synthesize window;
slouken@3400
    59
@synthesize windowID;
slouken@2765
    60
slouken@2765
    61
/* convenience method */
slouken@2765
    62
+(SDLUIKitDelegate *)sharedAppDelegate {
slouken@2765
    63
	/* the delegate is set in UIApplicationMain(), which is garaunteed to be called before this method */
slouken@2765
    64
	return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
slouken@2765
    65
}
slouken@2765
    66
slouken@2765
    67
- (id)init {
slouken@2765
    68
	self = [super init];
slouken@2765
    69
	window = nil;
slouken@3400
    70
	windowID = 0;
slouken@2765
    71
	return self;
slouken@2765
    72
}
slouken@2765
    73
slouken@3422
    74
- (void)postFinishLaunch {
slouken@3422
    75
slouken@2765
    76
	/* run the user's application, passing argc and argv */
slouken@2765
    77
	int exit_status = SDL_main(forward_argc, forward_argv);
slouken@2765
    78
	
slouken@2765
    79
	/* free the memory we used to hold copies of argc and argv */
slouken@2765
    80
	int i;
slouken@2765
    81
	for (i=0; i<forward_argc; i++) {
slouken@2765
    82
		free(forward_argv[i]);
slouken@2765
    83
	}
slouken@2765
    84
	free(forward_argv);	
slouken@2765
    85
		
slouken@2765
    86
	/* exit, passing the return status from the user's application */
slouken@2765
    87
	exit(exit_status);
slouken@3422
    88
}
slouken@3422
    89
slouken@3422
    90
- (void)applicationDidFinishLaunching:(UIApplication *)application {
slouken@3422
    91
			
slouken@3422
    92
	/* Set working directory to resource path */
slouken@3422
    93
	[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
slouken@3422
    94
	
slouken@3422
    95
	[self performSelector:@selector(postFinishLaunch) withObject:nil
slouken@3422
    96
afterDelay:0.0];
slouken@2765
    97
}
slouken@2765
    98
slouken@2765
    99
- (void)applicationWillTerminate:(UIApplication *)application {
slouken@2765
   100
	
slouken@2765
   101
	SDL_SendQuit();
slouken@2765
   102
	 /* hack to prevent automatic termination.  See SDL_uikitevents.m for details */
slouken@2765
   103
	longjmp(*(jump_env()), 1);
slouken@2765
   104
	
slouken@2765
   105
}
slouken@2765
   106
slouken@3400
   107
- (void) applicationWillResignActive:(UIApplication*)application
slouken@3400
   108
{
slouken@3400
   109
//	NSLog(@"%@", NSStringFromSelector(_cmd));
slouken@3400
   110
	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@3400
   111
}
slouken@3400
   112
slouken@3400
   113
- (void) applicationDidBecomeActive:(UIApplication*)application
slouken@3400
   114
{
slouken@3400
   115
//	NSLog(@"%@", NSStringFromSelector(_cmd));
slouken@3400
   116
	SDL_SendWindowEvent(self.windowID, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@3400
   117
}
slouken@3400
   118
slouken@3400
   119
slouken@3400
   120
slouken@2765
   121
-(void)dealloc {
slouken@2765
   122
	[window release];
slouken@2765
   123
	[super dealloc];
slouken@2765
   124
}
slouken@2765
   125
slouken@2765
   126
@end