istereo

view src/EAGLView.m @ 35:23e5d274b2a2

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