cscg22-gearboy

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

GLViewController.mm (4849B)


      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 "GLViewController.h"
     21#import <MediaPlayer/MediaPlayer.h>
     22
     23@interface GLViewController ()
     24
     25@end
     26
     27@implementation GLViewController
     28
     29@synthesize context = _context;
     30
     31- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
     32{
     33    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     34    if (self) {
     35        self.theEmulator = [[Emulator alloc]init];
     36        if ([[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPhone)
     37        {
     38            self.view.hidden = YES;
     39        }
     40    }
     41    return self;
     42}
     43
     44- (void)viewDidLoad
     45{
     46    [super viewDidLoad];
     47
     48    self.paused = YES;
     49    
     50    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(step)];
     51    [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
     52    
     53    float scale =[[UIScreen mainScreen] nativeScale];
     54    GLKView *view = (GLKView *)self.view;
     55    
     56    self.theEmulator.glview = view;
     57    BOOL retina, iPad;
     58    retina = (scale != 1.0);
     59    
     60    float multiplier = 0;
     61    
     62    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
     63    {
     64        iPad = NO;
     65        if (retina)
     66        {
     67            CGRect screenBounds = [[UIScreen mainScreen] bounds];
     68            int h = (int)screenBounds.size.height;
     69            
     70            if (h == 667)
     71            {
     72                multiplier = 4.0;
     73                view.frame = CGRectMake(27, 26, 80 * multiplier, 72 * multiplier);
     74            }
     75            else if (h == 736)
     76            {
     77                multiplier = 4.0;
     78                view.frame = CGRectMake(45, 35, 80 * multiplier, 72 * multiplier);
     79            }
     80            else
     81            {
     82                multiplier = 3.0;
     83                view.frame = CGRectMake(40, 28, 80 * multiplier, 72 * multiplier);
     84            }
     85        }
     86        else
     87        {
     88            multiplier = 4.0;
     89            view.frame = CGRectMake(0, 0, 80 * multiplier, 72 * multiplier);
     90        }
     91    }
     92    else
     93    {
     94        iPad = YES;
     95        if (retina)
     96        {
     97            multiplier = 5.0;
     98            view.frame = CGRectMake(187, 53, 80 * multiplier, 72 * multiplier);
     99        }
    100        else
    101        {
    102            multiplier = 4.0;
    103            view.frame = CGRectMake(222, 82, 80 * multiplier, 72 * multiplier);
    104        }
    105        
    106    }
    107    
    108    self.theEmulator.multiplier = multiplier * scale;
    109    self.theEmulator.retina = retina;
    110    self.theEmulator.iPad = iPad;
    111}
    112
    113- (void)loadRomWithName: (NSString*) name
    114{
    115    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    116    NSString* documentsDirectoryPath = [paths objectAtIndex:0];
    117    
    118    NSString* path = [NSString stringWithFormat:@"%@/%@", documentsDirectoryPath, name];
    119    
    120    [self.theEmulator loadRomWithPath:path];
    121    
    122    self.view.hidden = NO;
    123    self.displayLink.paused = NO;
    124    self.paused = NO;
    125}
    126
    127- (void)dealloc
    128{
    129    if ([EAGLContext currentContext] == self.context) {
    130        [EAGLContext setCurrentContext:nil];
    131    }
    132    self.context = nil;
    133}
    134
    135- (BOOL)shouldAutorotate
    136{
    137    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    138    
    139    return (orientation == UIInterfaceOrientationPortrait) || (orientation == UIInterfaceOrientationPortraitUpsideDown);;
    140}
    141
    142- (void) step
    143{
    144    [self.theEmulator update];
    145    [self.theEmulator draw];
    146}
    147
    148-(void) releaseContext
    149{
    150    [self.theEmulator shutdownGL];
    151    self.context = nil;
    152    [EAGLContext setCurrentContext:nil];
    153}
    154
    155-(void) acquireContext
    156{
    157    self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
    158    
    159    if (!self.context) {
    160        NSLog(@"Failed to create ES context");
    161    }
    162    GLKView *view = (GLKView *)self.view;
    163    view.context = self.context;
    164    [EAGLContext setCurrentContext:self.context];
    165    [self.theEmulator initGL];
    166    
    167    if ([[MPMusicPlayerController systemMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying)
    168        [self.theEmulator setAudioEnabled:NO];
    169    else
    170    {
    171        [self.theEmulator setAudioEnabled:YES];
    172        [self.theEmulator resetAudio];
    173    }
    174}
    175
    176@end