Advertisement
Guest User

DiceRollerViewController.m

a guest
May 26th, 2012
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  RollViewController.m
  3. //  DiceRoller
  4.  
  5. #import "RollViewController.h"
  6. #import "Dice.h"
  7.  
  8. #import "Profile.h"
  9.  
  10. @interface RollViewController (){
  11.    
  12.     float _rotation;
  13.     float moveX;
  14.     float moveY;
  15.    
  16.     float totalMoveX;
  17.     float totalMoveY;
  18.    
  19.     CGPoint currentPoint;
  20.     CGPoint previousPoint;
  21.    
  22.     CGPoint firstPoint;
  23.     CGPoint lastPoint;
  24.    
  25.     bool firstTouch;
  26.    
  27.     GLKMatrix4 modelViewMatrix;
  28.    
  29.     bool u, d;
  30. }
  31. @property (strong, nonatomic) GLKBaseEffect *effect;
  32.  
  33. - (void) setupGL;
  34. - (void)tearDownGL;
  35. @end
  36.  
  37. @implementation RollViewController
  38. @synthesize RollView;
  39. @synthesize effect = _effect;
  40. @synthesize profile = _profile;
  41.  
  42. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  43. {
  44.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  45.     if (self) {
  46.         // Custom initialization
  47.     }
  48.    
  49.    
  50.     return self;
  51. }
  52.  
  53.  
  54.  
  55. - (void)viewDidLoad
  56. {
  57.     printf("before didload\n");
  58.    
  59.     [super viewDidLoad];
  60.     // Do any additional setup after loading the view.
  61.    
  62.     u = true;
  63.     d = true;
  64.    
  65.     if(RollView.context == nil){
  66.    
  67.         RollView.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
  68.     }
  69.    
  70.     NSLog(@"before setupGL\n");
  71.    
  72.     [self setupGL];
  73.    
  74.     moveX = 0;
  75.     moveY = 0;
  76.     firstTouch = true;
  77.    
  78.     for(int i = 0; i < self.profile.tableOfDice.count; i++){
  79.        
  80.         [[self.profile getDiceAt:i] Context:self.RollView.context Effect:self.effect];
  81.        
  82.         [self.profile getDiceAt:i]->boundary = GLKVector3Make(self.RollView.frame.size.width, self.RollView.frame.size.height, 900);
  83.        
  84.         [self.profile getDiceAt:i].position = GLKVector3Make(0, -500 + i* 200, 0);
  85.     }
  86.     NSLog(@"after viewdidload\n");
  87. }
  88.  
  89. - (void)viewDidUnload
  90. {
  91.     [self tearDownGL];
  92.     [super viewDidUnload];
  93.     // Release any retained subviews of the main view.
  94.    
  95.     [self setRollView:nil];
  96. }
  97.  
  98. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  99. {
  100.     return (interfaceOrientation == UIInterfaceOrientationPortrait);
  101. }
  102.  
  103. #pragma mark - GLKViewDelegate
  104.  
  105. - (void) setupGL {
  106.     [EAGLContext setCurrentContext:self.RollView.context];
  107.    
  108.     self.effect = nil;
  109.     self.effect = [[GLKBaseEffect alloc] init];
  110.     self.effect.light0.enabled = GL_TRUE;
  111.     self.effect.light0.diffuseColor = GLKVector4Make(1.0f, 1, 1, 1.0f);
  112.     self.effect.light0.specularColor = GLKVector4Make(1.0f, 1, 1, 1.0f);
  113.    
  114.     self.effect.light0.position = GLKVector4Make(1000, 1000, 0, 1);
  115.    
  116.     self.effect.colorMaterialEnabled = GL_TRUE;
  117.    
  118.     self.effect.material.diffuseColor = GLKVector4Make(.3f, .3f, .3f, 1.0f);
  119.     self.effect.material.specularColor = GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f);
  120.     self.effect.material.shininess = 10.0;
  121.    
  122.     self.effect.lightingType = GLKLightingTypePerPixel;
  123.    
  124.    
  125.    
  126.     glEnable(GL_DEPTH_TEST);
  127.     glEnable(GL_CULL_FACE);
  128.    
  129.  
  130. }
  131.  
  132. - (void)tearDownGL {
  133.    
  134.     [EAGLContext setCurrentContext:self.RollView.context];
  135.    
  136.     for(int i = 0; i < self.profile.tableOfDice.count; i++){
  137.         [[self.profile getDiceAt:i] tearDown];
  138.     }
  139.    
  140.     [EAGLContext setCurrentContext:nil];
  141.     self.effect = nil;
  142.     glFlush();
  143.    
  144. }
  145.  
  146. - (void)update {
  147.    
  148.     if(u){
  149.         NSLog(@"in update\n");
  150.         u = false;
  151.     }
  152.    
  153.     float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
  154.     GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 1.0f, 10000.0f);    
  155.     self.effect.transform.projectionMatrix = projectionMatrix;
  156.    
  157.     modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -1000.0f);
  158.    
  159.     for(int i = 0; i < self.profile.tableOfDice.count; i++){
  160.    
  161.         [self.profile getDiceAt:i].modelViewMatrix = modelViewMatrix;
  162.        
  163.         [[self.profile getDiceAt:i] update: self.timeSinceLastUpdate];
  164.        
  165.         for(int j = i+1; j < self.profile.tableOfDice.count; j++){
  166.            
  167.             [[self.profile getDiceAt:i]collidedWith:[self.profile getDiceAt:j]];
  168.            
  169.         }
  170.     }  
  171. }
  172.  
  173. - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
  174.    
  175.     if(d){
  176.         NSLog(@"in draw\n");
  177.     }
  178.    
  179.     [EAGLContext setCurrentContext:self.RollView.context];
  180.    
  181.     glClearColor(0.7, 0.7, 0.7, 1.0);
  182.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  183.    
  184.     if(d){
  185.         NSLog(@"before dice draw\n");
  186.     }
  187.    
  188.     for(int i = 0; i < self.profile.tableOfDice.count; i++){
  189.         [[self.profile getDiceAt:i] draw];
  190.     }
  191.    
  192.     if(d){
  193.         NSLog(@"after dice draw\n");
  194.         d = false;
  195.     }
  196. }
  197.  
  198. -(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
  199.    
  200.     firstPoint = [[touches anyObject] locationInView:self.view];
  201.    
  202. }
  203.  
  204. - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
  205.    
  206.     if(firstTouch){
  207.        
  208.         previousPoint = currentPoint;
  209.         currentPoint = [[touches anyObject] locationInView:self.view];
  210.         firstTouch = false;
  211.     }
  212.     else {
  213.        
  214.         previousPoint = currentPoint;
  215.         currentPoint = [[touches anyObject] locationInView:self.view];
  216.        
  217.         moveX = currentPoint.x - previousPoint.x;
  218.         moveY = currentPoint.y - previousPoint.y;
  219.     }
  220.    
  221.     totalMoveX = currentPoint.x - firstPoint.x;
  222.     totalMoveY = currentPoint.y - firstPoint.y;
  223.    
  224.     for(int i = 0; i < self.profile.tableOfDice.count; i++){
  225.         [[self.profile getDiceAt:i] moveInDirection:GLKVector3Make(moveX, -moveY, 0)];
  226.     }
  227.    
  228.    
  229. }
  230.  
  231. - (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
  232.    
  233.     firstTouch = true;
  234.    
  235.     for(int i = 0; i < self.profile.tableOfDice.count; i++){
  236.         [[self.profile getDiceAt:i] applyForceInDirection:GLKVector3Make(moveX, -moveY, 0)];
  237.     }
  238.    
  239. }
  240.  
  241. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement