istereo

annotate src/EAGLView.m @ 0:1bb950d0976b

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 06 Sep 2011 08:07:14 +0300
parents
children 8dd271942543
rev   line source
nuclear@0 1 //
nuclear@0 2 // EAGLView.m
nuclear@0 3 // istereo
nuclear@0 4 //
nuclear@0 5 // Created by nuclear on 9/6/11.
nuclear@0 6 // Copyright __MyCompanyName__ 2011. All rights reserved.
nuclear@0 7 //
nuclear@0 8
nuclear@0 9 #import "EAGLView.h"
nuclear@0 10
nuclear@0 11 #import "ES1Renderer.h"
nuclear@0 12 #import "ES2Renderer.h"
nuclear@0 13
nuclear@0 14 @implementation EAGLView
nuclear@0 15
nuclear@0 16 @synthesize animating;
nuclear@0 17 @dynamic animationFrameInterval;
nuclear@0 18
nuclear@0 19 // You must implement this method
nuclear@0 20 + (Class)layerClass
nuclear@0 21 {
nuclear@0 22 return [CAEAGLLayer class];
nuclear@0 23 }
nuclear@0 24
nuclear@0 25 //The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
nuclear@0 26 - (id)initWithCoder:(NSCoder*)coder
nuclear@0 27 {
nuclear@0 28 if ((self = [super initWithCoder:coder]))
nuclear@0 29 {
nuclear@0 30 // Get the layer
nuclear@0 31 CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
nuclear@0 32
nuclear@0 33 eaglLayer.opaque = TRUE;
nuclear@0 34 eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
nuclear@0 35 [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
nuclear@0 36
nuclear@0 37 renderer = [[ES2Renderer alloc] init];
nuclear@0 38
nuclear@0 39 if (!renderer)
nuclear@0 40 {
nuclear@0 41 renderer = [[ES1Renderer alloc] init];
nuclear@0 42
nuclear@0 43 if (!renderer)
nuclear@0 44 {
nuclear@0 45 [self release];
nuclear@0 46 return nil;
nuclear@0 47 }
nuclear@0 48 }
nuclear@0 49
nuclear@0 50 animating = FALSE;
nuclear@0 51 displayLinkSupported = FALSE;
nuclear@0 52 animationFrameInterval = 1;
nuclear@0 53 displayLink = nil;
nuclear@0 54 animationTimer = nil;
nuclear@0 55
nuclear@0 56 // A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
nuclear@0 57 // class is used as fallback when it isn't available.
nuclear@0 58 NSString *reqSysVer = @"3.1";
nuclear@0 59 NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
nuclear@0 60 if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
nuclear@0 61 displayLinkSupported = TRUE;
nuclear@0 62 }
nuclear@0 63
nuclear@0 64 return self;
nuclear@0 65 }
nuclear@0 66
nuclear@0 67 - (void)drawView:(id)sender
nuclear@0 68 {
nuclear@0 69 [renderer render];
nuclear@0 70 }
nuclear@0 71
nuclear@0 72 - (void)layoutSubviews
nuclear@0 73 {
nuclear@0 74 [renderer resizeFromLayer:(CAEAGLLayer*)self.layer];
nuclear@0 75 [self drawView:nil];
nuclear@0 76 }
nuclear@0 77
nuclear@0 78 - (NSInteger)animationFrameInterval
nuclear@0 79 {
nuclear@0 80 return animationFrameInterval;
nuclear@0 81 }
nuclear@0 82
nuclear@0 83 - (void)setAnimationFrameInterval:(NSInteger)frameInterval
nuclear@0 84 {
nuclear@0 85 // Frame interval defines how many display frames must pass between each time the
nuclear@0 86 // display link fires. The display link will only fire 30 times a second when the
nuclear@0 87 // frame internal is two on a display that refreshes 60 times a second. The default
nuclear@0 88 // frame interval setting of one will fire 60 times a second when the display refreshes
nuclear@0 89 // at 60 times a second. A frame interval setting of less than one results in undefined
nuclear@0 90 // behavior.
nuclear@0 91 if (frameInterval >= 1)
nuclear@0 92 {
nuclear@0 93 animationFrameInterval = frameInterval;
nuclear@0 94
nuclear@0 95 if (animating)
nuclear@0 96 {
nuclear@0 97 [self stopAnimation];
nuclear@0 98 [self startAnimation];
nuclear@0 99 }
nuclear@0 100 }
nuclear@0 101 }
nuclear@0 102
nuclear@0 103 - (void)startAnimation
nuclear@0 104 {
nuclear@0 105 if (!animating)
nuclear@0 106 {
nuclear@0 107 if (displayLinkSupported)
nuclear@0 108 {
nuclear@0 109 // CADisplayLink is API new to iPhone SDK 3.1. Compiling against earlier versions will result in a warning, but can be dismissed
nuclear@0 110 // if the system version runtime check for CADisplayLink exists in -initWithCoder:. The runtime check ensures this code will
nuclear@0 111 // not be called in system versions earlier than 3.1.
nuclear@0 112
nuclear@0 113 displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(drawView:)];
nuclear@0 114 [displayLink setFrameInterval:animationFrameInterval];
nuclear@0 115 [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
nuclear@0 116 }
nuclear@0 117 else
nuclear@0 118 animationTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)((1.0 / 60.0) * animationFrameInterval) target:self selector:@selector(drawView:) userInfo:nil repeats:TRUE];
nuclear@0 119
nuclear@0 120 animating = TRUE;
nuclear@0 121 }
nuclear@0 122 }
nuclear@0 123
nuclear@0 124 - (void)stopAnimation
nuclear@0 125 {
nuclear@0 126 if (animating)
nuclear@0 127 {
nuclear@0 128 if (displayLinkSupported)
nuclear@0 129 {
nuclear@0 130 [displayLink invalidate];
nuclear@0 131 displayLink = nil;
nuclear@0 132 }
nuclear@0 133 else
nuclear@0 134 {
nuclear@0 135 [animationTimer invalidate];
nuclear@0 136 animationTimer = nil;
nuclear@0 137 }
nuclear@0 138
nuclear@0 139 animating = FALSE;
nuclear@0 140 }
nuclear@0 141 }
nuclear@0 142
nuclear@0 143 - (void)dealloc
nuclear@0 144 {
nuclear@0 145 [renderer release];
nuclear@0 146
nuclear@0 147 [super dealloc];
nuclear@0 148 }
nuclear@0 149
nuclear@0 150 @end