cscg22-gearboy

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

AppDelegate.mm (6115B)


      1/*
      2 * Gearboy - Nintendo Game Boy Emulator
      3 * Copyright (C) 2012  Ignacio Sanchez
      4
      5 * This program is free software: you can redistribute it and/or modify
      6 * it under the terms of the GNU General Public License as published by
      7 * the Free Software Foundation, either version 3 of the License, or
      8 * any later version.
      9
     10 * This program 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
     13 * GNU General Public License for more details.
     14
     15 * You should have received a copy of the GNU General Public License
     16 * along with this program.  If not, see http://www.gnu.org/licenses/
     17 *
     18 */
     19
     20#import "AppDelegate.h"
     21#import "DetailViewController.h"
     22#import "MasterViewController.h"
     23
     24@interface AppDelegate () <UISplitViewControllerDelegate>
     25
     26@end
     27
     28@implementation AppDelegate
     29
     30- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     31    UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
     32    UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
     33    navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem;
     34    splitViewController.delegate = self;
     35    splitViewController.presentsWithGesture = NO;
     36    splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryOverlay;
     37    masterViewController = (MasterViewController *)[[splitViewController.viewControllers firstObject] topViewController];
     38    
     39    return YES;
     40}
     41
     42- (void)applicationWillResignActive:(UIApplication *)application {
     43    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     44    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     45    masterViewController.theGLViewController.displayLink.paused = YES;
     46    masterViewController.theGLViewController.paused = YES;
     47    [masterViewController.theGLViewController releaseContext];
     48    [masterViewController.theGLViewController.theEmulator save];
     49    [NSThread sleepForTimeInterval:1.5];
     50}
     51
     52- (void)applicationDidEnterBackground:(UIApplication *)application {
     53    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
     54    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
     55}
     56
     57- (void)applicationWillEnterForeground:(UIApplication *)application {
     58    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     59}
     60
     61- (void)applicationDidBecomeActive:(UIApplication *)application {
     62    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     63    [masterViewController.theGLViewController acquireContext];
     64    masterViewController.theGLViewController.paused = NO;
     65    masterViewController.theGLViewController.displayLink.paused = NO;
     66}
     67
     68- (void)applicationWillTerminate:(UIApplication *)application {
     69    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
     70}
     71
     72- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
     73{
     74    return [self openGearboyURL:url];
     75}
     76
     77- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
     78    return [self openGearboyURL:url];
     79}
     80
     81#pragma mark - Private Methods
     82
     83- (BOOL)openGearboyURL:(NSURL *)url {
     84    if (url != nil && [url isFileURL])
     85    {
     86        NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
     87        NSError* error;
     88        NSFileManager *fileManager = [[NSFileManager alloc] init];
     89
     90        NSString* srcPath = [url path];
     91        NSString* destPath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, [srcPath lastPathComponent]];
     92
     93        if ([fileManager copyItemAtPath:srcPath toPath:destPath error:&error])
     94        {
     95            if ([fileManager removeItemAtPath:[url path] error:&error])
     96            {
     97                [masterViewController reloadTableView];
     98                NSArray *extensions = [NSArray arrayWithObjects:@"zip", @"gb", @"sgb", @"gbc", @"rom", @"dmg", @"cgb", @"ZIP", @"GB", @"SGB", @"GBC", @"ROM", @"DMG", @"CGB", nil];
     99                if ([extensions containsObject:[srcPath pathExtension]]) {
    100                    [masterViewController loadWithROM:[[url path] lastPathComponent]];
    101                }
    102                return YES;
    103            }
    104        }
    105
    106        NSLog(@"ERROR %@", [error localizedDescription]);
    107    }
    108
    109    return NO;
    110}
    111
    112#pragma mark - Split view
    113
    114- (BOOL)splitViewController:(UISplitViewController *)splitViewController collapseSecondaryViewController:(UIViewController *)secondaryViewController ontoPrimaryViewController:(UIViewController *)primaryViewController {
    115    if ([secondaryViewController isKindOfClass:[UINavigationController class]] && [[(UINavigationController *)secondaryViewController topViewController] isKindOfClass:[DetailViewController class]] && ([(DetailViewController *)[(UINavigationController *)secondaryViewController topViewController] detailItem] == nil)) {
    116        // Return YES to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded.
    117        return YES;
    118    } else {
    119        return NO;
    120    }
    121}
    122
    123@end