123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608 |
- /****************************************************************************
- Copyright (c) 2008-2010 Ricardo Quesada
- Copyright (c) 2010-2013 cocos2d-x.org
- Copyright (c) 2011 Zynga Inc.
- Copyright (c) 2013-2017 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.
- ****************************************************************************/
- // cocos2d includes
- #include "base/CCDirector.h"
- // standard includes
- #include <string>
- #include "2d/CCDrawingPrimitives.h"
- #include "2d/CCSpriteFrameCache.h"
- #include "platform/CCFileUtils.h"
- #include "2d/CCActionManager.h"
- #include "2d/CCFontFNT.h"
- #include "2d/CCFontAtlasCache.h"
- #include "2d/CCAnimationCache.h"
- #include "2d/CCTransition.h"
- #include "2d/CCFontFreeType.h"
- #include "2d/CCLabelAtlas.h"
- #include "renderer/CCGLProgramCache.h"
- #include "renderer/CCGLProgramStateCache.h"
- #include "renderer/CCTextureCache.h"
- #include "renderer/ccGLStateCache.h"
- #include "renderer/CCRenderer.h"
- #include "renderer/CCRenderState.h"
- #include "renderer/CCFrameBuffer.h"
- #include "2d/CCCamera.h"
- #include "base/CCUserDefault.h"
- #include "base/ccFPSImages.h"
- #include "base/CCScheduler.h"
- #include "base/ccMacros.h"
- #include "base/CCEventDispatcher.h"
- #include "base/CCEventCustom.h"
- #include "base/CCConsole.h"
- #include "base/CCAutoreleasePool.h"
- #include "base/CCConfiguration.h"
- #include "base/CCAsyncTaskPool.h"
- #include "platform/CCApplication.h"
- #include "CCProfiling.h"
- #if CC_ENABLE_SCRIPT_BINDING
- #include "base/CCScriptSupport.h"
- #endif
- #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
- #include "platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxEngineDataManager.h"
- #endif
- /**
- Position of the FPS
-
- Default: 0,0 (bottom-left corner)
- */
- #ifndef CC_DIRECTOR_STATS_POSITION
- #define CC_DIRECTOR_STATS_POSITION Director::getInstance()->getVisibleOrigin()
- #endif // CC_DIRECTOR_STATS_POSITION
- using namespace std;
- NS_CC_BEGIN
- // FIXME: it should be a Director ivar. Move it there once support for multiple directors is added
- // singleton stuff
- static Director *s_SharedDirector = nullptr;
- // cpu,gpu 时间统计的现实间隔
- #define CPU_GPU_STAT_INTERVAL 0.2
- // 如果低于这个帧率,则用红色显示统计
- #define CPU_GPU_MIN_TIME_ECLAPSED (1.0/45)
- #define kDefaultFPS 60 // 60 frames per second
- extern const char* cocos2dVersion(void);
- const char *Director::EVENT_BEFORE_SET_NEXT_SCENE = "director_before_set_next_scene";
- const char *Director::EVENT_AFTER_SET_NEXT_SCENE = "director_after_set_next_scene";
- 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_BEFORE_UPDATE = "director_before_update";
- const char *Director::EVENT_AFTER_UPDATE = "director_after_update";
- const char *Director::EVENT_RESET = "director_reset";
- const char *Director::EVENT_BEFORE_DRAW = "director_before_draw";
- Director* Director::getInstance()
- {
- if (!s_SharedDirector)
- {
- s_SharedDirector = new (std::nothrow) Director;
- CCASSERT(s_SharedDirector, "FATAL: Not enough memory");
- s_SharedDirector->init();
- }
- return s_SharedDirector;
- }
- Director::Director()
- : _isStatusLabelUpdated(true)
- , _invalid(true)
- , _deltaTimePassedByCaller(false)
- {
- }
- bool Director::init(void)
- {
- setDefaultValues();
- // scenes
- _runningScene = nullptr;
- _nextScene = nullptr;
- _notificationNode = nullptr;
- _scenesStack.reserve(15);
- // FPS
- _accumDt = 0.0f;
- _frameRate = 0.0f;
- _accumDt4CpuGpu = 0;
- _cpuTLabel = _gpuTLabel = _FPSLabel = _drawnBatchesLabel = _drawnVerticesLabel = nullptr;
- _totalFrames = 0;
- _cpuOverload = false;
- _gpuOverload = false;
- _lastUpdate = std::chrono::steady_clock::now();
- _msSleeped = 0;
-
- _secondsPerFrame = 1.0f;
- _frames = 0;
- // paused ?
- _paused = false;
- // purge ?
- _purgeDirectorInNextLoop = false;
-
- // restart ?
- _restartDirectorInNextLoop = false;
-
- // invalid ?
- _invalid = false;
- _winSizeInPoints = Size::ZERO;
- _openGLView = nullptr;
- _defaultFBO = nullptr;
-
- _contentScaleFactor = 1.0f;
- _console = new (std::nothrow) Console;
- // scheduler
- _scheduler = new (std::nothrow) Scheduler();
- // action manager
- _actionManager = new (std::nothrow) ActionManager();
- _scheduler->scheduleUpdate(_actionManager, Scheduler::PRIORITY_SYSTEM, false);
- _eventDispatcher = new (std::nothrow) EventDispatcher();
-
- _beforeSetNextScene = new (std::nothrow) EventCustom(EVENT_BEFORE_SET_NEXT_SCENE);
- _beforeSetNextScene->setUserData(this);
- _afterSetNextScene = new (std::nothrow) EventCustom(EVENT_AFTER_SET_NEXT_SCENE);
- _afterSetNextScene->setUserData(this);
- _eventAfterDraw = new (std::nothrow) EventCustom(EVENT_AFTER_DRAW);
- _eventAfterDraw->setUserData(this);
- _eventBeforeDraw = new (std::nothrow) EventCustom(EVENT_BEFORE_DRAW);
- _eventBeforeDraw->setUserData(this);
- _eventAfterVisit = new (std::nothrow) EventCustom(EVENT_AFTER_VISIT);
- _eventAfterVisit->setUserData(this);
- _eventBeforeUpdate = new (std::nothrow) EventCustom(EVENT_BEFORE_UPDATE);
- _eventBeforeUpdate->setUserData(this);
- _eventAfterUpdate = new (std::nothrow) EventCustom(EVENT_AFTER_UPDATE);
- _eventAfterUpdate->setUserData(this);
- _eventProjectionChanged = new (std::nothrow) EventCustom(EVENT_PROJECTION_CHANGED);
- _eventProjectionChanged->setUserData(this);
- _eventResetDirector = new (std::nothrow) EventCustom(EVENT_RESET);
- //init TextureCache
- initTextureCache();
- initMatrixStack();
- _renderer = new (std::nothrow) Renderer;
- RenderState::initialize();
- #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
- EngineDataManager::init();
- #endif
- return true;
- }
- Director::~Director(void)
- {
- CCLOGINFO("deallocing Director: %p", this);
- CC_SAFE_RELEASE(_cpuTLabel);
- CC_SAFE_RELEASE(_gpuTLabel);
- 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);
- CC_SAFE_DELETE(_defaultFBO);
- CC_SAFE_RELEASE(_beforeSetNextScene);
- CC_SAFE_RELEASE(_afterSetNextScene);
- CC_SAFE_RELEASE(_eventBeforeUpdate);
- CC_SAFE_RELEASE(_eventAfterUpdate);
- CC_SAFE_RELEASE(_eventAfterDraw);
- CC_SAFE_RELEASE(_eventBeforeDraw);
- CC_SAFE_RELEASE(_eventAfterVisit);
- CC_SAFE_RELEASE(_eventProjectionChanged);
- CC_SAFE_RELEASE(_eventResetDirector);
- delete _renderer;
- delete _console;
- CC_SAFE_RELEASE(_eventDispatcher);
-
- Configuration::destroyInstance();
- s_SharedDirector = nullptr;
- }
- void Director::setDefaultValues(void)
- {
- Configuration *conf = Configuration::getInstance();
- // default FPS
- double fps = conf->getValue("cocos2d.x.fps", Value(kDefaultFPS)).asDouble();
- _oldAnimationInterval = _animationInterval = 1.0 / fps;
- // Display FPS
- _displayStats = conf->getValue("cocos2d.x.display_fps", Value(false)).asBool();
- // GL projection
- std::string projection = conf->getValue("cocos2d.x.gl.projection", Value("3d")).asString();
- 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();
- 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_premultiplied = conf->getValue("cocos2d.x.texture.pvrv2_has_alpha_premultiplied", Value(false)).asBool();
- Image::setPVRImagesHavePremultipliedAlpha(pvr_alpha_premultiplied);
- }
- void Director::setGLDefaultValues()
- {
- // This method SHOULD be called only after openGLView_ was initialized
- CCASSERT(_openGLView, "opengl view should not be null");
- setAlphaBlending(true);
- setDepthTest(false);
- setProjection(_projection);
- }
- // Draw the Scene
- void Director::drawScene()
- {
- auto beginT = chrono::high_resolution_clock::now();
- if (_displayStats) {
- long duration = static_cast<long>(chrono::duration_cast<chrono::microseconds>(beginT - _tLastEnd).count());
- _gpuElapsed = (float)(duration) / 1000.0 - (float)_msSleeped;
- }
-
- // calculate "global" dt
- calculateDeltaTime();
-
- if (_openGLView)
- {
- _openGLView->pollEvents();
- }
- //tick before glClear: issue #533
- if (! _paused)
- {
- _eventDispatcher->dispatchEvent(_eventBeforeUpdate);
- _scheduler->update(_deltaTime);
- _eventDispatcher->dispatchEvent(_eventAfterUpdate);
- }
- _renderer->clear();
- experimental::FrameBuffer::clearAllFBOs();
-
- _eventDispatcher->dispatchEvent(_eventBeforeDraw);
-
- /* to avoid flickr, nextScene MUST be here: after tick and before draw.
- * FIXME: 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);
-
- if (_runningScene)
- {
- #if (CC_USE_PHYSICS || (CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION) || CC_USE_NAVMESH)
- _runningScene->stepPhysicsAndNavigation(_deltaTime);
- #endif
- //clear draw stats
- _renderer->clearDrawStats();
-
- //render the scene
- _openGLView->renderScene(_runningScene, _renderer);
-
- _eventDispatcher->dispatchEvent(_eventAfterVisit);
- }
- // draw the notifications node
- if (_notificationNode)
- {
- _notificationNode->visit(_renderer, Mat4::IDENTITY, 0);
- }
- updateFrameRate();
-
- if (_displayStats)
- {
- #if !CC_STRIP_FPS
- showStats();
- #endif
- }
-
- _renderer->render();
- _eventDispatcher->dispatchEvent(_eventAfterDraw);
- popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
- _totalFrames++;
- if (_displayStats)
- {
- _tLastEnd = chrono::high_resolution_clock::now();
- long duration = static_cast<long>(chrono::duration_cast<chrono::microseconds>(_tLastEnd - beginT).count());
- _cpuElapsed = (float)duration / 1000.0;
-
- #if !CC_STRIP_FPS
- calculateMPF();
- #endif
- }
-
- // swap buffers
- if (_openGLView)
- {
- _openGLView->swapBuffers();
- }
- }
- void Director::calculateDeltaTime()
- {
- // new delta time. Re-fixed issue #1277
- if (_nextDeltaTimeZero)
- {
- _deltaTime = 0;
- _nextDeltaTimeZero = false;
- }
- else
- {
- // delta time may passed by invoke mainLoop(dt)
- if (!_deltaTimePassedByCaller)
- {
- auto now = std::chrono::steady_clock::now();
- _deltaTime = std::chrono::duration_cast<std::chrono::microseconds>(now - _lastUpdate).count() / 1000000.0f;
- _lastUpdate = now;
- }
- _deltaTime = MAX(0, _deltaTime);
- }
- #if COCOS2D_DEBUG
- // If we are debugging our code, prevent big delta time
- if (_deltaTime > 0.2f)
- {
- _deltaTime = 1 / 60.0f;
- }
- #endif
- }
- float Director::getDeltaTime() const
- {
- return _deltaTime;
- }
- void Director::setOpenGLView(GLView *openGLView)
- {
- CCASSERT(openGLView, "opengl view should not be null");
- if (_openGLView != openGLView)
- {
- // Configuration. Gather GPU info
- Configuration *conf = Configuration::getInstance();
- conf->gatherGPUInfo();
- CCLOG("%s\n",conf->getInfo().c_str());
- if(_openGLView)
- _openGLView->release();
- _openGLView = openGLView;
- _openGLView->retain();
- // set size
- _winSizeInPoints = _openGLView->getDesignResolutionSize();
- _isStatusLabelUpdated = true;
- if (_openGLView)
- {
- setGLDefaultValues();
- }
- _renderer->initGLView();
- CHECK_GL_ERROR_DEBUG();
- if (_eventDispatcher)
- {
- _eventDispatcher->setEnabled(true);
- }
-
- _defaultFBO = experimental::FrameBuffer::getOrCreateDefaultFBO(_openGLView);
- _defaultFBO->retain();
- }
- }
- TextureCache* Director::getTextureCache() const
- {
- return _textureCache;
- }
- void Director::initTextureCache()
- {
- _textureCache = new (std::nothrow) TextureCache();
- }
- 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;
- }
- //
- // FIXME TODO
- // Matrix code MUST NOT be part of the Director
- // MUST BE moved outside.
- // Why the Director must have this code ?
- //
- void Director::initMatrixStack()
- {
- while (!_modelViewMatrixStack.empty())
- {
- _modelViewMatrixStack.pop();
- }
- _projectionMatrixStackList.clear();
- while (!_textureMatrixStack.empty())
- {
- _textureMatrixStack.pop();
- }
- _modelViewMatrixStack.push(Mat4::IDENTITY);
- std::stack<Mat4> projectionMatrixStack;
- projectionMatrixStack.push(Mat4::IDENTITY);
- _projectionMatrixStackList.push_back(projectionMatrixStack);
- _textureMatrixStack.push(Mat4::IDENTITY);
- }
- void Director::resetMatrixStack()
- {
- initMatrixStack();
- }
- void Director::initProjectionMatrixStack(size_t stackCount)
- {
- _projectionMatrixStackList.clear();
- std::stack<Mat4> projectionMatrixStack;
- projectionMatrixStack.push(Mat4::IDENTITY);
- for (size_t i = 0; i < stackCount; ++i)
- _projectionMatrixStackList.push_back(projectionMatrixStack);
- }
- size_t Director::getProjectionMatrixStackSize()
- {
- return _projectionMatrixStackList.size();
- }
- 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)
- {
- _projectionMatrixStackList[0].pop();
- }
- else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
- {
- _textureMatrixStack.pop();
- }
- else
- {
- CCASSERT(false, "unknown matrix stack type");
- }
- }
- void Director::popProjectionMatrix(size_t index)
- {
- _projectionMatrixStackList[index].pop();
- }
- void Director::loadIdentityMatrix(MATRIX_STACK_TYPE type)
- {
- if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
- {
- _modelViewMatrixStack.top() = Mat4::IDENTITY;
- }
- else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
- {
- _projectionMatrixStackList[0].top() = Mat4::IDENTITY;
- }
- else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
- {
- _textureMatrixStack.top() = Mat4::IDENTITY;
- }
- else
- {
- CCASSERT(false, "unknown matrix stack type");
- }
- }
- void Director::loadProjectionIdentityMatrix(size_t index)
- {
- _projectionMatrixStackList[index].top() = Mat4::IDENTITY;
- }
- void Director::loadMatrix(MATRIX_STACK_TYPE type, const Mat4& mat)
- {
- if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
- {
- _modelViewMatrixStack.top() = mat;
- }
- else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
- {
- _projectionMatrixStackList[0].top() = mat;
- }
- else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
- {
- _textureMatrixStack.top() = mat;
- }
- else
- {
- CCASSERT(false, "unknown matrix stack type");
- }
- }
- void Director::loadProjectionMatrix(const Mat4& mat, size_t index)
- {
- _projectionMatrixStackList[index].top() = mat;
- }
- void Director::multiplyMatrix(MATRIX_STACK_TYPE type, const Mat4& mat)
- {
- if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
- {
- _modelViewMatrixStack.top() *= mat;
- }
- else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
- {
- _projectionMatrixStackList[0].top() *= mat;
- }
- else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
- {
- _textureMatrixStack.top() *= mat;
- }
- else
- {
- CCASSERT(false, "unknown matrix stack type");
- }
- }
- void Director::multiplyProjectionMatrix(const Mat4& mat, size_t index)
- {
- _projectionMatrixStackList[index].top() *= mat;
- }
- void Director::pushMatrix(MATRIX_STACK_TYPE type)
- {
- if(type == MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW)
- {
- _modelViewMatrixStack.push(_modelViewMatrixStack.top());
- }
- else if(type == MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION)
- {
- _projectionMatrixStackList[0].push(_projectionMatrixStackList[0].top());
- }
- else if(type == MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE)
- {
- _textureMatrixStack.push(_textureMatrixStack.top());
- }
- else
- {
- CCASSERT(false, "unknown matrix stack type");
- }
- }
- void Director::pushProjectionMatrix(size_t index)
- {
- _projectionMatrixStackList[index].push(_projectionMatrixStackList[index].top());
- }
- const Mat4& Director::getMatrix(MATRIX_STACK_TYPE type) const
- {
- if(type == MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW)
- {
- return _modelViewMatrixStack.top();
- }
- else if(type == MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION)
- {
- return _projectionMatrixStackList[0].top();
- }
- else if(type == MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE)
- {
- return _textureMatrixStack.top();
- }
- CCASSERT(false, "unknown matrix stack type, will return modelview matrix instead");
- return _modelViewMatrixStack.top();
- }
- const Mat4& Director::getProjectionMatrix(size_t index) const
- {
- return _projectionMatrixStackList[index].top();
- }
- void Director::setProjection(Projection projection)
- {
- Size size = _winSizeInPoints;
- if (size.width == 0 || size.height == 0)
- {
- CCLOGERROR("cocos2d: warning, Director::setProjection() failed because size is 0");
- return;
- }
- setViewport();
- switch (projection)
- {
- case Projection::_2D:
- {
- Mat4 orthoMatrix;
- Mat4::createOrthographicOffCenter(0, size.width, 0, size.height, -1024, 1024, &orthoMatrix);
- loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, orthoMatrix);
- loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
- break;
- }
-
- case Projection::_3D:
- {
- float zeye = this->getZEye();
- Mat4 matrixPerspective, matrixLookup;
- // issue #1334
- Mat4::createPerspective(60, (GLfloat)size.width/size.height, 10, zeye+size.height/2, &matrixPerspective);
- 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);
- Mat4 proj3d = matrixPerspective * matrixLookup;
- loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, proj3d);
- 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();
- _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.154700538379252f);//(2 * tanf(M_PI/6))
- }
- void Director::setAlphaBlending(bool on)
- {
- if (on)
- {
- GL::blendFunc(CC_BLEND_SRC, CC_BLEND_DST);
- }
- else
- {
- GL::blendFunc(GL_ONE, GL_ZERO);
- }
- CHECK_GL_ERROR_DEBUG();
- }
- void Director::setDepthTest(bool on)
- {
- _renderer->setDepthTest(on);
- }
- void Director::setClearColor(const Color4F& clearColor)
- {
- _renderer->setClearColor(clearColor);
- auto defaultFBO = experimental::FrameBuffer::getOrCreateDefaultFBO(_openGLView);
-
- if(defaultFBO) defaultFBO->setClearColor(clearColor);
- }
- static void GLToClipTransform(Mat4 *transformOut)
- {
- if(nullptr == transformOut) return;
-
- Director* director = Director::getInstance();
- CCASSERT(nullptr != director, "Director is null when setting matrix stack");
- auto projection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
- auto modelview = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
- *transformOut = projection * modelview;
- }
- Vec2 Director::convertToGL(const Vec2& uiPoint)
- {
- Mat4 transform;
- GLToClipTransform(&transform);
- Mat4 transformInv = transform.getInversed();
- // Calculate z=0 using -> transform*[0, 0, 0, 1]/w
- float zClip = transform.m[14]/transform.m[15];
- Size glSize = _openGLView->getDesignResolutionSize();
- Vec4 clipCoord(2.0f*uiPoint.x/glSize.width - 1.0f, 1.0f - 2.0f*uiPoint.y/glSize.height, zClip, 1);
- Vec4 glCoord;
- //transformInv.transformPoint(clipCoord, &glCoord);
- transformInv.transformVector(clipCoord, &glCoord);
- float factor = 1.0f / glCoord.w;
- return Vec2(glCoord.x * factor, glCoord.y * factor);
- }
- Vec2 Director::convertToUI(const Vec2& glPoint)
- {
- Mat4 transform;
- GLToClipTransform(&transform);
- Vec4 clipCoord;
- // Need to calculate the zero depth from the transform.
- Vec4 glCoord(glPoint.x, glPoint.y, 0.0, 1);
- transform.transformVector(glCoord, &clipCoord);
- /*
- BUG-FIX #5506
- a = (Vx, Vy, Vz, 1)
- b = (a×M)T
- Out = 1 ⁄ bw(bx, by, bz)
- */
-
- clipCoord.x = clipCoord.x / clipCoord.w;
- clipCoord.y = clipCoord.y / clipCoord.w;
- clipCoord.z = clipCoord.z / clipCoord.w;
- Size glSize = _openGLView->getDesignResolutionSize();
- float factor = 1.0f / glCoord.w;
- return Vec2(glSize.width * (clipCoord.x * 0.5f + 0.5f) * factor, glSize.height * (-clipCoord.y * 0.5f + 0.5f) * 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;
- }
- }
- Vec2 Director::getVisibleOrigin() const
- {
- if (_openGLView)
- {
- return _openGLView->getVisibleOrigin();
- }
- else
- {
- return Vec2::ZERO;
- }
- }
- // scene management
- void Director::runWithScene(Scene *scene)
- {
- 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");
- pushScene(scene);
- startAnimation();
- }
- void Director::replaceScene(Scene *scene)
- {
- //CCASSERT(_runningScene, "Use runWithScene: instead to start the director");
- CCASSERT(scene != nullptr, "the scene should not be null");
-
- if (_runningScene == nullptr) {
- runWithScene(scene);
- return;
- }
-
- if (scene == _nextScene)
- return;
-
- if (_nextScene)
- {
- if (_nextScene->isRunning())
- {
- _nextScene->onExit();
- }
- _nextScene->cleanup();
- _nextScene = nullptr;
- }
- ssize_t index = _scenesStack.size() - 1;
- _sendCleanupToScene = true;
- #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
- if (sEngine)
- {
- sEngine->retainScriptObject(this, scene);
- sEngine->releaseScriptObject(this, _scenesStack.at(index));
- }
- #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- _scenesStack.replace(index, scene);
- _nextScene = scene;
- }
- void Director::pushScene(Scene *scene)
- {
- CCASSERT(scene, "the scene should not null");
- _sendCleanupToScene = false;
- #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
- if (sEngine)
- {
- sEngine->retainScriptObject(this, scene);
- }
- #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- _scenesStack.pushBack(scene);
- _nextScene = scene;
- }
- void Director::popScene(void)
- {
- CCASSERT(_runningScene != nullptr, "running scene should not null");
-
- #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
- if (sEngine)
- {
- sEngine->releaseScriptObject(this, _scenesStack.back());
- }
- #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- _scenesStack.popBack();
- 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)
- {
- CCASSERT(_runningScene != nullptr, "A running Scene is needed");
- ssize_t c = _scenesStack.size();
- // level 0? -> end
- if (level == 0)
- {
- end();
- return;
- }
- // current level or lower -> nothing
- if (level >= c)
- return;
- auto firstOnStackScene = _scenesStack.back();
- if (firstOnStackScene == _runningScene)
- {
- #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
- if (sEngine)
- {
- sEngine->releaseScriptObject(this, _scenesStack.back());
- }
- #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- _scenesStack.popBack();
- --c;
- }
- // pop stack until reaching desired level
- while (c > level)
- {
- auto current = _scenesStack.back();
- if (current->isRunning())
- {
- current->onExit();
- }
- current->cleanup();
- #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
- if (sEngine)
- {
- sEngine->releaseScriptObject(this, _scenesStack.back());
- }
- #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- _scenesStack.popBack();
- --c;
- }
- _nextScene = _scenesStack.back();
- // cleanup running scene
- _sendCleanupToScene = true;
- }
- void Director::end()
- {
- _purgeDirectorInNextLoop = true;
-
- #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
- exit(0);
- #endif
-
- }
- void Director::restart()
- {
- _restartDirectorInNextLoop = true;
- }
- void Director::reset()
- {
- #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
- #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
-
- if (_runningScene)
- {
- #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- if (sEngine)
- {
- sEngine->releaseScriptObject(this, _runningScene);
- }
- #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- _runningScene->onExit();
- _runningScene->cleanup();
- _runningScene->release();
- }
-
- _runningScene = nullptr;
- _nextScene = nullptr;
- _eventDispatcher->dispatchEvent(_eventResetDirector);
-
- // cleanup scheduler
- getScheduler()->unscheduleAll();
-
- // Remove all events
- if (_eventDispatcher)
- {
- _eventDispatcher->removeAllEventListeners();
- }
-
- if(_notificationNode)
- {
- _notificationNode->onExit();
- _notificationNode->cleanup();
- _notificationNode->release();
- }
-
- _notificationNode = nullptr;
-
- // remove all objects, but don't release it.
- // runWithScene might be executed after 'end'.
- #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- if (sEngine)
- {
- for (const auto &scene : _scenesStack)
- {
- if (scene)
- sEngine->releaseScriptObject(this, scene);
- }
- }
- #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- _scenesStack.clear();
-
- stopAnimation();
-
- CC_SAFE_RELEASE_NULL(_notificationNode);
- CC_SAFE_RELEASE_NULL(_cpuTLabel);
- CC_SAFE_RELEASE_NULL(_gpuTLabel);
- CC_SAFE_RELEASE_NULL(_FPSLabel);
- CC_SAFE_RELEASE_NULL(_drawnBatchesLabel);
- CC_SAFE_RELEASE_NULL(_drawnVerticesLabel);
-
- // purge bitmap cache
- FontFNT::purgeCachedData();
- FontAtlasCache::purgeCachedData();
-
- FontFreeType::shutdownFreeType();
-
- // purge all managed caches
-
- #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
- #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
- #elif _MSC_VER >= 1400 //vs 2005 or higher
- #pragma warning (push)
- #pragma warning (disable: 4996)
- #endif
- //it will crash clang static analyzer so hide it if __clang_analyzer__ defined
- #ifndef __clang_analyzer__
- DrawPrimitives::free();
- #endif
- #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
- #pragma GCC diagnostic warning "-Wdeprecated-declarations"
- #elif _MSC_VER >= 1400 //vs 2005 or higher
- #pragma warning (pop)
- #endif
- AnimationCache::destroyInstance();
- SpriteFrameCache::destroyInstance();
- GLProgramCache::destroyInstance();
- GLProgramStateCache::destroyInstance();
- FileUtils::destroyInstance();
- AsyncTaskPool::destroyInstance();
-
- // cocos2d-x specific data structures
- UserDefault::destroyInstance();
-
- GL::invalidateStateCache();
- RenderState::finalize();
-
- destroyTextureCache();
- }
- void Director::purgeDirector()
- {
- reset();
- CHECK_GL_ERROR_DEBUG();
-
- // OpenGL view
- if (_openGLView)
- {
- _openGLView->end();
- _openGLView = nullptr;
- }
- #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
- EngineDataManager::destroy();
- #endif
- // delete Director
- release();
- }
- void Director::restartDirector()
- {
- reset();
-
- // RenderState need to be reinitialized
- RenderState::initialize();
- // Texture cache need to be reinitialized
- initTextureCache();
-
- // Reschedule for action manager
- getScheduler()->scheduleUpdate(getActionManager(), Scheduler::PRIORITY_SYSTEM, false);
-
- // release the objects
- PoolManager::getInstance()->getCurrentPool()->clear();
- // Restart animation
- startAnimation();
-
- // Real restart in script level
- #if CC_ENABLE_SCRIPT_BINDING
- ScriptEvent scriptEvent(kRestartGame, nullptr);
- ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
- #endif
- }
- void Director::setNextScene()
- {
- _eventDispatcher->dispatchEvent(_beforeSetNextScene);
- 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();
- _nextScene = nullptr;
- if ((! runningIsTransition) && _runningScene)
- {
- _runningScene->onEnter();
- _runningScene->onEnterTransitionDidFinish();
- }
-
- _eventDispatcher->dispatchEvent(_afterSetNextScene);
- }
- void Director::pause()
- {
- if (_paused)
- {
- return;
- }
- _oldAnimationInterval = _animationInterval;
- // when paused, don't consume CPU
- setAnimationInterval(1 / 4.0, SetIntervalReason::BY_DIRECTOR_PAUSE);
- _paused = true;
- }
- void Director::resume()
- {
- if (! _paused)
- {
- return;
- }
- setAnimationInterval(_oldAnimationInterval, SetIntervalReason::BY_ENGINE);
- _paused = false;
- _deltaTime = 0;
- // fix issue #3509, skip one fps to avoid incorrect time calculation.
- setNextDeltaTimeZero(true);
- }
- void Director::updateFrameRate()
- {
- // static const float FPS_FILTER = 0.1f;
- // static float prevDeltaTime = 0.016f; // 60FPS
- //
- // float dt = _deltaTime * FPS_FILTER + (1.0f-FPS_FILTER) * prevDeltaTime;
- // prevDeltaTime = dt;
- // _frameRate = 1.0f/dt;
- // Frame rate should be the real value of current frame.
- _frameRate = 1.0f / _deltaTime;
- }
- #if !CC_STRIP_FPS
- // display the FPS using a LabelAtlas
- // updates the FPS every frame
- void Director::showStats()
- {
- if (_isStatusLabelUpdated)
- {
- createStatsLabel();
- _isStatusLabelUpdated = false;
- }
- static unsigned long prevCalls = 0;
- static unsigned long prevVerts = 0;
- ++_frames;
- _accumDt += _deltaTime;
- _accumDt4CpuGpu += _deltaTime;
-
- if (_displayStats && _FPSLabel && _drawnBatchesLabel && _drawnVerticesLabel)
- {
- char buffer[30] = {0};
-
- // 每隔两秒,更新一下cpu和gpu的时间
- // 或者超出了报警阈值
- if (_accumDt4CpuGpu > CPU_GPU_STAT_INTERVAL) {
- sprintf(buffer, "CPU time: %.2f", _cpuElapsed);
- _cpuTLabel->setString(buffer);
- sprintf(buffer, "GPU time: %.2f", _gpuElapsed);
- _gpuTLabel->setString(buffer);
- _accumDt4CpuGpu = 0;
- bool co = _cpuElapsed > (CPU_GPU_MIN_TIME_ECLAPSED * 1000.0);
- bool go = _gpuElapsed > (CPU_GPU_MIN_TIME_ECLAPSED * 1000.0);
- if ((co != _cpuOverload) || (go != _gpuOverload)) {
- _isStatusLabelUpdated = true;
- }
- _cpuOverload = co;
- _gpuOverload = go;
- }
- // Probably we don't need this anymore since
- // the framerate is using a low-pass filter
- // to make the FPS stable
- if (_accumDt > CC_DIRECTOR_STATS_INTERVAL)
- {
- sprintf(buffer, "%.1f / %.3f", _frames / _accumDt, _secondsPerFrame);
- _FPSLabel->setString(buffer);
- _accumDt = 0;
- _frames = 0;
- }
- auto currentCalls = (unsigned long)_renderer->getDrawnBatches();
- auto currentVerts = (unsigned long)_renderer->getDrawnVertices();
- if( currentCalls != prevCalls ) {
- sprintf(buffer, "GL calls:%6lu", currentCalls);
- _drawnBatchesLabel->setString(buffer);
- prevCalls = currentCalls;
- }
- if( currentVerts != prevVerts) {
- sprintf(buffer, "GL verts:%6lu", currentVerts);
- _drawnVerticesLabel->setString(buffer);
- prevVerts = currentVerts;
- }
- const Mat4& identity = Mat4::IDENTITY;
- _drawnVerticesLabel->visit(_renderer, identity, 0);
- _drawnBatchesLabel->visit(_renderer, identity, 0);
- _FPSLabel->visit(_renderer, identity, 0);
- _cpuTLabel->visit(_renderer, identity, 0);
- _gpuTLabel->visit(_renderer, identity, 0);
- }
- }
- void Director::calculateMPF()
- {
- static float prevSecondsPerFrame = 0;
- static const float MPF_FILTER = 0.10f;
- _secondsPerFrame = _deltaTime * MPF_FILTER + (1-MPF_FILTER) * prevSecondsPerFrame;
- prevSecondsPerFrame = _secondsPerFrame;
- }
- // returns the FPS image data pointer and len
- void Director::getFPSImageData(unsigned char** datapointer, ssize_t* length)
- {
- // FIXME: fixed me if it should be used
- *datapointer = cc_fps_images_png;
- *length = cc_fps_images_len();
- }
- void Director::createStatsLabel()
- {
- Texture2D *texture = nullptr;
- std::string cpuString = "000";
- std::string gpuString = "000";
- std::string fpsString = "00.0";
- std::string drawBatchString = "000";
- std::string drawVerticesString = "00000";
- if (_FPSLabel)
- {
- cpuString = _cpuTLabel->getString();
- gpuString = _gpuTLabel->getString();
- fpsString = _FPSLabel->getString();
- drawBatchString = _drawnBatchesLabel->getString();
- drawVerticesString = _drawnVerticesLabel->getString();
-
- CC_SAFE_RELEASE_NULL(_cpuTLabel);
- CC_SAFE_RELEASE_NULL(_gpuTLabel);
- CC_SAFE_RELEASE_NULL(_FPSLabel);
- CC_SAFE_RELEASE_NULL(_drawnBatchesLabel);
- CC_SAFE_RELEASE_NULL(_drawnVerticesLabel);
- _textureCache->removeTextureForKey("/cc_fps_images");
- FileUtils::getInstance()->purgeCachedEntries();
- }
- Texture2D::PixelFormat currentFormat = Texture2D::getDefaultAlphaPixelFormat();
- Texture2D::setDefaultAlphaPixelFormat(Texture2D::PixelFormat::RGBA4444);
- unsigned char *data = nullptr;
- ssize_t dataLength = 0;
- getFPSImageData(&data, &dataLength);
- Image* image = new (std::nothrow) Image();
- 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();
-
- _cpuTLabel = LabelAtlas::create();
- _cpuTLabel->retain();
- _cpuTLabel->setIgnoreContentScaleFactor(true);
- _cpuTLabel->initWithString(cpuString, texture, 12, 32 , '.');
- _cpuTLabel->setScale(scaleFactor);
- if (_cpuOverload) {
- _cpuTLabel->setColor(Color3B(255,0,0));
- }
-
- _gpuTLabel = LabelAtlas::create();
- _gpuTLabel->retain();
- _gpuTLabel->setIgnoreContentScaleFactor(true);
- _gpuTLabel->initWithString(gpuString, texture, 12, 32 , '.');
- _gpuTLabel->setScale(scaleFactor);
- if (_gpuOverload) {
- _gpuTLabel->setColor(Color3B(255,0,0));
- }
- _FPSLabel = LabelAtlas::create();
- _FPSLabel->retain();
- _FPSLabel->setIgnoreContentScaleFactor(true);
- _FPSLabel->initWithString(fpsString, texture, 12, 32 , '.');
- _FPSLabel->setScale(scaleFactor);
- _drawnBatchesLabel = LabelAtlas::create();
- _drawnBatchesLabel->retain();
- _drawnBatchesLabel->setIgnoreContentScaleFactor(true);
- _drawnBatchesLabel->initWithString(drawBatchString, texture, 12, 32, '.');
- _drawnBatchesLabel->setScale(scaleFactor);
- _drawnVerticesLabel = LabelAtlas::create();
- _drawnVerticesLabel->retain();
- _drawnVerticesLabel->setIgnoreContentScaleFactor(true);
- _drawnVerticesLabel->initWithString(drawVerticesString, texture, 12, 32, '.');
- _drawnVerticesLabel->setScale(scaleFactor);
- Texture2D::setDefaultAlphaPixelFormat(currentFormat);
- const int height_spacing = 22 / CC_CONTENT_SCALE_FACTOR();
- _drawnVerticesLabel->setPosition(Vec2(0, height_spacing*4) + CC_DIRECTOR_STATS_POSITION);
- _drawnBatchesLabel->setPosition(Vec2(0, height_spacing*3) + CC_DIRECTOR_STATS_POSITION);
- _FPSLabel->setPosition(Vec2(0, height_spacing*2)+CC_DIRECTOR_STATS_POSITION);
- _cpuTLabel->setPosition(Vec2(0, height_spacing*1)+CC_DIRECTOR_STATS_POSITION);
- _gpuTLabel->setPosition(Vec2(0, height_spacing*0)+CC_DIRECTOR_STATS_POSITION);
- }
- #endif // #if !CC_STRIP_FPS
- void Director::setContentScaleFactor(float scaleFactor)
- {
- if (scaleFactor != _contentScaleFactor)
- {
- _contentScaleFactor = scaleFactor;
- _isStatusLabelUpdated = true;
- }
- }
- void Director::setNotificationNode(Node *node)
- {
- if (_notificationNode != nullptr){
- _notificationNode->onExitTransitionDidStart();
- _notificationNode->onExit();
- _notificationNode->cleanup();
- }
- CC_SAFE_RELEASE(_notificationNode);
- _notificationNode = node;
- if (node == nullptr)
- return;
- _notificationNode->onEnter();
- _notificationNode->onEnterTransitionDidFinish();
- CC_SAFE_RETAIN(_notificationNode);
- }
- void Director::setScheduler(Scheduler* scheduler)
- {
- if (_scheduler != scheduler)
- {
- CC_SAFE_RETAIN(scheduler);
- CC_SAFE_RELEASE(_scheduler);
- _scheduler = scheduler;
- }
- }
- void Director::setActionManager(ActionManager* actionManager)
- {
- if (_actionManager != actionManager)
- {
- CC_SAFE_RETAIN(actionManager);
- CC_SAFE_RELEASE(_actionManager);
- _actionManager = actionManager;
- }
- }
- void Director::setEventDispatcher(EventDispatcher* dispatcher)
- {
- if (_eventDispatcher != dispatcher)
- {
- CC_SAFE_RETAIN(dispatcher);
- CC_SAFE_RELEASE(_eventDispatcher);
- _eventDispatcher = dispatcher;
- }
- }
- void Director::startAnimation()
- {
- startAnimation(SetIntervalReason::BY_ENGINE);
- }
- void Director::startAnimation(SetIntervalReason reason)
- {
- _lastUpdate = std::chrono::steady_clock::now();
- _invalid = false;
- _cocos2d_thread_id = std::this_thread::get_id();
- Application::getInstance()->setAnimationInterval(_animationInterval, reason);
- // fix issue #3509, skip one fps to avoid incorrect time calculation.
- setNextDeltaTimeZero(true);
- }
- void Director::mainLoop()
- {
- if (_purgeDirectorInNextLoop)
- {
- _purgeDirectorInNextLoop = false;
- purgeDirector();
- }
- else if (_restartDirectorInNextLoop)
- {
- _restartDirectorInNextLoop = false;
- restartDirector();
- }
- else if (! _invalid)
- {
- drawScene();
-
- // release the objects
- PoolManager::getInstance()->getCurrentPool()->clear();
- }
- }
- void Director::mainLoop(float dt)
- {
- _deltaTime = dt;
- _deltaTimePassedByCaller = true;
- mainLoop();
- }
- void Director::stopAnimation()
- {
- _invalid = true;
- }
- void Director::setAnimationInterval(float interval)
- {
- setAnimationInterval(interval, SetIntervalReason::BY_GAME);
- }
- void Director::setAnimationInterval(float interval, SetIntervalReason reason)
- {
- _animationInterval = interval;
- if (! _invalid)
- {
- stopAnimation();
- startAnimation(reason);
- }
- }
- NS_CC_END
|