/**************************************************************************** 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