src/video/uikit/SDL_uikitappdelegate.m
author Sam Lantinga <slouken@libsdl.org>
Tue, 05 Apr 2011 09:50:25 -0700
changeset 5532 8a04b596b472
parent 5487 ad3abbbe0381
child 5535 96594ac5fd1a
permissions -rw-r--r--
Allow overriding the app delegate on iOS

Vittorio G. to Eric, Sam

Actually this is much simpler than i thought, I just had to specify a
class method to get the delegate name and then the category can
override that method!
I've attached the patch that enables this features: in my code i could
remove my custom main() and simply add

@implementation SDLUIKitDelegate (customDelegate)
+(NSString *)getAppDelegateClassName {
return @"HedgewarsAppDelegate";
}
@end

I tested it and with the sdl demos it loads the normal
SDLUIKitDelegate, while in my code it loads my HedgewarsAppDelegate!
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@5532
    53
    UIApplicationMain(argc, argv, NULL, [SDLUIKitDelegate getAppDelegateClassName]);
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@5532
    67
+(NSString *)getAppDelegateClassName {
slouken@5532
    68
    /* subclassing notice: when you subclass this appdelegate, make sure to add a category to override
slouken@5532
    69
       this method and return the actual name of the delegate */
slouken@5532
    70
    return @"SDLUIKitDelegate";
slouken@5532
    71
}
slouken@5532
    72
slouken@2765
    73
- (id)init {
slouken@5131
    74
    self = [super init];
slouken@5131
    75
    return self;
slouken@2765
    76
}
slouken@2765
    77
slouken@3422
    78
- (void)postFinishLaunch {
slouken@3422
    79
slouken@5131
    80
    /* run the user's application, passing argc and argv */
slouken@5131
    81
    int exit_status = SDL_main(forward_argc, forward_argv);
slouken@5131
    82
    
slouken@5131
    83
    /* free the memory we used to hold copies of argc and argv */
slouken@5131
    84
    int i;
slouken@5131
    85
    for (i=0; i<forward_argc; i++) {
slouken@5131
    86
        free(forward_argv[i]);
slouken@5131
    87
    }
slouken@5131
    88
    free(forward_argv);    
slouken@5131
    89
        
slouken@5131
    90
    /* exit, passing the return status from the user's application */
slouken@5131
    91
    exit(exit_status);
slouken@3422
    92
}
slouken@3422
    93
slouken@5487
    94
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
slouken@5131
    95
            
slouken@5131
    96
    /* Set working directory to resource path */
slouken@5131
    97
    [[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
slouken@5131
    98
    
slouken@5532
    99
    [self performSelector:@selector(postFinishLaunch) withObject:nil afterDelay:0.0];
slouken@5487
   100
slouken@5487
   101
    return YES;
slouken@2765
   102
}
slouken@2765
   103
slouken@2765
   104
- (void)applicationWillTerminate:(UIApplication *)application {
slouken@5131
   105
    
slouken@5131
   106
    SDL_SendQuit();
slouken@5131
   107
     /* hack to prevent automatic termination.  See SDL_uikitevents.m for details */
slouken@5131
   108
    longjmp(*(jump_env()), 1);
slouken@2765
   109
}
slouken@2765
   110
slouken@3400
   111
- (void) applicationWillResignActive:(UIApplication*)application
slouken@3400
   112
{
icculus@4446
   113
    //NSLog(@"%@", NSStringFromSelector(_cmd));
icculus@4446
   114
icculus@4446
   115
    // Send every window on every screen a MINIMIZED event.
icculus@4446
   116
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
icculus@4446
   117
    if (!_this) {
icculus@4446
   118
        return;
icculus@4446
   119
    }
slouken@5251
   120
	
slouken@5532
   121
    SDL_Window *window;
slouken@5251
   122
    for (window = _this->windows; window != nil; window = window->next) {
slouken@5251
   123
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
icculus@4446
   124
    }
slouken@3400
   125
}
slouken@3400
   126
slouken@3400
   127
- (void) applicationDidBecomeActive:(UIApplication*)application
slouken@3400
   128
{
icculus@4446
   129
    //NSLog(@"%@", NSStringFromSelector(_cmd));
slouken@3400
   130
icculus@4446
   131
    // Send every window on every screen a RESTORED event.
icculus@4446
   132
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
icculus@4446
   133
    if (!_this) {
icculus@4446
   134
        return;
icculus@4446
   135
    }
slouken@5251
   136
	
slouken@5251
   137
	SDL_Window *window;
slouken@5251
   138
    for (window = _this->windows; window != nil; window = window->next) {
slouken@5251
   139
		SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
icculus@4446
   140
    }
slouken@2765
   141
}
slouken@2765
   142
slouken@2765
   143
@end
slouken@5132
   144
slouken@5132
   145
/* vi: set ts=4 sw=4 expandtab: */