src/video/uikit/SDL_uikitappdelegate.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 14 Mar 2011 23:04:52 -0700
changeset 5487 ad3abbbe0381
parent 5332 ae8b6fb1feaf
child 5532 8a04b596b472
permissions -rw-r--r--
Delegate should use application: didFinishLaunchingWithOptions:

Vittorio G. to Sam

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