2012-07-21 12:23:40 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#import "CCDirectorCaller.h"
|
|
|
|
#import "CCDirector.h"
|
|
|
|
#import "EAGLView.h"
|
|
|
|
#import "CCEventDispatcher.h"
|
|
|
|
#include "CCAutoreleasePool.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)
|
|
|
|
{
|
2012-09-04 01:22:06 +08:00
|
|
|
s_sharedDirectorCaller = [[CCDirectorCaller alloc] init];
|
2012-07-21 12:23:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return s_sharedDirectorCaller;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) alloc
|
|
|
|
{
|
2012-09-04 01:22:06 +08:00
|
|
|
interval = 1;
|
2012-07-21 12:23:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void) dealloc
|
|
|
|
{
|
2012-09-04 01:22:06 +08:00
|
|
|
s_sharedDirectorCaller = nil;
|
2013-06-06 09:21:05 +08:00
|
|
|
CCLOG("cocos2d: deallocing CCDirectorCaller %p", self);
|
2012-07-21 12:23:40 +08:00
|
|
|
if (displayLink) {
|
|
|
|
CVDisplayLinkRelease(displayLink);
|
|
|
|
}
|
2012-09-13 10:26:47 +08:00
|
|
|
|
|
|
|
CCLOG("--------------------------------------------------------------------------------");
|
|
|
|
CCLOG("");
|
|
|
|
CCLOG("");
|
|
|
|
CCLOG("");
|
2012-07-21 12:23:40 +08:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CVReturn) getFrameForTime:(const CVTimeStamp*)outputTime
|
|
|
|
{
|
|
|
|
#if CC_DIRECTOR_MAC_USE_DISPLAY_LINK_THREAD
|
|
|
|
//if( ! runningThread_ )
|
|
|
|
//runningThread_ = [NSThread currentThread];
|
2012-09-04 01:22:06 +08:00
|
|
|
|
2012-07-21 12:23:40 +08:00
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
|
|
|
cocos2d::CCDirector::sharedDirector()->drawScene();
|
|
|
|
cocos2d::CCPoolManager::sharedPoolManager()->pop();
|
|
|
|
[[CCEventDispatcher sharedDispatcher] dispatchQueuedEvents];
|
|
|
|
|
|
|
|
[[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)
|
|
|
|
{
|
2012-09-04 01:22:06 +08:00
|
|
|
// CVReturn result = [(CCDirectorCaller*)displayLinkContext getFrameForTime:outputTime];
|
|
|
|
// return result;
|
2012-07-21 12:23:40 +08:00
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
cocos2d::CCDirector::sharedDirector()->mainLoop();
|
|
|
|
[pool release];
|
2012-09-04 01:22:06 +08:00
|
|
|
|
2012-07-21 12:23:40 +08:00
|
|
|
return kCVReturnSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)timerFired:(id)sender
|
|
|
|
{
|
|
|
|
// It is good practice in a Cocoa application to allow the system to send the -drawRect:
|
2012-09-04 01:22:06 +08:00
|
|
|
// message when it needs to draw, and not to invoke it directly from the timer.
|
2012-07-21 12:23:40 +08:00
|
|
|
// All we do here is tell the display it needs a refresh
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
2012-09-04 01:22:06 +08:00
|
|
|
|
2012-07-21 12:23:40 +08:00
|
|
|
// get the opengl view
|
|
|
|
EAGLView *openGLView = [EAGLView sharedEGLView];
|
|
|
|
[openGLView lockOpenGLContext];
|
2012-09-04 01:22:06 +08:00
|
|
|
|
2012-07-21 12:23:40 +08:00
|
|
|
// run the main cocos2d loop
|
|
|
|
cocos2d::CCDirector::sharedDirector()->mainLoop();
|
2012-09-04 01:22:06 +08:00
|
|
|
|
2012-07-21 12:23:40 +08:00
|
|
|
// flush buffer (this line is very important!)
|
2012-09-04 01:22:06 +08:00
|
|
|
[[openGLView openGLContext] flushBuffer];
|
2012-07-21 12:23:40 +08:00
|
|
|
|
|
|
|
[openGLView unlockOpenGLContext];
|
2012-09-04 01:22:06 +08:00
|
|
|
|
2012-07-21 17:19:03 +08:00
|
|
|
// send any queued events
|
|
|
|
[[CCEventDispatcher sharedDispatcher] dispatchQueuedEvents];
|
2012-09-04 01:22:06 +08:00
|
|
|
|
2012-07-21 12:23:40 +08:00
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) startMainLoop
|
|
|
|
{
|
2012-09-04 01:22:06 +08:00
|
|
|
// CCDirector::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];
|
2012-07-21 12:23:40 +08:00
|
|
|
#if ! CC_DIRECTOR_MAC_USE_DISPLAY_LINK_THREAD
|
|
|
|
NSThread* thread = [[NSThread alloc] initWithTarget:self selector:@selector(mainLoop) object:nil];
|
2012-09-04 01:22:06 +08:00
|
|
|
[thread start];
|
2012-07-21 12:23:40 +08:00
|
|
|
#endif
|
|
|
|
// NSTimer
|
2012-09-04 01:22:06 +08:00
|
|
|
[renderTimer invalidate];
|
|
|
|
renderTimer = nil;
|
|
|
|
|
2012-07-21 12:23:40 +08:00
|
|
|
renderTimer = [NSTimer timerWithTimeInterval:self.interval/60.0f //a 1ms time interval
|
2012-09-04 01:22:06 +08:00
|
|
|
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::CCDirector::sharedDirector()->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
|
|
|
|
EAGLView *openGLView_ = (EAGLView*)[EAGLView 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];
|
2012-07-21 12:23:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
-(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];
|
|
|
|
}
|
2012-09-04 01:22:06 +08:00
|
|
|
|
2012-07-21 12:23:40 +08:00
|
|
|
-(void) doCaller: (id) sender
|
|
|
|
{
|
|
|
|
cocos2d::CCDirector::sharedDirector()->mainLoop();
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|