istereo
diff 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 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/EAGLView.m Tue Sep 06 08:07:14 2011 +0300 1.3 @@ -0,0 +1,150 @@ 1.4 +// 1.5 +// EAGLView.m 1.6 +// istereo 1.7 +// 1.8 +// Created by nuclear on 9/6/11. 1.9 +// Copyright __MyCompanyName__ 2011. All rights reserved. 1.10 +// 1.11 + 1.12 +#import "EAGLView.h" 1.13 + 1.14 +#import "ES1Renderer.h" 1.15 +#import "ES2Renderer.h" 1.16 + 1.17 +@implementation EAGLView 1.18 + 1.19 +@synthesize animating; 1.20 +@dynamic animationFrameInterval; 1.21 + 1.22 +// You must implement this method 1.23 ++ (Class)layerClass 1.24 +{ 1.25 + return [CAEAGLLayer class]; 1.26 +} 1.27 + 1.28 +//The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder: 1.29 +- (id)initWithCoder:(NSCoder*)coder 1.30 +{ 1.31 + if ((self = [super initWithCoder:coder])) 1.32 + { 1.33 + // Get the layer 1.34 + CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; 1.35 + 1.36 + eaglLayer.opaque = TRUE; 1.37 + eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: 1.38 + [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil]; 1.39 + 1.40 + renderer = [[ES2Renderer alloc] init]; 1.41 + 1.42 + if (!renderer) 1.43 + { 1.44 + renderer = [[ES1Renderer alloc] init]; 1.45 + 1.46 + if (!renderer) 1.47 + { 1.48 + [self release]; 1.49 + return nil; 1.50 + } 1.51 + } 1.52 + 1.53 + animating = FALSE; 1.54 + displayLinkSupported = FALSE; 1.55 + animationFrameInterval = 1; 1.56 + displayLink = nil; 1.57 + animationTimer = nil; 1.58 + 1.59 + // A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer 1.60 + // class is used as fallback when it isn't available. 1.61 + NSString *reqSysVer = @"3.1"; 1.62 + NSString *currSysVer = [[UIDevice currentDevice] systemVersion]; 1.63 + if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) 1.64 + displayLinkSupported = TRUE; 1.65 + } 1.66 + 1.67 + return self; 1.68 +} 1.69 + 1.70 +- (void)drawView:(id)sender 1.71 +{ 1.72 + [renderer render]; 1.73 +} 1.74 + 1.75 +- (void)layoutSubviews 1.76 +{ 1.77 + [renderer resizeFromLayer:(CAEAGLLayer*)self.layer]; 1.78 + [self drawView:nil]; 1.79 +} 1.80 + 1.81 +- (NSInteger)animationFrameInterval 1.82 +{ 1.83 + return animationFrameInterval; 1.84 +} 1.85 + 1.86 +- (void)setAnimationFrameInterval:(NSInteger)frameInterval 1.87 +{ 1.88 + // Frame interval defines how many display frames must pass between each time the 1.89 + // display link fires. The display link will only fire 30 times a second when the 1.90 + // frame internal is two on a display that refreshes 60 times a second. The default 1.91 + // frame interval setting of one will fire 60 times a second when the display refreshes 1.92 + // at 60 times a second. A frame interval setting of less than one results in undefined 1.93 + // behavior. 1.94 + if (frameInterval >= 1) 1.95 + { 1.96 + animationFrameInterval = frameInterval; 1.97 + 1.98 + if (animating) 1.99 + { 1.100 + [self stopAnimation]; 1.101 + [self startAnimation]; 1.102 + } 1.103 + } 1.104 +} 1.105 + 1.106 +- (void)startAnimation 1.107 +{ 1.108 + if (!animating) 1.109 + { 1.110 + if (displayLinkSupported) 1.111 + { 1.112 + // CADisplayLink is API new to iPhone SDK 3.1. Compiling against earlier versions will result in a warning, but can be dismissed 1.113 + // if the system version runtime check for CADisplayLink exists in -initWithCoder:. The runtime check ensures this code will 1.114 + // not be called in system versions earlier than 3.1. 1.115 + 1.116 + displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(drawView:)]; 1.117 + [displayLink setFrameInterval:animationFrameInterval]; 1.118 + [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 1.119 + } 1.120 + else 1.121 + animationTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)((1.0 / 60.0) * animationFrameInterval) target:self selector:@selector(drawView:) userInfo:nil repeats:TRUE]; 1.122 + 1.123 + animating = TRUE; 1.124 + } 1.125 +} 1.126 + 1.127 +- (void)stopAnimation 1.128 +{ 1.129 + if (animating) 1.130 + { 1.131 + if (displayLinkSupported) 1.132 + { 1.133 + [displayLink invalidate]; 1.134 + displayLink = nil; 1.135 + } 1.136 + else 1.137 + { 1.138 + [animationTimer invalidate]; 1.139 + animationTimer = nil; 1.140 + } 1.141 + 1.142 + animating = FALSE; 1.143 + } 1.144 +} 1.145 + 1.146 +- (void)dealloc 1.147 +{ 1.148 + [renderer release]; 1.149 + 1.150 + [super dealloc]; 1.151 +} 1.152 + 1.153 +@end