瀏覽代碼

Added setting for CADisplayLink on iOS, so you no longer need to recompile to change it

steve 10 年之前
父節點
當前提交
15a826571c
共有 4 個文件被更改,包括 45 次插入42 次删除
  1. 2 0
      platform/iphone/app_delegate.mm
  2. 4 5
      platform/iphone/gl_view.h
  3. 38 37
      platform/iphone/gl_view.mm
  4. 1 0
      platform/iphone/globals/global_defaults.cpp

+ 2 - 0
platform/iphone/app_delegate.mm

@@ -236,6 +236,8 @@ static int frame_count = 0;
 	view_controller.view = glView;
 	view_controller.view = glView;
 	window.rootViewController = view_controller;
 	window.rootViewController = view_controller;
 
 
+	glView.useCADisplayLink = bool(GLOBAL_DEF("display.iOS/use_cadisplaylink",true)) ? YES : NO;
+	printf("cadisaplylink: %d", glView.useCADisplayLink);
 	glView.animationInterval = 1.0 / kRenderingFrequency;
 	glView.animationInterval = 1.0 / kRenderingFrequency;
 	[glView startAnimation];
 	[glView startAnimation];
 	
 	

+ 4 - 5
platform/iphone/gl_view.h

@@ -34,8 +34,6 @@
 #import <MediaPlayer/MediaPlayer.h>
 #import <MediaPlayer/MediaPlayer.h>
 #import <AVFoundation/AVFoundation.h>
 #import <AVFoundation/AVFoundation.h>
 
 
-#define USE_CADISPLAYLINK      0   //iOS version 3.1+ is required
-
 @protocol GLViewDelegate;
 @protocol GLViewDelegate;
 
 
 @interface GLView : UIView<UIKeyInput>
 @interface GLView : UIView<UIKeyInput>
@@ -53,13 +51,13 @@
 	// OpenGL name for the depth buffer that is attached to viewFramebuffer, if it exists (0 if it does not exist)
 	// OpenGL name for the depth buffer that is attached to viewFramebuffer, if it exists (0 if it does not exist)
 	GLuint depthRenderbuffer;
 	GLuint depthRenderbuffer;
 	
 	
-#if USE_CADISPLAYLINK
+	BOOL useCADisplayLink;
 	// CADisplayLink available on 3.1+ synchronizes the animation timer & drawing with the refresh rate of the display, only supports animation intervals of 1/60 1/30 & 1/15
 	// CADisplayLink available on 3.1+ synchronizes the animation timer & drawing with the refresh rate of the display, only supports animation intervals of 1/60 1/30 & 1/15
 	CADisplayLink *displayLink;
 	CADisplayLink *displayLink;
-#else
+
 	// An animation timer that, when animation is started, will periodically call -drawView at the given rate.
 	// An animation timer that, when animation is started, will periodically call -drawView at the given rate.
+	// Only used if CADisplayLink is not
 	NSTimer *animationTimer;
 	NSTimer *animationTimer;
-#endif
 	
 	
 	NSTimeInterval animationInterval;
 	NSTimeInterval animationInterval;
 	
 	
@@ -104,6 +102,7 @@
 - (void)audioRouteChangeListenerCallback:(NSNotification*)notification;
 - (void)audioRouteChangeListenerCallback:(NSNotification*)notification;
 
 
 @property NSTimeInterval animationInterval;
 @property NSTimeInterval animationInterval;
+@property(nonatomic, assign) BOOL useCADisplayLink;
 
 
 @end
 @end
 
 

+ 38 - 37
platform/iphone/gl_view.mm

@@ -334,13 +334,15 @@ static void clear_touches() {
 	delegateSetup = ![delegate respondsToSelector:@selector(setupView:)];
 	delegateSetup = ![delegate respondsToSelector:@selector(setupView:)];
 }
 }
 
 
+@synthesize useCADisplayLink;
+
 // If our view is resized, we'll be asked to layout subviews.
 // If our view is resized, we'll be asked to layout subviews.
 // This is the perfect opportunity to also update the framebuffer so that it is
 // This is the perfect opportunity to also update the framebuffer so that it is
 // the same size as our display area.
 // the same size as our display area.
 
 
 -(void)layoutSubviews
 -(void)layoutSubviews
 {
 {
-	printf("HERE\n");
+	//printf("HERE\n");
 	[EAGLContext setCurrentContext:context];
 	[EAGLContext setCurrentContext:context];
 	[self destroyFramebuffer];
 	[self destroyFramebuffer];
 	[self createFramebuffer];
 	[self createFramebuffer];
@@ -418,19 +420,21 @@ static void clear_touches() {
 		return;
 		return;
 	active = TRUE;
 	active = TRUE;
 	printf("start animation!\n");
 	printf("start animation!\n");
-#if USE_CADISPLAYLINK
-	// Approximate frame rate
-	// assumes device refreshes at 60 fps
-	int frameInterval = (int) floor(animationInterval * 60.0f);
-
-	displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawView)];
-	[displayLink setFrameInterval:frameInterval];
-
-	// Setup DisplayLink in main thread
-	[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
-#else
-	animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(drawView) userInfo:nil repeats:YES];
-#endif
+	if (useCADisplayLink) {
+
+		// Approximate frame rate
+		// assumes device refreshes at 60 fps
+		int frameInterval = (int) floor(animationInterval * 60.0f);
+
+		displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawView)];
+		[displayLink setFrameInterval:frameInterval];
+
+		// Setup DisplayLink in main thread
+		[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
+	}
+	else {
+		animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(drawView) userInfo:nil repeats:YES];
+	}
 
 
 	if (video_playing)
 	if (video_playing)
 	{
 	{
@@ -444,13 +448,16 @@ static void clear_touches() {
 		return;
 		return;
 	active = FALSE;
 	active = FALSE;
 	printf("******** stop animation!\n");
 	printf("******** stop animation!\n");
-#if USE_CADISPLAYLINK
-	[displayLink invalidate];
-	displayLink = nil;
-#else
-	[animationTimer invalidate];
-	animationTimer = nil;
-#endif
+
+	if (useCADisplayLink) {
+		[displayLink invalidate];
+		displayLink = nil;
+	}
+	else {
+		[animationTimer invalidate];
+		animationTimer = nil;
+	}
+
 	clear_touches();
 	clear_touches();
 
 
 	if (video_playing)
 	if (video_playing)
@@ -462,13 +469,7 @@ static void clear_touches() {
 - (void)setAnimationInterval:(NSTimeInterval)interval
 - (void)setAnimationInterval:(NSTimeInterval)interval
 {
 {
 	animationInterval = interval;
 	animationInterval = interval;
-	
-#if USE_CADISPLAYLINK
-	if(displayLink)
-#else
-	if(animationTimer)
-#endif
-	{
+	if ( (useCADisplayLink && displayLink) || ( !useCADisplayLink && animationTimer ) ) {
 		[self stopAnimation];
 		[self stopAnimation];
 		[self startAnimation];
 		[self startAnimation];
 	}
 	}
@@ -477,16 +478,16 @@ static void clear_touches() {
 // Updates the OpenGL view when the timer fires
 // Updates the OpenGL view when the timer fires
 - (void)drawView
 - (void)drawView
 {
 {
-#if USE_CADISPLAYLINK
-	// Pause the CADisplayLink to avoid recursion
-	[displayLink setPaused: YES];
+	if (useCADisplayLink) {
+		// Pause the CADisplayLink to avoid recursion
+		[displayLink setPaused: YES];
 
 
-	// Process all input events
-	while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, TRUE) == kCFRunLoopRunHandledSource);
+		// Process all input events
+		while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, TRUE) == kCFRunLoopRunHandledSource);
 
 
-	// We are good to go, resume the CADisplayLink
-	[displayLink setPaused: NO];
-#endif
+		// We are good to go, resume the CADisplayLink
+		[displayLink setPaused: NO];
+	}
 
 
 	if (!active) {
 	if (!active) {
 		printf("draw view not active!\n");
 		printf("draw view not active!\n");
@@ -632,7 +633,7 @@ static void clear_touches() {
 		case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
 		case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
 			NSLog(@"AVAudioSessionRouteChangeReasonOldDeviceUnavailable");
 			NSLog(@"AVAudioSessionRouteChangeReasonOldDeviceUnavailable");
 			NSLog(@"Headphone/Line was pulled. Resuming video play....");
 			NSLog(@"Headphone/Line was pulled. Resuming video play....");
-			if (_is_video_playing) {
+			if (_is_video_playing()) {
 
 
 				dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5f * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
 				dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5f * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
 							[_instance.avPlayer play]; // NOTE: change this line according your current player implementation
 							[_instance.avPlayer play]; // NOTE: change this line according your current player implementation

+ 1 - 0
platform/iphone/globals/global_defaults.cpp

@@ -9,4 +9,5 @@ void register_iphone_global_defaults() {
 	GLOBAL_DEF("rasterizer.iOS/fp16_framebuffer",false);
 	GLOBAL_DEF("rasterizer.iOS/fp16_framebuffer",false);
 	GLOBAL_DEF("display.iOS/driver","GLES2");
 	GLOBAL_DEF("display.iOS/driver","GLES2");
 	Globals::get_singleton()->set_custom_property_info("display.iOS/driver",PropertyInfo(Variant::STRING,"display.iOS/driver",PROPERTY_HINT_ENUM,"GLES1,GLES2"));
 	Globals::get_singleton()->set_custom_property_info("display.iOS/driver",PropertyInfo(Variant::STRING,"display.iOS/driver",PROPERTY_HINT_ENUM,"GLES1,GLES2"));
+	GLOBAL_DEF("display.iOS/use_cadisplaylink",true);
 }
 }