istereo

view src/EAGLView.m @ 39:ff055bff6a15

copyright statements and stuff
author John Tsiombikas <nuclear@mutantstargoat.com>
date Sun, 11 Sep 2011 09:03:18 +0300
parents 23e5d274b2a2
children
line source
1 /*
2 Stereoscopic tunnel for iOS.
3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
19 /* XXX this file is mostly generated from Xcode and therefore sucks ass */
21 #import "EAGLView.h"
22 #import "ES2Renderer.h"
23 #import "ui.h"
26 static UI *optgui;
29 @implementation EAGLView
31 @synthesize animating;
32 @dynamic animationFrameInterval;
34 // You must implement this method
35 + (Class)layerClass
36 {
37 return [CAEAGLLayer class];
38 }
40 //The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
41 - (id)initWithCoder:(NSCoder*)coder
42 {
43 if ((self = [super initWithCoder:coder]))
44 {
45 // Get the layer
46 CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
48 //self.contentScaleFactor = 2.0;
50 eaglLayer.opaque = TRUE;
51 eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
52 [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
54 renderer = [[ES2Renderer alloc] init];
55 if (!renderer) {
56 [self release];
57 return nil;
58 }
60 animating = FALSE;
61 displayLinkSupported = FALSE;
62 animationFrameInterval = 1;
63 displayLink = nil;
64 animationTimer = nil;
66 // A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
67 // class is used as fallback when it isn't available.
68 NSString *reqSysVer = @"3.1";
69 NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
70 if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
71 displayLinkSupported = TRUE;
73 self.multipleTouchEnabled = 1;
75 // load the options gui
76 optgui = [[UI alloc] initWithNibName:@"ui" bundle: [NSBundle mainBundle]];
77 hide_options();
78 [self addSubview: optgui.view];
79 }
81 return self;
82 }
84 - (void)drawView:(id)sender
85 {
86 [renderer render];
87 }
89 - (void)layoutSubviews
90 {
91 [renderer resizeFromLayer:(CAEAGLLayer*)self.layer];
92 [self drawView:nil];
93 }
95 - (NSInteger)animationFrameInterval
96 {
97 return animationFrameInterval;
98 }
100 - (void)setAnimationFrameInterval:(NSInteger)frameInterval
101 {
102 // Frame interval defines how many display frames must pass between each time the
103 // display link fires. The display link will only fire 30 times a second when the
104 // frame internal is two on a display that refreshes 60 times a second. The default
105 // frame interval setting of one will fire 60 times a second when the display refreshes
106 // at 60 times a second. A frame interval setting of less than one results in undefined
107 // behavior.
108 if (frameInterval >= 1)
109 {
110 animationFrameInterval = frameInterval;
112 if (animating)
113 {
114 [self stopAnimation];
115 [self startAnimation];
116 }
117 }
118 }
120 - (void)startAnimation
121 {
122 if (!animating)
123 {
124 if (displayLinkSupported)
125 {
126 // CADisplayLink is API new to iPhone SDK 3.1. Compiling against earlier versions will result in a warning, but can be dismissed
127 // if the system version runtime check for CADisplayLink exists in -initWithCoder:. The runtime check ensures this code will
128 // not be called in system versions earlier than 3.1.
130 displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(drawView:)];
131 [displayLink setFrameInterval:animationFrameInterval];
132 [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
133 }
134 else
135 animationTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)((1.0 / 60.0) * animationFrameInterval) target:self selector:@selector(drawView:) userInfo:nil repeats:TRUE];
137 animating = TRUE;
138 }
139 }
141 - (void)stopAnimation
142 {
143 if (animating)
144 {
145 if (displayLinkSupported)
146 {
147 [displayLink invalidate];
148 displayLink = nil;
149 }
150 else
151 {
152 [animationTimer invalidate];
153 animationTimer = nil;
154 }
156 animating = FALSE;
157 }
158 }
160 static int touch_active;
161 static CGPoint start_touch;
162 extern int use_bump;
164 - (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event
165 {
166 UITouch *touch = [[touches allObjects] objectAtIndex: 0];
168 start_touch = [touch locationInView: self];
169 touch_active = 1;
170 }
172 - (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event
173 {
174 UITouch *touch = [[touches allObjects] objectAtIndex: 0];
176 CGPoint end_touch = [touch locationInView: self];
177 int dx = end_touch.x - start_touch.x;
178 int dy = end_touch.y - start_touch.y;
180 if(dx * dx + dy * dy < 30) {
181 show_options();
182 }
183 }
185 - (void)dealloc
186 {
187 [renderer release];
189 [super dealloc];
190 }
192 @end
194 void show_options(void)
195 {
196 assert(optgui);
197 optgui.view.hidden = NO;
198 }
200 void hide_options(void)
201 {
202 assert(optgui);
203 optgui.view.hidden = YES;
204 }