istereo

annotate src/EAGLView.m @ 30:8dd271942543

fixed everything
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 08 Sep 2011 14:52:13 +0300
parents 1bb950d0976b
children 23e5d274b2a2
rev   line source
nuclear@0 1 //
nuclear@0 2 // EAGLView.m
nuclear@0 3 // istereo
nuclear@0 4 //
nuclear@0 5 // Created by nuclear on 9/6/11.
nuclear@0 6 // Copyright __MyCompanyName__ 2011. All rights reserved.
nuclear@0 7 //
nuclear@0 8
nuclear@0 9 #import "EAGLView.h"
nuclear@0 10
nuclear@0 11 #import "ES1Renderer.h"
nuclear@0 12 #import "ES2Renderer.h"
nuclear@0 13
nuclear@0 14 @implementation EAGLView
nuclear@0 15
nuclear@0 16 @synthesize animating;
nuclear@0 17 @dynamic animationFrameInterval;
nuclear@0 18
nuclear@0 19 // You must implement this method
nuclear@0 20 + (Class)layerClass
nuclear@0 21 {
nuclear@0 22 return [CAEAGLLayer class];
nuclear@0 23 }
nuclear@0 24
nuclear@0 25 //The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
nuclear@0 26 - (id)initWithCoder:(NSCoder*)coder
nuclear@30 27 {
nuclear@0 28 if ((self = [super initWithCoder:coder]))
nuclear@0 29 {
nuclear@0 30 // Get the layer
nuclear@0 31 CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
nuclear@0 32
nuclear@30 33 //self.contentScaleFactor = 2.0;
nuclear@30 34
nuclear@0 35 eaglLayer.opaque = TRUE;
nuclear@0 36 eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
nuclear@0 37 [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
nuclear@0 38
nuclear@0 39 renderer = [[ES2Renderer alloc] init];
nuclear@0 40
nuclear@0 41 if (!renderer)
nuclear@0 42 {
nuclear@0 43 renderer = [[ES1Renderer alloc] init];
nuclear@0 44
nuclear@0 45 if (!renderer)
nuclear@0 46 {
nuclear@0 47 [self release];
nuclear@0 48 return nil;
nuclear@0 49 }
nuclear@0 50 }
nuclear@0 51
nuclear@0 52 animating = FALSE;
nuclear@0 53 displayLinkSupported = FALSE;
nuclear@0 54 animationFrameInterval = 1;
nuclear@0 55 displayLink = nil;
nuclear@0 56 animationTimer = nil;
nuclear@0 57
nuclear@0 58 // A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
nuclear@0 59 // class is used as fallback when it isn't available.
nuclear@0 60 NSString *reqSysVer = @"3.1";
nuclear@0 61 NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
nuclear@0 62 if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
nuclear@0 63 displayLinkSupported = TRUE;
nuclear@30 64
nuclear@30 65 self.multipleTouchEnabled = 1;
nuclear@0 66 }
nuclear@0 67
nuclear@0 68 return self;
nuclear@0 69 }
nuclear@0 70
nuclear@0 71 - (void)drawView:(id)sender
nuclear@0 72 {
nuclear@0 73 [renderer render];
nuclear@0 74 }
nuclear@0 75
nuclear@0 76 - (void)layoutSubviews
nuclear@0 77 {
nuclear@0 78 [renderer resizeFromLayer:(CAEAGLLayer*)self.layer];
nuclear@0 79 [self drawView:nil];
nuclear@0 80 }
nuclear@0 81
nuclear@0 82 - (NSInteger)animationFrameInterval
nuclear@0 83 {
nuclear@0 84 return animationFrameInterval;
nuclear@0 85 }
nuclear@0 86
nuclear@0 87 - (void)setAnimationFrameInterval:(NSInteger)frameInterval
nuclear@0 88 {
nuclear@0 89 // Frame interval defines how many display frames must pass between each time the
nuclear@0 90 // display link fires. The display link will only fire 30 times a second when the
nuclear@0 91 // frame internal is two on a display that refreshes 60 times a second. The default
nuclear@0 92 // frame interval setting of one will fire 60 times a second when the display refreshes
nuclear@0 93 // at 60 times a second. A frame interval setting of less than one results in undefined
nuclear@0 94 // behavior.
nuclear@0 95 if (frameInterval >= 1)
nuclear@0 96 {
nuclear@0 97 animationFrameInterval = frameInterval;
nuclear@0 98
nuclear@0 99 if (animating)
nuclear@0 100 {
nuclear@0 101 [self stopAnimation];
nuclear@0 102 [self startAnimation];
nuclear@0 103 }
nuclear@0 104 }
nuclear@0 105 }
nuclear@0 106
nuclear@0 107 - (void)startAnimation
nuclear@0 108 {
nuclear@0 109 if (!animating)
nuclear@0 110 {
nuclear@0 111 if (displayLinkSupported)
nuclear@0 112 {
nuclear@0 113 // CADisplayLink is API new to iPhone SDK 3.1. Compiling against earlier versions will result in a warning, but can be dismissed
nuclear@0 114 // if the system version runtime check for CADisplayLink exists in -initWithCoder:. The runtime check ensures this code will
nuclear@0 115 // not be called in system versions earlier than 3.1.
nuclear@0 116
nuclear@0 117 displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(drawView:)];
nuclear@0 118 [displayLink setFrameInterval:animationFrameInterval];
nuclear@0 119 [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
nuclear@0 120 }
nuclear@0 121 else
nuclear@0 122 animationTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)((1.0 / 60.0) * animationFrameInterval) target:self selector:@selector(drawView:) userInfo:nil repeats:TRUE];
nuclear@0 123
nuclear@0 124 animating = TRUE;
nuclear@0 125 }
nuclear@0 126 }
nuclear@0 127
nuclear@0 128 - (void)stopAnimation
nuclear@0 129 {
nuclear@0 130 if (animating)
nuclear@0 131 {
nuclear@0 132 if (displayLinkSupported)
nuclear@0 133 {
nuclear@0 134 [displayLink invalidate];
nuclear@0 135 displayLink = nil;
nuclear@0 136 }
nuclear@0 137 else
nuclear@0 138 {
nuclear@0 139 [animationTimer invalidate];
nuclear@0 140 animationTimer = nil;
nuclear@0 141 }
nuclear@0 142
nuclear@0 143 animating = FALSE;
nuclear@0 144 }
nuclear@0 145 }
nuclear@0 146
nuclear@30 147 static int touch_active;
nuclear@30 148 static CGPoint start_touch;
nuclear@30 149 extern int use_bump;
nuclear@30 150
nuclear@30 151 - (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event
nuclear@30 152 {
nuclear@30 153 UITouch *touch = [[touches allObjects] objectAtIndex: 0];
nuclear@30 154
nuclear@30 155 start_touch = [touch locationInView: self];
nuclear@30 156 touch_active = 1;
nuclear@30 157 }
nuclear@30 158
nuclear@30 159 - (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event
nuclear@30 160 {
nuclear@30 161 UITouch *touch = [[touches allObjects] objectAtIndex: 0];
nuclear@30 162
nuclear@30 163 CGPoint end_touch = [touch locationInView: self];
nuclear@30 164 int dx = end_touch.x - start_touch.x;
nuclear@30 165 int dy = end_touch.y - start_touch.y;
nuclear@30 166
nuclear@30 167 if(dx * dx + dy * dy < 30) {
nuclear@30 168 use_bump = !use_bump;
nuclear@30 169 }
nuclear@30 170 }
nuclear@30 171
nuclear@0 172 - (void)dealloc
nuclear@0 173 {
nuclear@0 174 [renderer release];
nuclear@0 175
nuclear@0 176 [super dealloc];
nuclear@0 177 }
nuclear@0 178
nuclear@0 179 @end