// // ViewController.m // iOS GLEssentials // // Created by Joshua Moerman on 07/03/14. // Copyright (c) 2014 Joshua Moerman. All rights reserved. // #import "ViewController.h" #import "Game.h" @interface ViewController () { Game * game; } @property (nonatomic) EAGLContext *context; - (void)setupGL; - (void)tearDownGL; @end @implementation ViewController - (void)viewDidLoad{ [super viewDidLoad]; // all devices supporting ios 5 also support ES 2.0 // only a couple of devices support ES 3.0 self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; assert(self.context); GLKView *view = (GLKView *)self.view; view.context = self.context; view.drawableDepthFormat = GLKViewDrawableDepthFormat24; view.drawableMultisample = GLKViewDrawableMultisampleNone; //view.contentScaleFactor = 1.0; self.preferredFramesPerSecond = 60; [self setupGL]; game.windowSize = self.view.bounds.size; } - (void)dealloc{ [self tearDownGL]; if ([EAGLContext currentContext] == self.context) { [EAGLContext setCurrentContext:nil]; } } - (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; if (self.isViewLoaded && (!self.view.window)) { self.view = nil; [self tearDownGL]; if ([EAGLContext currentContext] == self.context) { [EAGLContext setCurrentContext:nil]; } self.context = nil; } // Dispose of any resources that can be recreated. } -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ game.windowSize = self.view.bounds.size; } - (void)setupGL{ [EAGLContext setCurrentContext:self.context]; game = [[Game alloc] init]; CGFloat x = 20; CGFloat y = 20; for (NSString * a in game.actions) { UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(x, y, 200, 22)]; [myButton setTitle:a forState:UIControlStateNormal]; [myButton addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview: myButton]; y += 22; } } - (void) action:(UIButton*) sender{ [game action:[sender titleForState:UIControlStateNormal]]; } - (void)tearDownGL{ [EAGLContext setCurrentContext:self.context]; game = nil; } #pragma mark - GLKView and GLKViewController delegate methods - (void)update{ [game update:(float)self.timeSinceLastUpdate]; } - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect{ // If we drew on a different fbo in the meantime, do this [((GLKView *) self.view) bindDrawable]; [game draw]; } @end