istereo

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