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