axmol/cocos2dx/CCDirector.cpp

911 lines
21 KiB
C++
Raw Normal View History

2010-12-29 18:01:37 +08:00
/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org
Copyright (c) 2008-2010 Ricardo Quesada
2011-07-05 14:51:17 +08:00
Copyright (c) 2011 Zynga Inc.
2010-12-29 18:01:37 +08:00
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 "cocoa/CCNS.h"
2010-12-29 18:01:37 +08:00
#include "CCDirector.h"
#include "CCScene.h"
#include "CCMutableArray.h"
2010-12-29 18:01:37 +08:00
#include "CCScheduler.h"
#include "ccMacros.h"
#include "CCTouchDispatcher.h"
#include "CCPointExtension.h"
2010-12-29 18:01:37 +08:00
#include "CCTransition.h"
#include "CCTextureCache.h"
#include "CCTransition.h"
#include "CCSpriteFrameCache.h"
#include "CCAutoreleasePool.h"
2010-12-29 18:01:37 +08:00
#include "platform/platform.h"
#include "CCApplication.h"
2010-12-29 18:01:37 +08:00
#include "CCLabelBMFont.h"
#include "CCActionManager.h"
#include "CCLabelTTF.h"
#include "CCConfiguration.h"
#include "CCKeypadDispatcher.h"
#include "CCGL.h"
#include "CCAnimationCache.h"
2011-03-18 09:39:34 +08:00
#include "CCTouch.h"
#if (CC_TARGET_PLATFORM != CC_PLATFORM_AIRPLAY)
#include "CCUserDefault.h"
#endif
2010-12-29 18:01:37 +08:00
#if CC_ENABLE_PROFILERS
#include "support/CCProfiling.h"
#endif // CC_ENABLE_PROFILERS
#include <string>
using namespace std;
using namespace cocos2d;
namespace cocos2d
{
// singleton stuff
static CCDisplayLinkDirector s_sharedDirector;
static bool s_bFirstRun = true;
#define kDefaultFPS 60 // 60 frames per second
extern const char* cocos2dVersion(void);
CCDirector* CCDirector::sharedDirector(void)
{
if (s_bFirstRun)
{
s_sharedDirector.init();
s_bFirstRun = false;
}
return &s_sharedDirector;
}
bool CCDirector::init(void)
{
CCLOG("cocos2d: %s", cocos2dVersion());
// scenes
m_pRunningScene = NULL;
m_pNextScene = NULL;
m_pNotificationNode = NULL;
m_dOldAnimationInterval = m_dAnimationInterval = 1.0 / kDefaultFPS;
m_pobScenesStack = new CCMutableArray<CCScene*>();
2010-12-29 18:01:37 +08:00
// Set default projection (3D)
m_eProjection = kCCDirectorProjectionDefault;
// projection delegate if "Custom" projection is used
m_pProjectionDelegate = NULL;
// FPS
m_bDisplayFPS = false;
2011-08-02 15:45:27 +08:00
m_uTotalFrames = m_uFrames = 0;
2010-12-29 18:01:37 +08:00
m_pszFPS = new char[10];
m_pLastUpdate = new struct cc_timeval();
// paused ?
m_bPaused = false;
// purge ?
m_bPurgeDirecotorInNextLoop = false;
2010-12-29 18:01:37 +08:00
m_obWinSizeInPixels = m_obWinSizeInPoints = CCSizeZero;
2010-12-29 18:01:37 +08:00
// portrait mode default
m_eDeviceOrientation = CCDeviceOrientationPortrait;
m_pobOpenGLView = NULL;
m_bRetinaDisplay = false;
2010-12-29 18:01:37 +08:00
m_fContentScaleFactor = 1;
m_bIsContentScaleSupported = false;
// create autorelease pool
2011-03-18 14:31:29 +08:00
CCPoolManager::getInstance()->push();
2010-12-29 18:01:37 +08:00
return true;
}
2010-12-29 18:01:37 +08:00
CCDirector::~CCDirector(void)
{
CCLOGINFO("cocos2d: deallocing %p", this);
#if CC_DIRECTOR_FAST_FPS
CC_SAFE_RELEASE(m_pFPSLabel);
2010-12-29 18:01:37 +08:00
#endif
CC_SAFE_RELEASE(m_pRunningScene);
CC_SAFE_RELEASE(m_pNotificationNode);
CC_SAFE_RELEASE(m_pobScenesStack);
2010-12-29 18:01:37 +08:00
// pop the autorelease pool
2011-03-18 14:31:29 +08:00
CCPoolManager::getInstance()->pop();
2010-12-29 18:01:37 +08:00
// delete m_pLastUpdate
CC_SAFE_DELETE(m_pLastUpdate);
2010-12-29 18:01:37 +08:00
CCKeypadDispatcher::purgeSharedDispatcher();
// delete fps string
delete []m_pszFPS;
}
void CCDirector::setGLDefaultValues(void)
{
// This method SHOULD be called only after openGLView_ was initialized
assert(m_pobOpenGLView);
setAlphaBlending(true);
setDepthTest(true);
setProjection(m_eProjection);
// set other opengl default values
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
#if CC_DIRECTOR_FAST_FPS
if (! m_pFPSLabel)
{
m_pFPSLabel = CCLabelTTF::labelWithString("00.0", "Arial", 24);
2010-12-29 18:01:37 +08:00
m_pFPSLabel->retain();
}
#endif
}
// Draw the SCene
void CCDirector::drawScene(void)
{
// calculate "global" dt
calculateDeltaTime();
//tick before glClear: issue #533
if (! m_bPaused)
{
CCScheduler::sharedScheduler()->tick(m_fDeltaTime);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* to avoid flickr, nextScene MUST be here: after tick and before draw.
XXX: Which bug is this one. It seems that it can't be reproduced with v0.9 */
if (m_pNextScene)
{
setNextScene();
}
glPushMatrix();
applyOrientation();
// By default enable VertexArray, ColorArray, TextureCoordArray and Texture2D
CC_ENABLE_DEFAULT_GL_STATES();
// draw the scene
if (m_pRunningScene)
{
m_pRunningScene->visit();
}
// draw the notifications node
if (m_pNotificationNode)
{
m_pNotificationNode->visit();
}
if (m_bDisplayFPS)
{
showFPS();
}
#if CC_ENABLE_PROFILERS
showProfilers();
#endif
CC_DISABLE_DEFAULT_GL_STATES();
glPopMatrix();
2011-08-02 15:45:27 +08:00
m_uTotalFrames++;
2010-12-29 18:01:37 +08:00
// swap buffers
if (m_pobOpenGLView)
{
m_pobOpenGLView->swapBuffers();
}
}
void CCDirector::calculateDeltaTime(void)
{
struct cc_timeval now;
if (CCTime::gettimeofdayCocos2d(&now, NULL) != 0)
{
CCLOG("error in gettimeofday");
m_fDeltaTime = 0;
return;
}
// new delta time
if (m_bNextDeltaTimeZero)
{
m_fDeltaTime = 0;
m_bNextDeltaTimeZero = false;
}
else
{
m_fDeltaTime = (now.tv_sec - m_pLastUpdate->tv_sec) + (now.tv_usec - m_pLastUpdate->tv_usec) / 1000000.0f;
m_fDeltaTime = MAX(0, m_fDeltaTime);
}
2011-07-05 14:51:17 +08:00
#ifdef DEBUG
// If we are debugging our code, prevent big delta time
if(m_fDeltaTime > 0.2f)
{
m_fDeltaTime = 1 / 60.0f;
}
#endif
2010-12-29 18:01:37 +08:00
*m_pLastUpdate = now;
}
// m_pobOpenGLView
void CCDirector::setOpenGLView(CC_GLVIEW *pobOpenGLView)
{
assert(pobOpenGLView);
if (m_pobOpenGLView != pobOpenGLView)
{
// because EAGLView is not kind of CCObject
2010-12-29 18:01:37 +08:00
delete m_pobOpenGLView; // [openGLView_ release]
m_pobOpenGLView = pobOpenGLView;
// set size
2011-01-07 10:25:32 +08:00
m_obWinSizeInPoints = m_pobOpenGLView->getSize();
m_obWinSizeInPixels = CCSizeMake(m_obWinSizeInPoints.width * m_fContentScaleFactor, m_obWinSizeInPoints.height * m_fContentScaleFactor);
2011-01-05 11:24:52 +08:00
setGLDefaultValues();
2010-12-29 18:01:37 +08:00
if (m_fContentScaleFactor != 1)
{
updateContentScaleFactor();
}
CCTouchDispatcher *pTouchDispatcher = CCTouchDispatcher::sharedDispatcher();
m_pobOpenGLView->setTouchDelegate(pTouchDispatcher);
2011-01-05 11:24:52 +08:00
pTouchDispatcher->setDispatchEvents(true);
2010-12-29 18:01:37 +08:00
}
}
void CCDirector::setNextDeltaTimeZero(bool bNextDeltaTimeZero)
{
m_bNextDeltaTimeZero = bNextDeltaTimeZero;
}
void CCDirector::setProjection(ccDirectorProjection kProjection)
{
CCSize size = m_obWinSizeInPixels;
2011-07-05 14:51:17 +08:00
float zeye = this->getZEye();
2010-12-29 18:01:37 +08:00
switch (kProjection)
{
case kCCDirectorProjection2D:
if (m_pobOpenGLView)
{
m_pobOpenGLView->setViewPortInPoints(0, 0, size.width, size.height);
}
2010-12-29 18:01:37 +08:00
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
ccglOrtho(0, size.width, 0, size.height, -1024 * CC_CONTENT_SCALE_FACTOR(),
1024 * CC_CONTENT_SCALE_FACTOR());
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
break;
2011-07-05 14:51:17 +08:00
case kCCDirectorProjection3D:
if (m_pobOpenGLView)
{
m_pobOpenGLView->setViewPortInPoints(0, 0, size.width, size.height);
}
2010-12-29 18:01:37 +08:00
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
2011-07-12 16:04:13 +08:00
gluPerspective(60, (GLfloat)size.width/size.height, 0.5f, 1500.0f);
2010-12-29 18:01:37 +08:00
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
2011-07-05 14:51:17 +08:00
gluLookAt( size.width/2, size.height/2, zeye,
2010-12-29 18:01:37 +08:00
size.width/2, size.height/2, 0,
0.0f, 1.0f, 0.0f);
break;
case kCCDirectorProjectionCustom:
if (m_pProjectionDelegate)
{
m_pProjectionDelegate->updateProjection();
}
break;
default:
CCLOG("cocos2d: Director: unrecognized projecgtion");
break;
}
m_eProjection = kProjection;
}
void CCDirector::purgeCachedData(void)
{
CCLabelBMFont::purgeCachedData();
2011-07-05 14:51:17 +08:00
CCTextureCache::sharedTextureCache()->removeUnusedTextures();
2010-12-29 18:01:37 +08:00
}
float CCDirector::getZEye(void)
{
return (m_obWinSizeInPixels.height / 1.1566f);
}
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)
{
ccglClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
2011-07-05 14:51:17 +08:00
// glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2010-12-29 18:01:37 +08:00
}
else
{
glDisable(GL_DEPTH_TEST);
}
}
CCPoint CCDirector::convertToGL(CCPoint obPoint)
2010-12-29 18:01:37 +08:00
{
CCSize s = m_obWinSizeInPoints;
2010-12-29 18:01:37 +08:00
float newY = s.height - obPoint.y;
float newX = s.width - obPoint.x;
CCPoint ret = CCPointZero;
2010-12-29 18:01:37 +08:00
switch (m_eDeviceOrientation)
{
case CCDeviceOrientationPortrait:
ret = ccp(obPoint.x, newY);
break;
case CCDeviceOrientationPortraitUpsideDown:
ret = ccp(newX, obPoint.y);
break;
case CCDeviceOrientationLandscapeLeft:
ret.x = obPoint.y;
ret.y = obPoint.x;
break;
case CCDeviceOrientationLandscapeRight:
ret.x = newY;
ret.y = newX;
break;
}
return ret;
}
CCPoint CCDirector::convertToUI(CCPoint obPoint)
2010-12-29 18:01:37 +08:00
{
CCSize winSize = m_obWinSizeInPoints;
2010-12-29 18:01:37 +08:00
float oppositeX = winSize.width - obPoint.x;
float oppositeY = winSize.height - obPoint.y;
CCPoint uiPoint = CCPointZero;
2010-12-29 18:01:37 +08:00
switch (m_eDeviceOrientation)
{
case CCDeviceOrientationPortrait:
uiPoint = ccp(obPoint.x, oppositeY);
break;
case CCDeviceOrientationPortraitUpsideDown:
uiPoint = ccp(oppositeX, obPoint.y);
break;
case CCDeviceOrientationLandscapeLeft:
uiPoint = ccp(obPoint.y, obPoint.x);
break;
case CCDeviceOrientationLandscapeRight:
// Can't use oppositeX/Y because x/y are flipped
uiPoint = ccp(winSize.width - obPoint.y, winSize.height - obPoint.x);
break;
}
return uiPoint;
}
CCSize CCDirector::getWinSize(void)
2010-12-29 18:01:37 +08:00
{
CCSize s = m_obWinSizeInPoints;
2010-12-29 18:01:37 +08:00
if (m_eDeviceOrientation == CCDeviceOrientationLandscapeLeft
|| m_eDeviceOrientation == CCDeviceOrientationLandscapeRight)
{
// swap x,y in landspace mode
CCSize tmp = s;
2010-12-29 18:01:37 +08:00
s.width = tmp.height;
s.height = tmp.width;
}
return s;
}
CCSize CCDirector::getWinSizeInPixels()
2010-12-29 18:01:37 +08:00
{
CCSize s = getWinSize();
2010-12-29 18:01:37 +08:00
s.width *= CC_CONTENT_SCALE_FACTOR();
s.height *= CC_CONTENT_SCALE_FACTOR();
return s;
}
// return the current frame size
CCSize CCDirector::getDisplaySizeInPixels(void)
2010-12-29 18:01:37 +08:00
{
return m_obWinSizeInPixels;
}
void CCDirector::reshapeProjection(CCSize newWindowSize)
2010-12-29 18:01:37 +08:00
{
2011-06-10 17:51:37 +08:00
CC_UNUSED_PARAM(newWindowSize);
2010-12-29 18:01:37 +08:00
m_obWinSizeInPoints = m_pobOpenGLView->getSize();
m_obWinSizeInPixels = CCSizeMake(m_obWinSizeInPoints.width * m_fContentScaleFactor,
2010-12-29 18:01:37 +08:00
m_obWinSizeInPoints.height * m_fContentScaleFactor);
setProjection(m_eProjection);
}
// 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);
unsigned int index = m_pobScenesStack->count();
m_bSendCleanupToScene = true;
m_pobScenesStack->replaceObjectAtIndex(index - 1, pScene);
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();
unsigned int c = m_pobScenesStack->count();
if (c == 0)
{
end();
}
else
{
m_bSendCleanupToScene = true;
m_pNextScene = m_pobScenesStack->getObjectAtIndex(c - 1);
}
}
void CCDirector::end()
{
m_bPurgeDirecotorInNextLoop = true;
}
void CCDirector::purgeDirector()
2010-12-29 18:01:37 +08:00
{
// don't release the event handlers
// They are needed in case the director is run again
CCTouchDispatcher::sharedDispatcher()->removeAllDelegates();
if (m_pRunningScene)
{
m_pRunningScene->onExit();
m_pRunningScene->cleanup();
m_pRunningScene->release();
}
2010-12-29 18:01:37 +08:00
m_pRunningScene = NULL;
m_pNextScene = NULL;
// remove all objects, but don't release it.
// runWithScene might be executed after 'end'.
m_pobScenesStack->removeAllObjects();
stopAnimation();
#if CC_DIRECTOR_FAST_FPS
CC_SAFE_RELEASE_NULL(m_pFPSLabel);
2010-12-29 18:01:37 +08:00
#endif
CC_SAFE_RELEASE_NULL(m_pProjectionDelegate);
2010-12-29 18:01:37 +08:00
// purge bitmap cache
CCLabelBMFont::purgeCachedData();
// purge all managers
CCAnimationCache::purgeSharedAnimationCache();
CCSpriteFrameCache::purgeSharedSpriteFrameCache();
CCActionManager::sharedManager()->purgeSharedManager();
CCScheduler::purgeSharedScheduler();
CCTextureCache::purgeSharedTextureCache();
#if (CC_TARGET_PLATFORM != CC_PLATFORM_AIRPLAY)
CCUserDefault::purgeSharedUserDefault();
#endif
2010-12-29 18:01:37 +08:00
// OpenGL view
m_pobOpenGLView->release();
m_pobOpenGLView = NULL;
}
void CCDirector::setNextScene(void)
{
ccSceneFlag runningSceneType = ccNormalScene;
ccSceneFlag newSceneType = m_pNextScene->getSceneType();
if (m_pRunningScene)
{
runningSceneType = m_pRunningScene->getSceneType();
}
// 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();
}
}
if (m_pRunningScene)
{
m_pRunningScene->release();
}
m_pRunningScene = m_pNextScene;
m_pNextScene->retain();
m_pNextScene = NULL;
if (! (runningSceneType & ccTransitionScene) && m_pRunningScene)
{
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);
if (CCTime::gettimeofdayCocos2d(m_pLastUpdate, NULL) != 0)
{
CCLOG("cocos2d: Director: Error in gettimeofday");
}
m_bPaused = false;
m_fDeltaTime = 0;
}
#if CC_DIRECTOR_FAST_FPS
// display the FPS using a LabelAtlas
// updates the FPS every frame
void CCDirector::showFPS(void)
{
2011-08-02 15:45:27 +08:00
m_uFrames++;
m_fAccumDt += m_fDeltaTime;
2010-12-29 18:01:37 +08:00
if (m_fAccumDt > CC_DIRECTOR_FPS_INTERVAL)
{
2011-08-02 15:45:27 +08:00
m_fFrameRate = m_uFrames / m_fAccumDt;
m_uFrames = 0;
m_fAccumDt = 0;
2010-12-29 18:01:37 +08:00
sprintf(m_pszFPS, "%.1f", m_fFrameRate);
m_pFPSLabel->setString(m_pszFPS);
2010-12-29 18:01:37 +08:00
}
m_pFPSLabel->draw();
2010-12-29 18:01:37 +08:00
}
#endif // CC_DIRECTOR_FAST_FPS
2010-12-29 18:01:37 +08:00
void CCDirector::showProfilers()
{
#if CC_ENABLE_PROFILERS
m_fAccumDtForProfiler += m_fDeltaTime;
if (m_fAccumDtForProfiler > 1.0f)
{
m_fAccumDtForProfiler = 0;
CCProfiler::sharedProfiler()->displayTimers();
}
#endif
}
2011-01-07 09:35:27 +08:00
/***************************************************
* mobile platforms specific functions
2010-12-29 18:01:37 +08:00
**************************************************/
void CCDirector::updateContentScaleFactor()
{
// [openGLView responseToSelector:@selector(setContentScaleFactor)]
if (m_pobOpenGLView->canSetContentScaleFactor())
{
m_pobOpenGLView->setContentScaleFactor(m_fContentScaleFactor);
m_bIsContentScaleSupported = true;
}
else
{
CCLOG("cocos2d: setContentScaleFactor:'is not supported on this device");
}
}
bool CCDirector::setDirectorType(ccDirectorType obDirectorType)
{
2011-06-10 17:51:37 +08:00
CC_UNUSED_PARAM(obDirectorType);
2010-12-29 18:01:37 +08:00
// we only support CCDisplayLinkDirector
CCDirector::sharedDirector();
return true;
}
bool CCDirector::enableRetinaDisplay(bool enabled)
{
// Already enabled?
if (enabled && m_fContentScaleFactor == 2)
{
return true;
}
// Already diabled?
if (!enabled && m_fContentScaleFactor == 1)
{
return false;
}
// setContentScaleFactor is not supported
if (! m_pobOpenGLView->canSetContentScaleFactor())
{
return false;
}
2011-01-07 10:25:32 +08:00
float newScale = (float)(enabled ? 2 : 1);
2011-01-07 09:35:27 +08:00
setContentScaleFactor(newScale);
// release cached texture
CCTextureCache::purgeSharedTextureCache();
#if CC_DIRECTOR_FAST_FPS
if (m_pFPSLabel)
{
CC_SAFE_RELEASE_NULL(m_pFPSLabel);
m_pFPSLabel = CCLabelTTF::labelWithString("00.0", "Arial", 24);
m_pFPSLabel->retain();
}
#endif
if (m_fContentScaleFactor == 2)
{
m_bRetinaDisplay = true;
}
else
{
m_bRetinaDisplay = false;
}
2010-12-29 18:01:37 +08:00
return true;
}
CGFloat CCDirector::getContentScaleFactor(void)
{
return m_fContentScaleFactor;
}
void CCDirector::setContentScaleFactor(CGFloat scaleFactor)
{
if (scaleFactor != m_fContentScaleFactor)
{
m_fContentScaleFactor = scaleFactor;
m_obWinSizeInPixels = CCSizeMake(m_obWinSizeInPoints.width * scaleFactor, m_obWinSizeInPoints.height * scaleFactor);
2010-12-29 18:01:37 +08:00
if (m_pobOpenGLView)
{
updateContentScaleFactor();
}
// update projection
setProjection(m_eProjection);
}
}
void CCDirector::applyOrientation(void)
{
CCSize s = m_obWinSizeInPixels;
2010-12-29 18:01:37 +08:00
float w = s.width / 2;
float h = s.height / 2;
// XXX it's using hardcoded values.
// What if the the screen size changes in the future?
switch (m_eDeviceOrientation)
{
case CCDeviceOrientationPortrait:
// nothing
break;
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);
break;
}
}
ccDeviceOrientation CCDirector::getDeviceOrientation(void)
{
return m_eDeviceOrientation;
}
void CCDirector::setDeviceOrientation(ccDeviceOrientation kDeviceOrientation)
{
ccDeviceOrientation eNewOrientation;
eNewOrientation = (ccDeviceOrientation)CCApplication::sharedApplication().setOrientation(
(CCApplication::Orientation)kDeviceOrientation);
2010-12-29 18:01:37 +08:00
if (m_eDeviceOrientation != eNewOrientation)
{
m_eDeviceOrientation = eNewOrientation;
2010-12-29 18:01:37 +08:00
}
else
{
// this logic is only run on win32 now
// On win32,the return value of CCApplication::setDeviceOrientation is always kCCDeviceOrientationPortrait
// So,we should calculate the Projection and window size again.
m_obWinSizeInPoints = m_pobOpenGLView->getSize();
m_obWinSizeInPixels = CCSizeMake(m_obWinSizeInPoints.width * m_fContentScaleFactor, m_obWinSizeInPoints.height * m_fContentScaleFactor);
setProjection(m_eProjection);
}
2010-12-29 18:01:37 +08:00
}
2011-05-31 14:04:14 +08:00
2011-01-07 09:35:27 +08:00
/***************************************************
* implementation of DisplayLinkDirector
2010-12-29 18:01:37 +08:00
**************************************************/
// should we afford 4 types of director ??
// I think DisplayLinkDirector is enough
// so we now only support DisplayLinkDirector
void CCDisplayLinkDirector::startAnimation(void)
{
if (CCTime::gettimeofdayCocos2d(m_pLastUpdate, NULL) != 0)
{
CCLOG("cocos2d: DisplayLinkDirector: Error on gettimeofday");
}
m_bInvalid = false;
CCApplication::sharedApplication().setAnimationInterval(m_dAnimationInterval);
2010-12-29 18:01:37 +08:00
}
void CCDisplayLinkDirector::mainLoop(void)
{
if (m_bPurgeDirecotorInNextLoop)
{
purgeDirector();
m_bPurgeDirecotorInNextLoop = false;
}
else if (! m_bInvalid)
2010-12-29 18:01:37 +08:00
{
drawScene();
2010-12-29 18:01:37 +08:00
// release the objects
2011-03-18 14:31:29 +08:00
CCPoolManager::getInstance()->pop();
2010-12-29 18:01:37 +08:00
}
}
void CCDisplayLinkDirector::stopAnimation(void)
{
m_bInvalid = true;
}
void CCDisplayLinkDirector::setAnimationInterval(double dValue)
{
m_dAnimationInterval = dValue;
if (! m_bInvalid)
{
stopAnimation();
startAnimation();
}
}
} //namespace cocos2d