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.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 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"
|
|
|
|
#include "2d/CCScene.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"
|
|
|
|
#include "platform/CCImage.h"
|
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-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"
|
|
|
|
#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/CCTouch.h"
|
|
|
|
#include "base/CCAutoreleasePool.h"
|
|
|
|
#include "base/CCProfiling.h"
|
|
|
|
#include "base/CCConfiguration.h"
|
|
|
|
#include "base/CCNS.h"
|
2014-04-26 13:03:25 +08:00
|
|
|
#include "math/CCMath.h"
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "CCApplication.h"
|
|
|
|
#include "CCGLView.h"
|
2014-02-06 11:51:24 +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
|
2013-07-12 06:24:23 +08:00
|
|
|
#define CC_DIRECTOR_STATS_POSITION Director::getInstance()->getVisibleOrigin()
|
2012-08-07 14:29:46 +08:00
|
|
|
#endif
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
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
|
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";
|
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
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
s_SharedDirector = new DisplayLinkDirector();
|
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()
|
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;
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalFrames = _frames = 0;
|
2013-07-19 13:42:45 +08:00
|
|
|
_lastUpdate = new struct timeval;
|
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;
|
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;
|
2014-01-14 06:48:12 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_contentScaleFactor = 1.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// scheduler
|
2013-06-20 14:13:12 +08:00
|
|
|
_scheduler = new Scheduler();
|
2012-04-19 14:35:52 +08:00
|
|
|
// action manager
|
2013-06-20 14:13:12 +08:00
|
|
|
_actionManager = new ActionManager();
|
2014-03-03 11:00:30 +08:00
|
|
|
_scheduler->scheduleUpdate(_actionManager, Scheduler::PRIORITY_SYSTEM, false);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher = new EventDispatcher();
|
2013-12-21 16:56:28 +08:00
|
|
|
_eventAfterDraw = new EventCustom(EVENT_AFTER_DRAW);
|
|
|
|
_eventAfterDraw->setUserData(this);
|
|
|
|
_eventAfterVisit = new EventCustom(EVENT_AFTER_VISIT);
|
|
|
|
_eventAfterVisit->setUserData(this);
|
|
|
|
_eventAfterUpdate = new EventCustom(EVENT_AFTER_UPDATE);
|
|
|
|
_eventAfterUpdate->setUserData(this);
|
|
|
|
_eventProjectionChanged = new EventCustom(EVENT_PROJECTION_CHANGED);
|
|
|
|
_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
|
|
|
|
|
|
|
_renderer = new Renderer;
|
|
|
|
|
2014-04-20 01:08:01 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
|
2014-03-22 20:55:08 +08:00
|
|
|
_console = new Console;
|
|
|
|
#endif
|
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);
|
2014-04-17 10:15:11 +08:00
|
|
|
|
2013-12-18 09:50:17 +08:00
|
|
|
|
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-04-20 01:08:01 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
|
2014-01-11 09:11:14 +08:00
|
|
|
delete _console;
|
2014-03-22 20:55:08 +08:00
|
|
|
#endif
|
2013-12-18 09:50:17 +08:00
|
|
|
|
2014-04-17 10:15:11 +08:00
|
|
|
CC_SAFE_RELEASE(_eventDispatcher);
|
|
|
|
|
2014-01-20 17:08:22 +08:00
|
|
|
// clean auto release pool
|
2014-01-21 10:14:00 +08:00
|
|
|
PoolManager::destroyInstance();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
// delete _lastUpdate
|
|
|
|
CC_SAFE_DELETE(_lastUpdate);
|
2012-09-21 00:29:16 +08:00
|
|
|
|
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();
|
|
|
|
Texture2D::PVRImagesHavePremultipliedAlpha(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);
|
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);
|
2013-06-15 14:03:30 +08:00
|
|
|
setProjection(_projection);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// 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
|
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
|
|
|
|
|
|
|
// skip one flame when _deltaTime equal to zero.
|
|
|
|
if(_deltaTime < FLT_EPSILON)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-08 10:26:53 +08:00
|
|
|
if (_openGLView)
|
|
|
|
{
|
|
|
|
_openGLView->pollInputEvents();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
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-01-14 06:48:12 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// draw the scene
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_runningScene)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
_runningScene->visit(_renderer, Mat4::IDENTITY, false);
|
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-05-15 01:07:09 +08:00
|
|
|
_notificationNode->visit(_renderer, Mat4::IDENTITY, false);
|
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();
|
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
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
createStatsLabel();
|
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);
|
|
|
|
}
|
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()
|
|
|
|
{
|
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
_textureCache = new TextureCacheEmscripten();
|
|
|
|
#else
|
|
|
|
_textureCache = new TextureCache();
|
|
|
|
#endif // EMSCRIPTEN
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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)
|
|
|
|
{
|
|
|
|
if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
|
|
|
|
{
|
|
|
|
_modelViewMatrixStack.push(_modelViewMatrixStack.top());
|
|
|
|
}
|
|
|
|
else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
|
|
|
|
{
|
|
|
|
_projectionMatrixStack.push(_projectionMatrixStack.top());
|
|
|
|
}
|
|
|
|
else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
|
|
|
|
{
|
|
|
|
_textureMatrixStack.push(_textureMatrixStack.top());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCASSERT(false, "unknow matrix stack type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 Director::getMatrix(MATRIX_STACK_TYPE type)
|
2014-04-03 17:26:06 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 result;
|
2014-04-03 17:26:06 +08:00
|
|
|
if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
|
|
|
|
{
|
|
|
|
result = _modelViewMatrixStack.top();
|
|
|
|
}
|
|
|
|
else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
|
|
|
|
{
|
|
|
|
result = _projectionMatrixStack.top();
|
|
|
|
}
|
|
|
|
else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
|
|
|
|
{
|
|
|
|
result = _textureMatrixStack.top();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCASSERT(false, "unknow matrix stack type, will return modelview matrix instead");
|
|
|
|
result = _modelViewMatrixStack.top();
|
|
|
|
}
|
2014-04-03 17:32:40 +08:00
|
|
|
// float diffResult(0);
|
|
|
|
// for (int index = 0; index <16; ++index)
|
|
|
|
// {
|
|
|
|
// diffResult += abs(result2.mat[index] - result.mat[index]);
|
|
|
|
// }
|
|
|
|
// if(diffResult > 1e-30)
|
|
|
|
// {
|
|
|
|
// CCASSERT(false, "Error in director matrix stack");
|
|
|
|
// }
|
2014-04-04 17:31:21 +08:00
|
|
|
return result;
|
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);
|
2014-03-22 20:55:08 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_WP8
|
|
|
|
if(getOpenGLView() != nullptr)
|
|
|
|
{
|
2014-04-02 22:46:37 +08:00
|
|
|
multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, getOpenGLView()->getOrientationMatrix());
|
2014-03-22 20:55:08 +08:00
|
|
|
}
|
|
|
|
#endif
|
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);
|
2014-03-22 20:55:08 +08:00
|
|
|
|
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_WP8
|
|
|
|
//if needed, we need to add a rotation for Landscape orientations on Windows Phone 8 since it is always in Portrait Mode
|
|
|
|
GLView* view = getOpenGLView();
|
|
|
|
if(getOpenGLView() != nullptr)
|
|
|
|
{
|
2014-04-02 22:46:37 +08:00
|
|
|
multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, getOpenGLView()->getOrientationMatrix());
|
2014-03-22 20:55:08 +08:00
|
|
|
}
|
|
|
|
#endif
|
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
|
|
|
{
|
2013-08-08 16:31:44 +08:00
|
|
|
if (on)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
CCASSERT(nullptr != director, "Director is null when seting matrix stack");
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 projection;
|
2014-04-04 18:13:10 +08:00
|
|
|
projection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
2014-01-14 06:48:12 +08:00
|
|
|
|
2014-03-22 20:55:08 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_WP8
|
|
|
|
//if needed, we need to undo the rotation for Landscape orientation in order to get the correct positions
|
2014-04-14 09:30:51 +08:00
|
|
|
projection = Director::getInstance()->getOpenGLView()->getReverseOrientationMatrix() * projection;
|
2014-03-22 20:55:08 +08:00
|
|
|
#endif
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 modelview;
|
2014-04-04 18:13:10 +08:00
|
|
|
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
|
|
|
|
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
|
|
|
{
|
2013-07-20 13:01:27 +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-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
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t index = _scenesStack.size();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_sendCleanupToScene = true;
|
2013-12-07 14:22:51 +08:00
|
|
|
_scenesStack.replace(index - 1, 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
|
|
|
|
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");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
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
|
|
|
{
|
|
|
|
_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();
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Director::purgeDirector()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
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
|
|
|
|
2014-04-08 15:08:34 +08:00
|
|
|
// Disable event dispatching
|
2014-04-08 15:11:04 +08:00
|
|
|
if (_eventDispatcher)
|
|
|
|
{
|
|
|
|
_eventDispatcher->setEnabled(false);
|
|
|
|
}
|
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->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;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// remove all objects, but don't release it.
|
|
|
|
// runWithScene might be executed after 'end'.
|
2013-12-07 14:22:51 +08:00
|
|
|
_scenesStack.clear();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
stopAnimation();
|
|
|
|
|
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);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// purge bitmap cache
|
2014-02-20 22:33:52 +08:00
|
|
|
FontFNT::purgeCachedData();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-10-29 20:25:03 +08:00
|
|
|
FontFreeType::shutdownFreeType();
|
|
|
|
|
2012-06-20 11:48:31 +08:00
|
|
|
// purge all managed caches
|
2013-07-25 22:38:55 +08:00
|
|
|
DrawPrimitives::free();
|
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();
|
|
|
|
Configuration::destroyInstance();
|
2012-05-23 12:11:53 +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();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-07 18:52:36 +08:00
|
|
|
destroyTextureCache();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-02-12 08:41:32 +08:00
|
|
|
static unsigned long prevCalls = 0;
|
|
|
|
static unsigned long prevVerts = 0;
|
|
|
|
|
2013-08-08 16:31:44 +08:00
|
|
|
++_frames;
|
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-02-08 11:37:44 +08:00
|
|
|
if (_accumDt > CC_DIRECTOR_STATS_INTERVAL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-02-08 11:37:44 +08:00
|
|
|
_frameRate = _frames / _accumDt;
|
|
|
|
_frames = 0;
|
|
|
|
_accumDt = 0;
|
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-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-05-15 01:07:09 +08:00
|
|
|
Mat4 identity = Mat4::IDENTITY;
|
2014-02-28 13:43:54 +08:00
|
|
|
|
|
|
|
_drawnVerticesLabel->visit(_renderer, identity, false);
|
|
|
|
_drawnBatchesLabel->visit(_renderer, identity, false);
|
|
|
|
_FPSLabel->visit(_renderer, identity, false);
|
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
|
|
|
{
|
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;
|
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
|
|
|
{
|
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;
|
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;
|
2013-05-23 15:11:48 +08:00
|
|
|
|
2014-02-12 06:58:34 +08:00
|
|
|
if (_FPSLabel)
|
2012-06-12 02:50:32 +08:00
|
|
|
{
|
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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Image* image = new 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);
|
|
|
|
_FPSLabel->initWithString("00.0", 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);
|
|
|
|
_drawnBatchesLabel->initWithString("000", 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);
|
|
|
|
_drawnVerticesLabel->initWithString("00000", 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;
|
2012-10-23 10:50:47 +08:00
|
|
|
createStatsLabel();
|
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
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_notificationNode);
|
|
|
|
_notificationNode = node;
|
|
|
|
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
|
|
|
|
|
|
|
Application::getInstance()->setAnimationInterval(_animationInterval);
|
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();
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
void DisplayLinkDirector::setAnimationInterval(double 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
|
|
|
|
|