cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

SDL_uikitappdelegate.m (8905B)


      1/*
      2  Simple DirectMedia Layer
      3  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
      4
      5  This software is provided 'as-is', without any express or implied
      6  warranty.  In no event will the authors be held liable for any damages
      7  arising from the use of this software.
      8
      9  Permission is granted to anyone to use this software for any purpose,
     10  including commercial applications, and to alter it and redistribute it
     11  freely, subject to the following restrictions:
     12
     13  1. The origin of this software must not be misrepresented; you must not
     14     claim that you wrote the original software. If you use this software
     15     in a product, an acknowledgment in the product documentation would be
     16     appreciated but is not required.
     17  2. Altered source versions must be plainly marked as such, and must not be
     18     misrepresented as being the original software.
     19  3. This notice may not be removed or altered from any source distribution.
     20*/
     21#include "../../SDL_internal.h"
     22
     23#if SDL_VIDEO_DRIVER_UIKIT
     24
     25#include "../SDL_sysvideo.h"
     26#include "SDL_assert.h"
     27#include "SDL_hints.h"
     28#include "SDL_system.h"
     29#include "SDL_main.h"
     30
     31#include "SDL_uikitappdelegate.h"
     32#include "SDL_uikitmodes.h"
     33#include "../../events/SDL_events_c.h"
     34
     35#ifdef main
     36#undef main
     37#endif
     38
     39static int forward_argc;
     40static char **forward_argv;
     41static int exit_status;
     42static UIWindow *launch_window;
     43
     44int main(int argc, char **argv)
     45{
     46    int i;
     47    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     48
     49    /* store arguments */
     50    forward_argc = argc;
     51    forward_argv = (char **)malloc((argc+1) * sizeof(char *));
     52    for (i = 0; i < argc; i++) {
     53        forward_argv[i] = malloc( (strlen(argv[i])+1) * sizeof(char));
     54        strcpy(forward_argv[i], argv[i]);
     55    }
     56    forward_argv[i] = NULL;
     57
     58    /* Give over control to run loop, SDLUIKitDelegate will handle most things from here */
     59    UIApplicationMain(argc, argv, NULL, [SDLUIKitDelegate getAppDelegateClassName]);
     60
     61    /* free the memory we used to hold copies of argc and argv */
     62    for (i = 0; i < forward_argc; i++) {
     63        free(forward_argv[i]);
     64    }
     65    free(forward_argv);
     66
     67    [pool release];
     68    return exit_status;
     69}
     70
     71static void
     72SDL_IdleTimerDisabledChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
     73{
     74    BOOL disable = (hint && *hint != '0');
     75    [UIApplication sharedApplication].idleTimerDisabled = disable;
     76}
     77
     78@interface SDL_splashviewcontroller : UIViewController {
     79    UIImageView *splash;
     80    UIImage *splashPortrait;
     81    UIImage *splashLandscape;
     82}
     83
     84- (void)updateSplashImage:(UIInterfaceOrientation)interfaceOrientation;
     85@end
     86
     87@implementation SDL_splashviewcontroller
     88
     89- (id)init
     90{
     91    self = [super init];
     92    if (self == nil) {
     93        return nil;
     94    }
     95
     96    self->splash = [[UIImageView alloc] init];
     97    [self setView:self->splash];
     98
     99    CGSize size = [UIScreen mainScreen].bounds.size;
    100    float height = SDL_max(size.width, size.height);
    101    self->splashPortrait = [UIImage imageNamed:[NSString stringWithFormat:@"Default-%dh.png", (int)height]];
    102    if (!self->splashPortrait) {
    103        self->splashPortrait = [UIImage imageNamed:@"Default.png"];
    104    }
    105    self->splashLandscape = [UIImage imageNamed:@"Default-Landscape.png"];
    106    if (!self->splashLandscape && self->splashPortrait) {
    107        self->splashLandscape = [[UIImage alloc] initWithCGImage: self->splashPortrait.CGImage
    108                                                           scale: 1.0
    109                                                     orientation: UIImageOrientationRight];
    110    }
    111    if (self->splashPortrait) {
    112        [self->splashPortrait retain];
    113    }
    114    if (self->splashLandscape) {
    115        [self->splashLandscape retain];
    116    }
    117
    118    [self updateSplashImage:[[UIApplication sharedApplication] statusBarOrientation]];
    119
    120    return self;
    121}
    122
    123- (NSUInteger)supportedInterfaceOrientations
    124{
    125    NSUInteger orientationMask = UIInterfaceOrientationMaskAll;
    126
    127    /* Don't allow upside-down orientation on the phone, so answering calls is in the natural orientation */
    128    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    129        orientationMask &= ~UIInterfaceOrientationMaskPortraitUpsideDown;
    130    }
    131    return orientationMask;
    132}
    133
    134- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
    135{
    136    NSUInteger orientationMask = [self supportedInterfaceOrientations];
    137    return (orientationMask & (1 << orient));
    138}
    139
    140- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
    141{
    142    [self updateSplashImage:interfaceOrientation];
    143}
    144
    145- (void)updateSplashImage:(UIInterfaceOrientation)interfaceOrientation
    146{
    147    UIImage *image;
    148
    149    if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
    150        image = self->splashLandscape;
    151    } else {
    152        image = self->splashPortrait;
    153    }
    154    if (image)
    155    {
    156        splash.image = image;
    157    }
    158}
    159
    160@end
    161
    162
    163@implementation SDLUIKitDelegate
    164
    165/* convenience method */
    166+ (id) sharedAppDelegate
    167{
    168    /* the delegate is set in UIApplicationMain(), which is garaunteed to be called before this method */
    169    return [[UIApplication sharedApplication] delegate];
    170}
    171
    172+ (NSString *)getAppDelegateClassName
    173{
    174    /* subclassing notice: when you subclass this appdelegate, make sure to add a category to override
    175       this method and return the actual name of the delegate */
    176    return @"SDLUIKitDelegate";
    177}
    178
    179- (id)init
    180{
    181    self = [super init];
    182    return self;
    183}
    184
    185- (void)postFinishLaunch
    186{
    187    /* run the user's application, passing argc and argv */
    188    SDL_iPhoneSetEventPump(SDL_TRUE);
    189    exit_status = SDL_main(forward_argc, forward_argv);
    190    SDL_iPhoneSetEventPump(SDL_FALSE);
    191
    192    /* If we showed a splash image, clean it up */
    193    if (launch_window) {
    194        [launch_window release];
    195        launch_window = NULL;
    196    }
    197
    198    /* exit, passing the return status from the user's application */
    199    /* We don't actually exit to support applications that do setup in
    200     * their main function and then allow the Cocoa event loop to run.
    201     */
    202    /* exit(exit_status); */
    203}
    204
    205- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    206{
    207    /* Keep the launch image up until we set a video mode */
    208    launch_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    209
    210    UIViewController *splashViewController = [[SDL_splashviewcontroller alloc] init];
    211    launch_window.rootViewController = splashViewController;
    212    [launch_window addSubview:splashViewController.view];
    213    [launch_window makeKeyAndVisible];
    214
    215    /* Set working directory to resource path */
    216    [[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
    217
    218    /* register a callback for the idletimer hint */
    219    SDL_AddHintCallback(SDL_HINT_IDLE_TIMER_DISABLED,
    220                        SDL_IdleTimerDisabledChanged, NULL);
    221
    222    SDL_SetMainReady();
    223    [self performSelector:@selector(postFinishLaunch) withObject:nil afterDelay:0.0];
    224
    225    return YES;
    226}
    227
    228- (void)applicationWillTerminate:(UIApplication *)application
    229{
    230    SDL_SendAppEvent(SDL_APP_TERMINATING);
    231}
    232
    233- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
    234{
    235    SDL_SendAppEvent(SDL_APP_LOWMEMORY);
    236}
    237
    238- (void) applicationWillResignActive:(UIApplication*)application
    239{
    240    SDL_VideoDevice *_this = SDL_GetVideoDevice();
    241    if (_this) {
    242        SDL_Window *window;
    243        for (window = _this->windows; window != nil; window = window->next) {
    244            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
    245            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
    246        }
    247    }
    248    SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
    249}
    250
    251- (void) applicationDidEnterBackground:(UIApplication*)application
    252{
    253    SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
    254}
    255
    256- (void) applicationWillEnterForeground:(UIApplication*)application
    257{
    258    SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
    259}
    260
    261- (void) applicationDidBecomeActive:(UIApplication*)application
    262{
    263    SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
    264
    265    SDL_VideoDevice *_this = SDL_GetVideoDevice();
    266    if (_this) {
    267        SDL_Window *window;
    268        for (window = _this->windows; window != nil; window = window->next) {
    269            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
    270            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
    271        }
    272    }
    273}
    274
    275- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    276{
    277    NSURL *fileURL = [url filePathURL];
    278    if (fileURL != nil) {
    279        SDL_SendDropFile([[fileURL path] UTF8String]);
    280    } else {
    281        SDL_SendDropFile([[url absoluteString] UTF8String]);
    282    }
    283    return YES;
    284}
    285
    286@end
    287
    288#endif /* SDL_VIDEO_DRIVER_UIKIT */
    289
    290/* vi: set ts=4 sw=4 expandtab: */