Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // RollViewController.m
- // DiceRoller
- #import "RollViewController.h"
- #import "Dice.h"
- #import "Profile.h"
- @interface RollViewController (){
- float _rotation;
- float moveX;
- float moveY;
- float totalMoveX;
- float totalMoveY;
- CGPoint currentPoint;
- CGPoint previousPoint;
- CGPoint firstPoint;
- CGPoint lastPoint;
- bool firstTouch;
- GLKMatrix4 modelViewMatrix;
- bool u, d;
- }
- @property (strong, nonatomic) GLKBaseEffect *effect;
- - (void) setupGL;
- - (void)tearDownGL;
- @end
- @implementation RollViewController
- @synthesize RollView;
- @synthesize effect = _effect;
- @synthesize profile = _profile;
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- printf("before didload\n");
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- u = true;
- d = true;
- if(RollView.context == nil){
- RollView.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
- }
- NSLog(@"before setupGL\n");
- [self setupGL];
- moveX = 0;
- moveY = 0;
- firstTouch = true;
- for(int i = 0; i < self.profile.tableOfDice.count; i++){
- [[self.profile getDiceAt:i] Context:self.RollView.context Effect:self.effect];
- [self.profile getDiceAt:i]->boundary = GLKVector3Make(self.RollView.frame.size.width, self.RollView.frame.size.height, 900);
- [self.profile getDiceAt:i].position = GLKVector3Make(0, -500 + i* 200, 0);
- }
- NSLog(@"after viewdidload\n");
- }
- - (void)viewDidUnload
- {
- [self tearDownGL];
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- [self setRollView:nil];
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- #pragma mark - GLKViewDelegate
- - (void) setupGL {
- [EAGLContext setCurrentContext:self.RollView.context];
- self.effect = nil;
- self.effect = [[GLKBaseEffect alloc] init];
- self.effect.light0.enabled = GL_TRUE;
- self.effect.light0.diffuseColor = GLKVector4Make(1.0f, 1, 1, 1.0f);
- self.effect.light0.specularColor = GLKVector4Make(1.0f, 1, 1, 1.0f);
- self.effect.light0.position = GLKVector4Make(1000, 1000, 0, 1);
- self.effect.colorMaterialEnabled = GL_TRUE;
- self.effect.material.diffuseColor = GLKVector4Make(.3f, .3f, .3f, 1.0f);
- self.effect.material.specularColor = GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f);
- self.effect.material.shininess = 10.0;
- self.effect.lightingType = GLKLightingTypePerPixel;
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_CULL_FACE);
- }
- - (void)tearDownGL {
- [EAGLContext setCurrentContext:self.RollView.context];
- for(int i = 0; i < self.profile.tableOfDice.count; i++){
- [[self.profile getDiceAt:i] tearDown];
- }
- [EAGLContext setCurrentContext:nil];
- self.effect = nil;
- glFlush();
- }
- - (void)update {
- if(u){
- NSLog(@"in update\n");
- u = false;
- }
- float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
- GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 1.0f, 10000.0f);
- self.effect.transform.projectionMatrix = projectionMatrix;
- modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -1000.0f);
- for(int i = 0; i < self.profile.tableOfDice.count; i++){
- [self.profile getDiceAt:i].modelViewMatrix = modelViewMatrix;
- [[self.profile getDiceAt:i] update: self.timeSinceLastUpdate];
- for(int j = i+1; j < self.profile.tableOfDice.count; j++){
- [[self.profile getDiceAt:i]collidedWith:[self.profile getDiceAt:j]];
- }
- }
- }
- - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
- if(d){
- NSLog(@"in draw\n");
- }
- [EAGLContext setCurrentContext:self.RollView.context];
- glClearColor(0.7, 0.7, 0.7, 1.0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- if(d){
- NSLog(@"before dice draw\n");
- }
- for(int i = 0; i < self.profile.tableOfDice.count; i++){
- [[self.profile getDiceAt:i] draw];
- }
- if(d){
- NSLog(@"after dice draw\n");
- d = false;
- }
- }
- -(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
- firstPoint = [[touches anyObject] locationInView:self.view];
- }
- - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
- if(firstTouch){
- previousPoint = currentPoint;
- currentPoint = [[touches anyObject] locationInView:self.view];
- firstTouch = false;
- }
- else {
- previousPoint = currentPoint;
- currentPoint = [[touches anyObject] locationInView:self.view];
- moveX = currentPoint.x - previousPoint.x;
- moveY = currentPoint.y - previousPoint.y;
- }
- totalMoveX = currentPoint.x - firstPoint.x;
- totalMoveY = currentPoint.y - firstPoint.y;
- for(int i = 0; i < self.profile.tableOfDice.count; i++){
- [[self.profile getDiceAt:i] moveInDirection:GLKVector3Make(moveX, -moveY, 0)];
- }
- }
- - (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
- firstTouch = true;
- for(int i = 0; i < self.profile.tableOfDice.count; i++){
- [[self.profile getDiceAt:i] applyForceInDirection:GLKVector3Make(moveX, -moveY, 0)];
- }
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement