From c5bf3950bcb21e4ac237c2018f4b9255aa74bca2 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 21 Feb 2014 11:55:37 +0800 Subject: [PATCH] closed #4107: Director::setAnimationInterval could not work on Mac platform. Also removed unused files (CCDirectorCaller.h/.mm). --- .../project.pbxproj.REMOVED.git-id | 2 +- cocos/2d/platform/linux/CCApplication.cpp | 4 +- cocos/2d/platform/mac/CCApplication.h | 1 + cocos/2d/platform/mac/CCApplication.mm | 18 +- cocos/2d/platform/mac/CCDirectorCaller.h | 46 ---- cocos/2d/platform/mac/CCDirectorCaller.mm | 207 ------------------ 6 files changed, 21 insertions(+), 257 deletions(-) delete mode 100644 cocos/2d/platform/mac/CCDirectorCaller.h delete mode 100644 cocos/2d/platform/mac/CCDirectorCaller.mm diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id b/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id index b8bd735fa2..456cde7d43 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -ca767f87c916145961c198473fb7b17d71480ac2 \ No newline at end of file +e3331b31586222de318fe42b3d7d9a22c23f2d1f \ No newline at end of file diff --git a/cocos/2d/platform/linux/CCApplication.cpp b/cocos/2d/platform/linux/CCApplication.cpp index 97ae71ba74..9d8e81fb1d 100644 --- a/cocos/2d/platform/linux/CCApplication.cpp +++ b/cocos/2d/platform/linux/CCApplication.cpp @@ -50,6 +50,7 @@ static long getCurrentMillSecond() { } Application::Application() +: _animationInterval(1.0f/60.0f*1000.0f) { CC_ASSERT(! sm_pSharedApplication); sm_pSharedApplication = this; @@ -59,7 +60,6 @@ Application::~Application() { CC_ASSERT(this == sm_pSharedApplication); sm_pSharedApplication = NULL; - _animationInterval = 1.0f/60.0f*1000.0f; } int Application::run() @@ -75,7 +75,7 @@ int Application::run() while (!glview->windowShouldClose()) { - long iLastTime = getCurrentMillSecond(); + long iLastTime = getCurrentMillSecond(); director->mainLoop(); glview->pollEvents(); long iCurTime = getCurrentMillSecond(); diff --git a/cocos/2d/platform/mac/CCApplication.h b/cocos/2d/platform/mac/CCApplication.h index ed39ee0f35..5d1c9b5cd5 100644 --- a/cocos/2d/platform/mac/CCApplication.h +++ b/cocos/2d/platform/mac/CCApplication.h @@ -104,6 +104,7 @@ public: protected: static Application * sm_pSharedApplication; + long _animationInterval; //micro second std::string _resourceRootPath; std::string _startupScriptFilename; }; diff --git a/cocos/2d/platform/mac/CCApplication.mm b/cocos/2d/platform/mac/CCApplication.mm index 99ac7613a6..d739404b45 100644 --- a/cocos/2d/platform/mac/CCApplication.mm +++ b/cocos/2d/platform/mac/CCApplication.mm @@ -37,9 +37,20 @@ THE SOFTWARE. NS_CC_BEGIN +static long getCurrentMillSecond() +{ + long lLastTime = 0; + struct timeval stCurrentTime; + + gettimeofday(&stCurrentTime,NULL); + lLastTime = stCurrentTime.tv_sec*1000+stCurrentTime.tv_usec*0.001; //millseconds + return lLastTime; +} + Application* Application::sm_pSharedApplication = 0; Application::Application() +: _animationInterval(1.0f/60.0f*1000.0f) { CCASSERT(! sm_pSharedApplication, "sm_pSharedApplication already exist"); sm_pSharedApplication = this; @@ -61,8 +72,13 @@ int Application::run() while (!glview->windowShouldClose()) { + long iLastTime = getCurrentMillSecond(); Director::getInstance()->mainLoop(); glview->pollEvents(); + long iCurTime = getCurrentMillSecond(); + if (iCurTime-iLastTime<_animationInterval){ + usleep(static_cast((_animationInterval - iCurTime+iLastTime)*1000)); + } } /* Only work on Desktop @@ -77,7 +93,7 @@ int Application::run() void Application::setAnimationInterval(double interval) { - [[CCDirectorCaller sharedDirectorCaller] setAnimationInterval: interval ]; + _animationInterval = interval*1000.0f; } Application::Platform Application::getTargetPlatform() diff --git a/cocos/2d/platform/mac/CCDirectorCaller.h b/cocos/2d/platform/mac/CCDirectorCaller.h deleted file mode 100644 index 7e7d9070c3..0000000000 --- a/cocos/2d/platform/mac/CCDirectorCaller.h +++ /dev/null @@ -1,46 +0,0 @@ -/**************************************************************************** -Copyright (c) 2010-2012 cocos2d-x.org -Copyright (c) 2013-2014 Chukong Technologies Inc. - -http://www.cocos2d-x.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -****************************************************************************/ - -#include "CCPlatformConfig.h" -#if CC_TARGET_PLATFORM == CC_PLATFORM_MAC - -#import -#import - -@interface CCDirectorCaller : NSObject { - CVDisplayLinkRef displayLink; - NSTimer *renderTimer; - int interval; -} -@property (readwrite) int interval; --(void) startMainLoop; --(void) end; --(void) doCaller: (id) sender; --(void) setAnimationInterval:(double)interval; - -+(id) sharedDirectorCaller; -@end - -#endif // CC_TARGET_PLATFORM == CC_PLATFORM_MAC diff --git a/cocos/2d/platform/mac/CCDirectorCaller.mm b/cocos/2d/platform/mac/CCDirectorCaller.mm deleted file mode 100644 index ea32f5b6b2..0000000000 --- a/cocos/2d/platform/mac/CCDirectorCaller.mm +++ /dev/null @@ -1,207 +0,0 @@ -/**************************************************************************** -Copyright (c) 2010-2012 cocos2d-x.org -Copyright (c) 2013-2014 Chukong Technologies Inc. - -http://www.cocos2d-x.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -****************************************************************************/ - -#include "CCPlatformConfig.h" -#if CC_TARGET_PLATFORM == CC_PLATFORM_MAC - -#import "CCDirectorCaller.h" -#include "CCDirector.h" -#include "CCAutoreleasePool.h" - -#import -#import - -static id s_sharedDirectorCaller; - -@interface NSObject(CADisplayLink) -+(id) displayLinkWithTarget: (id)arg1 selector:(SEL)arg2; --(void) addToRunLoop: (id)arg1 forMode: (id)arg2; --(void) setFrameInterval: (int)interval; --(void) invalidate; -@end - -@implementation CCDirectorCaller - -@synthesize interval; - -+(id) sharedDirectorCaller -{ - if (s_sharedDirectorCaller == nil) - { - s_sharedDirectorCaller = [[CCDirectorCaller alloc] init]; - } - - return s_sharedDirectorCaller; -} - --(void) alloc -{ - interval = 1; -} - --(void) dealloc -{ - s_sharedDirectorCaller = nil; - CCLOGINFO("deallocing DirectorCaller: %p", self); - if (displayLink) { - CVDisplayLinkRelease(displayLink); - } - - CCLOG("--------------------------------------------------------------------------------"); - CCLOG(""); - CCLOG(""); - CCLOG(""); - [super dealloc]; -} - -- (CVReturn) getFrameForTime:(const CVTimeStamp*)outputTime -{ -#if CC_DIRECTOR_MAC_USE_DISPLAY_LINK_THREAD - //if( ! runningThread_ ) - //runningThread_ = [NSThread currentThread]; - - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - - cocos2d::Director::getInstance()->drawScene(); - cocos2d::PoolManager::getInstance()->getCurrentPool()->clear(); - - [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:nil]; - - // release the objects - [pool release]; - -#else - [self performSelector:@selector(drawScene) onThread:[NSThread currentThread] withObject:nil waitUntilDone:YES]; -#endif - - return kCVReturnSuccess; -} - -// This is the renderer output callback function -static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* displayLinkContext) -{ - // CVReturn result = [(DirectorCaller*)displayLinkContext getFrameForTime:outputTime]; - // return result; - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - cocos2d::Director::getInstance()->mainLoop(); - [pool release]; - - return kCVReturnSuccess; -} - -- (void)timerFired:(id)sender -{ - // It is good practice in a Cocoa application to allow the system to send the -drawRect: - // message when it needs to draw, and not to invoke it directly from the timer. - // All we do here is tell the display it needs a refresh - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - - // run the main cocos2d loop - cocos2d::Director::getInstance()->mainLoop(); - - [pool release]; -} - --(void) startMainLoop -{ - // Director::setAnimationInterval() is called, we should invalide it first - // [displayLink invalidate]; - // displayLink = nil; - // - // displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)]; - // [displayLink setFrameInterval: self.interval]; - // [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; -#if ! CC_DIRECTOR_MAC_USE_DISPLAY_LINK_THREAD - NSThread* thread = [[NSThread alloc] initWithTarget:self selector:@selector(mainLoop) object:nil]; - [thread start]; -#endif - // NSTimer - [renderTimer invalidate]; - renderTimer = nil; - - renderTimer = [NSTimer timerWithTimeInterval:self.interval/60.0f //a 1ms time interval - target:self - selector:@selector(timerFired:) - userInfo:nil - repeats:YES]; - - [[NSRunLoop currentRunLoop] addTimer:renderTimer - forMode:NSDefaultRunLoopMode]; - [[NSRunLoop currentRunLoop] addTimer:renderTimer - forMode:NSEventTrackingRunLoopMode]; //Ensure timer fires during resize - - /* - // CVDisplayLink - //cocos2d::Director::getInstance()->gettimeofday(); - - // Create a display link capable of being used with all active displays - CVDisplayLinkCreateWithActiveCGDisplays(&displayLink); - - // Set the renderer output callback function - CVDisplayLinkSetOutputCallback(displayLink, &MyDisplayLinkCallback, self); - - // Set the display link for the current renderer - CCEAGLView *openGLView_ = (CCEAGLView*)[CCEAGLView sharedEGLView]; - CGLContextObj cglContext = (CGLContextObj)[[openGLView_ openGLContext] CGLContextObj]; - CGLPixelFormatObj cglPixelFormat = (CGLPixelFormatObj)[[openGLView_ pixelFormat] CGLPixelFormatObj]; - CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, cglContext, cglPixelFormat); - - // Activate the display link - CVDisplayLinkStart(displayLink); - */ -} - --(void) end -{ - [renderTimer invalidate]; - renderTimer = nil; - [self release]; -} - --(void) setAnimationInterval:(double)intervalNew -{ - self.interval = 60.0 * intervalNew; - [renderTimer invalidate]; - renderTimer = nil; - renderTimer = [NSTimer timerWithTimeInterval:self.interval/60.0f //a 1ms time interval - target:self - selector:@selector(timerFired:) - userInfo:nil - repeats:YES]; - - [[NSRunLoop currentRunLoop] addTimer:renderTimer - forMode:NSDefaultRunLoopMode]; - [[NSRunLoop currentRunLoop] addTimer:renderTimer - forMode:NSEventTrackingRunLoopMode]; -} - --(void) doCaller: (id) sender -{ - cocos2d::Director::getInstance()->mainLoop(); -} - -@end - -#endif //s CC_TARGET_PLATFORM == CC_PLATFORM_MAC