#5506 bug fix
when Director->convertToUI() called, returned wrong value.
kazmath code (under 3.0 version.)
//********************************************************
kmVec3* kmVec3TransformCoord(kmVec3* pOut, const kmVec3* pV, const kmMat4* pM)
{
/*
a = (Vx, Vy, Vz, 1)
b = (a×M)T
Out = 1 ⁄ bw(bx, by, bz)
*/
kmVec4 v;
kmVec4 inV;
kmVec4Fill(&inV, pV->x, pV->y, pV->z, 1.0);
kmVec4Transform(&v, &inV,pM);
pOut->x = v.x / v.w;
pOut->y = v.y / v.w;
pOut->z = v.z / v.w;
return pOut;
}
//********************************************************
Mat4.h & MathUtil version (3.1 or later)
//********************************************************
inline void MathUtil::transformVec4(const float* m, const float* v, float* dst)
{
// Handle case where v == dst.
float x = v[0] * m[0] + v[1] * m[4] + v[2] * m[8] + v[3] * m[12];
float y = v[0] * m[1] + v[1] * m[5] + v[2] * m[9] + v[3] * m[13];
float z = v[0] * m[2] + v[1] * m[6] + v[2] * m[10] + v[3] * m[14];
float w = v[0] * m[3] + v[1] * m[7] + v[2] * m[11] + v[3] * m[15];
dst[0] = x;
dst[1] = y;
dst[2] = z;
dst[3] = w;
}
//****************************************************
Transforms 3-D vector or an array of 3-D vectors using a given matrix, projecting the result back into w = 1.
but, it is not apply w = 1.
2014-06-10 17:15:07 +08:00
|
|
|
|
/****************************************************************************
|
2012-04-19 14:35:52 +08:00
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
|
Copyright (c) 2010-2013 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2015-04-02 23:35:56 +08:00
|
|
|
|
Copyright (c) 2013-2015 Chukong Technologies Inc.
|
2012-04-19 14:35:52 +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.
|
|
|
|
|
****************************************************************************/
|
2013-05-23 04:00:34 +08:00
|
|
|
|
|
2013-07-19 13:42:45 +08:00
|
|
|
|
// cocos2d includes
|
2014-04-30 08:37:36 +08:00
|
|
|
|
#include "base/CCDirector.h"
|
2013-07-19 13:42:45 +08:00
|
|
|
|
|
2013-05-23 04:00:34 +08:00
|
|
|
|
// standard includes
|
|
|
|
|
#include <string>
|
|
|
|
|
|
2014-05-01 10:09:13 +08:00
|
|
|
|
#include "2d/CCDrawingPrimitives.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
|
#include "2d/CCSpriteFrameCache.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
|
#include "platform/CCFileUtils.h"
|
2014-08-29 15:39:52 +08:00
|
|
|
|
|
2014-05-01 10:09:13 +08:00
|
|
|
|
#include "2d/CCActionManager.h"
|
|
|
|
|
#include "2d/CCFontFNT.h"
|
|
|
|
|
#include "2d/CCFontAtlasCache.h"
|
|
|
|
|
#include "2d/CCAnimationCache.h"
|
|
|
|
|
#include "2d/CCTransition.h"
|
|
|
|
|
#include "2d/CCFontFreeType.h"
|
2014-08-29 15:39:52 +08:00
|
|
|
|
#include "2d/CCLabelAtlas.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
|
#include "renderer/CCGLProgramCache.h"
|
|
|
|
|
#include "renderer/CCGLProgramStateCache.h"
|
|
|
|
|
#include "renderer/CCTextureCache.h"
|
|
|
|
|
#include "renderer/ccGLStateCache.h"
|
|
|
|
|
#include "renderer/CCRenderer.h"
|
2015-05-06 04:07:32 +08:00
|
|
|
|
#include "renderer/CCRenderState.h"
|
2015-06-09 15:32:12 +08:00
|
|
|
|
#include "renderer/CCFrameBuffer.h"
|
2014-10-20 16:25:24 +08:00
|
|
|
|
#include "2d/CCCamera.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
|
#include "base/CCUserDefault.h"
|
|
|
|
|
#include "base/ccFPSImages.h"
|
2014-05-01 10:09:13 +08:00
|
|
|
|
#include "base/CCScheduler.h"
|
|
|
|
|
#include "base/ccMacros.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
|
#include "base/CCEventDispatcher.h"
|
|
|
|
|
#include "base/CCEventCustom.h"
|
2014-04-27 01:35:57 +08:00
|
|
|
|
#include "base/CCConsole.h"
|
2014-05-01 10:09:13 +08:00
|
|
|
|
#include "base/CCAutoreleasePool.h"
|
|
|
|
|
#include "base/CCConfiguration.h"
|
2014-12-04 18:31:10 +08:00
|
|
|
|
#include "base/CCAsyncTaskPool.h"
|
2014-09-10 07:50:02 +08:00
|
|
|
|
#include "platform/CCApplication.h"
|
2014-02-06 11:51:24 +08:00
|
|
|
|
|
2014-12-28 11:55:06 +08:00
|
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
|
|
|
|
#include "CCScriptSupport.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-08-07 14:29:46 +08:00
|
|
|
|
/**
|
|
|
|
|
Position of the FPS
|
|
|
|
|
|
|
|
|
|
Default: 0,0 (bottom-left corner)
|
|
|
|
|
*/
|
|
|
|
|
#ifndef CC_DIRECTOR_STATS_POSITION
|
2013-07-12 06:24:23 +08:00
|
|
|
|
#define CC_DIRECTOR_STATS_POSITION Director::getInstance()->getVisibleOrigin()
|
2014-11-27 12:07:42 +08:00
|
|
|
|
#endif // CC_DIRECTOR_STATS_POSITION
|
2012-08-07 14:29:46 +08:00
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
2014-08-30 03:54:24 +08:00
|
|
|
|
// FIXME: 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
|
2013-08-08 16:31:44 +08:00
|
|
|
|
static DisplayLinkDirector *s_SharedDirector = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
#define kDefaultFPS 60 // 60 frames per second
|
|
|
|
|
extern const char* cocos2dVersion(void);
|
|
|
|
|
|
2013-12-21 16:56:28 +08:00
|
|
|
|
const char *Director::EVENT_PROJECTION_CHANGED = "director_projection_changed";
|
|
|
|
|
const char *Director::EVENT_AFTER_DRAW = "director_after_draw";
|
|
|
|
|
const char *Director::EVENT_AFTER_VISIT = "director_after_visit";
|
2015-10-27 17:32:57 +08:00
|
|
|
|
const char *Director::EVENT_BEFORE_UPDATE = "director_before_update";
|
2014-03-05 11:25:33 +08:00
|
|
|
|
const char *Director::EVENT_AFTER_UPDATE = "director_after_update";
|
2013-12-21 16:56:28 +08:00
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
|
Director* Director::getInstance()
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2012-09-21 00:29:16 +08:00
|
|
|
|
if (!s_SharedDirector)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2014-05-31 07:42:05 +08:00
|
|
|
|
s_SharedDirector = new (std::nothrow) DisplayLinkDirector();
|
|
|
|
|
CCASSERT(s_SharedDirector, "FATAL: Not enough memory");
|
2012-09-21 00:29:16 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2013-12-21 16:56:28 +08:00
|
|
|
|
Director::Director()
|
2015-04-02 23:35:56 +08:00
|
|
|
|
: _isStatusLabelUpdated(true)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
bool Director::init(void)
|
2013-06-07 03:23:44 +08:00
|
|
|
|
{
|
2013-07-08 10:26:53 +08:00
|
|
|
|
setDefaultValues();
|
2013-06-07 03:23:44 +08:00
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
// scenes
|
2013-08-08 16:31:44 +08:00
|
|
|
|
_runningScene = nullptr;
|
|
|
|
|
_nextScene = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
_notificationNode = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-12-07 14:22:51 +08:00
|
|
|
|
_scenesStack.reserve(15);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// FPS
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_accumDt = 0.0f;
|
|
|
|
|
_frameRate = 0.0f;
|
2014-02-12 06:58:34 +08:00
|
|
|
|
_FPSLabel = _drawnBatchesLabel = _drawnVerticesLabel = nullptr;
|
2014-11-27 05:31:44 +08:00
|
|
|
|
_totalFrames = 0;
|
2015-12-16 14:02:55 +08:00
|
|
|
|
_lastUpdate = new (std::nothrow) struct timeval;
|
2014-12-27 22:05:29 +08:00
|
|
|
|
_secondsPerFrame = 1.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// paused ?
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_paused = false;
|
2014-01-14 06:48:12 +08:00
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
// purge ?
|
2014-01-04 13:22:09 +08:00
|
|
|
|
_purgeDirectorInNextLoop = false;
|
2014-12-28 11:55:06 +08:00
|
|
|
|
|
|
|
|
|
// restart ?
|
|
|
|
|
_restartDirectorInNextLoop = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2014-01-14 06:48:12 +08:00
|
|
|
|
_winSizeInPoints = Size::ZERO;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
_openGLView = nullptr;
|
2015-06-04 15:53:13 +08:00
|
|
|
|
_defaultFBO = nullptr;
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_contentScaleFactor = 1.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2015-03-17 01:22:20 +08:00
|
|
|
|
_console = new (std::nothrow) Console;
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
// scheduler
|
2014-08-28 07:31:57 +08:00
|
|
|
|
_scheduler = new (std::nothrow) Scheduler();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
// action manager
|
2014-08-28 07:31:57 +08:00
|
|
|
|
_actionManager = new (std::nothrow) ActionManager();
|
2014-03-03 11:00:30 +08:00
|
|
|
|
_scheduler->scheduleUpdate(_actionManager, Scheduler::PRIORITY_SYSTEM, false);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
|
_eventDispatcher = new (std::nothrow) EventDispatcher();
|
|
|
|
|
_eventAfterDraw = new (std::nothrow) EventCustom(EVENT_AFTER_DRAW);
|
2013-12-21 16:56:28 +08:00
|
|
|
|
_eventAfterDraw->setUserData(this);
|
2014-08-28 07:31:57 +08:00
|
|
|
|
_eventAfterVisit = new (std::nothrow) EventCustom(EVENT_AFTER_VISIT);
|
2013-12-21 16:56:28 +08:00
|
|
|
|
_eventAfterVisit->setUserData(this);
|
2015-10-27 17:32:57 +08:00
|
|
|
|
_eventBeforeUpdate = new (std::nothrow) EventCustom(EVENT_BEFORE_UPDATE);
|
|
|
|
|
_eventBeforeUpdate->setUserData(this);
|
2014-08-28 07:31:57 +08:00
|
|
|
|
_eventAfterUpdate = new (std::nothrow) EventCustom(EVENT_AFTER_UPDATE);
|
2013-12-21 16:56:28 +08:00
|
|
|
|
_eventAfterUpdate->setUserData(this);
|
2014-08-28 07:31:57 +08:00
|
|
|
|
_eventProjectionChanged = new (std::nothrow) EventCustom(EVENT_PROJECTION_CHANGED);
|
2013-12-21 16:56:28 +08:00
|
|
|
|
_eventProjectionChanged->setUserData(this);
|
2013-11-07 18:52:36 +08:00
|
|
|
|
//init TextureCache
|
|
|
|
|
initTextureCache();
|
2014-04-02 16:36:11 +08:00
|
|
|
|
initMatrixStack();
|
2013-12-18 09:50:17 +08:00
|
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
|
_renderer = new (std::nothrow) Renderer;
|
2015-05-06 04:07:32 +08:00
|
|
|
|
RenderState::initialize();
|
2013-12-18 09:50:17 +08:00
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-01-14 06:48:12 +08:00
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
Director::~Director(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-08-22 11:05:06 +08:00
|
|
|
|
CCLOGINFO("deallocing Director: %p", this);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
CC_SAFE_RELEASE(_FPSLabel);
|
2014-02-08 11:37:44 +08:00
|
|
|
|
CC_SAFE_RELEASE(_drawnVerticesLabel);
|
|
|
|
|
CC_SAFE_RELEASE(_drawnBatchesLabel);
|
2014-01-14 06:48:12 +08:00
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
CC_SAFE_RELEASE(_runningScene);
|
|
|
|
|
CC_SAFE_RELEASE(_notificationNode);
|
|
|
|
|
CC_SAFE_RELEASE(_scheduler);
|
|
|
|
|
CC_SAFE_RELEASE(_actionManager);
|
2015-06-04 15:53:13 +08:00
|
|
|
|
CC_SAFE_DELETE(_defaultFBO);
|
2014-04-17 10:15:11 +08:00
|
|
|
|
|
2015-10-27 17:32:57 +08:00
|
|
|
|
delete _eventBeforeUpdate;
|
2013-12-21 16:56:28 +08:00
|
|
|
|
delete _eventAfterUpdate;
|
|
|
|
|
delete _eventAfterDraw;
|
|
|
|
|
delete _eventAfterVisit;
|
|
|
|
|
delete _eventProjectionChanged;
|
|
|
|
|
|
2013-12-18 09:50:17 +08:00
|
|
|
|
delete _renderer;
|
2014-03-22 20:55:08 +08:00
|
|
|
|
|
2014-01-11 09:11:14 +08:00
|
|
|
|
delete _console;
|
2014-10-23 22:01:04 +08:00
|
|
|
|
|
2013-12-18 09:50:17 +08:00
|
|
|
|
|
2014-04-17 10:15:11 +08:00
|
|
|
|
CC_SAFE_RELEASE(_eventDispatcher);
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
// delete _lastUpdate
|
|
|
|
|
CC_SAFE_DELETE(_lastUpdate);
|
2012-09-21 00:29:16 +08:00
|
|
|
|
|
2014-07-08 18:22:48 +08:00
|
|
|
|
Configuration::destroyInstance();
|
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
s_SharedDirector = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
void Director::setDefaultValues(void)
|
2013-06-07 03:23:44 +08:00
|
|
|
|
{
|
2014-04-28 09:51:10 +08:00
|
|
|
|
Configuration *conf = Configuration::getInstance();
|
2013-06-07 03:23:44 +08:00
|
|
|
|
|
2014-04-28 09:51:10 +08:00
|
|
|
|
// default FPS
|
|
|
|
|
double fps = conf->getValue("cocos2d.x.fps", Value(kDefaultFPS)).asDouble();
|
|
|
|
|
_oldAnimationInterval = _animationInterval = 1.0 / fps;
|
2013-06-07 03:23:44 +08:00
|
|
|
|
|
2014-04-28 09:51:10 +08:00
|
|
|
|
// Display FPS
|
|
|
|
|
_displayStats = conf->getValue("cocos2d.x.display_fps", Value(false)).asBool();
|
2013-06-07 03:23:44 +08:00
|
|
|
|
|
2014-04-28 09:51:10 +08:00
|
|
|
|
// GL projection
|
2013-12-11 14:39:21 +08:00
|
|
|
|
std::string projection = conf->getValue("cocos2d.x.gl.projection", Value("3d")).asString();
|
2014-04-28 09:51:10 +08:00
|
|
|
|
if (projection == "3d")
|
|
|
|
|
_projection = Projection::_3D;
|
|
|
|
|
else if (projection == "2d")
|
|
|
|
|
_projection = Projection::_2D;
|
|
|
|
|
else if (projection == "custom")
|
|
|
|
|
_projection = Projection::CUSTOM;
|
|
|
|
|
else
|
|
|
|
|
CCASSERT(false, "Invalid projection value");
|
|
|
|
|
|
|
|
|
|
// Default pixel format for PNG images with alpha
|
2013-12-11 14:39:21 +08:00
|
|
|
|
std::string pixel_format = conf->getValue("cocos2d.x.texture.pixel_format_for_png", Value("rgba8888")).asString();
|
2014-04-28 09:51:10 +08:00
|
|
|
|
if (pixel_format == "rgba8888")
|
|
|
|
|
Texture2D::setDefaultAlphaPixelFormat(Texture2D::PixelFormat::RGBA8888);
|
|
|
|
|
else if(pixel_format == "rgba4444")
|
|
|
|
|
Texture2D::setDefaultAlphaPixelFormat(Texture2D::PixelFormat::RGBA4444);
|
|
|
|
|
else if(pixel_format == "rgba5551")
|
|
|
|
|
Texture2D::setDefaultAlphaPixelFormat(Texture2D::PixelFormat::RGB5A1);
|
|
|
|
|
|
|
|
|
|
// PVR v2 has alpha premultiplied ?
|
|
|
|
|
bool pvr_alpha_premultipled = conf->getValue("cocos2d.x.texture.pvrv2_has_alpha_premultiplied", Value(false)).asBool();
|
2014-08-15 12:06:16 +08:00
|
|
|
|
Image::setPVRImagesHavePremultipliedAlpha(pvr_alpha_premultipled);
|
2013-06-07 03:23:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
void Director::setGLDefaultValues()
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
// This method SHOULD be called only after openGLView_ was initialized
|
2013-07-20 13:01:27 +08:00
|
|
|
|
CCASSERT(_openGLView, "opengl view should not be null");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
setAlphaBlending(true);
|
2013-03-05 10:36:32 +08:00
|
|
|
|
setDepthTest(false);
|
2013-06-15 14:03:30 +08:00
|
|
|
|
setProjection(_projection);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-15 06:26:38 +08:00
|
|
|
|
// Draw the Scene
|
2013-08-08 16:31:44 +08:00
|
|
|
|
void Director::drawScene()
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
// calculate "global" dt
|
|
|
|
|
calculateDeltaTime();
|
2014-04-02 14:05:07 +08:00
|
|
|
|
|
2013-07-08 10:26:53 +08:00
|
|
|
|
if (_openGLView)
|
|
|
|
|
{
|
2014-07-31 00:53:04 +08:00
|
|
|
|
_openGLView->pollEvents();
|
2013-07-08 10:26:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
//tick before glClear: issue #533
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (! _paused)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2015-10-27 17:32:57 +08:00
|
|
|
|
_eventDispatcher->dispatchEvent(_eventBeforeUpdate);
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_scheduler->update(_deltaTime);
|
2013-12-21 16:56:28 +08:00
|
|
|
|
_eventDispatcher->dispatchEvent(_eventAfterUpdate);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-10 06:06:21 +08:00
|
|
|
|
_renderer->clear();
|
2015-06-09 10:59:43 +08:00
|
|
|
|
experimental::FrameBuffer::clearAllFBOs();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
/* to avoid flickr, nextScene MUST be here: after tick and before draw.
|
2014-08-30 03:54:24 +08:00
|
|
|
|
* FIXME: Which bug is this one. It seems that it can't be reproduced with v0.9
|
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (_nextScene)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
setNextScene();
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-02 22:46:37 +08:00
|
|
|
|
pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
2014-08-07 15:23:31 +08:00
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (_runningScene)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2015-06-09 13:49:07 +08:00
|
|
|
|
#if (CC_USE_PHYSICS || (CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION) || CC_USE_NAVMESH)
|
|
|
|
|
_runningScene->stepPhysicsAndNavigation(_deltaTime);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
#endif
|
2014-09-02 11:12:15 +08:00
|
|
|
|
//clear draw stats
|
|
|
|
|
_renderer->clearDrawStats();
|
|
|
|
|
|
2014-09-26 09:36:43 +08:00
|
|
|
|
//render the scene
|
|
|
|
|
_runningScene->render(_renderer);
|
2014-08-07 16:24:44 +08:00
|
|
|
|
|
2013-12-21 16:56:28 +08:00
|
|
|
|
_eventDispatcher->dispatchEvent(_eventAfterVisit);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// draw the notifications node
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (_notificationNode)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2014-08-08 11:52:40 +08:00
|
|
|
|
_notificationNode->visit(_renderer, Mat4::IDENTITY, 0);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2014-01-14 06:48:12 +08:00
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (_displayStats)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
showStats();
|
|
|
|
|
}
|
2013-12-18 09:50:17 +08:00
|
|
|
|
_renderer->render();
|
2014-08-07 15:23:31 +08:00
|
|
|
|
|
2013-12-21 16:56:28 +08:00
|
|
|
|
_eventDispatcher->dispatchEvent(_eventAfterDraw);
|
2013-11-05 01:14:22 +08:00
|
|
|
|
|
2014-04-02 22:46:37 +08:00
|
|
|
|
popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_totalFrames++;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// swap buffers
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (_openGLView)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_openGLView->swapBuffers();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2014-01-14 06:48:12 +08:00
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (_displayStats)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
calculateMPF();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
void Director::calculateDeltaTime()
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-07-19 13:42:45 +08:00
|
|
|
|
struct timeval now;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
if (gettimeofday(&now, nullptr) != 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
CCLOG("error in gettimeofday");
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_deltaTime = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-11 10:59:57 +08:00
|
|
|
|
// new delta time. Re-fixed issue #1277
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (_nextDeltaTimeZero)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_deltaTime = 0;
|
|
|
|
|
_nextDeltaTimeZero = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_deltaTime = (now.tv_sec - _lastUpdate->tv_sec) + (now.tv_usec - _lastUpdate->tv_usec) / 1000000.0f;
|
|
|
|
|
_deltaTime = MAX(0, _deltaTime);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-26 11:16:25 +08:00
|
|
|
|
#if COCOS2D_DEBUG
|
2012-04-19 14:35:52 +08:00
|
|
|
|
// If we are debugging our code, prevent big delta time
|
2013-08-08 16:31:44 +08:00
|
|
|
|
if (_deltaTime > 0.2f)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_deltaTime = 1 / 60.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
*_lastUpdate = now;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
|
float Director::getDeltaTime() const
|
2013-03-02 16:31:19 +08:00
|
|
|
|
{
|
2014-04-28 09:51:10 +08:00
|
|
|
|
return _deltaTime;
|
2013-03-02 16:31:19 +08:00
|
|
|
|
}
|
2014-01-31 07:40:56 +08:00
|
|
|
|
void Director::setOpenGLView(GLView *openGLView)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
|
CCASSERT(openGLView, "opengl view should not be null");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
|
if (_openGLView != openGLView)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2014-04-28 09:51:10 +08:00
|
|
|
|
// Configuration. Gather GPU info
|
|
|
|
|
Configuration *conf = Configuration::getInstance();
|
|
|
|
|
conf->gatherGPUInfo();
|
2014-01-15 09:22:45 +08:00
|
|
|
|
CCLOG("%s\n",conf->getInfo().c_str());
|
2013-05-23 04:00:34 +08:00
|
|
|
|
|
2014-01-24 07:36:55 +08:00
|
|
|
|
if(_openGLView)
|
|
|
|
|
_openGLView->release();
|
2013-11-16 21:08:00 +08:00
|
|
|
|
_openGLView = openGLView;
|
2014-01-24 07:36:55 +08:00
|
|
|
|
_openGLView->retain();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// set size
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_winSizeInPoints = _openGLView->getDesignResolutionSize();
|
2014-01-14 06:48:12 +08:00
|
|
|
|
|
2015-04-02 23:35:56 +08:00
|
|
|
|
_isStatusLabelUpdated = true;
|
2014-01-14 06:48:12 +08:00
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (_openGLView)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
setGLDefaultValues();
|
2014-01-14 06:48:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-18 09:50:17 +08:00
|
|
|
|
_renderer->initGLView();
|
2014-01-14 06:48:12 +08:00
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
2014-04-08 15:11:04 +08:00
|
|
|
|
if (_eventDispatcher)
|
|
|
|
|
{
|
|
|
|
|
_eventDispatcher->setEnabled(true);
|
|
|
|
|
}
|
2015-05-13 14:24:53 +08:00
|
|
|
|
|
2015-06-09 10:59:43 +08:00
|
|
|
|
_defaultFBO = experimental::FrameBuffer::getOrCreateDefaultFBO(_openGLView);
|
2015-06-04 15:53:13 +08:00
|
|
|
|
_defaultFBO->retain();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-07 18:52:36 +08:00
|
|
|
|
TextureCache* Director::getTextureCache() const
|
|
|
|
|
{
|
|
|
|
|
return _textureCache;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Director::initTextureCache()
|
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
|
_textureCache = new (std::nothrow) TextureCache();
|
2013-11-07 18:52:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Director::destroyTextureCache()
|
|
|
|
|
{
|
|
|
|
|
if (_textureCache)
|
|
|
|
|
{
|
|
|
|
|
_textureCache->waitForQuit();
|
|
|
|
|
CC_SAFE_RELEASE_NULL(_textureCache);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
void Director::setViewport()
|
2013-02-27 15:44:36 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (_openGLView)
|
2013-02-27 15:52:14 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_openGLView->setViewPortInPoints(0, 0, _winSizeInPoints.width, _winSizeInPoints.height);
|
2013-02-27 15:52:14 +08:00
|
|
|
|
}
|
2013-02-27 15:44:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-02 14:05:07 +08:00
|
|
|
|
void Director::setNextDeltaTimeZero(bool nextDeltaTimeZero)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2014-04-02 14:05:07 +08:00
|
|
|
|
_nextDeltaTimeZero = nextDeltaTimeZero;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2014-11-27 05:31:44 +08:00
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// FIXME TODO
|
|
|
|
|
// Matrix code MUST NOT be part of the Director
|
2015-12-09 00:45:39 +08:00
|
|
|
|
// MUST BE moved outside.
|
2014-11-27 05:31:44 +08:00
|
|
|
|
// Why the Director must have this code ?
|
|
|
|
|
//
|
2014-04-02 16:36:11 +08:00
|
|
|
|
void Director::initMatrixStack()
|
|
|
|
|
{
|
2014-04-03 17:40:14 +08:00
|
|
|
|
while (!_modelViewMatrixStack.empty())
|
|
|
|
|
{
|
|
|
|
|
_modelViewMatrixStack.pop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (!_projectionMatrixStack.empty())
|
|
|
|
|
{
|
|
|
|
|
_projectionMatrixStack.pop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (!_textureMatrixStack.empty())
|
|
|
|
|
{
|
|
|
|
|
_textureMatrixStack.pop();
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
|
_modelViewMatrixStack.push(Mat4::IDENTITY);
|
|
|
|
|
_projectionMatrixStack.push(Mat4::IDENTITY);
|
|
|
|
|
_textureMatrixStack.push(Mat4::IDENTITY);
|
2014-04-02 16:36:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-03 17:40:14 +08:00
|
|
|
|
void Director::resetMatrixStack()
|
|
|
|
|
{
|
|
|
|
|
initMatrixStack();
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-02 16:36:11 +08:00
|
|
|
|
void Director::popMatrix(MATRIX_STACK_TYPE type)
|
|
|
|
|
{
|
|
|
|
|
if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
|
|
|
|
|
{
|
|
|
|
|
_modelViewMatrixStack.pop();
|
|
|
|
|
}
|
|
|
|
|
else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
|
|
|
|
|
{
|
|
|
|
|
_projectionMatrixStack.pop();
|
|
|
|
|
}
|
|
|
|
|
else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
|
|
|
|
|
{
|
|
|
|
|
_textureMatrixStack.pop();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CCASSERT(false, "unknow matrix stack type");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-02 16:47:23 +08:00
|
|
|
|
void Director::loadIdentityMatrix(MATRIX_STACK_TYPE type)
|
|
|
|
|
{
|
|
|
|
|
if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
|
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
|
_modelViewMatrixStack.top() = Mat4::IDENTITY;
|
2014-04-02 16:47:23 +08:00
|
|
|
|
}
|
|
|
|
|
else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
|
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
|
_projectionMatrixStack.top() = Mat4::IDENTITY;
|
2014-04-02 16:47:23 +08:00
|
|
|
|
}
|
|
|
|
|
else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
|
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
|
_textureMatrixStack.top() = Mat4::IDENTITY;
|
2014-04-02 16:47:23 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CCASSERT(false, "unknow matrix stack type");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
|
void Director::loadMatrix(MATRIX_STACK_TYPE type, const Mat4& mat)
|
2014-04-02 16:36:11 +08:00
|
|
|
|
{
|
|
|
|
|
if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
|
|
|
|
|
{
|
2014-04-04 17:58:37 +08:00
|
|
|
|
_modelViewMatrixStack.top() = mat;
|
2014-04-02 16:36:11 +08:00
|
|
|
|
}
|
|
|
|
|
else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
|
|
|
|
|
{
|
2014-04-04 17:58:37 +08:00
|
|
|
|
_projectionMatrixStack.top() = mat;
|
2014-04-02 16:36:11 +08:00
|
|
|
|
}
|
|
|
|
|
else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
|
|
|
|
|
{
|
2014-04-04 17:58:37 +08:00
|
|
|
|
_textureMatrixStack.top() = mat;
|
2014-04-02 16:36:11 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CCASSERT(false, "unknow matrix stack type");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
|
void Director::multiplyMatrix(MATRIX_STACK_TYPE type, const Mat4& mat)
|
2014-04-02 16:36:11 +08:00
|
|
|
|
{
|
|
|
|
|
if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
|
|
|
|
|
{
|
2014-04-04 17:58:37 +08:00
|
|
|
|
_modelViewMatrixStack.top() *= mat;
|
2014-04-02 16:36:11 +08:00
|
|
|
|
}
|
|
|
|
|
else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
|
|
|
|
|
{
|
2014-04-04 17:58:37 +08:00
|
|
|
|
_projectionMatrixStack.top() *= mat;
|
2014-04-02 16:36:11 +08:00
|
|
|
|
}
|
|
|
|
|
else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
|
|
|
|
|
{
|
2014-04-04 17:58:37 +08:00
|
|
|
|
_textureMatrixStack.top() *= mat;
|
2014-04-02 16:36:11 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CCASSERT(false, "unknow matrix stack type");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Director::pushMatrix(MATRIX_STACK_TYPE type)
|
|
|
|
|
{
|
2014-11-05 08:46:14 +08:00
|
|
|
|
if(type == MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW)
|
2014-04-02 16:36:11 +08:00
|
|
|
|
{
|
|
|
|
|
_modelViewMatrixStack.push(_modelViewMatrixStack.top());
|
|
|
|
|
}
|
2014-11-05 08:46:14 +08:00
|
|
|
|
else if(type == MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION)
|
2014-04-02 16:36:11 +08:00
|
|
|
|
{
|
|
|
|
|
_projectionMatrixStack.push(_projectionMatrixStack.top());
|
|
|
|
|
}
|
2014-11-05 08:46:14 +08:00
|
|
|
|
else if(type == MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE)
|
2014-04-02 16:36:11 +08:00
|
|
|
|
{
|
|
|
|
|
_textureMatrixStack.push(_textureMatrixStack.top());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CCASSERT(false, "unknow matrix stack type");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-05 08:46:14 +08:00
|
|
|
|
const Mat4& Director::getMatrix(MATRIX_STACK_TYPE type)
|
2014-04-03 17:26:06 +08:00
|
|
|
|
{
|
2014-11-05 08:46:14 +08:00
|
|
|
|
if(type == MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW)
|
2014-04-03 17:26:06 +08:00
|
|
|
|
{
|
2014-11-05 08:46:14 +08:00
|
|
|
|
return _modelViewMatrixStack.top();
|
2014-04-03 17:26:06 +08:00
|
|
|
|
}
|
2014-11-05 08:46:14 +08:00
|
|
|
|
else if(type == MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION)
|
2014-04-03 17:26:06 +08:00
|
|
|
|
{
|
2014-11-05 08:46:14 +08:00
|
|
|
|
return _projectionMatrixStack.top();
|
2014-04-03 17:26:06 +08:00
|
|
|
|
}
|
2014-11-05 08:46:14 +08:00
|
|
|
|
else if(type == MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE)
|
2014-04-03 17:26:06 +08:00
|
|
|
|
{
|
2014-11-05 08:46:14 +08:00
|
|
|
|
return _textureMatrixStack.top();
|
2014-04-03 17:26:06 +08:00
|
|
|
|
}
|
2014-11-05 08:46:14 +08:00
|
|
|
|
|
|
|
|
|
CCASSERT(false, "unknow matrix stack type, will return modelview matrix instead");
|
|
|
|
|
return _modelViewMatrixStack.top();
|
2014-04-03 17:26:06 +08:00
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-07-26 04:36:19 +08:00
|
|
|
|
void Director::setProjection(Projection projection)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
|
Size size = _winSizeInPoints;
|
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
|
|
|
|
|
2013-07-26 04:36:19 +08:00
|
|
|
|
switch (projection)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-07-26 04:36:19 +08:00
|
|
|
|
case Projection::_2D:
|
2014-04-10 17:36:47 +08:00
|
|
|
|
{
|
2014-04-02 22:46:37 +08:00
|
|
|
|
loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
2015-05-09 00:19:13 +08:00
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
|
Mat4 orthoMatrix;
|
|
|
|
|
Mat4::createOrthographicOffCenter(0, size.width, 0, size.height, -1024, 1024, &orthoMatrix);
|
2014-04-04 18:04:29 +08:00
|
|
|
|
multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, orthoMatrix);
|
2014-04-02 22:46:37 +08:00
|
|
|
|
loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
2013-07-26 04:36:19 +08:00
|
|
|
|
break;
|
2014-04-10 17:36:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-26 04:36:19 +08:00
|
|
|
|
case Projection::_3D:
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
float zeye = this->getZEye();
|
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
|
Mat4 matrixPerspective, matrixLookup;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2014-04-02 22:46:37 +08:00
|
|
|
|
loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
2015-05-09 00:19:13 +08:00
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
// issue #1334
|
2014-05-15 01:07:09 +08:00
|
|
|
|
Mat4::createPerspective(60, (GLfloat)size.width/size.height, 10, zeye+size.height/2, &matrixPerspective);
|
2012-06-12 01:43:07 +08:00
|
|
|
|
|
2014-04-04 18:04:29 +08:00
|
|
|
|
multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, matrixPerspective);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
|
Vec3 eye(size.width/2, size.height/2, zeye), center(size.width/2, size.height/2, 0.0f), up(0.0f, 1.0f, 0.0f);
|
|
|
|
|
Mat4::createLookAt(eye, center, up, &matrixLookup);
|
2014-04-04 18:04:29 +08:00
|
|
|
|
multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, matrixLookup);
|
2014-04-02 22:46:37 +08:00
|
|
|
|
|
|
|
|
|
loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
2013-07-26 04:36:19 +08:00
|
|
|
|
break;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2014-01-14 06:48:12 +08:00
|
|
|
|
|
2013-07-26 04:36:19 +08:00
|
|
|
|
case Projection::CUSTOM:
|
2014-01-11 09:11:14 +08:00
|
|
|
|
// Projection Delegate is no longer needed
|
|
|
|
|
// since the event "PROJECTION CHANGED" is emitted
|
2013-07-26 04:36:19 +08:00
|
|
|
|
break;
|
2014-01-14 06:48:12 +08:00
|
|
|
|
|
2013-07-26 04:36:19 +08:00
|
|
|
|
default:
|
|
|
|
|
CCLOG("cocos2d: Director: unrecognized projection");
|
|
|
|
|
break;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-26 04:36:19 +08:00
|
|
|
|
_projection = projection;
|
2013-07-26 09:42:53 +08:00
|
|
|
|
GL::setProjectionMatrixDirty();
|
2013-12-21 16:56:28 +08:00
|
|
|
|
|
|
|
|
|
_eventDispatcher->dispatchEvent(_eventProjectionChanged);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
void Director::purgeCachedData(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2014-02-20 22:33:52 +08:00
|
|
|
|
FontFNT::purgeCachedData();
|
2014-03-22 20:55:08 +08:00
|
|
|
|
FontAtlasCache::purgeCachedData();
|
|
|
|
|
|
2012-11-13 11:06:32 +08:00
|
|
|
|
if (s_SharedDirector->getOpenGLView())
|
|
|
|
|
{
|
2013-07-22 18:13:13 +08:00
|
|
|
|
SpriteFrameCache::getInstance()->removeUnusedSpriteFrames();
|
2013-11-07 19:11:09 +08:00
|
|
|
|
_textureCache->removeUnusedTextures();
|
2014-03-22 20:55:08 +08:00
|
|
|
|
|
|
|
|
|
// Note: some tests such as ActionsTest are leaking refcounted textures
|
|
|
|
|
// There should be no test textures left in the cache
|
|
|
|
|
log("%s\n", _textureCache->getCachedTextureInfo().c_str());
|
2012-11-13 11:06:32 +08:00
|
|
|
|
}
|
2013-07-12 06:24:23 +08:00
|
|
|
|
FileUtils::getInstance()->purgeCachedEntries();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
|
float Director::getZEye(void) const
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
return (_winSizeInPoints.height / 1.1566f);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
void Director::setAlphaBlending(bool on)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-08-08 16:31:44 +08:00
|
|
|
|
if (on)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-07-26 09:42:53 +08:00
|
|
|
|
GL::blendFunc(CC_BLEND_SRC, CC_BLEND_DST);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-07-26 09:42:53 +08:00
|
|
|
|
GL::blendFunc(GL_ONE, GL_ZERO);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
void Director::setDepthTest(bool on)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2015-01-16 08:41:07 +08:00
|
|
|
|
_renderer->setDepthTest(on);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-24 11:53:38 +08:00
|
|
|
|
void Director::setClearColor(const Color4F& clearColor)
|
|
|
|
|
{
|
2015-01-10 06:06:21 +08:00
|
|
|
|
_renderer->setClearColor(clearColor);
|
2015-06-24 11:40:23 +08:00
|
|
|
|
auto defaultFBO = experimental::FrameBuffer::getOrCreateDefaultFBO(_openGLView);
|
|
|
|
|
|
|
|
|
|
if(defaultFBO) defaultFBO->setClearColor(clearColor);
|
2014-12-24 11:53:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
|
static void GLToClipTransform(Mat4 *transformOut)
|
2012-11-13 11:06:32 +08:00
|
|
|
|
{
|
2014-04-10 17:36:47 +08:00
|
|
|
|
if(nullptr == transformOut) return;
|
|
|
|
|
|
2014-04-02 22:46:37 +08:00
|
|
|
|
Director* director = Director::getInstance();
|
2015-09-22 16:08:23 +08:00
|
|
|
|
CCASSERT(nullptr != director, "Director is null when setting matrix stack");
|
2014-11-05 08:46:14 +08:00
|
|
|
|
|
|
|
|
|
auto projection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
|
|
|
|
auto modelview = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
2014-04-10 17:36:47 +08:00
|
|
|
|
*transformOut = projection * modelview;
|
2012-11-13 11:06:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
|
Vec2 Director::convertToGL(const Vec2& uiPoint)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
|
Mat4 transform;
|
2014-04-28 09:51:10 +08:00
|
|
|
|
GLToClipTransform(&transform);
|
2014-01-14 06:48:12 +08:00
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
|
Mat4 transformInv = transform.getInversed();
|
2014-01-14 06:48:12 +08:00
|
|
|
|
|
2014-04-28 09:51:10 +08:00
|
|
|
|
// Calculate z=0 using -> transform*[0, 0, 0, 1]/w
|
|
|
|
|
float zClip = transform.m[14]/transform.m[15];
|
2014-01-14 06:48:12 +08:00
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
Size glSize = _openGLView->getDesignResolutionSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
|
Vec4 clipCoord(2.0f*uiPoint.x/glSize.width - 1.0f, 1.0f - 2.0f*uiPoint.y/glSize.height, zClip, 1);
|
2014-01-14 06:48:12 +08:00
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
|
Vec4 glCoord;
|
2014-04-10 17:36:47 +08:00
|
|
|
|
//transformInv.transformPoint(clipCoord, &glCoord);
|
|
|
|
|
transformInv.transformVector(clipCoord, &glCoord);
|
|
|
|
|
float factor = 1.0/glCoord.w;
|
2014-05-15 01:07:09 +08:00
|
|
|
|
return Vec2(glCoord.x * factor, glCoord.y * factor);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
|
Vec2 Director::convertToUI(const Vec2& glPoint)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
|
Mat4 transform;
|
2014-04-28 09:51:10 +08:00
|
|
|
|
GLToClipTransform(&transform);
|
2014-01-14 06:48:12 +08:00
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
|
Vec4 clipCoord;
|
2014-04-28 09:51:10 +08:00
|
|
|
|
// Need to calculate the zero depth from the transform.
|
2014-05-15 01:07:09 +08:00
|
|
|
|
Vec4 glCoord(glPoint.x, glPoint.y, 0.0, 1);
|
2014-04-10 17:36:47 +08:00
|
|
|
|
transform.transformVector(glCoord, &clipCoord);
|
2014-01-14 06:48:12 +08:00
|
|
|
|
|
#5506 bug fix
when Director->convertToUI() called, returned wrong value.
kazmath code (under 3.0 version.)
//********************************************************
kmVec3* kmVec3TransformCoord(kmVec3* pOut, const kmVec3* pV, const kmMat4* pM)
{
/*
a = (Vx, Vy, Vz, 1)
b = (a×M)T
Out = 1 ⁄ bw(bx, by, bz)
*/
kmVec4 v;
kmVec4 inV;
kmVec4Fill(&inV, pV->x, pV->y, pV->z, 1.0);
kmVec4Transform(&v, &inV,pM);
pOut->x = v.x / v.w;
pOut->y = v.y / v.w;
pOut->z = v.z / v.w;
return pOut;
}
//********************************************************
Mat4.h & MathUtil version (3.1 or later)
//********************************************************
inline void MathUtil::transformVec4(const float* m, const float* v, float* dst)
{
// Handle case where v == dst.
float x = v[0] * m[0] + v[1] * m[4] + v[2] * m[8] + v[3] * m[12];
float y = v[0] * m[1] + v[1] * m[5] + v[2] * m[9] + v[3] * m[13];
float z = v[0] * m[2] + v[1] * m[6] + v[2] * m[10] + v[3] * m[14];
float w = v[0] * m[3] + v[1] * m[7] + v[2] * m[11] + v[3] * m[15];
dst[0] = x;
dst[1] = y;
dst[2] = z;
dst[3] = w;
}
//****************************************************
Transforms 3-D vector or an array of 3-D vectors using a given matrix, projecting the result back into w = 1.
but, it is not apply w = 1.
2014-06-10 17:15:07 +08:00
|
|
|
|
/*
|
|
|
|
|
BUG-FIX #5506
|
|
|
|
|
|
|
|
|
|
a = (Vx, Vy, Vz, 1)
|
|
|
|
|
b = (a×M)T
|
|
|
|
|
Out = 1 ⁄ bw(bx, by, bz)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
clipCoord.x = clipCoord.x / clipCoord.w;
|
|
|
|
|
clipCoord.y = clipCoord.y / clipCoord.w;
|
|
|
|
|
clipCoord.z = clipCoord.z / clipCoord.w;
|
|
|
|
|
|
2014-04-28 09:51:10 +08:00
|
|
|
|
Size glSize = _openGLView->getDesignResolutionSize();
|
2014-04-10 17:36:47 +08:00
|
|
|
|
float factor = 1.0/glCoord.w;
|
2014-05-15 01:07:09 +08:00
|
|
|
|
return Vec2(glSize.width*(clipCoord.x*0.5 + 0.5) * factor, glSize.height*(-clipCoord.y*0.5 + 0.5) * factor);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
|
const Size& Director::getWinSize(void) const
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
return _winSizeInPoints;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
|
Size Director::getWinSizeInPixels() const
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-07-12 14:30:26 +08:00
|
|
|
|
return Size(_winSizeInPoints.width * _contentScaleFactor, _winSizeInPoints.height * _contentScaleFactor);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
|
Size Director::getVisibleSize() const
|
2012-08-09 10:23:39 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (_openGLView)
|
2012-08-09 10:23:39 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
return _openGLView->getVisibleSize();
|
2012-08-09 10:23:39 +08:00
|
|
|
|
}
|
2014-01-14 06:48:12 +08:00
|
|
|
|
else
|
2012-08-09 10:23:39 +08:00
|
|
|
|
{
|
2013-07-12 14:47:36 +08:00
|
|
|
|
return Size::ZERO;
|
2012-08-09 10:23:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
|
Vec2 Director::getVisibleOrigin() const
|
2012-08-09 10:23:39 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (_openGLView)
|
2012-08-09 10:23:39 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
return _openGLView->getVisibleOrigin();
|
2012-08-09 10:23:39 +08:00
|
|
|
|
}
|
2014-01-14 06:48:12 +08:00
|
|
|
|
else
|
2012-08-09 10:23:39 +08:00
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
|
return Vec2::ZERO;
|
2012-08-09 10:23:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
// scene management
|
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
void Director::runWithScene(Scene *scene)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-08-08 16:31:44 +08:00
|
|
|
|
CCASSERT(scene != nullptr, "This command can only be used to start the Director. There is already a scene present.");
|
|
|
|
|
CCASSERT(_runningScene == nullptr, "_runningScene should be null");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
pushScene(scene);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
startAnimation();
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
void Director::replaceScene(Scene *scene)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2014-08-07 18:34:21 +08:00
|
|
|
|
//CCASSERT(_runningScene, "Use runWithScene: instead to start the director");
|
2013-08-08 16:31:44 +08:00
|
|
|
|
CCASSERT(scene != nullptr, "the scene should not be null");
|
2014-04-28 09:51:10 +08:00
|
|
|
|
|
2014-08-07 18:34:21 +08:00
|
|
|
|
if (_runningScene == nullptr) {
|
|
|
|
|
runWithScene(scene);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-12 15:05:21 +08:00
|
|
|
|
if (scene == _nextScene)
|
|
|
|
|
return;
|
|
|
|
|
|
2014-04-21 22:24:44 +08:00
|
|
|
|
if (_nextScene)
|
|
|
|
|
{
|
2014-04-21 22:31:34 +08:00
|
|
|
|
if (_nextScene->isRunning())
|
|
|
|
|
{
|
|
|
|
|
_nextScene->onExit();
|
|
|
|
|
}
|
2014-04-21 22:24:44 +08:00
|
|
|
|
_nextScene->cleanup();
|
|
|
|
|
_nextScene = nullptr;
|
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2015-12-17 21:55:47 +08:00
|
|
|
|
ssize_t index = _scenesStack.size() - 1;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_sendCleanupToScene = true;
|
2015-12-17 21:55:47 +08:00
|
|
|
|
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
|
|
|
|
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
|
|
|
|
|
sEngine->retainScriptObject(this, scene);
|
|
|
|
|
sEngine->releaseScriptObject(this, _scenesStack.at(index));
|
|
|
|
|
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
|
|
|
|
_scenesStack.replace(index, scene);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
_nextScene = scene;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
void Director::pushScene(Scene *scene)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-08-08 16:31:44 +08:00
|
|
|
|
CCASSERT(scene, "the scene should not null");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_sendCleanupToScene = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2015-12-17 21:55:47 +08:00
|
|
|
|
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
|
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->retainScriptObject(this, scene);
|
|
|
|
|
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2013-12-07 14:22:51 +08:00
|
|
|
|
_scenesStack.pushBack(scene);
|
2013-08-08 16:31:44 +08:00
|
|
|
|
_nextScene = scene;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
void Director::popScene(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-08-08 16:31:44 +08:00
|
|
|
|
CCASSERT(_runningScene != nullptr, "running scene should not null");
|
2015-12-17 21:55:47 +08:00
|
|
|
|
|
|
|
|
|
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
|
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->releaseScriptObject(this, _scenesStack.back());
|
|
|
|
|
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2013-12-07 14:22:51 +08:00
|
|
|
|
_scenesStack.popBack();
|
2013-12-12 12:07:20 +08:00
|
|
|
|
ssize_t c = _scenesStack.size();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
if (c == 0)
|
|
|
|
|
{
|
|
|
|
|
end();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_sendCleanupToScene = true;
|
2013-12-07 14:22:51 +08:00
|
|
|
|
_nextScene = _scenesStack.at(c - 1);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
void Director::popToRootScene(void)
|
2013-05-01 06:33:22 +08:00
|
|
|
|
{
|
|
|
|
|
popToSceneStackLevel(1);
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
void Director::popToSceneStackLevel(int level)
|
2012-06-11 10:59:57 +08:00
|
|
|
|
{
|
2013-08-08 16:31:44 +08:00
|
|
|
|
CCASSERT(_runningScene != nullptr, "A running Scene is needed");
|
2013-12-12 12:07:20 +08:00
|
|
|
|
ssize_t c = _scenesStack.size();
|
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;
|
|
|
|
|
|
2014-05-15 14:43:18 +08:00
|
|
|
|
auto fisrtOnStackScene = _scenesStack.back();
|
|
|
|
|
if (fisrtOnStackScene == _runningScene)
|
2014-05-15 04:28:47 +08:00
|
|
|
|
{
|
2015-12-17 21:55:47 +08:00
|
|
|
|
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
|
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->releaseScriptObject(this, _scenesStack.back());
|
|
|
|
|
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2014-05-15 04:28:47 +08:00
|
|
|
|
_scenesStack.popBack();
|
|
|
|
|
--c;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-28 09:51:10 +08:00
|
|
|
|
// pop stack until reaching desired level
|
|
|
|
|
while (c > level)
|
2012-06-12 02:50:32 +08:00
|
|
|
|
{
|
2013-12-07 14:22:51 +08:00
|
|
|
|
auto current = _scenesStack.back();
|
2013-05-01 06:33:22 +08:00
|
|
|
|
|
2014-04-28 09:51:10 +08:00
|
|
|
|
if (current->isRunning())
|
2012-06-12 02:50:32 +08:00
|
|
|
|
{
|
2013-05-01 06:33:22 +08:00
|
|
|
|
current->onExit();
|
2014-04-28 09:51:10 +08:00
|
|
|
|
}
|
2013-05-01 06:33:22 +08:00
|
|
|
|
|
|
|
|
|
current->cleanup();
|
2015-12-17 21:55:47 +08:00
|
|
|
|
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
|
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->releaseScriptObject(this, _scenesStack.back());
|
|
|
|
|
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2013-12-07 14:22:51 +08:00
|
|
|
|
_scenesStack.popBack();
|
2014-04-28 09:51:10 +08:00
|
|
|
|
--c;
|
|
|
|
|
}
|
2013-05-01 06:33:22 +08:00
|
|
|
|
|
2013-12-07 14:22:51 +08:00
|
|
|
|
_nextScene = _scenesStack.back();
|
2014-05-15 14:43:18 +08:00
|
|
|
|
|
|
|
|
|
// cleanup running scene
|
2014-05-15 04:28:47 +08:00
|
|
|
|
_sendCleanupToScene = true;
|
2012-06-11 10:59:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
void Director::end()
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2014-01-04 13:22:09 +08:00
|
|
|
|
_purgeDirectorInNextLoop = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-30 10:09:56 +08:00
|
|
|
|
void Director::restart()
|
|
|
|
|
{
|
|
|
|
|
_restartDirectorInNextLoop = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Director::reset()
|
2015-04-18 15:06:09 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (_runningScene)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2015-12-17 21:55:47 +08:00
|
|
|
|
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
|
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->releaseScriptObject(this, _runningScene);
|
|
|
|
|
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_runningScene->onExit();
|
|
|
|
|
_runningScene->cleanup();
|
|
|
|
|
_runningScene->release();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
_runningScene = nullptr;
|
|
|
|
|
_nextScene = nullptr;
|
2015-04-18 15:06:09 +08:00
|
|
|
|
|
|
|
|
|
// cleanup scheduler
|
|
|
|
|
getScheduler()->unscheduleAll();
|
|
|
|
|
|
|
|
|
|
// Remove all events
|
|
|
|
|
if (_eventDispatcher)
|
|
|
|
|
{
|
|
|
|
|
_eventDispatcher->removeAllEventListeners();
|
|
|
|
|
}
|
2014-12-30 10:09:56 +08:00
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
// remove all objects, but don't release it.
|
|
|
|
|
// runWithScene might be executed after 'end'.
|
2015-12-17 21:55:47 +08:00
|
|
|
|
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
|
|
|
|
auto engine = ScriptEngineManager::getInstance()->getScriptEngine();
|
|
|
|
|
for (const auto &scene : _scenesStack)
|
|
|
|
|
{
|
|
|
|
|
if (scene)
|
|
|
|
|
engine->releaseScriptObject(this, scene);
|
|
|
|
|
}
|
|
|
|
|
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2013-12-07 14:22:51 +08:00
|
|
|
|
_scenesStack.clear();
|
2014-12-30 10:09:56 +08:00
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
stopAnimation();
|
2014-12-30 10:09:56 +08:00
|
|
|
|
|
2015-06-15 17:55:00 +08:00
|
|
|
|
CC_SAFE_RELEASE_NULL(_notificationNode);
|
2013-06-15 14:03:30 +08:00
|
|
|
|
CC_SAFE_RELEASE_NULL(_FPSLabel);
|
2014-02-08 11:37:44 +08:00
|
|
|
|
CC_SAFE_RELEASE_NULL(_drawnBatchesLabel);
|
|
|
|
|
CC_SAFE_RELEASE_NULL(_drawnVerticesLabel);
|
2014-12-30 10:09:56 +08:00
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
// purge bitmap cache
|
2014-02-20 22:33:52 +08:00
|
|
|
|
FontFNT::purgeCachedData();
|
2014-12-30 10:09:56 +08:00
|
|
|
|
|
2013-10-29 20:25:03 +08:00
|
|
|
|
FontFreeType::shutdownFreeType();
|
2014-12-30 10:09:56 +08:00
|
|
|
|
|
2012-06-20 11:48:31 +08:00
|
|
|
|
// purge all managed caches
|
2014-09-15 16:23:57 +08:00
|
|
|
|
|
|
|
|
|
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
|
|
|
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
|
#elif _MSC_VER >= 1400 //vs 2005 or higher
|
|
|
|
|
#pragma warning (push)
|
|
|
|
|
#pragma warning (disable: 4996)
|
|
|
|
|
#endif
|
2015-01-06 19:45:10 +08:00
|
|
|
|
//it will crash clang static analyzer so hide it if __clang_analyzer__ defined
|
2014-12-11 17:10:43 +08:00
|
|
|
|
#ifndef __clang_analyzer__
|
2013-07-25 22:38:55 +08:00
|
|
|
|
DrawPrimitives::free();
|
2014-12-11 17:10:43 +08:00
|
|
|
|
#endif
|
2014-09-15 16:23:57 +08:00
|
|
|
|
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
|
|
|
|
|
#pragma GCC diagnostic warning "-Wdeprecated-declarations"
|
|
|
|
|
#elif _MSC_VER >= 1400 //vs 2005 or higher
|
|
|
|
|
#pragma warning (pop)
|
|
|
|
|
#endif
|
2013-07-12 06:24:23 +08:00
|
|
|
|
AnimationCache::destroyInstance();
|
|
|
|
|
SpriteFrameCache::destroyInstance();
|
2014-05-10 09:39:25 +08:00
|
|
|
|
GLProgramCache::destroyInstance();
|
|
|
|
|
GLProgramStateCache::destroyInstance();
|
2013-07-12 06:24:23 +08:00
|
|
|
|
FileUtils::destroyInstance();
|
2014-12-05 15:01:30 +08:00
|
|
|
|
AsyncTaskPool::destoryInstance();
|
2014-12-30 10:09:56 +08:00
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
// cocos2d-x specific data structures
|
2013-07-12 06:24:23 +08:00
|
|
|
|
UserDefault::destroyInstance();
|
2013-09-18 13:00:43 +08:00
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
|
GL::invalidateStateCache();
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
|
|
RenderState::finalize();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-11-07 18:52:36 +08:00
|
|
|
|
destroyTextureCache();
|
2014-12-30 10:09:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Director::purgeDirector()
|
|
|
|
|
{
|
|
|
|
|
reset();
|
2013-11-07 18:52:36 +08:00
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
|
|
// OpenGL view
|
2013-09-18 23:52:09 +08:00
|
|
|
|
if (_openGLView)
|
|
|
|
|
{
|
|
|
|
|
_openGLView->end();
|
|
|
|
|
_openGLView = nullptr;
|
|
|
|
|
}
|
2012-09-21 00:29:16 +08:00
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
// delete Director
|
2012-09-21 00:29:16 +08:00
|
|
|
|
release();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-28 11:55:06 +08:00
|
|
|
|
void Director::restartDirector()
|
|
|
|
|
{
|
2014-12-30 10:09:56 +08:00
|
|
|
|
reset();
|
2014-12-28 11:55:06 +08:00
|
|
|
|
|
2015-06-23 14:41:41 +08:00
|
|
|
|
// RenderState need to be reinitialized
|
|
|
|
|
RenderState::initialize();
|
|
|
|
|
|
2014-12-30 10:09:56 +08:00
|
|
|
|
// Texture cache need to be reinitialized
|
|
|
|
|
initTextureCache();
|
2014-12-28 11:55:06 +08:00
|
|
|
|
|
2014-12-31 15:32:59 +08:00
|
|
|
|
// Reschedule for action manager
|
|
|
|
|
getScheduler()->scheduleUpdate(getActionManager(), Scheduler::PRIORITY_SYSTEM, false);
|
|
|
|
|
|
2014-12-28 11:55:06 +08:00
|
|
|
|
// release the objects
|
|
|
|
|
PoolManager::getInstance()->getCurrentPool()->clear();
|
|
|
|
|
|
2014-12-30 10:09:56 +08:00
|
|
|
|
// Real restart in script level
|
2014-12-28 11:55:06 +08:00
|
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
|
|
|
|
ScriptEvent scriptEvent(kRestartGame, NULL);
|
|
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
void Director::setNextScene()
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-08-08 16:31:44 +08:00
|
|
|
|
bool runningIsTransition = dynamic_cast<TransitionScene*>(_runningScene) != nullptr;
|
|
|
|
|
bool newIsTransition = dynamic_cast<TransitionScene*>(_nextScene) != nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// If it is not a transition, call onExit/cleanup
|
|
|
|
|
if (! newIsTransition)
|
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (_runningScene)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_runningScene->onExitTransitionDidStart();
|
|
|
|
|
_runningScene->onExit();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// issue #709. the root node (scene) should receive the cleanup message too
|
|
|
|
|
// otherwise it might be leaked.
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (_sendCleanupToScene && _runningScene)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_runningScene->cleanup();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (_runningScene)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_runningScene->release();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_runningScene = _nextScene;
|
|
|
|
|
_nextScene->retain();
|
2013-08-08 16:31:44 +08:00
|
|
|
|
_nextScene = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if ((! runningIsTransition) && _runningScene)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_runningScene->onEnter();
|
|
|
|
|
_runningScene->onEnterTransitionDidFinish();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
void Director::pause()
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (_paused)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_oldAnimationInterval = _animationInterval;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// when paused, don't consume CPU
|
|
|
|
|
setAnimationInterval(1 / 4.0);
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_paused = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
void Director::resume()
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (! _paused)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
setAnimationInterval(_oldAnimationInterval);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_paused = false;
|
|
|
|
|
_deltaTime = 0;
|
2013-12-27 15:53:55 +08:00
|
|
|
|
// fix issue #3509, skip one fps to avoid incorrect time calculation.
|
|
|
|
|
setNextDeltaTimeZero(true);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// display the FPS using a LabelAtlas
|
|
|
|
|
// updates the FPS every frame
|
2013-08-08 16:31:44 +08:00
|
|
|
|
void Director::showStats()
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2015-04-02 23:35:56 +08:00
|
|
|
|
if (_isStatusLabelUpdated)
|
|
|
|
|
{
|
|
|
|
|
createStatsLabel();
|
|
|
|
|
_isStatusLabelUpdated = false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-12 08:41:32 +08:00
|
|
|
|
static unsigned long prevCalls = 0;
|
|
|
|
|
static unsigned long prevVerts = 0;
|
2015-01-15 10:07:23 +08:00
|
|
|
|
static float prevDeltaTime = 0.016f; // 60FPS
|
|
|
|
|
static const float FPS_FILTER = 0.10f;
|
2014-02-12 08:41:32 +08:00
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_accumDt += _deltaTime;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2014-02-12 06:58:34 +08:00
|
|
|
|
if (_displayStats && _FPSLabel && _drawnBatchesLabel && _drawnVerticesLabel)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2014-02-12 08:41:32 +08:00
|
|
|
|
char buffer[30];
|
|
|
|
|
|
2014-11-27 05:31:44 +08:00
|
|
|
|
float dt = _deltaTime * FPS_FILTER + (1-FPS_FILTER) * prevDeltaTime;
|
|
|
|
|
prevDeltaTime = dt;
|
2014-11-27 08:55:39 +08:00
|
|
|
|
_frameRate = 1/dt;
|
2014-11-27 05:31:44 +08:00
|
|
|
|
|
|
|
|
|
// Probably we don't need this anymore since
|
|
|
|
|
// the framerate is using a low-pass filter
|
|
|
|
|
// to make the FPS stable
|
2014-02-08 11:37:44 +08:00
|
|
|
|
if (_accumDt > CC_DIRECTOR_STATS_INTERVAL)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2014-02-12 06:58:34 +08:00
|
|
|
|
sprintf(buffer, "%.1f / %.3f", _frameRate, _secondsPerFrame);
|
2014-02-08 11:37:44 +08:00
|
|
|
|
_FPSLabel->setString(buffer);
|
2014-11-27 05:31:44 +08:00
|
|
|
|
_accumDt = 0;
|
2014-02-12 08:41:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto currentCalls = (unsigned long)_renderer->getDrawnBatches();
|
|
|
|
|
auto currentVerts = (unsigned long)_renderer->getDrawnVertices();
|
|
|
|
|
if( currentCalls != prevCalls ) {
|
|
|
|
|
sprintf(buffer, "GL calls:%6lu", currentCalls);
|
2014-02-08 11:37:44 +08:00
|
|
|
|
_drawnBatchesLabel->setString(buffer);
|
2014-02-12 08:41:32 +08:00
|
|
|
|
prevCalls = currentCalls;
|
|
|
|
|
}
|
2014-02-08 11:37:44 +08:00
|
|
|
|
|
2014-02-12 08:41:32 +08:00
|
|
|
|
if( currentVerts != prevVerts) {
|
|
|
|
|
sprintf(buffer, "GL verts:%6lu", currentVerts);
|
2014-02-08 11:37:44 +08:00
|
|
|
|
_drawnVerticesLabel->setString(buffer);
|
2014-02-12 08:41:32 +08:00
|
|
|
|
prevVerts = currentVerts;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2014-02-08 11:37:44 +08:00
|
|
|
|
|
2014-12-23 18:07:22 +08:00
|
|
|
|
const Mat4& identity = Mat4::IDENTITY;
|
2014-05-31 07:42:05 +08:00
|
|
|
|
_drawnVerticesLabel->visit(_renderer, identity, 0);
|
|
|
|
|
_drawnBatchesLabel->visit(_renderer, identity, 0);
|
|
|
|
|
_FPSLabel->visit(_renderer, identity, 0);
|
2014-02-08 11:37:44 +08:00
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
void Director::calculateMPF()
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2014-11-27 05:31:44 +08:00
|
|
|
|
static float prevSecondsPerFrame = 0;
|
2015-01-15 10:07:23 +08:00
|
|
|
|
static const float MPF_FILTER = 0.10f;
|
2014-11-27 05:31:44 +08:00
|
|
|
|
|
2013-07-19 13:42:45 +08:00
|
|
|
|
struct timeval now;
|
2013-08-08 16:31:44 +08:00
|
|
|
|
gettimeofday(&now, nullptr);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_secondsPerFrame = (now.tv_sec - _lastUpdate->tv_sec) + (now.tv_usec - _lastUpdate->tv_usec) / 1000000.0f;
|
2014-11-27 05:31:44 +08:00
|
|
|
|
|
|
|
|
|
_secondsPerFrame = _secondsPerFrame * MPF_FILTER + (1-MPF_FILTER) * prevSecondsPerFrame;
|
|
|
|
|
prevSecondsPerFrame = _secondsPerFrame;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-27 09:38:30 +08:00
|
|
|
|
// returns the FPS image data pointer and len
|
2013-12-05 17:19:01 +08:00
|
|
|
|
void Director::getFPSImageData(unsigned char** datapointer, ssize_t* length)
|
2013-02-27 09:38:30 +08:00
|
|
|
|
{
|
2014-08-30 03:54:24 +08:00
|
|
|
|
// FIXME: fixed me if it should be used
|
2013-05-23 15:06:57 +08:00
|
|
|
|
*datapointer = cc_fps_images_png;
|
2014-04-28 09:51:10 +08:00
|
|
|
|
*length = cc_fps_images_len();
|
2013-02-27 09:38:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
void Director::createStatsLabel()
|
2013-05-23 15:06:57 +08:00
|
|
|
|
{
|
2013-08-08 16:31:44 +08:00
|
|
|
|
Texture2D *texture = nullptr;
|
2014-08-25 17:19:45 +08:00
|
|
|
|
std::string fpsString = "00.0";
|
|
|
|
|
std::string drawBatchString = "000";
|
|
|
|
|
std::string drawVerticesString = "00000";
|
2014-02-12 06:58:34 +08:00
|
|
|
|
if (_FPSLabel)
|
2012-06-12 02:50:32 +08:00
|
|
|
|
{
|
2014-08-25 17:19:45 +08:00
|
|
|
|
fpsString = _FPSLabel->getString();
|
|
|
|
|
drawBatchString = _drawnBatchesLabel->getString();
|
|
|
|
|
drawVerticesString = _drawnVerticesLabel->getString();
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
CC_SAFE_RELEASE_NULL(_FPSLabel);
|
2014-02-08 11:37:44 +08:00
|
|
|
|
CC_SAFE_RELEASE_NULL(_drawnBatchesLabel);
|
|
|
|
|
CC_SAFE_RELEASE_NULL(_drawnVerticesLabel);
|
2013-11-11 14:21:41 +08:00
|
|
|
|
_textureCache->removeTextureForKey("/cc_fps_images");
|
2013-07-12 06:24:23 +08:00
|
|
|
|
FileUtils::getInstance()->purgeCachedEntries();
|
2012-06-12 02:50:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-25 19:52:44 +08:00
|
|
|
|
Texture2D::PixelFormat currentFormat = Texture2D::getDefaultAlphaPixelFormat();
|
2013-07-26 04:36:19 +08:00
|
|
|
|
Texture2D::setDefaultAlphaPixelFormat(Texture2D::PixelFormat::RGBA4444);
|
2013-08-08 16:31:44 +08:00
|
|
|
|
unsigned char *data = nullptr;
|
2013-12-05 17:19:01 +08:00
|
|
|
|
ssize_t dataLength = 0;
|
2013-08-08 16:31:44 +08:00
|
|
|
|
getFPSImageData(&data, &dataLength);
|
2013-05-23 15:11:48 +08:00
|
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
|
Image* image = new (std::nothrow) Image();
|
2013-08-08 16:31:44 +08:00
|
|
|
|
bool isOK = image->initWithImageData(data, dataLength);
|
|
|
|
|
if (! isOK) {
|
2013-05-23 15:06:57 +08:00
|
|
|
|
CCLOGERROR("%s", "Fails: init fps_images");
|
|
|
|
|
return;
|
2012-10-23 10:50:47 +08:00
|
|
|
|
}
|
2013-05-23 15:11:48 +08:00
|
|
|
|
|
2013-11-11 14:21:41 +08:00
|
|
|
|
texture = _textureCache->addImage(image, "/cc_fps_images");
|
2013-05-23 15:06:57 +08:00
|
|
|
|
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.
|
|
|
|
|
|
2014-02-12 06:58:34 +08:00
|
|
|
|
To achieve this, we need to ignore 'contentScaleFactor' in 'AtlasNode' and 'LabelAtlas'.
|
2013-06-20 14:13:12 +08:00
|
|
|
|
So I added a new method called 'setIgnoreContentScaleFactor' for 'AtlasNode',
|
2013-05-23 16:55:53 +08:00
|
|
|
|
this is not exposed to game developers, it's only used for displaying FPS now.
|
|
|
|
|
*/
|
2014-02-12 06:58:34 +08:00
|
|
|
|
float scaleFactor = 1 / CC_CONTENT_SCALE_FACTOR();
|
2013-05-23 15:11:48 +08:00
|
|
|
|
|
2014-01-09 14:16:26 +08:00
|
|
|
|
_FPSLabel = LabelAtlas::create();
|
|
|
|
|
_FPSLabel->retain();
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_FPSLabel->setIgnoreContentScaleFactor(true);
|
2014-08-25 17:19:45 +08:00
|
|
|
|
_FPSLabel->initWithString(fpsString, texture, 12, 32 , '.');
|
2014-02-12 06:58:34 +08:00
|
|
|
|
_FPSLabel->setScale(scaleFactor);
|
2013-05-23 15:11:48 +08:00
|
|
|
|
|
2014-02-08 11:37:44 +08:00
|
|
|
|
_drawnBatchesLabel = LabelAtlas::create();
|
|
|
|
|
_drawnBatchesLabel->retain();
|
|
|
|
|
_drawnBatchesLabel->setIgnoreContentScaleFactor(true);
|
2014-08-25 17:19:45 +08:00
|
|
|
|
_drawnBatchesLabel->initWithString(drawBatchString, texture, 12, 32, '.');
|
2014-02-12 06:58:34 +08:00
|
|
|
|
_drawnBatchesLabel->setScale(scaleFactor);
|
2014-02-08 11:37:44 +08:00
|
|
|
|
|
|
|
|
|
_drawnVerticesLabel = LabelAtlas::create();
|
|
|
|
|
_drawnVerticesLabel->retain();
|
|
|
|
|
_drawnVerticesLabel->setIgnoreContentScaleFactor(true);
|
2014-08-25 17:19:45 +08:00
|
|
|
|
_drawnVerticesLabel->initWithString(drawVerticesString, texture, 12, 32, '.');
|
2014-02-12 06:58:34 +08:00
|
|
|
|
_drawnVerticesLabel->setScale(scaleFactor);
|
2014-02-08 11:37:44 +08:00
|
|
|
|
|
2013-05-23 15:11:48 +08:00
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
Texture2D::setDefaultAlphaPixelFormat(currentFormat);
|
2013-05-23 15:11:48 +08:00
|
|
|
|
|
2014-02-13 04:10:23 +08:00
|
|
|
|
const int height_spacing = 22 / CC_CONTENT_SCALE_FACTOR();
|
2014-05-15 01:07:09 +08:00
|
|
|
|
_drawnVerticesLabel->setPosition(Vec2(0, height_spacing*2) + CC_DIRECTOR_STATS_POSITION);
|
|
|
|
|
_drawnBatchesLabel->setPosition(Vec2(0, height_spacing*1) + CC_DIRECTOR_STATS_POSITION);
|
|
|
|
|
_FPSLabel->setPosition(Vec2(0, height_spacing*0)+CC_DIRECTOR_STATS_POSITION);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
void Director::setContentScaleFactor(float scaleFactor)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (scaleFactor != _contentScaleFactor)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_contentScaleFactor = scaleFactor;
|
2015-04-02 23:35:56 +08:00
|
|
|
|
_isStatusLabelUpdated = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
void Director::setNotificationNode(Node *node)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2015-08-21 10:20:59 +08:00
|
|
|
|
if (_notificationNode != nullptr){
|
|
|
|
|
_notificationNode->onExitTransitionDidStart();
|
|
|
|
|
_notificationNode->onExit();
|
|
|
|
|
_notificationNode->cleanup();
|
|
|
|
|
}
|
|
|
|
|
CC_SAFE_RELEASE(_notificationNode);
|
|
|
|
|
|
|
|
|
|
_notificationNode = node;
|
|
|
|
|
if (node == nullptr)
|
|
|
|
|
return;
|
|
|
|
|
_notificationNode->onEnter();
|
|
|
|
|
_notificationNode->onEnterTransitionDidFinish();
|
2013-06-15 14:03:30 +08:00
|
|
|
|
CC_SAFE_RETAIN(_notificationNode);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
void Director::setScheduler(Scheduler* scheduler)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-08-08 16:31:44 +08:00
|
|
|
|
if (_scheduler != scheduler)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-08-08 16:31:44 +08:00
|
|
|
|
CC_SAFE_RETAIN(scheduler);
|
2013-06-15 14:03:30 +08:00
|
|
|
|
CC_SAFE_RELEASE(_scheduler);
|
2013-08-08 16:31:44 +08:00
|
|
|
|
_scheduler = scheduler;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
void Director::setActionManager(ActionManager* actionManager)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-08-08 16:31:44 +08:00
|
|
|
|
if (_actionManager != actionManager)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-08-08 16:31:44 +08:00
|
|
|
|
CC_SAFE_RETAIN(actionManager);
|
2013-06-15 14:03:30 +08:00
|
|
|
|
CC_SAFE_RELEASE(_actionManager);
|
2013-08-08 16:31:44 +08:00
|
|
|
|
_actionManager = actionManager;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
|
void Director::setEventDispatcher(EventDispatcher* dispatcher)
|
|
|
|
|
{
|
|
|
|
|
if (_eventDispatcher != dispatcher)
|
|
|
|
|
{
|
|
|
|
|
CC_SAFE_RETAIN(dispatcher);
|
|
|
|
|
CC_SAFE_RELEASE(_eventDispatcher);
|
|
|
|
|
_eventDispatcher = dispatcher;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
/***************************************************
|
|
|
|
|
* 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
|
2013-08-08 16:31:44 +08:00
|
|
|
|
void DisplayLinkDirector::startAnimation()
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-08-08 16:31:44 +08:00
|
|
|
|
if (gettimeofday(_lastUpdate, nullptr) != 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
CCLOG("cocos2d: DisplayLinkDirector: Error on gettimeofday");
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_invalid = false;
|
2013-12-27 11:49:54 +08:00
|
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
|
_cocos2d_thread_id = std::this_thread::get_id();
|
|
|
|
|
|
2013-12-27 11:49:54 +08:00
|
|
|
|
Application::getInstance()->setAnimationInterval(_animationInterval);
|
2014-10-15 05:14:38 +08:00
|
|
|
|
|
2014-03-21 15:16:55 +08:00
|
|
|
|
// fix issue #3509, skip one fps to avoid incorrect time calculation.
|
|
|
|
|
setNextDeltaTimeZero(true);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
void DisplayLinkDirector::mainLoop()
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2014-01-04 13:22:09 +08:00
|
|
|
|
if (_purgeDirectorInNextLoop)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2014-01-04 13:22:09 +08:00
|
|
|
|
_purgeDirectorInNextLoop = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
purgeDirector();
|
|
|
|
|
}
|
2014-12-28 11:55:06 +08:00
|
|
|
|
else if (_restartDirectorInNextLoop)
|
|
|
|
|
{
|
|
|
|
|
_restartDirectorInNextLoop = false;
|
|
|
|
|
restartDirector();
|
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
|
else if (! _invalid)
|
2013-08-08 16:31:44 +08:00
|
|
|
|
{
|
|
|
|
|
drawScene();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
// release the objects
|
2014-01-20 17:08:22 +08:00
|
|
|
|
PoolManager::getInstance()->getCurrentPool()->clear();
|
2013-08-08 16:31:44 +08:00
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
|
void DisplayLinkDirector::stopAnimation()
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_invalid = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-06 14:57:42 +08:00
|
|
|
|
void DisplayLinkDirector::setAnimationInterval(float interval)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
|
_animationInterval = interval;
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (! _invalid)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
stopAnimation();
|
|
|
|
|
startAnimation();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|