// // ViewController.m // GLGameTemplate // // Created by Joshua Moerman on 5/4/12. // Copyright (c) 2012 Vadovas. All rights reserved. // #import "ViewController.h" #import "App.h" @interface ViewController () { App * app; float width; float height; } @property (strong, nonatomic) EAGLContext *context; - (void)setupGL; - (void)tearDownGL; @end @implementation ViewController @synthesize context = _context; - (void)dealloc { [left release]; [right release]; [_context release]; [super dealloc]; } #pragma mark - View stuff - (void)viewDidLoad { self.preferredFramesPerSecond = App::preferred_frames_per_second(); [super viewDidLoad]; self.context = [[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2] autorelease]; if (!self.context) NSLog(@"Failed to create ES context"); GLKView* view = (GLKView* )self.view; view.autoresizesSubviews = YES; view.context = self.context; if(App::depth()) view.drawableDepthFormat = GLKViewDrawableDepthFormat24; if(App::multisample()) view.drawableMultisample = GLKViewDrawableMultisample4X; [self setupGL]; left = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; left.frame = CGRectMake(0, 48, view.frame.size.width / 2, view.frame.size.height - 48); left.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; [view addSubview: left]; right = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; right.frame = CGRectMake(view.frame.size.width / 2, 48, view.frame.size.width / 2, view.frame.size.height - 48); right.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; [view addSubview: right]; UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0, 0, view.frame.size.width, 48); button.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; [button setTitle:@"Change Colours" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; [view addSubview:button]; } - (void)buttonPressed:(UIButton*)sender{ if(app) app->change_color(); } - (void)viewDidUnload { [super viewDidUnload]; [self tearDownGL]; if ([EAGLContext currentContext] == self.context) { [EAGLContext setCurrentContext:nil]; } self.context = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } else { return YES; } } #pragma mark - openGL setup - (void)setupGL { [EAGLContext setCurrentContext:self.context]; width = self.view.bounds.size.width; height = self.view.bounds.size.height; try { app = new App(width, height); } catch (std::exception & e) { std::cout << e.what() << std::endl; throw e; } } - (void)tearDownGL { [EAGLContext setCurrentContext:self.context]; delete app; app = 0; } #pragma mark - GLKView and GLKViewController delegate methods - (void)update { try { if(width != self.view.bounds.size.width || height != self.view.bounds.size.height){ width = self.view.bounds.size.width; height = self.view.bounds.size.height; app->resize(width, height); } app->update(self.timeSinceLastUpdate); if(left.highlighted) app->game.position.x -= 0.1; if(right.highlighted) app->game.position.x += 0.1; if(app->game.position.x > 2.0) app->game.position.x = 2.0; if(app->game.position.x < -2.0) app->game.position.x = -2.0; } catch (std::exception & e) { std::cout << e.what() << std::endl; throw e; } } - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect { try { app->draw(); } catch (std::exception & e) { std::cout << e.what() << std::endl; throw e; } } @end