istereo

view src/EAGLView.m @ 14:b39d8607f4bb

added textures
author John Tsiombikas <nuclear@mutantstargoat.com>
date Wed, 07 Sep 2011 09:03:51 +0300
parents
children 8dd271942543
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 eaglLayer.opaque = TRUE;
34 eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
35 [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
37 renderer = [[ES2Renderer alloc] init];
39 if (!renderer)
40 {
41 renderer = [[ES1Renderer alloc] init];
43 if (!renderer)
44 {
45 [self release];
46 return nil;
47 }
48 }
50 animating = FALSE;
51 displayLinkSupported = FALSE;
52 animationFrameInterval = 1;
53 displayLink = nil;
54 animationTimer = nil;
56 // A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
57 // class is used as fallback when it isn't available.
58 NSString *reqSysVer = @"3.1";
59 NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
60 if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
61 displayLinkSupported = TRUE;
62 }
64 return self;
65 }
67 - (void)drawView:(id)sender
68 {
69 [renderer render];
70 }
72 - (void)layoutSubviews
73 {
74 [renderer resizeFromLayer:(CAEAGLLayer*)self.layer];
75 [self drawView:nil];
76 }
78 - (NSInteger)animationFrameInterval
79 {
80 return animationFrameInterval;
81 }
83 - (void)setAnimationFrameInterval:(NSInteger)frameInterval
84 {
85 // Frame interval defines how many display frames must pass between each time the
86 // display link fires. The display link will only fire 30 times a second when the
87 // frame internal is two on a display that refreshes 60 times a second. The default
88 // frame interval setting of one will fire 60 times a second when the display refreshes
89 // at 60 times a second. A frame interval setting of less than one results in undefined
90 // behavior.
91 if (frameInterval >= 1)
92 {
93 animationFrameInterval = frameInterval;
95 if (animating)
96 {
97 [self stopAnimation];
98 [self startAnimation];
99 }
100 }
101 }
103 - (void)startAnimation
104 {
105 if (!animating)
106 {
107 if (displayLinkSupported)
108 {
109 // CADisplayLink is API new to iPhone SDK 3.1. Compiling against earlier versions will result in a warning, but can be dismissed
110 // if the system version runtime check for CADisplayLink exists in -initWithCoder:. The runtime check ensures this code will
111 // not be called in system versions earlier than 3.1.
113 displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(drawView:)];
114 [displayLink setFrameInterval:animationFrameInterval];
115 [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
116 }
117 else
118 animationTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)((1.0 / 60.0) * animationFrameInterval) target:self selector:@selector(drawView:) userInfo:nil repeats:TRUE];
120 animating = TRUE;
121 }
122 }
124 - (void)stopAnimation
125 {
126 if (animating)
127 {
128 if (displayLinkSupported)
129 {
130 [displayLink invalidate];
131 displayLink = nil;
132 }
133 else
134 {
135 [animationTimer invalidate];
136 animationTimer = nil;
137 }
139 animating = FALSE;
140 }
141 }
143 - (void)dealloc
144 {
145 [renderer release];
147 [super dealloc];
148 }
150 @end