2010-07-16 14:09:31 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "CCScene.h"
|
2010-08-04 16:45:00 +08:00
|
|
|
#include "NSMutableArray.h"
|
2010-07-16 14:09:31 +08:00
|
|
|
#include "CCScheduler.h"
|
2010-07-20 14:07:27 +08:00
|
|
|
#include "ccMacros.h"
|
2010-11-13 11:34:49 +08:00
|
|
|
#include "CCXCocos2dDefine.h"
|
2010-08-25 10:19:20 +08:00
|
|
|
#include "CCTouchDispatcher.h"
|
2010-07-20 14:07:27 +08:00
|
|
|
#include "support/opengl_support/glu.h"
|
2010-08-25 10:19:20 +08:00
|
|
|
#include "CGPointExtension.h"
|
2010-07-20 14:07:27 +08:00
|
|
|
#include "CCTransition.h"
|
2010-07-26 10:00:08 +08:00
|
|
|
#include "CCTextureCache.h"
|
2010-07-26 13:53:08 +08:00
|
|
|
#include "CCTransition.h"
|
2010-08-02 12:00:06 +08:00
|
|
|
#include "CCSpriteFrameCache.h"
|
2010-08-04 16:45:00 +08:00
|
|
|
#include "NSAutoreleasePool.h"
|
2010-08-04 16:18:09 +08:00
|
|
|
#include "platform/platform.h"
|
2010-08-05 11:02:30 +08:00
|
|
|
#include "CCXApplication.h"
|
2010-08-17 14:25:41 +08:00
|
|
|
#include "CCBitmapFontAtlas.h"
|
2010-08-30 15:04:46 +08:00
|
|
|
#include "CCActionManager.h"
|
2010-09-07 16:05:18 +08:00
|
|
|
#include "CCLabel.h"
|
2010-09-11 15:48:37 +08:00
|
|
|
#include "CCConfiguration.h"
|
2010-08-23 17:29:52 +08:00
|
|
|
|
|
|
|
#if CC_ENABLE_PROFILERS
|
|
|
|
#include "support/CCProfiling.h"
|
|
|
|
#endif // CC_ENABLE_PROFILERS
|
2010-07-16 14:09:31 +08:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace std;
|
2010-07-28 10:15:02 +08:00
|
|
|
using namespace cocos2d;
|
2010-09-02 10:33:39 +08:00
|
|
|
namespace cocos2d
|
|
|
|
{
|
2010-07-16 14:09:31 +08:00
|
|
|
|
|
|
|
// singleton stuff
|
2010-08-18 14:57:36 +08:00
|
|
|
static CCDisplayLinkDirector s_sharedDirector;
|
|
|
|
static bool s_bFirstRun = true;
|
2010-07-16 14:09:31 +08:00
|
|
|
|
2010-07-20 14:07:27 +08:00
|
|
|
#define kDefaultFPS 60 // 60 frames per second
|
2010-07-16 14:09:31 +08:00
|
|
|
extern string cocos2dVersion(void);
|
|
|
|
|
2010-11-11 11:18:58 +08:00
|
|
|
CCDirector* CCDirector::sharedDirector(void)
|
2010-07-16 14:09:31 +08:00
|
|
|
{
|
2010-08-18 14:57:36 +08:00
|
|
|
if (s_bFirstRun)
|
2010-07-16 14:09:31 +08:00
|
|
|
{
|
2010-08-18 14:57:36 +08:00
|
|
|
s_sharedDirector.init();
|
|
|
|
s_bFirstRun = false;
|
2010-07-16 14:09:31 +08:00
|
|
|
}
|
|
|
|
|
2010-08-18 14:57:36 +08:00
|
|
|
return &s_sharedDirector;
|
2010-07-16 14:09:31 +08:00
|
|
|
}
|
|
|
|
|
2010-11-11 11:18:58 +08:00
|
|
|
bool CCDirector::setDirectorType(ccDirectorType obDirectorType)
|
2010-07-16 14:09:31 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
switch (obDirectorType)
|
|
|
|
{
|
|
|
|
case CCDirectorTypeNSTimer:
|
2010-11-11 11:18:58 +08:00
|
|
|
CCTimerDirector::sharedDirector();
|
2010-07-16 14:09:31 +08:00
|
|
|
break;
|
|
|
|
case CCDirectorTypeMainLoop:
|
2010-11-11 11:18:58 +08:00
|
|
|
CCFastDirector::sharedDirector();
|
2010-07-16 14:09:31 +08:00
|
|
|
break;
|
|
|
|
case CCDirectorTypeThreadMainLoop:
|
2010-11-11 11:18:58 +08:00
|
|
|
CCThreadedFastDirector::sharedDirector();
|
2010-07-16 14:09:31 +08:00
|
|
|
break;
|
|
|
|
case CCDirectorTypeDiaplayLink:
|
2010-11-11 11:18:58 +08:00
|
|
|
CCDisplayLinkDirector::sharedDirector();
|
2010-07-16 14:09:31 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// we only support CCDisplayLinkDirector
|
2010-11-11 11:18:58 +08:00
|
|
|
CCDirector::sharedDirector();
|
2010-07-16 14:09:31 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-04 12:02:52 +08:00
|
|
|
bool CCDirector::init(void)
|
2010-07-16 14:09:31 +08:00
|
|
|
{
|
|
|
|
CCLOG("cocos2d: %s", cocos2dVersion());
|
2010-08-17 14:25:41 +08:00
|
|
|
|
2010-12-06 09:51:21 +08:00
|
|
|
CCLOG("cocos2d: Using Director Type: %d", kCCDirectorTypeDisplayLink);
|
2010-07-16 14:09:31 +08:00
|
|
|
|
|
|
|
// default values
|
|
|
|
m_ePixelFormat = kCCPixelFormatDefault;
|
2010-07-20 14:07:27 +08:00
|
|
|
m_eDepthBufferFormat = kCCDepthBufferNone; // 0
|
2010-07-16 14:09:31 +08:00
|
|
|
|
|
|
|
// scenes
|
|
|
|
m_pRunningScene = NULL;
|
|
|
|
m_pNextScene = NULL;
|
|
|
|
|
|
|
|
m_dOldAnimationInterval = m_dAnimationInterval = 1.0 / kDefaultFPS;
|
|
|
|
|
2010-07-28 15:16:00 +08:00
|
|
|
m_pobScenesStack = new NSMutableArray<CCScene*>();
|
2010-07-16 14:09:31 +08:00
|
|
|
|
|
|
|
// landspace
|
2010-07-22 15:56:02 +08:00
|
|
|
m_eDeviceOrientation = CCDeviceOrientationPortrait;
|
2010-07-16 14:09:31 +08:00
|
|
|
|
|
|
|
// FPS
|
|
|
|
m_bDisplayFPS = false;
|
|
|
|
m_nFrames = 0;
|
2010-09-30 15:54:15 +08:00
|
|
|
m_pszFPS = new char[10];
|
2010-10-12 12:07:49 +08:00
|
|
|
m_fExpectedFrameRate = (ccTime)(1 / m_dAnimationInterval);
|
2010-10-16 15:29:53 +08:00
|
|
|
m_fComputeFrameRateDeltaTime = 0;
|
|
|
|
m_pLastComputeFrameRate = new struct cc_timeval();
|
2010-07-16 14:09:31 +08:00
|
|
|
|
|
|
|
// paused ?
|
|
|
|
m_bPaused = false;
|
|
|
|
|
2010-08-02 17:10:04 +08:00
|
|
|
m_pobOpenGLView = NULL;
|
|
|
|
|
2010-07-16 14:09:31 +08:00
|
|
|
m_fContentScaleFactor = 1;
|
|
|
|
m_obScreenSize = m_obSurfaceSize = CGSizeZero;
|
2010-09-24 18:10:32 +08:00
|
|
|
m_bIsContentScaleSupported =false;
|
2010-07-16 14:09:31 +08:00
|
|
|
|
2010-08-04 16:50:51 +08:00
|
|
|
m_pLastUpdate = new struct cc_timeval();
|
|
|
|
|
2010-08-02 17:48:01 +08:00
|
|
|
// create autorelease pool
|
|
|
|
NSPoolManager::getInstance()->push();
|
|
|
|
|
2010-09-04 12:02:52 +08:00
|
|
|
return true;
|
2010-07-16 14:09:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCDirector::~CCDirector(void)
|
|
|
|
{
|
|
|
|
CCLOGINFO("cocos2d: deallocing %p", this);
|
|
|
|
|
|
|
|
#if CC_DIRECTOR_FAST_FPS
|
2010-08-23 17:29:52 +08:00
|
|
|
CCX_SAFE_RELEASE(m_pFPSLabel);
|
2010-07-16 14:09:31 +08:00
|
|
|
#endif
|
|
|
|
|
2010-08-18 14:57:36 +08:00
|
|
|
CCX_SAFE_RELEASE(m_pRunningScene);
|
|
|
|
CCX_SAFE_RELEASE(m_pobScenesStack);
|
|
|
|
|
2010-08-02 17:48:01 +08:00
|
|
|
// pop the autorelease pool
|
|
|
|
NSPoolManager::getInstance()->pop();
|
|
|
|
|
2010-08-04 16:50:51 +08:00
|
|
|
// delete m_pLastUpdate
|
2010-09-30 15:54:15 +08:00
|
|
|
CCX_SAFE_DELETE(m_pLastUpdate);
|
|
|
|
|
|
|
|
// delete last compute time
|
2010-10-16 15:29:53 +08:00
|
|
|
CCX_SAFE_DELETE(m_pLastComputeFrameRate);
|
2010-09-30 15:54:15 +08:00
|
|
|
|
|
|
|
// delete fps string
|
|
|
|
delete []m_pszFPS;
|
2010-07-16 14:09:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::setGLDefaultValues(void)
|
|
|
|
{
|
|
|
|
// This method SHOULD be called only after openGLView_ was initialized
|
|
|
|
assert(m_pobOpenGLView);
|
|
|
|
|
|
|
|
setAlphaBlending(true);
|
|
|
|
setDepthTest(true);
|
|
|
|
setProjection(kCCDirectorProjectionDefault);
|
|
|
|
|
|
|
|
// set other opengl default values
|
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
|
|
|
|
#if CC_DIRECTOR_FAST_FPS
|
2010-08-23 17:29:52 +08:00
|
|
|
if (! m_pFPSLabel)
|
|
|
|
{
|
2010-09-07 16:05:18 +08:00
|
|
|
m_pFPSLabel = CCLabel::labelWithString("00.0", "XXX", 24);
|
2010-08-23 17:29:52 +08:00
|
|
|
m_pFPSLabel->retain();
|
|
|
|
}
|
2010-07-16 14:09:31 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-09-24 18:10:32 +08:00
|
|
|
// Draw the SCene
|
|
|
|
void CCDirector::drawScene(void)
|
2010-07-16 14:09:31 +08:00
|
|
|
{
|
|
|
|
// calculate "global" dt
|
|
|
|
calculateDeltaTime();
|
|
|
|
|
|
|
|
//tick before glClear: issue #533
|
|
|
|
if (! m_bPaused)
|
|
|
|
{
|
2010-11-16 15:12:09 +08:00
|
|
|
CCScheduler::sharedScheduler()->tick(m_fDeltaTime);
|
2010-07-16 14:09:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
/* to avoid flickr, nextScene MUST be here: after tick and before draw.
|
2010-07-16 14:09:31 +08:00
|
|
|
XXX: Which bug is this one. It seems that it can't be reproduced with v0.9 */
|
|
|
|
if (m_pNextScene)
|
|
|
|
{
|
|
|
|
setNextScene();
|
|
|
|
}
|
|
|
|
|
|
|
|
glPushMatrix();
|
|
|
|
|
2010-09-24 18:10:32 +08:00
|
|
|
applyOrientation();
|
2010-07-16 14:09:31 +08:00
|
|
|
|
|
|
|
// By default enable VertexArray, ColorArray, TextureCoordArray and Texture2D
|
|
|
|
CC_ENABLE_DEFAULT_GL_STATES();
|
|
|
|
|
|
|
|
// draw the scene
|
2010-08-04 10:36:37 +08:00
|
|
|
if (m_pRunningScene)
|
|
|
|
{
|
|
|
|
m_pRunningScene->visit();
|
|
|
|
}
|
|
|
|
|
2010-07-16 14:09:31 +08:00
|
|
|
if (m_bDisplayFPS)
|
|
|
|
{
|
|
|
|
showFPS();
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CC_ENABLE_PROFILERS
|
|
|
|
showProfilers();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CC_DISABLE_DEFAULT_GL_STATES();
|
|
|
|
|
|
|
|
glPopMatrix();
|
|
|
|
|
|
|
|
// swap buffers
|
2010-09-06 18:35:24 +08:00
|
|
|
if (m_pobOpenGLView)
|
|
|
|
{
|
|
|
|
m_pobOpenGLView->swapBuffers();
|
|
|
|
}
|
2010-07-16 14:09:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::calculateDeltaTime(void)
|
|
|
|
{
|
2010-07-26 10:00:08 +08:00
|
|
|
struct cc_timeval now;
|
2010-07-16 14:09:31 +08:00
|
|
|
|
2010-09-02 16:58:04 +08:00
|
|
|
if (CCTime::gettimeofdayCocos2d(&now, NULL) != 0)
|
2010-07-16 14:09:31 +08:00
|
|
|
{
|
|
|
|
CCLOG("error in gettimeofday");
|
|
|
|
m_fDeltaTime = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// new delta time
|
|
|
|
if (m_bNextDeltaTimeZero)
|
|
|
|
{
|
|
|
|
m_fDeltaTime = 0;
|
|
|
|
m_bNextDeltaTimeZero = false;
|
|
|
|
}
|
2010-09-30 15:54:15 +08:00
|
|
|
else
|
2010-07-16 14:09:31 +08:00
|
|
|
{
|
2010-08-04 16:18:09 +08:00
|
|
|
m_fDeltaTime = (now.tv_sec - m_pLastUpdate->tv_sec) + (now.tv_usec - m_pLastUpdate->tv_usec) / 1000000.0f;
|
2010-07-16 14:09:31 +08:00
|
|
|
m_fDeltaTime = MAX(0, m_fDeltaTime);
|
|
|
|
}
|
|
|
|
|
2010-08-04 16:18:09 +08:00
|
|
|
*m_pLastUpdate = now;
|
2010-07-16 14:09:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// m_dAnimationInterval
|
|
|
|
void CCDirector::setAnimationInterval(double dValue)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Director#setAnimationInterval. Overrride me");
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// m_pobOpenGLView
|
|
|
|
|
2010-07-28 10:15:02 +08:00
|
|
|
void CCDirector::setOpenGLView(CCXEGLView *pobOpenGLView)
|
2010-07-16 14:09:31 +08:00
|
|
|
{
|
|
|
|
assert(pobOpenGLView);
|
|
|
|
|
|
|
|
if (m_pobOpenGLView != pobOpenGLView)
|
|
|
|
{
|
|
|
|
// because EAGLView is not kind of NSObject
|
2010-07-28 10:15:02 +08:00
|
|
|
delete m_pobOpenGLView; // [openGLView_ release]
|
2010-07-16 14:09:31 +08:00
|
|
|
m_pobOpenGLView = pobOpenGLView;
|
|
|
|
|
|
|
|
// set size
|
|
|
|
m_obScreenSize = pobOpenGLView->getSize();
|
|
|
|
m_obSurfaceSize = CGSizeMake(m_obScreenSize.width * m_fContentScaleFactor,
|
|
|
|
m_obScreenSize.height * m_fContentScaleFactor);
|
|
|
|
|
2010-09-24 18:10:32 +08:00
|
|
|
if (m_fContentScaleFactor != 1)
|
|
|
|
{
|
|
|
|
updateContentScaleFactor();
|
|
|
|
}
|
|
|
|
|
2010-11-16 15:12:09 +08:00
|
|
|
CCTouchDispatcher *pTouchDispatcher = CCTouchDispatcher::sharedDispatcher();
|
2010-07-28 18:18:27 +08:00
|
|
|
m_pobOpenGLView->setTouchDelegate(pTouchDispatcher);
|
2010-07-28 18:02:36 +08:00
|
|
|
pTouchDispatcher->setDispatchEvents(true);
|
2010-07-16 14:09:31 +08:00
|
|
|
|
|
|
|
setGLDefaultValues();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-24 18:10:32 +08:00
|
|
|
void CCDirector::updateContentScaleFactor()
|
|
|
|
{
|
|
|
|
// Do we support the scale factor
|
|
|
|
// now we don't support
|
|
|
|
m_bIsContentScaleSupported = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::recalculateProjectionAndEAGLViewSize()
|
|
|
|
{
|
|
|
|
m_obScreenSize = m_pobOpenGLView->getSize();
|
|
|
|
m_obSurfaceSize = CGSizeMake(m_obScreenSize.width * m_fContentScaleFactor,
|
|
|
|
m_obScreenSize.height * m_fContentScaleFactor);
|
|
|
|
setProjection(m_eProjection);
|
|
|
|
}
|
|
|
|
|
2010-07-16 14:09:31 +08:00
|
|
|
void CCDirector::setNextDeltaTimeZero(bool bNextDeltaTimeZero)
|
|
|
|
{
|
|
|
|
m_bNextDeltaTimeZero = bNextDeltaTimeZero;
|
|
|
|
}
|
|
|
|
|
|
|
|
// m_eDeviceOrientation
|
|
|
|
void CCDirector::setDeviceOrientation(ccDeviceOrientation kDeviceOrientation)
|
|
|
|
{
|
2010-10-15 16:06:05 +08:00
|
|
|
ccDeviceOrientation eNewOrientation;
|
2010-11-16 15:12:09 +08:00
|
|
|
eNewOrientation = CCXApplication::sharedApplication()->setDeviceOrientation(kDeviceOrientation);
|
2010-10-15 16:06:05 +08:00
|
|
|
if (m_eDeviceOrientation != eNewOrientation)
|
2010-07-16 14:09:31 +08:00
|
|
|
{
|
|
|
|
m_eDeviceOrientation = kDeviceOrientation;
|
2010-10-15 16:06:05 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
recalculateProjectionAndEAGLViewSize();
|
|
|
|
// m_obScreenSize = m_pobOpenGLView->getSize();
|
|
|
|
// m_obSurfaceSize = CGSizeMake(m_obScreenSize.width * m_fContentScaleFactor,
|
|
|
|
// m_obScreenSize.height * m_fContentScaleFactor);
|
|
|
|
}
|
|
|
|
// if (m_eDeviceOrientation != kDeviceOrientation)
|
|
|
|
// {
|
2010-08-17 14:25:41 +08:00
|
|
|
|
2010-09-16 15:53:58 +08:00
|
|
|
// switch (m_eDeviceOrientation)
|
|
|
|
// {
|
|
|
|
// case CCDeviceOrientationPortrait:
|
2010-11-16 15:12:09 +08:00
|
|
|
// CCXApplication::sharedApplication()->setDeviceOrientation(CCDeviceOrientationPortrait);
|
2010-09-16 15:53:58 +08:00
|
|
|
// break;
|
|
|
|
// case CCDeviceOrientationPortraitUpsideDown:
|
2010-11-16 15:12:09 +08:00
|
|
|
// CCXApplication::sharedApplication()->setDeviceOrientation(CCDeviceOrientationPortraitUpsideDown);
|
2010-09-16 15:53:58 +08:00
|
|
|
// break;
|
|
|
|
// case CCDeviceOrientationLandscapeLeft:
|
2010-11-16 15:12:09 +08:00
|
|
|
// CCXApplication::sharedApplication()->setDeviceOrientation(CCDeviceOrientationLandscapeLeft);
|
2010-09-16 15:53:58 +08:00
|
|
|
// break;
|
|
|
|
// case CCDeviceOrientationLandscapeRight:
|
2010-11-16 15:12:09 +08:00
|
|
|
// CCXApplication::sharedApplication()->setDeviceOrientation(CCDeviceOrientationLandscapeRight);
|
2010-09-16 15:53:58 +08:00
|
|
|
// break;
|
|
|
|
// default:
|
|
|
|
// CCLOG("Director: Unknown device orientation");
|
|
|
|
// break;
|
|
|
|
// }
|
2010-10-15 16:06:05 +08:00
|
|
|
// }
|
2010-07-16 14:09:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::setProjection(ccDirectorProjection kProjection)
|
|
|
|
{
|
|
|
|
CGSize size = m_obSurfaceSize;
|
|
|
|
switch (kProjection)
|
|
|
|
{
|
|
|
|
case kCCDirectorProjection2D:
|
2010-09-24 18:10:32 +08:00
|
|
|
glViewport((GLsizei)0, (GLsizei)0, (GLsizei)size.width, (GLsizei)size.height);
|
2010-07-16 14:09:31 +08:00
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
2010-09-24 18:10:32 +08:00
|
|
|
glOrthof(0, size.width, 0, size.height, -1024, 1000);
|
2010-07-20 14:07:27 +08:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
2010-07-16 14:09:31 +08:00
|
|
|
glLoadIdentity();
|
|
|
|
break;
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
case kCCDirectorProjection3D:
|
|
|
|
glViewport(0, 0, (GLsizei)size.width, (GLsizei)size.height);
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
|
|
|
gluPerspective(60, (GLfloat)size.width/size.height, 0.5f, 1500.0f);
|
|
|
|
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
|
|
|
gluLookAt( size.width/2, size.height/2, getZEye(),
|
|
|
|
size.width/2, size.height/2, 0,
|
|
|
|
0.0f, 1.0f, 0.0f);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kCCDirectorProjectionCustom:
|
|
|
|
// if custom, ignore it. The user is resposible for setting the correct projection
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
CCLOG("cocos2d: Director: unrecognized projecgtion");
|
2010-07-16 14:09:31 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_eProjection = kProjection;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::purgeCachedData(void)
|
|
|
|
{
|
2010-08-23 17:29:52 +08:00
|
|
|
CCBitmapFontAtlas::purgeCachedData();
|
2010-09-24 18:10:32 +08:00
|
|
|
// removed in 0.99.4 release
|
|
|
|
/*CCSpriteFrameCache::purgeSharedSpriteFrameCache();*/
|
2010-07-16 14:09:31 +08:00
|
|
|
CCTextureCache::purgeSharedTextureCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
float CCDirector::getZEye(void)
|
|
|
|
{
|
2010-09-16 15:53:58 +08:00
|
|
|
return (m_obSurfaceSize.height / 1.1566f);
|
2010-07-16 14:09:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::setAlphaBlending(bool bOn)
|
|
|
|
{
|
|
|
|
if (bOn)
|
|
|
|
{
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::setDepthTest(bool bOn)
|
|
|
|
{
|
|
|
|
if (bOn)
|
|
|
|
{
|
2010-08-02 10:58:00 +08:00
|
|
|
glClearDepthf(1.0f);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glDepthFunc(GL_LEQUAL);
|
2010-07-16 14:09:31 +08:00
|
|
|
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// is the view currently attached
|
|
|
|
bool CCDirector::isOpenGLAttached(void)
|
|
|
|
{
|
2010-07-28 10:15:02 +08:00
|
|
|
return m_pobOpenGLView->isOpenGLReady();
|
2010-07-16 14:09:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// detach or attach to a view or a window
|
|
|
|
bool CCDirector::detach(void)
|
|
|
|
{
|
|
|
|
assert(isOpenGLAttached());
|
2010-08-23 17:29:52 +08:00
|
|
|
|
2010-07-16 14:09:31 +08:00
|
|
|
CCX_SAFE_DELETE(m_pobOpenGLView);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGPoint CCDirector::convertToGL(CGPoint obPoint)
|
|
|
|
{
|
|
|
|
CGSize s = m_obScreenSize;
|
|
|
|
float newY = s.height - obPoint.y;
|
|
|
|
float newX = s.width - obPoint.x;
|
|
|
|
|
2010-09-24 18:10:32 +08:00
|
|
|
CGPoint ret = CGPointZero;
|
2010-07-16 14:09:31 +08:00
|
|
|
switch (m_eDeviceOrientation)
|
|
|
|
{
|
|
|
|
case CCDeviceOrientationPortrait:
|
|
|
|
ret = ccp(obPoint.x, newY);
|
|
|
|
break;
|
|
|
|
case CCDeviceOrientationPortraitUpsideDown:
|
|
|
|
ret = ccp(newX, obPoint.y);
|
|
|
|
break;
|
|
|
|
case CCDeviceOrientationLandscapeLeft:
|
2010-08-11 15:24:15 +08:00
|
|
|
ret.x = obPoint.y;
|
|
|
|
ret.y = obPoint.x;
|
2010-07-16 14:09:31 +08:00
|
|
|
break;
|
|
|
|
case CCDeviceOrientationLandscapeRight:
|
|
|
|
ret.x = newY;
|
|
|
|
ret.y = newX;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-09-24 18:10:32 +08:00
|
|
|
if (m_fContentScaleFactor != 1 && m_bIsContentScaleSupported)
|
|
|
|
{
|
|
|
|
ret = ccpMult(ret, m_fContentScaleFactor);
|
|
|
|
}
|
|
|
|
|
2010-07-16 14:09:31 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGPoint CCDirector::convertToUI(CGPoint obPoint)
|
|
|
|
{
|
|
|
|
CGSize winSize = m_obSurfaceSize;
|
2010-07-20 14:07:27 +08:00
|
|
|
float oppositeX = winSize.width - obPoint.x;
|
|
|
|
float oppositeY = winSize.height - obPoint.y;
|
2010-09-24 18:10:32 +08:00
|
|
|
CGPoint uiPoint = CGPointZero;
|
2010-07-16 14:09:31 +08:00
|
|
|
|
2010-07-20 14:07:27 +08:00
|
|
|
switch (m_eDeviceOrientation)
|
2010-07-16 14:09:31 +08:00
|
|
|
{
|
|
|
|
case CCDeviceOrientationPortrait:
|
2010-09-24 18:10:32 +08:00
|
|
|
uiPoint = ccp(obPoint.x, oppositeY);
|
2010-07-16 14:09:31 +08:00
|
|
|
break;
|
|
|
|
case CCDeviceOrientationPortraitUpsideDown:
|
2010-09-24 18:10:32 +08:00
|
|
|
uiPoint = ccp(oppositeX, obPoint.y);
|
2010-07-16 14:09:31 +08:00
|
|
|
break;
|
|
|
|
case CCDeviceOrientationLandscapeLeft:
|
2010-09-24 18:10:32 +08:00
|
|
|
uiPoint = ccp(obPoint.y, obPoint.x);
|
2010-07-16 14:09:31 +08:00
|
|
|
break;
|
|
|
|
case CCDeviceOrientationLandscapeRight:
|
2010-09-24 18:10:32 +08:00
|
|
|
// Can't use oppositeX/Y because x/y are flipped
|
|
|
|
uiPoint = ccp(winSize.width - obPoint.y, winSize.height - obPoint.x);
|
2010-07-16 14:09:31 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
uiPoint = ccpMult(uiPoint, 1/m_fContentScaleFactor);
|
|
|
|
return uiPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGSize CCDirector::getWinSize(void)
|
|
|
|
{
|
|
|
|
CGSize s = m_obSurfaceSize;
|
|
|
|
|
|
|
|
if (m_eDeviceOrientation == CCDeviceOrientationLandscapeLeft
|
|
|
|
|| m_eDeviceOrientation == CCDeviceOrientationLandscapeRight)
|
|
|
|
{
|
|
|
|
// swap x,y in landspace mode
|
|
|
|
CGSize tmp = s;
|
|
|
|
s.width = tmp.height;
|
|
|
|
s.height = tmp.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the current frame size
|
|
|
|
CGSize CCDirector::getDisplaySize(void)
|
|
|
|
{
|
|
|
|
return m_obSurfaceSize;
|
|
|
|
}
|
|
|
|
|
2010-09-24 18:10:32 +08:00
|
|
|
void CCDirector::applyOrientation(void)
|
2010-07-16 14:09:31 +08:00
|
|
|
{
|
|
|
|
CGSize s = m_obSurfaceSize;
|
|
|
|
float w = s.width / 2;
|
|
|
|
float h = s.height / 2;
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
// XXX it's using hardcoded values.
|
2010-07-16 14:09:31 +08:00
|
|
|
// What if the the screen size changes in the future?
|
|
|
|
switch (m_eDeviceOrientation)
|
|
|
|
{
|
|
|
|
case CCDeviceOrientationPortrait:
|
|
|
|
// nothing
|
|
|
|
break;
|
2010-08-02 10:58:00 +08:00
|
|
|
case CCDeviceOrientationPortraitUpsideDown:
|
|
|
|
// upside down
|
|
|
|
glTranslatef(w,h,0);
|
|
|
|
glRotatef(180,0,0,1);
|
|
|
|
glTranslatef(-w,-h,0);
|
|
|
|
break;
|
|
|
|
case CCDeviceOrientationLandscapeRight:
|
|
|
|
glTranslatef(w,h,0);
|
|
|
|
glRotatef(90,0,0,1);
|
|
|
|
glTranslatef(-h,-w,0);
|
|
|
|
break;
|
|
|
|
case CCDeviceOrientationLandscapeLeft:
|
|
|
|
glTranslatef(w,h,0);
|
|
|
|
glRotatef(-90,0,0,1);
|
|
|
|
glTranslatef(-h,-w,0);
|
2010-07-16 14:09:31 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// scene management
|
|
|
|
|
|
|
|
void CCDirector::runWithScene(CCScene *pScene)
|
|
|
|
{
|
|
|
|
assert(pScene != NULL);
|
|
|
|
assert(m_pRunningScene == NULL);
|
|
|
|
|
|
|
|
pushScene(pScene);
|
|
|
|
startAnimation();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::replaceScene(CCScene *pScene)
|
|
|
|
{
|
|
|
|
assert(pScene != NULL);
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
unsigned int index = m_pobScenesStack->count();
|
2010-07-16 14:09:31 +08:00
|
|
|
|
2010-07-20 14:07:27 +08:00
|
|
|
m_bSendCleanupToScene = true;
|
2010-07-16 14:22:43 +08:00
|
|
|
m_pobScenesStack->replaceObjectAtIndex(index - 1, pScene);
|
2010-07-16 14:09:31 +08:00
|
|
|
|
|
|
|
m_pNextScene = pScene;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::pushScene(CCScene *pScene)
|
|
|
|
{
|
|
|
|
assert(pScene);
|
|
|
|
|
|
|
|
m_bSendCleanupToScene = false;
|
|
|
|
|
|
|
|
m_pobScenesStack->addObject(pScene);
|
|
|
|
m_pNextScene = pScene;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::popScene(void)
|
|
|
|
{
|
|
|
|
assert(m_pRunningScene != NULL);
|
|
|
|
|
|
|
|
m_pobScenesStack->removeLastObject();
|
2010-08-02 10:58:00 +08:00
|
|
|
unsigned int c = m_pobScenesStack->count();
|
2010-07-16 14:09:31 +08:00
|
|
|
|
|
|
|
if (c == 0)
|
|
|
|
{
|
|
|
|
end();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_pNextScene = m_pobScenesStack->getObjectAtIndex(c - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::end(void)
|
|
|
|
{
|
|
|
|
m_pRunningScene->onExit();
|
|
|
|
m_pRunningScene->cleanup();
|
|
|
|
m_pRunningScene->release();
|
|
|
|
|
|
|
|
m_pRunningScene = NULL;
|
|
|
|
m_pNextScene = NULL;
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
// remove all objects, but don't release it.
|
2010-07-16 14:09:31 +08:00
|
|
|
// runWithScene might be executed after 'end'.
|
|
|
|
m_pobScenesStack->removeAllObjects();
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
// don't release the event handlers
|
2010-07-16 14:09:31 +08:00
|
|
|
// They are needed in case the director is run again
|
2010-11-16 15:12:09 +08:00
|
|
|
CCTouchDispatcher::sharedDispatcher()->removeAllDelegates();
|
2010-07-16 14:09:31 +08:00
|
|
|
|
|
|
|
stopAnimation();
|
|
|
|
|
|
|
|
#if CC_DIRECTOR_FAST_FPS
|
2010-09-01 15:36:25 +08:00
|
|
|
CCX_SAFE_RELEASE_NULL(m_pFPSLabel);
|
2010-07-16 14:09:31 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// purge bitmap cache
|
2010-08-17 14:25:41 +08:00
|
|
|
CCBitmapFontAtlas::purgeCachedData();
|
2010-07-16 14:09:31 +08:00
|
|
|
|
|
|
|
// purge all managers
|
2010-08-02 12:00:06 +08:00
|
|
|
CCSpriteFrameCache::purgeSharedSpriteFrameCache();
|
2010-11-16 15:12:09 +08:00
|
|
|
CCActionManager::sharedManager()->purgeSharedManager();
|
2010-08-18 14:57:36 +08:00
|
|
|
CCScheduler::purgeSharedScheduler();
|
2010-07-16 14:09:31 +08:00
|
|
|
CCTextureCache::purgeSharedTextureCache();
|
|
|
|
|
|
|
|
// OpenGL view
|
|
|
|
m_pobOpenGLView->release();
|
|
|
|
m_pobOpenGLView = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::setNextScene(void)
|
|
|
|
{
|
2010-08-31 11:20:37 +08:00
|
|
|
ccSceneFlag runningSceneType = ccNormalScene;
|
|
|
|
ccSceneFlag newSceneType = m_pNextScene->getSceneType();
|
2010-07-16 14:09:31 +08:00
|
|
|
|
2010-08-31 11:20:37 +08:00
|
|
|
if (m_pRunningScene)
|
2010-07-16 14:09:31 +08:00
|
|
|
{
|
2010-08-31 11:20:37 +08:00
|
|
|
runningSceneType = m_pRunningScene->getSceneType();
|
2010-07-16 14:09:31 +08:00
|
|
|
}
|
|
|
|
|
2010-08-31 11:20:37 +08:00
|
|
|
// If it is not a transition, call onExit/cleanup
|
|
|
|
/*if (! newIsTransition)*/
|
|
|
|
if (! (newSceneType & ccTransitionScene))
|
|
|
|
{
|
|
|
|
if (m_pRunningScene)
|
|
|
|
{
|
|
|
|
m_pRunningScene->onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
// issue #709. the root node (scene) should receive the cleanup message too
|
|
|
|
// otherwise it might be leaked.
|
|
|
|
if (m_bSendCleanupToScene && m_pRunningScene)
|
|
|
|
{
|
|
|
|
m_pRunningScene->cleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-04 10:36:37 +08:00
|
|
|
if (m_pRunningScene)
|
|
|
|
{
|
|
|
|
m_pRunningScene->release();
|
|
|
|
}
|
|
|
|
m_pRunningScene = m_pNextScene;
|
2010-07-16 14:09:31 +08:00
|
|
|
m_pNextScene->retain();
|
|
|
|
m_pNextScene = NULL;
|
|
|
|
|
2010-08-31 11:20:37 +08:00
|
|
|
if (! (runningSceneType & ccTransitionScene) && m_pRunningScene)
|
2010-07-16 14:09:31 +08:00
|
|
|
{
|
|
|
|
m_pRunningScene->onEnter();
|
|
|
|
m_pRunningScene->onEnterTransitionDidFinish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::pause(void)
|
|
|
|
{
|
|
|
|
if (m_bPaused)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_dOldAnimationInterval = m_dAnimationInterval;
|
|
|
|
|
|
|
|
// when paused, don't consume CPU
|
|
|
|
setAnimationInterval(1 / 4.0);
|
|
|
|
m_bPaused = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::resume(void)
|
|
|
|
{
|
|
|
|
if (! m_bPaused)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setAnimationInterval(m_dOldAnimationInterval);
|
|
|
|
|
2010-09-02 16:58:04 +08:00
|
|
|
if (CCTime::gettimeofdayCocos2d(m_pLastUpdate, NULL) != 0)
|
2010-07-16 14:09:31 +08:00
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Director: Error in gettimeofday");
|
|
|
|
}
|
|
|
|
|
|
|
|
m_bPaused = false;
|
|
|
|
m_fDeltaTime = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::startAnimation(void)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Director#startAnimation. Overrride me");
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::stopAnimation(void)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Director#stopAnimation. Overrride me");
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::preMainLoop(void)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Director#preMainLoop. Overrride me");
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CC_DIRECTOR_FAST_FPS
|
2010-08-02 10:58:00 +08:00
|
|
|
// display the FPS using a LabelAtlas
|
2010-07-16 14:09:31 +08:00
|
|
|
// updates the FPS every frame
|
|
|
|
void CCDirector::showFPS(void)
|
|
|
|
{
|
2010-09-30 15:54:15 +08:00
|
|
|
sprintf(m_pszFPS, "%.1f", m_fFrameRate);
|
|
|
|
m_pFPSLabel->setString(m_pszFPS);
|
2010-07-16 14:09:31 +08:00
|
|
|
|
2010-09-30 15:54:15 +08:00
|
|
|
m_pFPSLabel->draw();
|
|
|
|
}
|
|
|
|
#endif // CC_DIRECTOR_FAST_FPS
|
|
|
|
|
2010-10-16 15:29:53 +08:00
|
|
|
void CCDirector::calculateFramerateDeltaTime(void)
|
2010-09-30 15:54:15 +08:00
|
|
|
{
|
|
|
|
struct cc_timeval now;
|
|
|
|
|
2010-10-16 15:29:53 +08:00
|
|
|
if (CCTime::gettimeofdayCocos2d(&now, NULL) != 0)
|
|
|
|
{
|
|
|
|
CCLOG("error in gettimeofday");
|
|
|
|
m_fComputeFrameRateDeltaTime = 0;
|
|
|
|
return;
|
2010-07-16 14:09:31 +08:00
|
|
|
}
|
2010-08-25 13:45:28 +08:00
|
|
|
|
2010-10-16 15:29:53 +08:00
|
|
|
m_fComputeFrameRateDeltaTime = (now.tv_sec - m_pLastComputeFrameRate->tv_sec) + (now.tv_usec - m_pLastComputeFrameRate->tv_usec) / 1000000.0f;
|
|
|
|
m_fComputeFrameRateDeltaTime = MAX(0, m_fComputeFrameRateDeltaTime);
|
|
|
|
|
|
|
|
*m_pLastComputeFrameRate = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::computeFrameRate()
|
|
|
|
{
|
|
|
|
static bool bInvoked = true;
|
|
|
|
|
|
|
|
// compute delta time
|
|
|
|
calculateFramerateDeltaTime();
|
|
|
|
|
|
|
|
// only add frames if the director really draw the scene
|
|
|
|
if (bInvoked)
|
|
|
|
{
|
|
|
|
m_nFrames++;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_fAccumDt += m_fComputeFrameRateDeltaTime;
|
|
|
|
|
|
|
|
if (m_fAccumDt > CC_DIRECTOR_FPS_INTERVAL)
|
|
|
|
{
|
|
|
|
m_fFrameRate = m_nFrames / m_fAccumDt;
|
|
|
|
|
|
|
|
if (m_fFrameRate > m_fExpectedFrameRate)
|
|
|
|
{
|
|
|
|
bInvoked = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_nFrames = 0;
|
|
|
|
m_fAccumDt = 0;
|
|
|
|
bInvoked = true;
|
|
|
|
}
|
|
|
|
}
|
2010-07-16 14:09:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#if CC_ENABLE_PROFILERS
|
2010-08-23 17:29:52 +08:00
|
|
|
void CCDirector::showProfilers()
|
|
|
|
{
|
|
|
|
m_fAccumDtForProfiler += m_fDeltaTime;
|
|
|
|
if (m_fAccumDtForProfiler > 1.0f)
|
|
|
|
{
|
|
|
|
m_fAccumDtForProfiler = 0;
|
2010-11-16 15:12:09 +08:00
|
|
|
CCProfiler::sharedProfiler()->displayTimers();
|
2010-08-23 17:29:52 +08:00
|
|
|
}
|
|
|
|
}
|
2010-07-16 14:09:31 +08:00
|
|
|
#endif
|
|
|
|
|
2010-10-15 15:47:48 +08:00
|
|
|
void CCDirector::setContentScaleFactor(CGFloat scaleFactor)
|
|
|
|
{
|
|
|
|
if (scaleFactor != m_fContentScaleFactor)
|
|
|
|
{
|
|
|
|
m_fContentScaleFactor = scaleFactor;
|
|
|
|
m_obSurfaceSize = CGSizeMake(m_obScreenSize.width * scaleFactor, m_obScreenSize.height * scaleFactor);
|
|
|
|
|
|
|
|
if (m_pobOpenGLView)
|
|
|
|
{
|
|
|
|
updateContentScaleFactor();
|
|
|
|
}
|
|
|
|
|
|
|
|
// update projection
|
|
|
|
setProjection(m_eProjection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-16 14:09:31 +08:00
|
|
|
// should we afford 4 types of director ??
|
|
|
|
// I think DisplayLinkDirector is enough
|
|
|
|
// so we now only support DisplayLinkDirector
|
|
|
|
|
|
|
|
// implementation of DisplayLinkDirector
|
|
|
|
void CCDisplayLinkDirector::startAnimation(void)
|
|
|
|
{
|
2010-09-02 16:58:04 +08:00
|
|
|
if (CCTime::gettimeofdayCocos2d(m_pLastUpdate, NULL) != 0)
|
2010-07-16 14:09:31 +08:00
|
|
|
{
|
|
|
|
CCLOG("cocos2d: DisplayLinkDirector: Error on gettimeofday");
|
|
|
|
}
|
|
|
|
|
|
|
|
m_bInvalid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDisplayLinkDirector::preMainLoop(void)
|
|
|
|
{
|
2010-09-04 11:36:40 +08:00
|
|
|
if (! m_bInvalid)
|
|
|
|
{
|
2010-09-30 15:54:15 +08:00
|
|
|
// compute frame rate
|
|
|
|
computeFrameRate();
|
|
|
|
|
2010-10-16 15:29:53 +08:00
|
|
|
// control frame rate
|
|
|
|
if (m_fFrameRate <= m_fExpectedFrameRate)
|
2010-09-30 15:54:15 +08:00
|
|
|
{
|
|
|
|
drawScene();
|
|
|
|
|
|
|
|
// release the objects
|
|
|
|
NSPoolManager::getInstance()->pop();
|
|
|
|
}
|
2010-09-04 11:36:40 +08:00
|
|
|
}
|
2010-07-16 14:09:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCDisplayLinkDirector::stopAnimation(void)
|
|
|
|
{
|
|
|
|
m_bInvalid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDisplayLinkDirector::setAnimationInterval(double dValue)
|
|
|
|
{
|
|
|
|
m_dAnimationInterval = dValue;
|
2010-10-13 14:46:06 +08:00
|
|
|
m_fExpectedFrameRate = (ccTime)(1 / m_dAnimationInterval);
|
2010-07-16 14:09:31 +08:00
|
|
|
if (! m_bInvalid)
|
|
|
|
{
|
|
|
|
stopAnimation();
|
|
|
|
startAnimation();
|
|
|
|
}
|
|
|
|
}
|
2010-09-02 10:33:39 +08:00
|
|
|
|
|
|
|
} //namespace cocos2d
|