axmol/cocos/base/CCDirector.cpp

1281 lines
33 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2013 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
2013-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
// standard includes
#include <string>
2014-04-30 08:37:36 +08:00
#include "2d/ccFPSImages.h"
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
2014-05-01 10:09:13 +08:00
#include "2d/CCDrawingPrimitives.h"
#include "2d/CCScene.h"
#include "2d/CCSpriteFrameCache.h"
#include "2d/platform/CCFileUtils.h"
#include "renderer/ccGLStateCache.h"
#include "2d/platform/CCImage.h"
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
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/CCUserDefault.h"
2014-05-10 09:39:25 +08:00
#include "renderer/CCGLProgramCache.h"
#include "renderer/CCGLProgramStateCache.h"
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
2014-05-01 10:09:13 +08:00
#include "2d/CCTransition.h"
#include "2d/CCTextureCache.h"
#include "2d/CCFontFreeType.h"
#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"
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
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 "renderer/CCRenderer.h"
#include "base/CCNS.h"
2014-04-26 13:03:25 +08:00
#include "math/CCMath.h"
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
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
#define CC_DIRECTOR_STATS_POSITION Director::getInstance()->getVisibleOrigin()
2012-08-07 14:29:46 +08:00
#endif
using namespace std;
NS_CC_BEGIN
// XXX it should be a Director ivar. Move it there once support for multiple directors is added
// singleton stuff
2013-08-08 16:31:44 +08:00
static DisplayLinkDirector *s_SharedDirector = nullptr;
#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";
const char *Director::EVENT_AFTER_UPDATE = "director_after_update";
2013-12-21 16:56:28 +08:00
Director* Director::getInstance()
{
if (!s_SharedDirector)
{
s_SharedDirector = new DisplayLinkDirector();
s_SharedDirector->init();
}
return s_SharedDirector;
}
2013-12-21 16:56:28 +08:00
Director::Director()
{
}
bool Director::init(void)
{
setDefaultValues();
// scenes
2013-08-08 16:31:44 +08:00
_runningScene = nullptr;
_nextScene = nullptr;
2013-08-08 16:31:44 +08:00
_notificationNode = nullptr;
_scenesStack.reserve(15);
// FPS
_accumDt = 0.0f;
_frameRate = 0.0f;
_FPSLabel = _drawnBatchesLabel = _drawnVerticesLabel = nullptr;
_totalFrames = _frames = 0;
2013-07-19 13:42:45 +08:00
_lastUpdate = new struct timeval;
// paused ?
_paused = false;
// purge ?
2014-01-04 13:22:09 +08:00
_purgeDirectorInNextLoop = false;
_winSizeInPoints = Size::ZERO;
2013-08-08 16:31:44 +08:00
_openGLView = nullptr;
_contentScaleFactor = 1.0f;
// scheduler
_scheduler = new Scheduler();
// action manager
_actionManager = new ActionManager();
_scheduler->scheduleUpdate(_actionManager, Scheduler::PRIORITY_SYSTEM, false);
_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);
//init TextureCache
initTextureCache();
initMatrixStack();
_renderer = new Renderer;
2014-04-20 01:08:01 +08:00
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
_console = new Console;
#endif
return true;
}
Director::~Director(void)
{
CCLOGINFO("deallocing Director: %p", this);
CC_SAFE_RELEASE(_FPSLabel);
CC_SAFE_RELEASE(_drawnVerticesLabel);
CC_SAFE_RELEASE(_drawnBatchesLabel);
CC_SAFE_RELEASE(_runningScene);
CC_SAFE_RELEASE(_notificationNode);
CC_SAFE_RELEASE(_scheduler);
CC_SAFE_RELEASE(_actionManager);
2013-12-21 16:56:28 +08:00
delete _eventAfterUpdate;
delete _eventAfterDraw;
delete _eventAfterVisit;
delete _eventProjectionChanged;
delete _renderer;
2014-04-20 01:08:01 +08:00
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
delete _console;
#endif
CC_SAFE_RELEASE(_eventDispatcher);
// clean auto release pool
PoolManager::destroyInstance();
// delete _lastUpdate
CC_SAFE_DELETE(_lastUpdate);
2013-08-08 16:31:44 +08:00
s_SharedDirector = nullptr;
}
void Director::setDefaultValues(void)
{
2014-04-28 09:51:10 +08:00
Configuration *conf = Configuration::getInstance();
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;
2014-04-28 09:51:10 +08:00
// Display FPS
_displayStats = conf->getValue("cocos2d.x.display_fps", Value(false)).asBool();
2014-04-28 09:51:10 +08:00
// GL projection
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
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-08-08 16:31:44 +08:00
void Director::setGLDefaultValues()
{
// This method SHOULD be called only after openGLView_ was initialized
CCASSERT(_openGLView, "opengl view should not be null");
setAlphaBlending(true);
// XXX: Fix me, should enable/disable depth test according the depth format as cocos2d-iphone did
// [self setDepthTest: view_.depthFormat];
setDepthTest(false);
setProjection(_projection);
// set other opengl default values
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
// Draw the Scene
2013-08-08 16:31:44 +08:00
void Director::drawScene()
{
// calculate "global" dt
calculateDeltaTime();
// skip one flame when _deltaTime equal to zero.
if(_deltaTime < FLT_EPSILON)
{
return;
}
if (_openGLView)
{
_openGLView->pollInputEvents();
}
//tick before glClear: issue #533
if (! _paused)
{
_scheduler->update(_deltaTime);
2013-12-21 16:56:28 +08:00
_eventDispatcher->dispatchEvent(_eventAfterUpdate);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* to avoid flickr, nextScene MUST be here: after tick and before draw.
XXX: Which bug is this one. It seems that it can't be reproduced with v0.9 */
if (_nextScene)
{
setNextScene();
}
pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
// draw the scene
if (_runningScene)
{
_runningScene->visit(_renderer, Matrix::IDENTITY, false);
2013-12-21 16:56:28 +08:00
_eventDispatcher->dispatchEvent(_eventAfterVisit);
}
// draw the notifications node
if (_notificationNode)
{
_notificationNode->visit(_renderer, Matrix::IDENTITY, false);
}
if (_displayStats)
{
showStats();
}
_renderer->render();
2013-12-21 16:56:28 +08:00
_eventDispatcher->dispatchEvent(_eventAfterDraw);
popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
_totalFrames++;
// swap buffers
if (_openGLView)
{
_openGLView->swapBuffers();
}
if (_displayStats)
{
calculateMPF();
}
}
2013-08-08 16:31:44 +08:00
void Director::calculateDeltaTime()
{
2013-07-19 13:42:45 +08:00
struct timeval now;
2013-08-08 16:31:44 +08:00
if (gettimeofday(&now, nullptr) != 0)
{
CCLOG("error in gettimeofday");
_deltaTime = 0;
return;
}
// new delta time. Re-fixed issue #1277
if (_nextDeltaTimeZero)
{
_deltaTime = 0;
_nextDeltaTimeZero = false;
}
else
{
_deltaTime = (now.tv_sec - _lastUpdate->tv_sec) + (now.tv_usec - _lastUpdate->tv_usec) / 1000000.0f;
_deltaTime = MAX(0, _deltaTime);
}
#if COCOS2D_DEBUG
// If we are debugging our code, prevent big delta time
2013-08-08 16:31:44 +08:00
if (_deltaTime > 0.2f)
{
_deltaTime = 1 / 60.0f;
}
#endif
*_lastUpdate = now;
}
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)
{
2013-11-16 21:08:00 +08:00
CCASSERT(openGLView, "opengl view should not be null");
2013-11-16 21:08:00 +08:00
if (_openGLView != openGLView)
{
2014-04-28 09:51:10 +08:00
// Configuration. Gather GPU info
Configuration *conf = Configuration::getInstance();
conf->gatherGPUInfo();
CCLOG("%s\n",conf->getInfo().c_str());
if(_openGLView)
_openGLView->release();
2013-11-16 21:08:00 +08:00
_openGLView = openGLView;
_openGLView->retain();
// set size
_winSizeInPoints = _openGLView->getDesignResolutionSize();
createStatsLabel();
if (_openGLView)
{
setGLDefaultValues();
}
_renderer->initGLView();
CHECK_GL_ERROR_DEBUG();
2014-04-08 15:11:04 +08:00
if (_eventDispatcher)
{
_eventDispatcher->setEnabled(true);
}
}
}
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);
}
}
void Director::setViewport()
{
if (_openGLView)
{
_openGLView->setViewPortInPoints(0, 0, _winSizeInPoints.width, _winSizeInPoints.height);
}
}
void Director::setNextDeltaTimeZero(bool nextDeltaTimeZero)
{
_nextDeltaTimeZero = nextDeltaTimeZero;
}
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();
}
_modelViewMatrixStack.push(Matrix::IDENTITY);
_projectionMatrixStack.push(Matrix::IDENTITY);
_textureMatrixStack.push(Matrix::IDENTITY);
}
2014-04-03 17:40:14 +08:00
void Director::resetMatrixStack()
{
initMatrixStack();
}
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)
{
_modelViewMatrixStack.top() = Matrix::IDENTITY;
2014-04-02 16:47:23 +08:00
}
else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
{
_projectionMatrixStack.top() = Matrix::IDENTITY;
2014-04-02 16:47:23 +08:00
}
else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
{
_textureMatrixStack.top() = Matrix::IDENTITY;
2014-04-02 16:47:23 +08:00
}
else
{
CCASSERT(false, "unknow matrix stack type");
}
}
2014-04-04 17:58:37 +08:00
void Director::loadMatrix(MATRIX_STACK_TYPE type, const Matrix& mat)
{
if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
{
2014-04-04 17:58:37 +08:00
_modelViewMatrixStack.top() = mat;
}
else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
{
2014-04-04 17:58:37 +08:00
_projectionMatrixStack.top() = mat;
}
else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
{
2014-04-04 17:58:37 +08:00
_textureMatrixStack.top() = mat;
}
else
{
CCASSERT(false, "unknow matrix stack type");
}
}
2014-04-04 17:58:37 +08:00
void Director::multiplyMatrix(MATRIX_STACK_TYPE type, const Matrix& mat)
{
if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
{
2014-04-04 17:58:37 +08:00
_modelViewMatrixStack.top() *= mat;
}
else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
{
2014-04-04 17:58:37 +08:00
_projectionMatrixStack.top() *= mat;
}
else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
{
2014-04-04 17:58:37 +08:00
_textureMatrixStack.top() *= mat;
}
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");
}
}
Matrix Director::getMatrix(MATRIX_STACK_TYPE type)
{
2014-04-04 17:16:57 +08:00
Matrix result;
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");
// }
return result;
}
void Director::setProjection(Projection projection)
{
Size size = _winSizeInPoints;
setViewport();
switch (projection)
{
case Projection::_2D:
2014-04-10 17:36:47 +08:00
{
loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
#if CC_TARGET_PLATFORM == CC_PLATFORM_WP8
if(getOpenGLView() != nullptr)
{
multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, getOpenGLView()->getOrientationMatrix());
}
#endif
2014-04-10 17:36:47 +08:00
Matrix orthoMatrix;
Matrix::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);
loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
break;
2014-04-10 17:36:47 +08:00
}
case Projection::_3D:
{
float zeye = this->getZEye();
2014-04-10 17:36:47 +08:00
Matrix matrixPerspective, matrixLookup;
loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
#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)
{
multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, getOpenGLView()->getOrientationMatrix());
}
#endif
// issue #1334
2014-04-10 17:36:47 +08:00
Matrix::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);
2014-04-10 17:36:47 +08:00
Vector3 eye(size.width/2, size.height/2, zeye), center(size.width/2, size.height/2, 0.0f), up(0.0f, 1.0f, 0.0f);
Matrix::createLookAt(eye, center, up, &matrixLookup);
2014-04-04 18:04:29 +08:00
multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, matrixLookup);
loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
break;
}
case Projection::CUSTOM:
// Projection Delegate is no longer needed
// since the event "PROJECTION CHANGED" is emitted
break;
default:
CCLOG("cocos2d: Director: unrecognized projection");
break;
}
_projection = projection;
GL::setProjectionMatrixDirty();
2013-12-21 16:56:28 +08:00
_eventDispatcher->dispatchEvent(_eventProjectionChanged);
}
void Director::purgeCachedData(void)
{
FontFNT::purgeCachedData();
FontAtlasCache::purgeCachedData();
if (s_SharedDirector->getOpenGLView())
{
SpriteFrameCache::getInstance()->removeUnusedSpriteFrames();
_textureCache->removeUnusedTextures();
// 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());
}
FileUtils::getInstance()->purgeCachedEntries();
}
float Director::getZEye(void) const
{
return (_winSizeInPoints.height / 1.1566f);
}
2013-08-08 16:31:44 +08:00
void Director::setAlphaBlending(bool on)
{
2013-08-08 16:31:44 +08:00
if (on)
{
GL::blendFunc(CC_BLEND_SRC, CC_BLEND_DST);
}
else
{
GL::blendFunc(GL_ONE, GL_ZERO);
}
CHECK_GL_ERROR_DEBUG();
}
2013-08-08 16:31:44 +08:00
void Director::setDepthTest(bool on)
{
2013-08-08 16:31:44 +08:00
if (on)
{
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-04-10 17:36:47 +08:00
static void GLToClipTransform(Matrix *transformOut)
{
2014-04-10 17:36:47 +08:00
if(nullptr == transformOut) return;
Director* director = Director::getInstance();
CCASSERT(nullptr != director, "Director is null when seting matrix stack");
2014-04-28 09:51:10 +08:00
Matrix projection;
2014-04-04 18:13:10 +08:00
projection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
#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
projection = Director::getInstance()->getOpenGLView()->getReverseOrientationMatrix() * projection;
#endif
2014-04-28 09:51:10 +08:00
Matrix 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;
}
2014-04-15 14:18:40 +08:00
Vector2 Director::convertToGL(const Vector2& uiPoint)
{
2014-04-10 17:36:47 +08:00
Matrix transform;
2014-04-28 09:51:10 +08:00
GLToClipTransform(&transform);
Matrix transformInv = transform.getInversed();
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];
Size glSize = _openGLView->getDesignResolutionSize();
2014-04-28 09:51:10 +08:00
Vector4 clipCoord(2.0f*uiPoint.x/glSize.width - 1.0f, 1.0f - 2.0f*uiPoint.y/glSize.height, zClip, 1);
2014-04-28 09:51:10 +08:00
Vector4 glCoord;
2014-04-10 17:36:47 +08:00
//transformInv.transformPoint(clipCoord, &glCoord);
transformInv.transformVector(clipCoord, &glCoord);
float factor = 1.0/glCoord.w;
2014-04-28 09:51:10 +08:00
return Vector2(glCoord.x * factor, glCoord.y * factor);
}
2014-04-15 14:18:40 +08:00
Vector2 Director::convertToUI(const Vector2& glPoint)
{
2014-04-10 17:36:47 +08:00
Matrix transform;
2014-04-28 09:51:10 +08:00
GLToClipTransform(&transform);
2014-04-28 09:51:10 +08:00
Vector4 clipCoord;
// Need to calculate the zero depth from the transform.
Vector4 glCoord(glPoint.x, glPoint.y, 0.0, 1);
2014-04-10 17:36:47 +08:00
transform.transformVector(glCoord, &clipCoord);
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-04-28 09:51:10 +08:00
return Vector2(glSize.width*(clipCoord.x*0.5 + 0.5) * factor, glSize.height*(-clipCoord.y*0.5 + 0.5) * factor);
}
const Size& Director::getWinSize(void) const
{
return _winSizeInPoints;
}
Size Director::getWinSizeInPixels() const
{
return Size(_winSizeInPoints.width * _contentScaleFactor, _winSizeInPoints.height * _contentScaleFactor);
}
Size Director::getVisibleSize() const
{
if (_openGLView)
{
return _openGLView->getVisibleSize();
}
else
{
return Size::ZERO;
}
}
2014-04-15 14:18:40 +08:00
Vector2 Director::getVisibleOrigin() const
{
if (_openGLView)
{
return _openGLView->getVisibleOrigin();
}
else
{
2014-04-15 14:18:40 +08:00
return Vector2::ZERO;
}
}
// scene management
2013-08-08 16:31:44 +08:00
void Director::runWithScene(Scene *scene)
{
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");
2013-08-08 16:31:44 +08:00
pushScene(scene);
startAnimation();
}
2013-08-08 16:31:44 +08:00
void Director::replaceScene(Scene *scene)
{
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;
if (_nextScene)
{
if (_nextScene->isRunning())
{
_nextScene->onExit();
}
_nextScene->cleanup();
_nextScene = nullptr;
}
2013-12-12 12:07:20 +08:00
ssize_t index = _scenesStack.size();
_sendCleanupToScene = true;
_scenesStack.replace(index - 1, scene);
2013-08-08 16:31:44 +08:00
_nextScene = scene;
}
2013-08-08 16:31:44 +08:00
void Director::pushScene(Scene *scene)
{
2013-08-08 16:31:44 +08:00
CCASSERT(scene, "the scene should not null");
_sendCleanupToScene = false;
_scenesStack.pushBack(scene);
2013-08-08 16:31:44 +08:00
_nextScene = scene;
}
void Director::popScene(void)
{
2013-08-08 16:31:44 +08:00
CCASSERT(_runningScene != nullptr, "running scene should not null");
_scenesStack.popBack();
2013-12-12 12:07:20 +08:00
ssize_t c = _scenesStack.size();
if (c == 0)
{
end();
}
else
{
_sendCleanupToScene = true;
_nextScene = _scenesStack.at(c - 1);
}
}
void Director::popToRootScene(void)
{
popToSceneStackLevel(1);
}
void Director::popToSceneStackLevel(int level)
{
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
// level 0? -> end
if (level == 0)
2012-06-12 02:50:32 +08:00
{
end();
return;
}
// current level or lower -> nothing
if (level >= c)
return;
2014-05-15 04:28:47 +08:00
auto currentRunningScene = _scenesStack.back();
if (currentRunningScene == _runningScene)
{
_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
{
auto current = _scenesStack.back();
2014-04-28 09:51:10 +08:00
if (current->isRunning())
2012-06-12 02:50:32 +08:00
{
current->onExit();
2014-04-28 09:51:10 +08:00
}
current->cleanup();
_scenesStack.popBack();
2014-04-28 09:51:10 +08:00
--c;
}
_nextScene = _scenesStack.back();
2014-05-15 04:28:47 +08:00
_sendCleanupToScene = true;
}
void Director::end()
{
2014-01-04 13:22:09 +08:00
_purgeDirectorInNextLoop = true;
}
void Director::purgeDirector()
{
// cleanup scheduler
2012-11-14 18:05:15 +08:00
getScheduler()->unscheduleAll();
// Disable event dispatching
2014-04-08 15:11:04 +08:00
if (_eventDispatcher)
{
_eventDispatcher->setEnabled(false);
}
if (_runningScene)
{
_runningScene->onExit();
_runningScene->cleanup();
_runningScene->release();
}
2013-08-08 16:31:44 +08:00
_runningScene = nullptr;
_nextScene = nullptr;
// remove all objects, but don't release it.
// runWithScene might be executed after 'end'.
_scenesStack.clear();
stopAnimation();
CC_SAFE_RELEASE_NULL(_FPSLabel);
CC_SAFE_RELEASE_NULL(_drawnBatchesLabel);
CC_SAFE_RELEASE_NULL(_drawnVerticesLabel);
// purge bitmap cache
FontFNT::purgeCachedData();
FontFreeType::shutdownFreeType();
// purge all managed caches
DrawPrimitives::free();
AnimationCache::destroyInstance();
SpriteFrameCache::destroyInstance();
2014-05-10 09:39:25 +08:00
GLProgramCache::destroyInstance();
GLProgramStateCache::destroyInstance();
FileUtils::destroyInstance();
Configuration::destroyInstance();
// cocos2d-x specific data structures
UserDefault::destroyInstance();
GL::invalidateStateCache();
destroyTextureCache();
CHECK_GL_ERROR_DEBUG();
// OpenGL view
2013-09-18 23:52:09 +08:00
if (_openGLView)
{
_openGLView->end();
_openGLView = nullptr;
}
// delete Director
release();
}
2013-08-08 16:31:44 +08:00
void Director::setNextScene()
{
2013-08-08 16:31:44 +08:00
bool runningIsTransition = dynamic_cast<TransitionScene*>(_runningScene) != nullptr;
bool newIsTransition = dynamic_cast<TransitionScene*>(_nextScene) != nullptr;
// If it is not a transition, call onExit/cleanup
if (! newIsTransition)
{
if (_runningScene)
{
_runningScene->onExitTransitionDidStart();
_runningScene->onExit();
}
// issue #709. the root node (scene) should receive the cleanup message too
// otherwise it might be leaked.
if (_sendCleanupToScene && _runningScene)
{
_runningScene->cleanup();
}
}
if (_runningScene)
{
_runningScene->release();
}
_runningScene = _nextScene;
_nextScene->retain();
2013-08-08 16:31:44 +08:00
_nextScene = nullptr;
if ((! runningIsTransition) && _runningScene)
{
_runningScene->onEnter();
_runningScene->onEnterTransitionDidFinish();
}
}
2013-08-08 16:31:44 +08:00
void Director::pause()
{
if (_paused)
{
return;
}
_oldAnimationInterval = _animationInterval;
// when paused, don't consume CPU
setAnimationInterval(1 / 4.0);
_paused = true;
}
2013-08-08 16:31:44 +08:00
void Director::resume()
{
if (! _paused)
{
return;
}
setAnimationInterval(_oldAnimationInterval);
_paused = false;
_deltaTime = 0;
// fix issue #3509, skip one fps to avoid incorrect time calculation.
setNextDeltaTimeZero(true);
}
// display the FPS using a LabelAtlas
// updates the FPS every frame
2013-08-08 16:31:44 +08:00
void Director::showStats()
{
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;
_accumDt += _deltaTime;
if (_displayStats && _FPSLabel && _drawnBatchesLabel && _drawnVerticesLabel)
{
2014-02-12 08:41:32 +08:00
char buffer[30];
if (_accumDt > CC_DIRECTOR_STATS_INTERVAL)
{
_frameRate = _frames / _accumDt;
_frames = 0;
_accumDt = 0;
sprintf(buffer, "%.1f / %.3f", _frameRate, _secondsPerFrame);
_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);
_drawnBatchesLabel->setString(buffer);
2014-02-12 08:41:32 +08:00
prevCalls = currentCalls;
}
2014-02-12 08:41:32 +08:00
if( currentVerts != prevVerts) {
sprintf(buffer, "GL verts:%6lu", currentVerts);
_drawnVerticesLabel->setString(buffer);
2014-02-12 08:41:32 +08:00
prevVerts = currentVerts;
}
Matrix identity = Matrix::IDENTITY;
_drawnVerticesLabel->visit(_renderer, identity, false);
_drawnBatchesLabel->visit(_renderer, identity, false);
_FPSLabel->visit(_renderer, identity, false);
}
}
void Director::calculateMPF()
{
2013-07-19 13:42:45 +08:00
struct timeval now;
2013-08-08 16:31:44 +08:00
gettimeofday(&now, nullptr);
_secondsPerFrame = (now.tv_sec - _lastUpdate->tv_sec) + (now.tv_usec - _lastUpdate->tv_usec) / 1000000.0f;
}
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
*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
}
void Director::createStatsLabel()
{
2013-08-08 16:31:44 +08:00
Texture2D *texture = nullptr;
if (_FPSLabel)
2012-06-12 02:50:32 +08:00
{
CC_SAFE_RELEASE_NULL(_FPSLabel);
CC_SAFE_RELEASE_NULL(_drawnBatchesLabel);
CC_SAFE_RELEASE_NULL(_drawnVerticesLabel);
_textureCache->removeTextureForKey("/cc_fps_images");
FileUtils::getInstance()->purgeCachedEntries();
2012-06-12 02:50:32 +08:00
}
Texture2D::PixelFormat currentFormat = Texture2D::getDefaultAlphaPixelFormat();
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);
Image* image = new Image();
2013-08-08 16:31:44 +08:00
bool isOK = image->initWithImageData(data, dataLength);
if (! isOK) {
CCLOGERROR("%s", "Fails: init fps_images");
return;
}
texture = _textureCache->addImage(image, "/cc_fps_images");
CC_SAFE_RELEASE(image);
/*
We want to use an image which is stored in the file named ccFPSImage.c
for any design resolutions and all resource resolutions.
To achieve this, we need to ignore 'contentScaleFactor' in 'AtlasNode' and 'LabelAtlas'.
So I added a new method called 'setIgnoreContentScaleFactor' for 'AtlasNode',
this is not exposed to game developers, it's only used for displaying FPS now.
*/
float scaleFactor = 1 / CC_CONTENT_SCALE_FACTOR();
_FPSLabel = LabelAtlas::create();
_FPSLabel->retain();
_FPSLabel->setIgnoreContentScaleFactor(true);
_FPSLabel->initWithString("00.0", texture, 12, 32 , '.');
_FPSLabel->setScale(scaleFactor);
_drawnBatchesLabel = LabelAtlas::create();
_drawnBatchesLabel->retain();
_drawnBatchesLabel->setIgnoreContentScaleFactor(true);
_drawnBatchesLabel->initWithString("000", texture, 12, 32, '.');
_drawnBatchesLabel->setScale(scaleFactor);
_drawnVerticesLabel = LabelAtlas::create();
_drawnVerticesLabel->retain();
_drawnVerticesLabel->setIgnoreContentScaleFactor(true);
_drawnVerticesLabel->initWithString("00000", texture, 12, 32, '.');
_drawnVerticesLabel->setScale(scaleFactor);
Texture2D::setDefaultAlphaPixelFormat(currentFormat);
const int height_spacing = 22 / CC_CONTENT_SCALE_FACTOR();
_drawnVerticesLabel->setPosition(Vector2(0, height_spacing*2) + CC_DIRECTOR_STATS_POSITION);
_drawnBatchesLabel->setPosition(Vector2(0, height_spacing*1) + CC_DIRECTOR_STATS_POSITION);
_FPSLabel->setPosition(Vector2(0, height_spacing*0)+CC_DIRECTOR_STATS_POSITION);
}
void Director::setContentScaleFactor(float scaleFactor)
{
if (scaleFactor != _contentScaleFactor)
{
_contentScaleFactor = scaleFactor;
createStatsLabel();
}
}
void Director::setNotificationNode(Node *node)
{
CC_SAFE_RELEASE(_notificationNode);
_notificationNode = node;
CC_SAFE_RETAIN(_notificationNode);
}
2013-08-08 16:31:44 +08:00
void Director::setScheduler(Scheduler* scheduler)
{
2013-08-08 16:31:44 +08:00
if (_scheduler != scheduler)
{
2013-08-08 16:31:44 +08:00
CC_SAFE_RETAIN(scheduler);
CC_SAFE_RELEASE(_scheduler);
2013-08-08 16:31:44 +08:00
_scheduler = scheduler;
}
}
2013-08-08 16:31:44 +08:00
void Director::setActionManager(ActionManager* actionManager)
{
2013-08-08 16:31:44 +08:00
if (_actionManager != actionManager)
{
2013-08-08 16:31:44 +08:00
CC_SAFE_RETAIN(actionManager);
CC_SAFE_RELEASE(_actionManager);
2013-08-08 16:31:44 +08:00
_actionManager = actionManager;
}
}
void Director::setEventDispatcher(EventDispatcher* dispatcher)
{
if (_eventDispatcher != dispatcher)
{
CC_SAFE_RETAIN(dispatcher);
CC_SAFE_RELEASE(_eventDispatcher);
_eventDispatcher = dispatcher;
}
}
/***************************************************
* implementation of DisplayLinkDirector
**************************************************/
// should we implement 4 types of director ??
// I think DisplayLinkDirector is enough
// so we now only support DisplayLinkDirector
2013-08-08 16:31:44 +08:00
void DisplayLinkDirector::startAnimation()
{
2013-08-08 16:31:44 +08:00
if (gettimeofday(_lastUpdate, nullptr) != 0)
{
CCLOG("cocos2d: DisplayLinkDirector: Error on gettimeofday");
}
_invalid = false;
2013-12-27 11:49:54 +08:00
Application::getInstance()->setAnimationInterval(_animationInterval);
// fix issue #3509, skip one fps to avoid incorrect time calculation.
setNextDeltaTimeZero(true);
}
2013-08-08 16:31:44 +08:00
void DisplayLinkDirector::mainLoop()
{
2014-01-04 13:22:09 +08:00
if (_purgeDirectorInNextLoop)
{
2014-01-04 13:22:09 +08:00
_purgeDirectorInNextLoop = false;
purgeDirector();
}
else if (! _invalid)
2013-08-08 16:31:44 +08:00
{
drawScene();
2013-08-08 16:31:44 +08:00
// release the objects
PoolManager::getInstance()->getCurrentPool()->clear();
2013-08-08 16:31:44 +08:00
}
}
2013-08-08 16:31:44 +08:00
void DisplayLinkDirector::stopAnimation()
{
_invalid = true;
}
2013-11-16 21:08:00 +08:00
void DisplayLinkDirector::setAnimationInterval(double interval)
{
2013-11-16 21:08:00 +08:00
_animationInterval = interval;
if (! _invalid)
{
stopAnimation();
startAnimation();
}
}
NS_CC_END