src/video/uikit/SDL_uikitappdelegate.m
author Sam Lantinga <slouken@libsdl.org>
Tue, 01 Feb 2011 09:00:47 -0800
changeset 5132 5f09cb749d75
parent 5131 7b7da52e8775
child 5251 1b0de3574c58
permissions -rw-r--r--
Added vim spacing hints
     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_sysvideo.h"
    24 
    25 #import "SDL_uikitappdelegate.h"
    26 #import "SDL_uikitopenglview.h"
    27 #import "SDL_events_c.h"
    28 #import "jumphack.h"
    29 
    30 #ifdef main
    31 #undef main
    32 #endif
    33 
    34 extern int SDL_main(int argc, char *argv[]);
    35 static int forward_argc;
    36 static char **forward_argv;
    37 
    38 int main(int argc, char **argv) {
    39 
    40     int i;
    41     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    42     
    43     /* store arguments */
    44     forward_argc = argc;
    45     forward_argv = (char **)malloc((argc+1) * sizeof(char *));
    46     for (i=0; i<argc; i++) {
    47         forward_argv[i] = malloc( (strlen(argv[i])+1) * sizeof(char));
    48         strcpy(forward_argv[i], argv[i]);
    49     }
    50     forward_argv[i] = NULL;
    51 
    52     /* Give over control to run loop, SDLUIKitDelegate will handle most things from here */
    53     UIApplicationMain(argc, argv, NULL, @"SDLUIKitDelegate");
    54     
    55     [pool release];
    56     
    57 }
    58 
    59 @implementation SDLUIKitDelegate
    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     return self;
    70 }
    71 
    72 - (void)postFinishLaunch {
    73 
    74     /* run the user's application, passing argc and argv */
    75     int exit_status = SDL_main(forward_argc, forward_argv);
    76     
    77     /* free the memory we used to hold copies of argc and argv */
    78     int i;
    79     for (i=0; i<forward_argc; i++) {
    80         free(forward_argv[i]);
    81     }
    82     free(forward_argv);    
    83         
    84     /* exit, passing the return status from the user's application */
    85     exit(exit_status);
    86 }
    87 
    88 - (void)applicationDidFinishLaunching:(UIApplication *)application {
    89             
    90     /* Set working directory to resource path */
    91     [[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
    92     
    93     [self performSelector:@selector(postFinishLaunch) withObject:nil
    94 afterDelay:0.0];
    95 }
    96 
    97 - (void)applicationWillTerminate:(UIApplication *)application {
    98     
    99     SDL_SendQuit();
   100      /* hack to prevent automatic termination.  See SDL_uikitevents.m for details */
   101     longjmp(*(jump_env()), 1);
   102 }
   103 
   104 - (void) applicationWillResignActive:(UIApplication*)application
   105 {
   106     //NSLog(@"%@", NSStringFromSelector(_cmd));
   107 
   108     // Send every window on every screen a MINIMIZED event.
   109     SDL_VideoDevice *_this = SDL_GetVideoDevice();
   110     if (!_this) {
   111         return;
   112     }
   113 
   114     int i;
   115     for (i = 0; i < _this->num_displays; i++) {
   116         const SDL_VideoDisplay *display = &_this->displays[i];
   117         SDL_Window *window;
   118         for (window = display->windows; window != nil; window = window->next) {
   119             SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   120         }
   121     }
   122 }
   123 
   124 - (void) applicationDidBecomeActive:(UIApplication*)application
   125 {
   126     //NSLog(@"%@", NSStringFromSelector(_cmd));
   127 
   128     // Send every window on every screen a RESTORED event.
   129     SDL_VideoDevice *_this = SDL_GetVideoDevice();
   130     if (!_this) {
   131         return;
   132     }
   133 
   134     int i;
   135     for (i = 0; i < _this->num_displays; i++) {
   136         const SDL_VideoDisplay *display = &_this->displays[i];
   137         SDL_Window *window;
   138         for (window = display->windows; window != nil; window = window->next) {
   139             SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
   140         }
   141     }
   142 }
   143 
   144 @end
   145 
   146 /* vi: set ts=4 sw=4 expandtab: */