A sky roads like game with interesting shaders
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

147 lines
4.0 KiB

//
// 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 {
12 years ago
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;
12 years ago
view.autoresizesSubviews = YES;
view.context = self.context;
12 years ago
if(App::depth()) view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
if(App::multisample()) view.drawableMultisample = GLKViewDrawableMultisample4X;
[self setupGL];
12 years ago
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];
12 years ago
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];
12 years ago
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 {
12 years ago
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;
12 years ago
} catch (std::exception & e) {
std::cout << e.what() << std::endl;
throw e;
}
}
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
12 years ago
try {
app->draw();
} catch (std::exception & e) {
std::cout << e.what() << std::endl;
throw e;
}
}
@end