2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2012-06-12 01:43:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2011 Zynga 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.
|
|
|
|
****************************************************************************/
|
2013-05-23 04:00:34 +08:00
|
|
|
|
|
|
|
// standard includes
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
// cocos2d includes
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCDirector.h"
|
2013-02-27 09:38:30 +08:00
|
|
|
#include "ccFPSImages.h"
|
|
|
|
#include "draw_nodes/CCDrawingPrimitives.h"
|
|
|
|
#include "CCConfiguration.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "cocoa/CCNS.h"
|
|
|
|
#include "layers_scenes_transitions_nodes/CCScene.h"
|
|
|
|
#include "cocoa/CCArray.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCScheduler.h"
|
|
|
|
#include "ccMacros.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "touch_dispatcher/CCTouchDispatcher.h"
|
|
|
|
#include "support/CCPointExtension.h"
|
2012-07-19 17:22:36 +08:00
|
|
|
#include "support/CCNotificationCenter.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "layers_scenes_transitions_nodes/CCTransition.h"
|
|
|
|
#include "textures/CCTextureCache.h"
|
|
|
|
#include "sprite_nodes/CCSpriteFrameCache.h"
|
|
|
|
#include "cocoa/CCAutoreleasePool.h"
|
|
|
|
#include "platform/platform.h"
|
2012-07-19 17:22:36 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCApplication.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "label_nodes/CCLabelBMFont.h"
|
2012-07-19 17:22:36 +08:00
|
|
|
#include "label_nodes/CCLabelAtlas.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "actions/CCActionManager.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCConfiguration.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "keypad_dispatcher/CCKeypadDispatcher.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCAccelerometer.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "sprite_nodes/CCAnimationCache.h"
|
|
|
|
#include "touch_dispatcher/CCTouch.h"
|
2013-03-06 16:36:44 +08:00
|
|
|
#include "support/user_default/CCUserDefault.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "shaders/ccGLStateCache.h"
|
|
|
|
#include "shaders/CCShaderCache.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "kazmath/kazmath.h"
|
|
|
|
#include "kazmath/GL/matrix.h"
|
|
|
|
#include "support/CCProfiling.h"
|
2013-05-23 15:06:57 +08:00
|
|
|
#include "platform/CCImage.h"
|
2012-04-20 15:23:00 +08:00
|
|
|
#include "CCEGLView.h"
|
2013-05-23 04:00:34 +08:00
|
|
|
#include "CCConfiguration.h"
|
|
|
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-08-07 14:29:46 +08:00
|
|
|
/**
|
|
|
|
Position of the FPS
|
|
|
|
|
|
|
|
Default: 0,0 (bottom-left corner)
|
|
|
|
*/
|
|
|
|
#ifndef CC_DIRECTOR_STATS_POSITION
|
2012-08-09 10:23:39 +08:00
|
|
|
#define CC_DIRECTOR_STATS_POSITION CCDirector::sharedDirector()->getVisibleOrigin()
|
2012-08-07 14:29:46 +08:00
|
|
|
#endif
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
unsigned int g_uNumberOfDraws = 0;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
2012-09-16 05:19:14 +08:00
|
|
|
// XXX it should be a Director ivar. Move it there once support for multiple directors is added
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// singleton stuff
|
2012-09-21 00:29:16 +08:00
|
|
|
static CCDisplayLinkDirector *s_SharedDirector = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
#define kDefaultFPS 60 // 60 frames per second
|
|
|
|
extern const char* cocos2dVersion(void);
|
|
|
|
|
|
|
|
CCDirector* CCDirector::sharedDirector(void)
|
|
|
|
{
|
2012-09-21 00:29:16 +08:00
|
|
|
if (!s_SharedDirector)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-09-21 00:29:16 +08:00
|
|
|
s_SharedDirector = new CCDisplayLinkDirector();
|
|
|
|
s_SharedDirector->init();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-09-21 00:29:16 +08:00
|
|
|
return s_SharedDirector;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCDirector::CCDirector(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCDirector::init(void)
|
2013-06-07 03:23:44 +08:00
|
|
|
{
|
|
|
|
setDefaultValues();
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// scenes
|
|
|
|
m_pRunningScene = NULL;
|
|
|
|
m_pNextScene = NULL;
|
|
|
|
|
|
|
|
m_pNotificationNode = NULL;
|
|
|
|
|
|
|
|
m_pobScenesStack = new CCArray();
|
|
|
|
m_pobScenesStack->init();
|
|
|
|
|
|
|
|
// projection delegate if "Custom" projection is used
|
|
|
|
m_pProjectionDelegate = NULL;
|
|
|
|
|
|
|
|
// FPS
|
|
|
|
m_fAccumDt = 0.0f;
|
|
|
|
m_fFrameRate = 0.0f;
|
|
|
|
m_pFPSLabel = NULL;
|
|
|
|
m_pSPFLabel = NULL;
|
|
|
|
m_pDrawsLabel = NULL;
|
|
|
|
m_uTotalFrames = m_uFrames = 0;
|
|
|
|
m_pszFPS = new char[10];
|
|
|
|
m_pLastUpdate = new struct cc_timeval();
|
|
|
|
|
|
|
|
// paused ?
|
|
|
|
m_bPaused = false;
|
2012-06-11 10:59:57 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// purge ?
|
|
|
|
m_bPurgeDirecotorInNextLoop = false;
|
|
|
|
|
2012-10-23 10:50:47 +08:00
|
|
|
m_obWinSizeInPoints = CCSizeZero;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
m_pobOpenGLView = NULL;
|
|
|
|
|
2012-09-15 06:26:38 +08:00
|
|
|
m_fContentScaleFactor = 1.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// scheduler
|
|
|
|
m_pScheduler = new CCScheduler();
|
|
|
|
// action manager
|
|
|
|
m_pActionManager = new CCActionManager();
|
2012-06-12 01:43:07 +08:00
|
|
|
m_pScheduler->scheduleUpdateForTarget(m_pActionManager, kCCPrioritySystem, false);
|
2012-04-19 14:35:52 +08:00
|
|
|
// touchDispatcher
|
|
|
|
m_pTouchDispatcher = new CCTouchDispatcher();
|
|
|
|
m_pTouchDispatcher->init();
|
|
|
|
|
|
|
|
// KeypadDispatcher
|
|
|
|
m_pKeypadDispatcher = new CCKeypadDispatcher();
|
|
|
|
|
|
|
|
// Accelerometer
|
|
|
|
m_pAccelerometer = new CCAccelerometer();
|
|
|
|
|
|
|
|
// create autorelease pool
|
|
|
|
CCPoolManager::sharedPoolManager()->push();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCDirector::~CCDirector(void)
|
|
|
|
{
|
2012-09-21 00:29:16 +08:00
|
|
|
CCLOG("cocos2d: deallocing CCDirector %p", this);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CC_SAFE_RELEASE(m_pFPSLabel);
|
|
|
|
CC_SAFE_RELEASE(m_pSPFLabel);
|
|
|
|
CC_SAFE_RELEASE(m_pDrawsLabel);
|
|
|
|
|
|
|
|
CC_SAFE_RELEASE(m_pRunningScene);
|
|
|
|
CC_SAFE_RELEASE(m_pNotificationNode);
|
|
|
|
CC_SAFE_RELEASE(m_pobScenesStack);
|
|
|
|
CC_SAFE_RELEASE(m_pScheduler);
|
|
|
|
CC_SAFE_RELEASE(m_pActionManager);
|
|
|
|
CC_SAFE_RELEASE(m_pTouchDispatcher);
|
|
|
|
CC_SAFE_RELEASE(m_pKeypadDispatcher);
|
|
|
|
CC_SAFE_DELETE(m_pAccelerometer);
|
|
|
|
|
|
|
|
// pop the autorelease pool
|
|
|
|
CCPoolManager::sharedPoolManager()->pop();
|
|
|
|
CCPoolManager::purgePoolManager();
|
|
|
|
|
|
|
|
// delete m_pLastUpdate
|
|
|
|
CC_SAFE_DELETE(m_pLastUpdate);
|
|
|
|
// delete fps string
|
|
|
|
delete []m_pszFPS;
|
2012-09-21 00:29:16 +08:00
|
|
|
|
|
|
|
s_SharedDirector = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-07 03:23:44 +08:00
|
|
|
void CCDirector::setDefaultValues(void)
|
|
|
|
{
|
|
|
|
CCConfiguration *conf = CCConfiguration::sharedConfiguration();
|
|
|
|
|
|
|
|
// default FPS
|
|
|
|
double fps = conf->getNumber("cocos2d.x.fps", kDefaultFPS);
|
|
|
|
m_dOldAnimationInterval = m_dAnimationInterval = 1.0 / fps;
|
|
|
|
|
|
|
|
// Display FPS
|
2013-06-10 21:19:22 +08:00
|
|
|
m_bDisplayStats = conf->getBool("cocos2d.x.display_fps", false);
|
2013-06-07 03:23:44 +08:00
|
|
|
|
|
|
|
// GL projection
|
|
|
|
const char *projection = conf->getCString("cocos2d.x.gl.projection", "3d");
|
|
|
|
if( strcmp(projection, "3d") == 0 )
|
|
|
|
m_eProjection = kCCDirectorProjection3D;
|
|
|
|
else if (strcmp(projection, "2d") == 0)
|
|
|
|
m_eProjection = kCCDirectorProjection2D;
|
|
|
|
else if (strcmp(projection, "custom") == 0)
|
|
|
|
m_eProjection = kCCDirectorProjectionCustom;
|
|
|
|
else
|
|
|
|
CCAssert(false, "Invalid projection value");
|
|
|
|
|
|
|
|
// Default pixel format for PNG images with alpha
|
|
|
|
const char *pixel_format = conf->getCString("cocos2d.x.texture.pixel_format_for_png", "rgba8888");
|
|
|
|
if( strcmp(pixel_format, "rgba8888") == 0 )
|
|
|
|
CCTexture2D::setDefaultAlphaPixelFormat(kCCTexture2DPixelFormat_RGBA8888);
|
|
|
|
else if( strcmp(pixel_format, "rgba4444") == 0 )
|
|
|
|
CCTexture2D::setDefaultAlphaPixelFormat(kCCTexture2DPixelFormat_RGBA4444);
|
|
|
|
else if( strcmp(pixel_format, "rgba5551") == 0 )
|
|
|
|
CCTexture2D::setDefaultAlphaPixelFormat(kCCTexture2DPixelFormat_RGB5A1);
|
|
|
|
|
|
|
|
// PVR v2 has alpha premultiplied ?
|
|
|
|
bool pvr_alpha_premultipled = conf->getBool("cocos2d.x.texture.pvrv2_has_alpha_premultiplied", false);
|
|
|
|
CCTexture2D::PVRImagesHavePremultipliedAlpha(pvr_alpha_premultipled);
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
void CCDirector::setGLDefaultValues(void)
|
|
|
|
{
|
|
|
|
// This method SHOULD be called only after openGLView_ was initialized
|
|
|
|
CCAssert(m_pobOpenGLView, "opengl view should not be null");
|
|
|
|
|
|
|
|
setAlphaBlending(true);
|
2012-11-22 16:01:30 +08:00
|
|
|
// XXX: Fix me, should enable/disable depth test according the depth format as cocos2d-iphone did
|
|
|
|
// [self setDepthTest: view_.depthFormat];
|
2013-03-05 10:36:32 +08:00
|
|
|
setDepthTest(false);
|
2012-04-19 14:35:52 +08:00
|
|
|
setProjection(m_eProjection);
|
|
|
|
|
|
|
|
// set other opengl default values
|
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
}
|
|
|
|
|
2012-09-15 06:26:38 +08:00
|
|
|
// Draw the Scene
|
2012-04-19 14:35:52 +08:00
|
|
|
void CCDirector::drawScene(void)
|
|
|
|
{
|
|
|
|
// calculate "global" dt
|
|
|
|
calculateDeltaTime();
|
|
|
|
|
|
|
|
//tick before glClear: issue #533
|
|
|
|
if (! m_bPaused)
|
|
|
|
{
|
|
|
|
m_pScheduler->update(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();
|
|
|
|
}
|
|
|
|
|
|
|
|
kmGLPushMatrix();
|
|
|
|
|
|
|
|
// draw the scene
|
|
|
|
if (m_pRunningScene)
|
|
|
|
{
|
|
|
|
m_pRunningScene->visit();
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw the notifications node
|
|
|
|
if (m_pNotificationNode)
|
|
|
|
{
|
|
|
|
m_pNotificationNode->visit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_bDisplayStats)
|
|
|
|
{
|
|
|
|
showStats();
|
|
|
|
}
|
|
|
|
|
|
|
|
kmGLPopMatrix();
|
|
|
|
|
|
|
|
m_uTotalFrames++;
|
|
|
|
|
|
|
|
// swap buffers
|
|
|
|
if (m_pobOpenGLView)
|
|
|
|
{
|
|
|
|
m_pobOpenGLView->swapBuffers();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_bDisplayStats)
|
|
|
|
{
|
|
|
|
calculateMPF();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::calculateDeltaTime(void)
|
|
|
|
{
|
|
|
|
struct cc_timeval now;
|
|
|
|
|
|
|
|
if (CCTime::gettimeofdayCocos2d(&now, NULL) != 0)
|
|
|
|
{
|
|
|
|
CCLOG("error in gettimeofday");
|
|
|
|
m_fDeltaTime = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-11 10:59:57 +08:00
|
|
|
// new delta time. Re-fixed issue #1277
|
2012-04-19 14:35:52 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// If we are debugging our code, prevent big delta time
|
|
|
|
if(m_fDeltaTime > 0.2f)
|
|
|
|
{
|
|
|
|
m_fDeltaTime = 1 / 60.0f;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
*m_pLastUpdate = now;
|
|
|
|
}
|
2013-03-02 16:31:19 +08:00
|
|
|
float CCDirector::getDeltaTime()
|
|
|
|
{
|
|
|
|
return m_fDeltaTime;
|
|
|
|
}
|
2012-04-25 16:18:04 +08:00
|
|
|
void CCDirector::setOpenGLView(CCEGLView *pobOpenGLView)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCAssert(pobOpenGLView, "opengl view should not be null");
|
|
|
|
|
|
|
|
if (m_pobOpenGLView != pobOpenGLView)
|
|
|
|
{
|
2013-05-23 04:00:34 +08:00
|
|
|
// Configuration. Gather GPU info
|
|
|
|
CCConfiguration *conf = CCConfiguration::sharedConfiguration();
|
|
|
|
conf->gatherGPUInfo();
|
|
|
|
conf->dumpInfo();
|
|
|
|
|
2012-09-15 06:26:38 +08:00
|
|
|
// EAGLView is not a CCObject
|
2012-04-19 14:35:52 +08:00
|
|
|
delete m_pobOpenGLView; // [openGLView_ release]
|
|
|
|
m_pobOpenGLView = pobOpenGLView;
|
|
|
|
|
|
|
|
// set size
|
2012-10-23 10:50:47 +08:00
|
|
|
m_obWinSizeInPoints = m_pobOpenGLView->getDesignResolutionSize();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
createStatsLabel();
|
|
|
|
|
|
|
|
if (m_pobOpenGLView)
|
|
|
|
{
|
|
|
|
setGLDefaultValues();
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
2012-10-12 09:58:42 +08:00
|
|
|
m_pobOpenGLView->setTouchDelegate(m_pTouchDispatcher);
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pTouchDispatcher->setDispatchEvents(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-27 15:44:36 +08:00
|
|
|
void CCDirector::setViewport()
|
|
|
|
{
|
2013-02-27 15:52:14 +08:00
|
|
|
if (m_pobOpenGLView)
|
|
|
|
{
|
|
|
|
m_pobOpenGLView->setViewPortInPoints(0, 0, m_obWinSizeInPoints.width, m_obWinSizeInPoints.height);
|
|
|
|
}
|
2013-02-27 15:44:36 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
void CCDirector::setNextDeltaTimeZero(bool bNextDeltaTimeZero)
|
|
|
|
{
|
|
|
|
m_bNextDeltaTimeZero = bNextDeltaTimeZero;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::setProjection(ccDirectorProjection kProjection)
|
|
|
|
{
|
2012-10-11 17:02:27 +08:00
|
|
|
CCSize size = m_obWinSizeInPoints;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-02-27 15:52:14 +08:00
|
|
|
setViewport();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
switch (kProjection)
|
|
|
|
{
|
|
|
|
case kCCDirectorProjection2D:
|
|
|
|
{
|
|
|
|
kmGLMatrixMode(KM_GL_PROJECTION);
|
|
|
|
kmGLLoadIdentity();
|
|
|
|
kmMat4 orthoMatrix;
|
2012-09-24 18:22:02 +08:00
|
|
|
kmMat4OrthographicProjection(&orthoMatrix, 0, size.width, 0, size.height, -1024, 1024 );
|
2012-04-19 14:35:52 +08:00
|
|
|
kmGLMultMatrix(&orthoMatrix);
|
|
|
|
kmGLMatrixMode(KM_GL_MODELVIEW);
|
|
|
|
kmGLLoadIdentity();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kCCDirectorProjection3D:
|
|
|
|
{
|
|
|
|
float zeye = this->getZEye();
|
|
|
|
|
|
|
|
kmMat4 matrixPerspective, matrixLookup;
|
|
|
|
|
|
|
|
kmGLMatrixMode(KM_GL_PROJECTION);
|
|
|
|
kmGLLoadIdentity();
|
|
|
|
|
|
|
|
// issue #1334
|
2012-06-12 01:43:07 +08:00
|
|
|
kmMat4PerspectiveProjection( &matrixPerspective, 60, (GLfloat)size.width/size.height, 0.1f, zeye*2);
|
|
|
|
// kmMat4PerspectiveProjection( &matrixPerspective, 60, (GLfloat)size.width/size.height, 0.1f, 1500);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
kmGLMultMatrix(&matrixPerspective);
|
|
|
|
|
|
|
|
kmGLMatrixMode(KM_GL_MODELVIEW);
|
|
|
|
kmGLLoadIdentity();
|
|
|
|
kmVec3 eye, center, up;
|
2012-09-24 18:22:02 +08:00
|
|
|
kmVec3Fill( &eye, size.width/2, size.height/2, zeye );
|
|
|
|
kmVec3Fill( ¢er, size.width/2, size.height/2, 0.0f );
|
2012-04-20 17:33:28 +08:00
|
|
|
kmVec3Fill( &up, 0.0f, 1.0f, 0.0f);
|
2012-04-19 14:35:52 +08:00
|
|
|
kmMat4LookAt(&matrixLookup, &eye, ¢er, &up);
|
|
|
|
kmGLMultMatrix(&matrixLookup);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kCCDirectorProjectionCustom:
|
|
|
|
if (m_pProjectionDelegate)
|
|
|
|
{
|
|
|
|
m_pProjectionDelegate->updateProjection();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
CCLOG("cocos2d: Director: unrecognized projection");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_eProjection = kProjection;
|
|
|
|
ccSetProjectionMatrixDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::purgeCachedData(void)
|
|
|
|
{
|
|
|
|
CCLabelBMFont::purgeCachedData();
|
2012-11-13 11:06:32 +08:00
|
|
|
if (s_SharedDirector->getOpenGLView())
|
|
|
|
{
|
|
|
|
CCTextureCache::sharedTextureCache()->removeUnusedTextures();
|
|
|
|
}
|
2012-06-11 10:59:57 +08:00
|
|
|
CCFileUtils::sharedFileUtils()->purgeCachedEntries();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float CCDirector::getZEye(void)
|
|
|
|
{
|
2012-10-11 17:02:27 +08:00
|
|
|
return (m_obWinSizeInPoints.height / 1.1566f);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::setAlphaBlending(bool bOn)
|
|
|
|
{
|
|
|
|
if (bOn)
|
|
|
|
{
|
|
|
|
ccGLBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-13 11:06:32 +08:00
|
|
|
ccGLBlendFunc(GL_ONE, GL_ZERO);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::setDepthTest(bool bOn)
|
|
|
|
{
|
|
|
|
if (bOn)
|
|
|
|
{
|
|
|
|
glClearDepth(1.0f);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glDepthFunc(GL_LEQUAL);
|
|
|
|
// glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
}
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
}
|
|
|
|
|
2012-11-13 11:06:32 +08:00
|
|
|
static void
|
|
|
|
GLToClipTransform(kmMat4 *transformOut)
|
|
|
|
{
|
|
|
|
kmMat4 projection;
|
|
|
|
kmGLGetMatrix(KM_GL_PROJECTION, &projection);
|
|
|
|
|
|
|
|
kmMat4 modelview;
|
|
|
|
kmGLGetMatrix(KM_GL_MODELVIEW, &modelview);
|
|
|
|
|
|
|
|
kmMat4Multiply(transformOut, &projection, &modelview);
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCPoint CCDirector::convertToGL(const CCPoint& uiPoint)
|
|
|
|
{
|
2012-11-13 11:06:32 +08:00
|
|
|
kmMat4 transform;
|
|
|
|
GLToClipTransform(&transform);
|
|
|
|
|
|
|
|
kmMat4 transformInv;
|
|
|
|
kmMat4Inverse(&transformInv, &transform);
|
|
|
|
|
|
|
|
// Calculate z=0 using -> transform*[0, 0, 0, 1]/w
|
|
|
|
kmScalar zClip = transform.mat[14]/transform.mat[15];
|
|
|
|
|
2012-11-16 18:55:57 +08:00
|
|
|
CCSize glSize = m_pobOpenGLView->getDesignResolutionSize();
|
2012-12-10 18:10:41 +08:00
|
|
|
kmVec3 clipCoord = {2.0f*uiPoint.x/glSize.width - 1.0f, 1.0f - 2.0f*uiPoint.y/glSize.height, zClip};
|
2012-11-13 11:06:32 +08:00
|
|
|
|
|
|
|
kmVec3 glCoord;
|
|
|
|
kmVec3TransformCoord(&glCoord, &clipCoord, &transformInv);
|
|
|
|
|
|
|
|
return ccp(glCoord.x, glCoord.y);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCPoint CCDirector::convertToUI(const CCPoint& glPoint)
|
|
|
|
{
|
2012-11-13 11:06:32 +08:00
|
|
|
kmMat4 transform;
|
|
|
|
GLToClipTransform(&transform);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-13 11:06:32 +08:00
|
|
|
kmVec3 clipCoord;
|
|
|
|
// Need to calculate the zero depth from the transform.
|
|
|
|
kmVec3 glCoord = {glPoint.x, glPoint.y, 0.0};
|
|
|
|
kmVec3TransformCoord(&clipCoord, &glCoord, &transform);
|
|
|
|
|
2012-11-16 18:55:57 +08:00
|
|
|
CCSize glSize = m_pobOpenGLView->getDesignResolutionSize();
|
2012-11-13 11:06:32 +08:00
|
|
|
return ccp(glSize.width*(clipCoord.x*0.5 + 0.5), glSize.height*(-clipCoord.y*0.5 + 0.5));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCSize CCDirector::getWinSize(void)
|
|
|
|
{
|
|
|
|
return m_obWinSizeInPoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCSize CCDirector::getWinSizeInPixels()
|
|
|
|
{
|
2012-10-23 10:50:47 +08:00
|
|
|
return CCSizeMake(m_obWinSizeInPoints.width * m_fContentScaleFactor, m_obWinSizeInPoints.height * m_fContentScaleFactor);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-08-09 10:23:39 +08:00
|
|
|
CCSize CCDirector::getVisibleSize()
|
|
|
|
{
|
|
|
|
if (m_pobOpenGLView)
|
|
|
|
{
|
|
|
|
return m_pobOpenGLView->getVisibleSize();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return CCSizeZero;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CCPoint CCDirector::getVisibleOrigin()
|
|
|
|
{
|
|
|
|
if (m_pobOpenGLView)
|
|
|
|
{
|
|
|
|
return m_pobOpenGLView->getVisibleOrigin();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return CCPointZero;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// scene management
|
|
|
|
|
|
|
|
void CCDirector::runWithScene(CCScene *pScene)
|
|
|
|
{
|
2012-11-13 11:06:32 +08:00
|
|
|
CCAssert(pScene != NULL, "This command can only be used to start the CCDirector. There is already a scene present.");
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(m_pRunningScene == NULL, "m_pRunningScene should be null");
|
|
|
|
|
|
|
|
pushScene(pScene);
|
|
|
|
startAnimation();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::replaceScene(CCScene *pScene)
|
|
|
|
{
|
2012-11-13 11:06:32 +08:00
|
|
|
CCAssert(m_pRunningScene, "Use runWithScene: instead to start the director");
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(pScene != NULL, "the scene should not be null");
|
|
|
|
|
|
|
|
unsigned int index = m_pobScenesStack->count();
|
|
|
|
|
|
|
|
m_bSendCleanupToScene = true;
|
|
|
|
m_pobScenesStack->replaceObjectAtIndex(index - 1, pScene);
|
|
|
|
|
|
|
|
m_pNextScene = pScene;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::pushScene(CCScene *pScene)
|
|
|
|
{
|
|
|
|
CCAssert(pScene, "the scene should not null");
|
|
|
|
|
|
|
|
m_bSendCleanupToScene = false;
|
|
|
|
|
|
|
|
m_pobScenesStack->addObject(pScene);
|
|
|
|
m_pNextScene = pScene;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::popScene(void)
|
|
|
|
{
|
|
|
|
CCAssert(m_pRunningScene != NULL, "running scene should not null");
|
|
|
|
|
|
|
|
m_pobScenesStack->removeLastObject();
|
|
|
|
unsigned int c = m_pobScenesStack->count();
|
|
|
|
|
|
|
|
if (c == 0)
|
|
|
|
{
|
|
|
|
end();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_bSendCleanupToScene = true;
|
|
|
|
m_pNextScene = (CCScene*)m_pobScenesStack->objectAtIndex(c - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-11 10:59:57 +08:00
|
|
|
void CCDirector::popToRootScene(void)
|
2013-05-01 06:33:22 +08:00
|
|
|
{
|
|
|
|
popToSceneStackLevel(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::popToSceneStackLevel(int level)
|
2012-06-11 10:59:57 +08:00
|
|
|
{
|
2012-06-12 02:50:32 +08:00
|
|
|
CCAssert(m_pRunningScene != NULL, "A running Scene is needed");
|
2013-05-06 10:55:39 +08:00
|
|
|
int c = (int)m_pobScenesStack->count();
|
2012-06-12 02:50:32 +08:00
|
|
|
|
2013-05-01 06:33:22 +08:00
|
|
|
// level 0? -> end
|
|
|
|
if (level == 0)
|
2012-06-12 02:50:32 +08:00
|
|
|
{
|
2013-05-01 06:33:22 +08:00
|
|
|
end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// current level or lower -> nothing
|
|
|
|
if (level >= c)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// pop stack until reaching desired level
|
|
|
|
while (c > level)
|
2012-06-12 02:50:32 +08:00
|
|
|
{
|
2013-05-01 06:33:22 +08:00
|
|
|
CCScene *current = (CCScene*)m_pobScenesStack->lastObject();
|
|
|
|
|
|
|
|
if (current->isRunning())
|
2012-06-12 02:50:32 +08:00
|
|
|
{
|
2013-05-01 06:33:22 +08:00
|
|
|
current->onExitTransitionDidStart();
|
|
|
|
current->onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
current->cleanup();
|
|
|
|
m_pobScenesStack->removeLastObject();
|
|
|
|
c--;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pNextScene = (CCScene*)m_pobScenesStack->lastObject();
|
|
|
|
m_bSendCleanupToScene = false;
|
2012-06-11 10:59:57 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
void CCDirector::end()
|
|
|
|
{
|
|
|
|
m_bPurgeDirecotorInNextLoop = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::purgeDirector()
|
|
|
|
{
|
2012-09-04 01:21:32 +08:00
|
|
|
// cleanup scheduler
|
2012-11-14 18:05:15 +08:00
|
|
|
getScheduler()->unscheduleAll();
|
2012-09-04 01:21:32 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// don't release the event handlers
|
|
|
|
// They are needed in case the director is run again
|
|
|
|
m_pTouchDispatcher->removeAllDelegates();
|
|
|
|
|
|
|
|
if (m_pRunningScene)
|
|
|
|
{
|
2012-11-13 11:06:32 +08:00
|
|
|
m_pRunningScene->onExitTransitionDidStart();
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pRunningScene->onExit();
|
|
|
|
m_pRunningScene->cleanup();
|
|
|
|
m_pRunningScene->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pRunningScene = NULL;
|
|
|
|
m_pNextScene = NULL;
|
|
|
|
|
|
|
|
// remove all objects, but don't release it.
|
|
|
|
// runWithScene might be executed after 'end'.
|
|
|
|
m_pobScenesStack->removeAllObjects();
|
|
|
|
|
|
|
|
stopAnimation();
|
|
|
|
|
|
|
|
CC_SAFE_RELEASE_NULL(m_pFPSLabel);
|
|
|
|
CC_SAFE_RELEASE_NULL(m_pSPFLabel);
|
|
|
|
CC_SAFE_RELEASE_NULL(m_pDrawsLabel);
|
|
|
|
|
|
|
|
// purge bitmap cache
|
|
|
|
CCLabelBMFont::purgeCachedData();
|
|
|
|
|
2012-06-20 11:48:31 +08:00
|
|
|
// purge all managed caches
|
2013-02-27 09:38:30 +08:00
|
|
|
ccDrawFree();
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAnimationCache::purgeSharedAnimationCache();
|
2012-05-23 12:11:53 +08:00
|
|
|
CCSpriteFrameCache::purgeSharedSpriteFrameCache();
|
2012-04-19 14:35:52 +08:00
|
|
|
CCTextureCache::purgeSharedTextureCache();
|
|
|
|
CCShaderCache::purgeSharedShaderCache();
|
2012-06-11 10:59:57 +08:00
|
|
|
CCFileUtils::purgeFileUtils();
|
2012-05-23 12:11:53 +08:00
|
|
|
CCConfiguration::purgeConfiguration();
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// cocos2d-x specific data structures
|
|
|
|
CCUserDefault::purgeSharedUserDefault();
|
2012-07-19 17:22:36 +08:00
|
|
|
CCNotificationCenter::purgeNotificationCenter();
|
2012-04-27 18:47:49 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ccGLInvalidateStateCache();
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
// OpenGL view
|
2012-04-20 15:23:00 +08:00
|
|
|
m_pobOpenGLView->end();
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pobOpenGLView = NULL;
|
2012-09-21 00:29:16 +08:00
|
|
|
|
|
|
|
// delete CCDirector
|
|
|
|
release();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::setNextScene(void)
|
|
|
|
{
|
|
|
|
bool runningIsTransition = dynamic_cast<CCTransitionScene*>(m_pRunningScene) != NULL;
|
|
|
|
bool newIsTransition = dynamic_cast<CCTransitionScene*>(m_pNextScene) != NULL;
|
|
|
|
|
|
|
|
// If it is not a transition, call onExit/cleanup
|
|
|
|
if (! newIsTransition)
|
|
|
|
{
|
|
|
|
if (m_pRunningScene)
|
|
|
|
{
|
2012-11-13 11:06:32 +08:00
|
|
|
m_pRunningScene->onExitTransitionDidStart();
|
2012-04-19 14:35:52 +08:00
|
|
|
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 ((! runningIsTransition) && 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// display the FPS using a LabelAtlas
|
|
|
|
// updates the FPS every frame
|
|
|
|
void CCDirector::showStats(void)
|
|
|
|
{
|
|
|
|
m_uFrames++;
|
|
|
|
m_fAccumDt += m_fDeltaTime;
|
|
|
|
|
|
|
|
if (m_bDisplayStats)
|
|
|
|
{
|
|
|
|
if (m_pFPSLabel && m_pSPFLabel && m_pDrawsLabel)
|
|
|
|
{
|
|
|
|
if (m_fAccumDt > CC_DIRECTOR_STATS_INTERVAL)
|
|
|
|
{
|
|
|
|
sprintf(m_pszFPS, "%.3f", m_fSecondsPerFrame);
|
|
|
|
m_pSPFLabel->setString(m_pszFPS);
|
|
|
|
|
|
|
|
m_fFrameRate = m_uFrames / m_fAccumDt;
|
|
|
|
m_uFrames = 0;
|
|
|
|
m_fAccumDt = 0;
|
|
|
|
|
|
|
|
sprintf(m_pszFPS, "%.1f", m_fFrameRate);
|
|
|
|
m_pFPSLabel->setString(m_pszFPS);
|
|
|
|
|
2012-08-22 15:51:25 +08:00
|
|
|
sprintf(m_pszFPS, "%4lu", (unsigned long)g_uNumberOfDraws);
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pDrawsLabel->setString(m_pszFPS);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pDrawsLabel->visit();
|
|
|
|
m_pFPSLabel->visit();
|
|
|
|
m_pSPFLabel->visit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_uNumberOfDraws = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::calculateMPF()
|
|
|
|
{
|
|
|
|
struct cc_timeval now;
|
|
|
|
CCTime::gettimeofdayCocos2d(&now, NULL);
|
|
|
|
|
|
|
|
m_fSecondsPerFrame = (now.tv_sec - m_pLastUpdate->tv_sec) + (now.tv_usec - m_pLastUpdate->tv_usec) / 1000000.0f;
|
|
|
|
}
|
|
|
|
|
2013-02-27 09:38:30 +08:00
|
|
|
// returns the FPS image data pointer and len
|
|
|
|
void CCDirector::getFPSImageData(unsigned char** datapointer, unsigned int* length)
|
|
|
|
{
|
2013-03-01 09:51:42 +08:00
|
|
|
// XXX fixed me if it should be used
|
2013-05-23 15:06:57 +08:00
|
|
|
*datapointer = cc_fps_images_png;
|
|
|
|
*length = cc_fps_images_len();
|
2013-02-27 09:38:30 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
void CCDirector::createStatsLabel()
|
2013-05-23 15:06:57 +08:00
|
|
|
{
|
|
|
|
CCTexture2D *texture = NULL;
|
2013-05-23 15:11:48 +08:00
|
|
|
CCTextureCache *textureCache = CCTextureCache::sharedTextureCache();
|
|
|
|
|
|
|
|
if( m_pFPSLabel && m_pSPFLabel )
|
2012-06-12 02:50:32 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(m_pFPSLabel);
|
|
|
|
CC_SAFE_RELEASE_NULL(m_pSPFLabel);
|
|
|
|
CC_SAFE_RELEASE_NULL(m_pDrawsLabel);
|
2013-05-23 15:06:57 +08:00
|
|
|
textureCache->removeTextureForKey("cc_fps_images");
|
2012-06-12 02:50:32 +08:00
|
|
|
CCFileUtils::sharedFileUtils()->purgeCachedEntries();
|
|
|
|
}
|
|
|
|
|
2013-05-23 15:06:57 +08:00
|
|
|
CCTexture2DPixelFormat currentFormat = CCTexture2D::defaultAlphaPixelFormat();
|
|
|
|
CCTexture2D::setDefaultAlphaPixelFormat(kCCTexture2DPixelFormat_RGBA4444);
|
|
|
|
unsigned char *data = NULL;
|
2013-05-23 15:11:48 +08:00
|
|
|
unsigned int data_len = 0;
|
|
|
|
getFPSImageData(&data, &data_len);
|
|
|
|
|
2013-05-23 15:06:57 +08:00
|
|
|
CCImage* image = new CCImage();
|
|
|
|
bool isOK = image->initWithImageData(data, data_len);
|
|
|
|
if (!isOK) {
|
|
|
|
CCLOGERROR("%s", "Fails: init fps_images");
|
|
|
|
return;
|
2012-10-23 10:50:47 +08:00
|
|
|
}
|
2013-05-23 15:11:48 +08:00
|
|
|
|
2013-05-23 15:06:57 +08:00
|
|
|
texture = textureCache->addUIImage(image, "cc_fps_images");
|
|
|
|
CC_SAFE_RELEASE(image);
|
2013-05-23 15:11:48 +08:00
|
|
|
|
2013-05-23 16:55:53 +08:00
|
|
|
/*
|
|
|
|
We want to use an image which is stored in the file named ccFPSImage.c
|
|
|
|
for any design resolutions and all resource resolutions.
|
|
|
|
|
|
|
|
To achieve this,
|
|
|
|
|
|
|
|
Firstly, we need to ignore 'contentScaleFactor' in 'CCAtlasNode' and 'CCLabelAtlas'.
|
|
|
|
So I added a new method called 'setIgnoreContentScaleFactor' for 'CCAtlasNode',
|
|
|
|
this is not exposed to game developers, it's only used for displaying FPS now.
|
|
|
|
|
|
|
|
Secondly, the size of this image is 480*320, to display the FPS label with correct size,
|
|
|
|
a factor of design resolution ratio of 480x320 is also needed.
|
|
|
|
*/
|
2013-05-23 15:06:57 +08:00
|
|
|
float factor = CCEGLView::sharedOpenGLView()->getDesignResolutionSize().height / 320.0f;
|
2013-05-23 15:11:48 +08:00
|
|
|
|
2013-05-23 15:06:57 +08:00
|
|
|
m_pFPSLabel = new CCLabelAtlas();
|
|
|
|
m_pFPSLabel->setIgnoreContentScaleFactor(true);
|
|
|
|
m_pFPSLabel->initWithString("00.0", texture, 12, 32 , '.');
|
|
|
|
m_pFPSLabel->setScale(factor);
|
2013-05-23 15:11:48 +08:00
|
|
|
|
2013-05-23 15:06:57 +08:00
|
|
|
m_pSPFLabel = new CCLabelAtlas();
|
|
|
|
m_pSPFLabel->setIgnoreContentScaleFactor(true);
|
|
|
|
m_pSPFLabel->initWithString("0.000", texture, 12, 32, '.');
|
|
|
|
m_pSPFLabel->setScale(factor);
|
2013-05-23 15:11:48 +08:00
|
|
|
|
2013-05-23 15:06:57 +08:00
|
|
|
m_pDrawsLabel = new CCLabelAtlas();
|
|
|
|
m_pDrawsLabel->setIgnoreContentScaleFactor(true);
|
|
|
|
m_pDrawsLabel->initWithString("000", texture, 12, 32, '.');
|
|
|
|
m_pDrawsLabel->setScale(factor);
|
2013-05-23 15:11:48 +08:00
|
|
|
|
2013-05-23 15:06:57 +08:00
|
|
|
CCTexture2D::setDefaultAlphaPixelFormat(currentFormat);
|
2013-05-23 15:11:48 +08:00
|
|
|
|
2013-05-23 15:06:57 +08:00
|
|
|
m_pDrawsLabel->setPosition(ccpAdd(ccp(0, 34*factor), CC_DIRECTOR_STATS_POSITION));
|
|
|
|
m_pSPFLabel->setPosition(ccpAdd(ccp(0, 17*factor), CC_DIRECTOR_STATS_POSITION));
|
|
|
|
m_pFPSLabel->setPosition(CC_DIRECTOR_STATS_POSITION);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
float CCDirector::getContentScaleFactor(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return m_fContentScaleFactor;
|
|
|
|
}
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
void CCDirector::setContentScaleFactor(float scaleFactor)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if (scaleFactor != m_fContentScaleFactor)
|
|
|
|
{
|
|
|
|
m_fContentScaleFactor = scaleFactor;
|
2012-10-23 10:50:47 +08:00
|
|
|
createStatsLabel();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CCNode* CCDirector::getNotificationNode()
|
|
|
|
{
|
|
|
|
return m_pNotificationNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::setNotificationNode(CCNode *node)
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(m_pNotificationNode);
|
|
|
|
m_pNotificationNode = node;
|
|
|
|
CC_SAFE_RETAIN(m_pNotificationNode);
|
|
|
|
}
|
|
|
|
|
2012-11-13 11:06:32 +08:00
|
|
|
CCDirectorDelegate* CCDirector::getDelegate() const
|
|
|
|
{
|
|
|
|
return m_pProjectionDelegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::setDelegate(CCDirectorDelegate* pDelegate)
|
|
|
|
{
|
|
|
|
m_pProjectionDelegate = pDelegate;
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
void CCDirector::setScheduler(CCScheduler* pScheduler)
|
|
|
|
{
|
|
|
|
if (m_pScheduler != pScheduler)
|
|
|
|
{
|
|
|
|
CC_SAFE_RETAIN(pScheduler);
|
|
|
|
CC_SAFE_RELEASE(m_pScheduler);
|
|
|
|
m_pScheduler = pScheduler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CCScheduler* CCDirector::getScheduler()
|
|
|
|
{
|
|
|
|
return m_pScheduler;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::setActionManager(CCActionManager* pActionManager)
|
|
|
|
{
|
|
|
|
if (m_pActionManager != pActionManager)
|
|
|
|
{
|
|
|
|
CC_SAFE_RETAIN(pActionManager);
|
|
|
|
CC_SAFE_RELEASE(m_pActionManager);
|
|
|
|
m_pActionManager = pActionManager;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CCActionManager* CCDirector::getActionManager()
|
|
|
|
{
|
|
|
|
return m_pActionManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::setTouchDispatcher(CCTouchDispatcher* pTouchDispatcher)
|
|
|
|
{
|
|
|
|
if (m_pTouchDispatcher != pTouchDispatcher)
|
|
|
|
{
|
|
|
|
CC_SAFE_RETAIN(pTouchDispatcher);
|
|
|
|
CC_SAFE_RELEASE(m_pTouchDispatcher);
|
|
|
|
m_pTouchDispatcher = pTouchDispatcher;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CCTouchDispatcher* CCDirector::getTouchDispatcher()
|
|
|
|
{
|
|
|
|
return m_pTouchDispatcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::setKeypadDispatcher(CCKeypadDispatcher* pKeypadDispatcher)
|
|
|
|
{
|
|
|
|
CC_SAFE_RETAIN(pKeypadDispatcher);
|
|
|
|
CC_SAFE_RELEASE(m_pKeypadDispatcher);
|
|
|
|
m_pKeypadDispatcher = pKeypadDispatcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCKeypadDispatcher* CCDirector::getKeypadDispatcher()
|
|
|
|
{
|
|
|
|
return m_pKeypadDispatcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDirector::setAccelerometer(CCAccelerometer* pAccelerometer)
|
|
|
|
{
|
|
|
|
if (m_pAccelerometer != pAccelerometer)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(m_pAccelerometer);
|
|
|
|
m_pAccelerometer = pAccelerometer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CCAccelerometer* CCDirector::getAccelerometer()
|
|
|
|
{
|
|
|
|
return m_pAccelerometer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************
|
|
|
|
* implementation of DisplayLinkDirector
|
|
|
|
**************************************************/
|
|
|
|
|
2012-09-15 06:26:38 +08:00
|
|
|
// should we implement 4 types of director ??
|
2012-04-19 14:35:52 +08:00
|
|
|
// 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;
|
2013-04-20 09:15:54 +08:00
|
|
|
#ifndef EMSCRIPTEN
|
2012-08-21 15:28:43 +08:00
|
|
|
CCApplication::sharedApplication()->setAnimationInterval(m_dAnimationInterval);
|
2013-04-20 09:15:54 +08:00
|
|
|
#endif // EMSCRIPTEN
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCDisplayLinkDirector::mainLoop(void)
|
|
|
|
{
|
|
|
|
if (m_bPurgeDirecotorInNextLoop)
|
|
|
|
{
|
|
|
|
m_bPurgeDirecotorInNextLoop = false;
|
|
|
|
purgeDirector();
|
|
|
|
}
|
|
|
|
else if (! m_bInvalid)
|
|
|
|
{
|
|
|
|
drawScene();
|
|
|
|
|
|
|
|
// release the objects
|
|
|
|
CCPoolManager::sharedPoolManager()->pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDisplayLinkDirector::stopAnimation(void)
|
|
|
|
{
|
|
|
|
m_bInvalid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCDisplayLinkDirector::setAnimationInterval(double dValue)
|
|
|
|
{
|
|
|
|
m_dAnimationInterval = dValue;
|
|
|
|
if (! m_bInvalid)
|
|
|
|
{
|
|
|
|
stopAnimation();
|
|
|
|
startAnimation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|