closed #4107: Director::setAnimationInterval could not work on Mac platform. Also removed unused files (CCDirectorCaller.h/.mm).

This commit is contained in:
James Chen 2014-02-21 11:55:37 +08:00
parent 97a6ac65d3
commit c5bf3950bc
6 changed files with 21 additions and 257 deletions

View File

@ -1 +1 @@
ca767f87c916145961c198473fb7b17d71480ac2
e3331b31586222de318fe42b3d7d9a22c23f2d1f

View File

@ -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()

View File

@ -104,6 +104,7 @@ public:
protected:
static Application * sm_pSharedApplication;
long _animationInterval; //micro second
std::string _resourceRootPath;
std::string _startupScriptFilename;
};

View File

@ -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<useconds_t>((_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()

View File

@ -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 <Foundation/Foundation.h>
#import <QuartzCore/CVDisplayLink.h>
@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

View File

@ -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 <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
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