114 lines
2.6 KiB
Text
114 lines
2.6 KiB
Text
//
|
|
// 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 {
|
|
[_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;
|
|
glEnable(GL_DEPTH_TEST);
|
|
}
|
|
if(App::multisample()) view.drawableMultisample = GLKViewDrawableMultisample4X;
|
|
|
|
[self setupGL];
|
|
}
|
|
|
|
- (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);
|
|
} 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
|