CCDirector.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2013 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2017 Chukong Technologies Inc.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. // cocos2d includes
  24. #include "base/CCDirector.h"
  25. // standard includes
  26. #include <string>
  27. #include "2d/CCDrawingPrimitives.h"
  28. #include "2d/CCSpriteFrameCache.h"
  29. #include "platform/CCFileUtils.h"
  30. #include "2d/CCActionManager.h"
  31. #include "2d/CCFontFNT.h"
  32. #include "2d/CCFontAtlasCache.h"
  33. #include "2d/CCAnimationCache.h"
  34. #include "2d/CCTransition.h"
  35. #include "2d/CCFontFreeType.h"
  36. #include "2d/CCLabelAtlas.h"
  37. #include "renderer/CCGLProgramCache.h"
  38. #include "renderer/CCGLProgramStateCache.h"
  39. #include "renderer/CCTextureCache.h"
  40. #include "renderer/ccGLStateCache.h"
  41. #include "renderer/CCRenderer.h"
  42. #include "renderer/CCRenderState.h"
  43. #include "renderer/CCFrameBuffer.h"
  44. #include "2d/CCCamera.h"
  45. #include "base/CCUserDefault.h"
  46. #include "base/ccFPSImages.h"
  47. #include "base/CCScheduler.h"
  48. #include "base/ccMacros.h"
  49. #include "base/CCEventDispatcher.h"
  50. #include "base/CCEventCustom.h"
  51. #include "base/CCConsole.h"
  52. #include "base/CCAutoreleasePool.h"
  53. #include "base/CCConfiguration.h"
  54. #include "base/CCAsyncTaskPool.h"
  55. #include "platform/CCApplication.h"
  56. #include "CCProfiling.h"
  57. #if CC_ENABLE_SCRIPT_BINDING
  58. #include "base/CCScriptSupport.h"
  59. #endif
  60. #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
  61. #include "platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxEngineDataManager.h"
  62. #endif
  63. /**
  64. Position of the FPS
  65. Default: 0,0 (bottom-left corner)
  66. */
  67. #ifndef CC_DIRECTOR_STATS_POSITION
  68. #define CC_DIRECTOR_STATS_POSITION Director::getInstance()->getVisibleOrigin()
  69. #endif // CC_DIRECTOR_STATS_POSITION
  70. using namespace std;
  71. NS_CC_BEGIN
  72. // FIXME: it should be a Director ivar. Move it there once support for multiple directors is added
  73. // singleton stuff
  74. static Director *s_SharedDirector = nullptr;
  75. // cpu,gpu 时间统计的现实间隔
  76. #define CPU_GPU_STAT_INTERVAL 0.2
  77. // 如果低于这个帧率,则用红色显示统计
  78. #define CPU_GPU_MIN_TIME_ECLAPSED (1.0/45)
  79. #define kDefaultFPS 60 // 60 frames per second
  80. extern const char* cocos2dVersion(void);
  81. const char *Director::EVENT_BEFORE_SET_NEXT_SCENE = "director_before_set_next_scene";
  82. const char *Director::EVENT_AFTER_SET_NEXT_SCENE = "director_after_set_next_scene";
  83. const char *Director::EVENT_PROJECTION_CHANGED = "director_projection_changed";
  84. const char *Director::EVENT_AFTER_DRAW = "director_after_draw";
  85. const char *Director::EVENT_AFTER_VISIT = "director_after_visit";
  86. const char *Director::EVENT_BEFORE_UPDATE = "director_before_update";
  87. const char *Director::EVENT_AFTER_UPDATE = "director_after_update";
  88. const char *Director::EVENT_RESET = "director_reset";
  89. const char *Director::EVENT_BEFORE_DRAW = "director_before_draw";
  90. Director* Director::getInstance()
  91. {
  92. if (!s_SharedDirector)
  93. {
  94. s_SharedDirector = new (std::nothrow) Director;
  95. CCASSERT(s_SharedDirector, "FATAL: Not enough memory");
  96. s_SharedDirector->init();
  97. }
  98. return s_SharedDirector;
  99. }
  100. Director::Director()
  101. : _isStatusLabelUpdated(true)
  102. , _invalid(true)
  103. , _deltaTimePassedByCaller(false)
  104. {
  105. }
  106. bool Director::init(void)
  107. {
  108. setDefaultValues();
  109. // scenes
  110. _runningScene = nullptr;
  111. _nextScene = nullptr;
  112. _notificationNode = nullptr;
  113. _scenesStack.reserve(15);
  114. // FPS
  115. _accumDt = 0.0f;
  116. _frameRate = 0.0f;
  117. _accumDt4CpuGpu = 0;
  118. _cpuTLabel = _gpuTLabel = _FPSLabel = _drawnBatchesLabel = _drawnVerticesLabel = nullptr;
  119. _totalFrames = 0;
  120. _cpuOverload = false;
  121. _gpuOverload = false;
  122. _lastUpdate = std::chrono::steady_clock::now();
  123. _msSleeped = 0;
  124. _secondsPerFrame = 1.0f;
  125. _frames = 0;
  126. // paused ?
  127. _paused = false;
  128. // purge ?
  129. _purgeDirectorInNextLoop = false;
  130. // restart ?
  131. _restartDirectorInNextLoop = false;
  132. // invalid ?
  133. _invalid = false;
  134. _winSizeInPoints = Size::ZERO;
  135. _openGLView = nullptr;
  136. _defaultFBO = nullptr;
  137. _contentScaleFactor = 1.0f;
  138. _console = new (std::nothrow) Console;
  139. // scheduler
  140. _scheduler = new (std::nothrow) Scheduler();
  141. // action manager
  142. _actionManager = new (std::nothrow) ActionManager();
  143. _scheduler->scheduleUpdate(_actionManager, Scheduler::PRIORITY_SYSTEM, false);
  144. _eventDispatcher = new (std::nothrow) EventDispatcher();
  145. _beforeSetNextScene = new (std::nothrow) EventCustom(EVENT_BEFORE_SET_NEXT_SCENE);
  146. _beforeSetNextScene->setUserData(this);
  147. _afterSetNextScene = new (std::nothrow) EventCustom(EVENT_AFTER_SET_NEXT_SCENE);
  148. _afterSetNextScene->setUserData(this);
  149. _eventAfterDraw = new (std::nothrow) EventCustom(EVENT_AFTER_DRAW);
  150. _eventAfterDraw->setUserData(this);
  151. _eventBeforeDraw = new (std::nothrow) EventCustom(EVENT_BEFORE_DRAW);
  152. _eventBeforeDraw->setUserData(this);
  153. _eventAfterVisit = new (std::nothrow) EventCustom(EVENT_AFTER_VISIT);
  154. _eventAfterVisit->setUserData(this);
  155. _eventBeforeUpdate = new (std::nothrow) EventCustom(EVENT_BEFORE_UPDATE);
  156. _eventBeforeUpdate->setUserData(this);
  157. _eventAfterUpdate = new (std::nothrow) EventCustom(EVENT_AFTER_UPDATE);
  158. _eventAfterUpdate->setUserData(this);
  159. _eventProjectionChanged = new (std::nothrow) EventCustom(EVENT_PROJECTION_CHANGED);
  160. _eventProjectionChanged->setUserData(this);
  161. _eventResetDirector = new (std::nothrow) EventCustom(EVENT_RESET);
  162. //init TextureCache
  163. initTextureCache();
  164. initMatrixStack();
  165. _renderer = new (std::nothrow) Renderer;
  166. RenderState::initialize();
  167. #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
  168. EngineDataManager::init();
  169. #endif
  170. return true;
  171. }
  172. Director::~Director(void)
  173. {
  174. CCLOGINFO("deallocing Director: %p", this);
  175. CC_SAFE_RELEASE(_cpuTLabel);
  176. CC_SAFE_RELEASE(_gpuTLabel);
  177. CC_SAFE_RELEASE(_FPSLabel);
  178. CC_SAFE_RELEASE(_drawnVerticesLabel);
  179. CC_SAFE_RELEASE(_drawnBatchesLabel);
  180. CC_SAFE_RELEASE(_runningScene);
  181. CC_SAFE_RELEASE(_notificationNode);
  182. CC_SAFE_RELEASE(_scheduler);
  183. CC_SAFE_RELEASE(_actionManager);
  184. CC_SAFE_DELETE(_defaultFBO);
  185. CC_SAFE_RELEASE(_beforeSetNextScene);
  186. CC_SAFE_RELEASE(_afterSetNextScene);
  187. CC_SAFE_RELEASE(_eventBeforeUpdate);
  188. CC_SAFE_RELEASE(_eventAfterUpdate);
  189. CC_SAFE_RELEASE(_eventAfterDraw);
  190. CC_SAFE_RELEASE(_eventBeforeDraw);
  191. CC_SAFE_RELEASE(_eventAfterVisit);
  192. CC_SAFE_RELEASE(_eventProjectionChanged);
  193. CC_SAFE_RELEASE(_eventResetDirector);
  194. delete _renderer;
  195. delete _console;
  196. CC_SAFE_RELEASE(_eventDispatcher);
  197. Configuration::destroyInstance();
  198. s_SharedDirector = nullptr;
  199. }
  200. void Director::setDefaultValues(void)
  201. {
  202. Configuration *conf = Configuration::getInstance();
  203. // default FPS
  204. double fps = conf->getValue("cocos2d.x.fps", Value(kDefaultFPS)).asDouble();
  205. _oldAnimationInterval = _animationInterval = 1.0 / fps;
  206. // Display FPS
  207. _displayStats = conf->getValue("cocos2d.x.display_fps", Value(false)).asBool();
  208. // GL projection
  209. std::string projection = conf->getValue("cocos2d.x.gl.projection", Value("3d")).asString();
  210. if (projection == "3d")
  211. _projection = Projection::_3D;
  212. else if (projection == "2d")
  213. _projection = Projection::_2D;
  214. else if (projection == "custom")
  215. _projection = Projection::CUSTOM;
  216. else
  217. CCASSERT(false, "Invalid projection value");
  218. // Default pixel format for PNG images with alpha
  219. std::string pixel_format = conf->getValue("cocos2d.x.texture.pixel_format_for_png", Value("rgba8888")).asString();
  220. if (pixel_format == "rgba8888")
  221. Texture2D::setDefaultAlphaPixelFormat(Texture2D::PixelFormat::RGBA8888);
  222. else if(pixel_format == "rgba4444")
  223. Texture2D::setDefaultAlphaPixelFormat(Texture2D::PixelFormat::RGBA4444);
  224. else if(pixel_format == "rgba5551")
  225. Texture2D::setDefaultAlphaPixelFormat(Texture2D::PixelFormat::RGB5A1);
  226. // PVR v2 has alpha premultiplied ?
  227. bool pvr_alpha_premultiplied = conf->getValue("cocos2d.x.texture.pvrv2_has_alpha_premultiplied", Value(false)).asBool();
  228. Image::setPVRImagesHavePremultipliedAlpha(pvr_alpha_premultiplied);
  229. }
  230. void Director::setGLDefaultValues()
  231. {
  232. // This method SHOULD be called only after openGLView_ was initialized
  233. CCASSERT(_openGLView, "opengl view should not be null");
  234. setAlphaBlending(true);
  235. setDepthTest(false);
  236. setProjection(_projection);
  237. }
  238. // Draw the Scene
  239. void Director::drawScene()
  240. {
  241. auto beginT = chrono::high_resolution_clock::now();
  242. if (_displayStats) {
  243. long duration = static_cast<long>(chrono::duration_cast<chrono::microseconds>(beginT - _tLastEnd).count());
  244. _gpuElapsed = (float)(duration) / 1000.0 - (float)_msSleeped;
  245. }
  246. // calculate "global" dt
  247. calculateDeltaTime();
  248. if (_openGLView)
  249. {
  250. _openGLView->pollEvents();
  251. }
  252. //tick before glClear: issue #533
  253. if (! _paused)
  254. {
  255. _eventDispatcher->dispatchEvent(_eventBeforeUpdate);
  256. _scheduler->update(_deltaTime);
  257. _eventDispatcher->dispatchEvent(_eventAfterUpdate);
  258. }
  259. _renderer->clear();
  260. experimental::FrameBuffer::clearAllFBOs();
  261. _eventDispatcher->dispatchEvent(_eventBeforeDraw);
  262. /* to avoid flickr, nextScene MUST be here: after tick and before draw.
  263. * FIXME: Which bug is this one. It seems that it can't be reproduced with v0.9
  264. */
  265. if (_nextScene)
  266. {
  267. setNextScene();
  268. }
  269. pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  270. if (_runningScene)
  271. {
  272. #if (CC_USE_PHYSICS || (CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION) || CC_USE_NAVMESH)
  273. _runningScene->stepPhysicsAndNavigation(_deltaTime);
  274. #endif
  275. //clear draw stats
  276. _renderer->clearDrawStats();
  277. //render the scene
  278. _openGLView->renderScene(_runningScene, _renderer);
  279. _eventDispatcher->dispatchEvent(_eventAfterVisit);
  280. }
  281. // draw the notifications node
  282. if (_notificationNode)
  283. {
  284. _notificationNode->visit(_renderer, Mat4::IDENTITY, 0);
  285. }
  286. updateFrameRate();
  287. if (_displayStats)
  288. {
  289. #if !CC_STRIP_FPS
  290. showStats();
  291. #endif
  292. }
  293. _renderer->render();
  294. _eventDispatcher->dispatchEvent(_eventAfterDraw);
  295. popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  296. _totalFrames++;
  297. if (_displayStats)
  298. {
  299. _tLastEnd = chrono::high_resolution_clock::now();
  300. long duration = static_cast<long>(chrono::duration_cast<chrono::microseconds>(_tLastEnd - beginT).count());
  301. _cpuElapsed = (float)duration / 1000.0;
  302. #if !CC_STRIP_FPS
  303. calculateMPF();
  304. #endif
  305. }
  306. // swap buffers
  307. if (_openGLView)
  308. {
  309. _openGLView->swapBuffers();
  310. }
  311. }
  312. void Director::calculateDeltaTime()
  313. {
  314. // new delta time. Re-fixed issue #1277
  315. if (_nextDeltaTimeZero)
  316. {
  317. _deltaTime = 0;
  318. _nextDeltaTimeZero = false;
  319. }
  320. else
  321. {
  322. // delta time may passed by invoke mainLoop(dt)
  323. if (!_deltaTimePassedByCaller)
  324. {
  325. auto now = std::chrono::steady_clock::now();
  326. _deltaTime = std::chrono::duration_cast<std::chrono::microseconds>(now - _lastUpdate).count() / 1000000.0f;
  327. _lastUpdate = now;
  328. }
  329. _deltaTime = MAX(0, _deltaTime);
  330. }
  331. #if COCOS2D_DEBUG
  332. // If we are debugging our code, prevent big delta time
  333. if (_deltaTime > 0.2f)
  334. {
  335. _deltaTime = 1 / 60.0f;
  336. }
  337. #endif
  338. }
  339. float Director::getDeltaTime() const
  340. {
  341. return _deltaTime;
  342. }
  343. void Director::setOpenGLView(GLView *openGLView)
  344. {
  345. CCASSERT(openGLView, "opengl view should not be null");
  346. if (_openGLView != openGLView)
  347. {
  348. // Configuration. Gather GPU info
  349. Configuration *conf = Configuration::getInstance();
  350. conf->gatherGPUInfo();
  351. CCLOG("%s\n",conf->getInfo().c_str());
  352. if(_openGLView)
  353. _openGLView->release();
  354. _openGLView = openGLView;
  355. _openGLView->retain();
  356. // set size
  357. _winSizeInPoints = _openGLView->getDesignResolutionSize();
  358. _isStatusLabelUpdated = true;
  359. if (_openGLView)
  360. {
  361. setGLDefaultValues();
  362. }
  363. _renderer->initGLView();
  364. CHECK_GL_ERROR_DEBUG();
  365. if (_eventDispatcher)
  366. {
  367. _eventDispatcher->setEnabled(true);
  368. }
  369. _defaultFBO = experimental::FrameBuffer::getOrCreateDefaultFBO(_openGLView);
  370. _defaultFBO->retain();
  371. }
  372. }
  373. TextureCache* Director::getTextureCache() const
  374. {
  375. return _textureCache;
  376. }
  377. void Director::initTextureCache()
  378. {
  379. _textureCache = new (std::nothrow) TextureCache();
  380. }
  381. void Director::destroyTextureCache()
  382. {
  383. if (_textureCache)
  384. {
  385. _textureCache->waitForQuit();
  386. CC_SAFE_RELEASE_NULL(_textureCache);
  387. }
  388. }
  389. void Director::setViewport()
  390. {
  391. if (_openGLView)
  392. {
  393. _openGLView->setViewPortInPoints(0, 0, _winSizeInPoints.width, _winSizeInPoints.height);
  394. }
  395. }
  396. void Director::setNextDeltaTimeZero(bool nextDeltaTimeZero)
  397. {
  398. _nextDeltaTimeZero = nextDeltaTimeZero;
  399. }
  400. //
  401. // FIXME TODO
  402. // Matrix code MUST NOT be part of the Director
  403. // MUST BE moved outside.
  404. // Why the Director must have this code ?
  405. //
  406. void Director::initMatrixStack()
  407. {
  408. while (!_modelViewMatrixStack.empty())
  409. {
  410. _modelViewMatrixStack.pop();
  411. }
  412. _projectionMatrixStackList.clear();
  413. while (!_textureMatrixStack.empty())
  414. {
  415. _textureMatrixStack.pop();
  416. }
  417. _modelViewMatrixStack.push(Mat4::IDENTITY);
  418. std::stack<Mat4> projectionMatrixStack;
  419. projectionMatrixStack.push(Mat4::IDENTITY);
  420. _projectionMatrixStackList.push_back(projectionMatrixStack);
  421. _textureMatrixStack.push(Mat4::IDENTITY);
  422. }
  423. void Director::resetMatrixStack()
  424. {
  425. initMatrixStack();
  426. }
  427. void Director::initProjectionMatrixStack(size_t stackCount)
  428. {
  429. _projectionMatrixStackList.clear();
  430. std::stack<Mat4> projectionMatrixStack;
  431. projectionMatrixStack.push(Mat4::IDENTITY);
  432. for (size_t i = 0; i < stackCount; ++i)
  433. _projectionMatrixStackList.push_back(projectionMatrixStack);
  434. }
  435. size_t Director::getProjectionMatrixStackSize()
  436. {
  437. return _projectionMatrixStackList.size();
  438. }
  439. void Director::popMatrix(MATRIX_STACK_TYPE type)
  440. {
  441. if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
  442. {
  443. _modelViewMatrixStack.pop();
  444. }
  445. else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
  446. {
  447. _projectionMatrixStackList[0].pop();
  448. }
  449. else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
  450. {
  451. _textureMatrixStack.pop();
  452. }
  453. else
  454. {
  455. CCASSERT(false, "unknown matrix stack type");
  456. }
  457. }
  458. void Director::popProjectionMatrix(size_t index)
  459. {
  460. _projectionMatrixStackList[index].pop();
  461. }
  462. void Director::loadIdentityMatrix(MATRIX_STACK_TYPE type)
  463. {
  464. if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
  465. {
  466. _modelViewMatrixStack.top() = Mat4::IDENTITY;
  467. }
  468. else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
  469. {
  470. _projectionMatrixStackList[0].top() = Mat4::IDENTITY;
  471. }
  472. else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
  473. {
  474. _textureMatrixStack.top() = Mat4::IDENTITY;
  475. }
  476. else
  477. {
  478. CCASSERT(false, "unknown matrix stack type");
  479. }
  480. }
  481. void Director::loadProjectionIdentityMatrix(size_t index)
  482. {
  483. _projectionMatrixStackList[index].top() = Mat4::IDENTITY;
  484. }
  485. void Director::loadMatrix(MATRIX_STACK_TYPE type, const Mat4& mat)
  486. {
  487. if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
  488. {
  489. _modelViewMatrixStack.top() = mat;
  490. }
  491. else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
  492. {
  493. _projectionMatrixStackList[0].top() = mat;
  494. }
  495. else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
  496. {
  497. _textureMatrixStack.top() = mat;
  498. }
  499. else
  500. {
  501. CCASSERT(false, "unknown matrix stack type");
  502. }
  503. }
  504. void Director::loadProjectionMatrix(const Mat4& mat, size_t index)
  505. {
  506. _projectionMatrixStackList[index].top() = mat;
  507. }
  508. void Director::multiplyMatrix(MATRIX_STACK_TYPE type, const Mat4& mat)
  509. {
  510. if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
  511. {
  512. _modelViewMatrixStack.top() *= mat;
  513. }
  514. else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
  515. {
  516. _projectionMatrixStackList[0].top() *= mat;
  517. }
  518. else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
  519. {
  520. _textureMatrixStack.top() *= mat;
  521. }
  522. else
  523. {
  524. CCASSERT(false, "unknown matrix stack type");
  525. }
  526. }
  527. void Director::multiplyProjectionMatrix(const Mat4& mat, size_t index)
  528. {
  529. _projectionMatrixStackList[index].top() *= mat;
  530. }
  531. void Director::pushMatrix(MATRIX_STACK_TYPE type)
  532. {
  533. if(type == MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW)
  534. {
  535. _modelViewMatrixStack.push(_modelViewMatrixStack.top());
  536. }
  537. else if(type == MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION)
  538. {
  539. _projectionMatrixStackList[0].push(_projectionMatrixStackList[0].top());
  540. }
  541. else if(type == MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE)
  542. {
  543. _textureMatrixStack.push(_textureMatrixStack.top());
  544. }
  545. else
  546. {
  547. CCASSERT(false, "unknown matrix stack type");
  548. }
  549. }
  550. void Director::pushProjectionMatrix(size_t index)
  551. {
  552. _projectionMatrixStackList[index].push(_projectionMatrixStackList[index].top());
  553. }
  554. const Mat4& Director::getMatrix(MATRIX_STACK_TYPE type) const
  555. {
  556. if(type == MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW)
  557. {
  558. return _modelViewMatrixStack.top();
  559. }
  560. else if(type == MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION)
  561. {
  562. return _projectionMatrixStackList[0].top();
  563. }
  564. else if(type == MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE)
  565. {
  566. return _textureMatrixStack.top();
  567. }
  568. CCASSERT(false, "unknown matrix stack type, will return modelview matrix instead");
  569. return _modelViewMatrixStack.top();
  570. }
  571. const Mat4& Director::getProjectionMatrix(size_t index) const
  572. {
  573. return _projectionMatrixStackList[index].top();
  574. }
  575. void Director::setProjection(Projection projection)
  576. {
  577. Size size = _winSizeInPoints;
  578. if (size.width == 0 || size.height == 0)
  579. {
  580. CCLOGERROR("cocos2d: warning, Director::setProjection() failed because size is 0");
  581. return;
  582. }
  583. setViewport();
  584. switch (projection)
  585. {
  586. case Projection::_2D:
  587. {
  588. Mat4 orthoMatrix;
  589. Mat4::createOrthographicOffCenter(0, size.width, 0, size.height, -1024, 1024, &orthoMatrix);
  590. loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, orthoMatrix);
  591. loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  592. break;
  593. }
  594. case Projection::_3D:
  595. {
  596. float zeye = this->getZEye();
  597. Mat4 matrixPerspective, matrixLookup;
  598. // issue #1334
  599. Mat4::createPerspective(60, (GLfloat)size.width/size.height, 10, zeye+size.height/2, &matrixPerspective);
  600. 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);
  601. Mat4::createLookAt(eye, center, up, &matrixLookup);
  602. Mat4 proj3d = matrixPerspective * matrixLookup;
  603. loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, proj3d);
  604. loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  605. break;
  606. }
  607. case Projection::CUSTOM:
  608. // Projection Delegate is no longer needed
  609. // since the event "PROJECTION CHANGED" is emitted
  610. break;
  611. default:
  612. CCLOG("cocos2d: Director: unrecognized projection");
  613. break;
  614. }
  615. _projection = projection;
  616. GL::setProjectionMatrixDirty();
  617. _eventDispatcher->dispatchEvent(_eventProjectionChanged);
  618. }
  619. void Director::purgeCachedData(void)
  620. {
  621. FontFNT::purgeCachedData();
  622. FontAtlasCache::purgeCachedData();
  623. if (s_SharedDirector->getOpenGLView())
  624. {
  625. SpriteFrameCache::getInstance()->removeUnusedSpriteFrames();
  626. _textureCache->removeUnusedTextures();
  627. // Note: some tests such as ActionsTest are leaking refcounted textures
  628. // There should be no test textures left in the cache
  629. log("%s\n", _textureCache->getCachedTextureInfo().c_str());
  630. }
  631. FileUtils::getInstance()->purgeCachedEntries();
  632. }
  633. float Director::getZEye(void) const
  634. {
  635. return (_winSizeInPoints.height / 1.154700538379252f);//(2 * tanf(M_PI/6))
  636. }
  637. void Director::setAlphaBlending(bool on)
  638. {
  639. if (on)
  640. {
  641. GL::blendFunc(CC_BLEND_SRC, CC_BLEND_DST);
  642. }
  643. else
  644. {
  645. GL::blendFunc(GL_ONE, GL_ZERO);
  646. }
  647. CHECK_GL_ERROR_DEBUG();
  648. }
  649. void Director::setDepthTest(bool on)
  650. {
  651. _renderer->setDepthTest(on);
  652. }
  653. void Director::setClearColor(const Color4F& clearColor)
  654. {
  655. _renderer->setClearColor(clearColor);
  656. auto defaultFBO = experimental::FrameBuffer::getOrCreateDefaultFBO(_openGLView);
  657. if(defaultFBO) defaultFBO->setClearColor(clearColor);
  658. }
  659. static void GLToClipTransform(Mat4 *transformOut)
  660. {
  661. if(nullptr == transformOut) return;
  662. Director* director = Director::getInstance();
  663. CCASSERT(nullptr != director, "Director is null when setting matrix stack");
  664. auto projection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
  665. auto modelview = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  666. *transformOut = projection * modelview;
  667. }
  668. Vec2 Director::convertToGL(const Vec2& uiPoint)
  669. {
  670. Mat4 transform;
  671. GLToClipTransform(&transform);
  672. Mat4 transformInv = transform.getInversed();
  673. // Calculate z=0 using -> transform*[0, 0, 0, 1]/w
  674. float zClip = transform.m[14]/transform.m[15];
  675. Size glSize = _openGLView->getDesignResolutionSize();
  676. Vec4 clipCoord(2.0f*uiPoint.x/glSize.width - 1.0f, 1.0f - 2.0f*uiPoint.y/glSize.height, zClip, 1);
  677. Vec4 glCoord;
  678. //transformInv.transformPoint(clipCoord, &glCoord);
  679. transformInv.transformVector(clipCoord, &glCoord);
  680. float factor = 1.0f / glCoord.w;
  681. return Vec2(glCoord.x * factor, glCoord.y * factor);
  682. }
  683. Vec2 Director::convertToUI(const Vec2& glPoint)
  684. {
  685. Mat4 transform;
  686. GLToClipTransform(&transform);
  687. Vec4 clipCoord;
  688. // Need to calculate the zero depth from the transform.
  689. Vec4 glCoord(glPoint.x, glPoint.y, 0.0, 1);
  690. transform.transformVector(glCoord, &clipCoord);
  691. /*
  692. BUG-FIX #5506
  693. a = (Vx, Vy, Vz, 1)
  694. b = (a×M)T
  695. Out = 1 ⁄ bw(bx, by, bz)
  696. */
  697. clipCoord.x = clipCoord.x / clipCoord.w;
  698. clipCoord.y = clipCoord.y / clipCoord.w;
  699. clipCoord.z = clipCoord.z / clipCoord.w;
  700. Size glSize = _openGLView->getDesignResolutionSize();
  701. float factor = 1.0f / glCoord.w;
  702. return Vec2(glSize.width * (clipCoord.x * 0.5f + 0.5f) * factor, glSize.height * (-clipCoord.y * 0.5f + 0.5f) * factor);
  703. }
  704. const Size& Director::getWinSize(void) const
  705. {
  706. return _winSizeInPoints;
  707. }
  708. Size Director::getWinSizeInPixels() const
  709. {
  710. return Size(_winSizeInPoints.width * _contentScaleFactor, _winSizeInPoints.height * _contentScaleFactor);
  711. }
  712. Size Director::getVisibleSize() const
  713. {
  714. if (_openGLView)
  715. {
  716. return _openGLView->getVisibleSize();
  717. }
  718. else
  719. {
  720. return Size::ZERO;
  721. }
  722. }
  723. Vec2 Director::getVisibleOrigin() const
  724. {
  725. if (_openGLView)
  726. {
  727. return _openGLView->getVisibleOrigin();
  728. }
  729. else
  730. {
  731. return Vec2::ZERO;
  732. }
  733. }
  734. // scene management
  735. void Director::runWithScene(Scene *scene)
  736. {
  737. CCASSERT(scene != nullptr, "This command can only be used to start the Director. There is already a scene present.");
  738. CCASSERT(_runningScene == nullptr, "_runningScene should be null");
  739. pushScene(scene);
  740. startAnimation();
  741. }
  742. void Director::replaceScene(Scene *scene)
  743. {
  744. //CCASSERT(_runningScene, "Use runWithScene: instead to start the director");
  745. CCASSERT(scene != nullptr, "the scene should not be null");
  746. if (_runningScene == nullptr) {
  747. runWithScene(scene);
  748. return;
  749. }
  750. if (scene == _nextScene)
  751. return;
  752. if (_nextScene)
  753. {
  754. if (_nextScene->isRunning())
  755. {
  756. _nextScene->onExit();
  757. }
  758. _nextScene->cleanup();
  759. _nextScene = nullptr;
  760. }
  761. ssize_t index = _scenesStack.size() - 1;
  762. _sendCleanupToScene = true;
  763. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  764. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  765. if (sEngine)
  766. {
  767. sEngine->retainScriptObject(this, scene);
  768. sEngine->releaseScriptObject(this, _scenesStack.at(index));
  769. }
  770. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  771. _scenesStack.replace(index, scene);
  772. _nextScene = scene;
  773. }
  774. void Director::pushScene(Scene *scene)
  775. {
  776. CCASSERT(scene, "the scene should not null");
  777. _sendCleanupToScene = false;
  778. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  779. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  780. if (sEngine)
  781. {
  782. sEngine->retainScriptObject(this, scene);
  783. }
  784. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  785. _scenesStack.pushBack(scene);
  786. _nextScene = scene;
  787. }
  788. void Director::popScene(void)
  789. {
  790. CCASSERT(_runningScene != nullptr, "running scene should not null");
  791. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  792. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  793. if (sEngine)
  794. {
  795. sEngine->releaseScriptObject(this, _scenesStack.back());
  796. }
  797. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  798. _scenesStack.popBack();
  799. ssize_t c = _scenesStack.size();
  800. if (c == 0)
  801. {
  802. end();
  803. }
  804. else
  805. {
  806. _sendCleanupToScene = true;
  807. _nextScene = _scenesStack.at(c - 1);
  808. }
  809. }
  810. void Director::popToRootScene(void)
  811. {
  812. popToSceneStackLevel(1);
  813. }
  814. void Director::popToSceneStackLevel(int level)
  815. {
  816. CCASSERT(_runningScene != nullptr, "A running Scene is needed");
  817. ssize_t c = _scenesStack.size();
  818. // level 0? -> end
  819. if (level == 0)
  820. {
  821. end();
  822. return;
  823. }
  824. // current level or lower -> nothing
  825. if (level >= c)
  826. return;
  827. auto firstOnStackScene = _scenesStack.back();
  828. if (firstOnStackScene == _runningScene)
  829. {
  830. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  831. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  832. if (sEngine)
  833. {
  834. sEngine->releaseScriptObject(this, _scenesStack.back());
  835. }
  836. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  837. _scenesStack.popBack();
  838. --c;
  839. }
  840. // pop stack until reaching desired level
  841. while (c > level)
  842. {
  843. auto current = _scenesStack.back();
  844. if (current->isRunning())
  845. {
  846. current->onExit();
  847. }
  848. current->cleanup();
  849. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  850. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  851. if (sEngine)
  852. {
  853. sEngine->releaseScriptObject(this, _scenesStack.back());
  854. }
  855. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  856. _scenesStack.popBack();
  857. --c;
  858. }
  859. _nextScene = _scenesStack.back();
  860. // cleanup running scene
  861. _sendCleanupToScene = true;
  862. }
  863. void Director::end()
  864. {
  865. _purgeDirectorInNextLoop = true;
  866. #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
  867. exit(0);
  868. #endif
  869. }
  870. void Director::restart()
  871. {
  872. _restartDirectorInNextLoop = true;
  873. }
  874. void Director::reset()
  875. {
  876. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  877. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  878. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  879. if (_runningScene)
  880. {
  881. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  882. if (sEngine)
  883. {
  884. sEngine->releaseScriptObject(this, _runningScene);
  885. }
  886. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  887. _runningScene->onExit();
  888. _runningScene->cleanup();
  889. _runningScene->release();
  890. }
  891. _runningScene = nullptr;
  892. _nextScene = nullptr;
  893. _eventDispatcher->dispatchEvent(_eventResetDirector);
  894. // cleanup scheduler
  895. getScheduler()->unscheduleAll();
  896. // Remove all events
  897. if (_eventDispatcher)
  898. {
  899. _eventDispatcher->removeAllEventListeners();
  900. }
  901. if(_notificationNode)
  902. {
  903. _notificationNode->onExit();
  904. _notificationNode->cleanup();
  905. _notificationNode->release();
  906. }
  907. _notificationNode = nullptr;
  908. // remove all objects, but don't release it.
  909. // runWithScene might be executed after 'end'.
  910. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  911. if (sEngine)
  912. {
  913. for (const auto &scene : _scenesStack)
  914. {
  915. if (scene)
  916. sEngine->releaseScriptObject(this, scene);
  917. }
  918. }
  919. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  920. _scenesStack.clear();
  921. stopAnimation();
  922. CC_SAFE_RELEASE_NULL(_notificationNode);
  923. CC_SAFE_RELEASE_NULL(_cpuTLabel);
  924. CC_SAFE_RELEASE_NULL(_gpuTLabel);
  925. CC_SAFE_RELEASE_NULL(_FPSLabel);
  926. CC_SAFE_RELEASE_NULL(_drawnBatchesLabel);
  927. CC_SAFE_RELEASE_NULL(_drawnVerticesLabel);
  928. // purge bitmap cache
  929. FontFNT::purgeCachedData();
  930. FontAtlasCache::purgeCachedData();
  931. FontFreeType::shutdownFreeType();
  932. // purge all managed caches
  933. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  934. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  935. #elif _MSC_VER >= 1400 //vs 2005 or higher
  936. #pragma warning (push)
  937. #pragma warning (disable: 4996)
  938. #endif
  939. //it will crash clang static analyzer so hide it if __clang_analyzer__ defined
  940. #ifndef __clang_analyzer__
  941. DrawPrimitives::free();
  942. #endif
  943. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  944. #pragma GCC diagnostic warning "-Wdeprecated-declarations"
  945. #elif _MSC_VER >= 1400 //vs 2005 or higher
  946. #pragma warning (pop)
  947. #endif
  948. AnimationCache::destroyInstance();
  949. SpriteFrameCache::destroyInstance();
  950. GLProgramCache::destroyInstance();
  951. GLProgramStateCache::destroyInstance();
  952. FileUtils::destroyInstance();
  953. AsyncTaskPool::destroyInstance();
  954. // cocos2d-x specific data structures
  955. UserDefault::destroyInstance();
  956. GL::invalidateStateCache();
  957. RenderState::finalize();
  958. destroyTextureCache();
  959. }
  960. void Director::purgeDirector()
  961. {
  962. reset();
  963. CHECK_GL_ERROR_DEBUG();
  964. // OpenGL view
  965. if (_openGLView)
  966. {
  967. _openGLView->end();
  968. _openGLView = nullptr;
  969. }
  970. #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
  971. EngineDataManager::destroy();
  972. #endif
  973. // delete Director
  974. release();
  975. }
  976. void Director::restartDirector()
  977. {
  978. reset();
  979. // RenderState need to be reinitialized
  980. RenderState::initialize();
  981. // Texture cache need to be reinitialized
  982. initTextureCache();
  983. // Reschedule for action manager
  984. getScheduler()->scheduleUpdate(getActionManager(), Scheduler::PRIORITY_SYSTEM, false);
  985. // release the objects
  986. PoolManager::getInstance()->getCurrentPool()->clear();
  987. // Restart animation
  988. startAnimation();
  989. // Real restart in script level
  990. #if CC_ENABLE_SCRIPT_BINDING
  991. ScriptEvent scriptEvent(kRestartGame, nullptr);
  992. ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
  993. #endif
  994. }
  995. void Director::setNextScene()
  996. {
  997. _eventDispatcher->dispatchEvent(_beforeSetNextScene);
  998. bool runningIsTransition = dynamic_cast<TransitionScene*>(_runningScene) != nullptr;
  999. bool newIsTransition = dynamic_cast<TransitionScene*>(_nextScene) != nullptr;
  1000. // If it is not a transition, call onExit/cleanup
  1001. if (! newIsTransition)
  1002. {
  1003. if (_runningScene)
  1004. {
  1005. _runningScene->onExitTransitionDidStart();
  1006. _runningScene->onExit();
  1007. }
  1008. // issue #709. the root node (scene) should receive the cleanup message too
  1009. // otherwise it might be leaked.
  1010. if (_sendCleanupToScene && _runningScene)
  1011. {
  1012. _runningScene->cleanup();
  1013. }
  1014. }
  1015. if (_runningScene)
  1016. {
  1017. _runningScene->release();
  1018. }
  1019. _runningScene = _nextScene;
  1020. _nextScene->retain();
  1021. _nextScene = nullptr;
  1022. if ((! runningIsTransition) && _runningScene)
  1023. {
  1024. _runningScene->onEnter();
  1025. _runningScene->onEnterTransitionDidFinish();
  1026. }
  1027. _eventDispatcher->dispatchEvent(_afterSetNextScene);
  1028. }
  1029. void Director::pause()
  1030. {
  1031. if (_paused)
  1032. {
  1033. return;
  1034. }
  1035. _oldAnimationInterval = _animationInterval;
  1036. // when paused, don't consume CPU
  1037. setAnimationInterval(1 / 4.0, SetIntervalReason::BY_DIRECTOR_PAUSE);
  1038. _paused = true;
  1039. }
  1040. void Director::resume()
  1041. {
  1042. if (! _paused)
  1043. {
  1044. return;
  1045. }
  1046. setAnimationInterval(_oldAnimationInterval, SetIntervalReason::BY_ENGINE);
  1047. _paused = false;
  1048. _deltaTime = 0;
  1049. // fix issue #3509, skip one fps to avoid incorrect time calculation.
  1050. setNextDeltaTimeZero(true);
  1051. }
  1052. void Director::updateFrameRate()
  1053. {
  1054. // static const float FPS_FILTER = 0.1f;
  1055. // static float prevDeltaTime = 0.016f; // 60FPS
  1056. //
  1057. // float dt = _deltaTime * FPS_FILTER + (1.0f-FPS_FILTER) * prevDeltaTime;
  1058. // prevDeltaTime = dt;
  1059. // _frameRate = 1.0f/dt;
  1060. // Frame rate should be the real value of current frame.
  1061. _frameRate = 1.0f / _deltaTime;
  1062. }
  1063. #if !CC_STRIP_FPS
  1064. // display the FPS using a LabelAtlas
  1065. // updates the FPS every frame
  1066. void Director::showStats()
  1067. {
  1068. if (_isStatusLabelUpdated)
  1069. {
  1070. createStatsLabel();
  1071. _isStatusLabelUpdated = false;
  1072. }
  1073. static unsigned long prevCalls = 0;
  1074. static unsigned long prevVerts = 0;
  1075. ++_frames;
  1076. _accumDt += _deltaTime;
  1077. _accumDt4CpuGpu += _deltaTime;
  1078. if (_displayStats && _FPSLabel && _drawnBatchesLabel && _drawnVerticesLabel)
  1079. {
  1080. char buffer[30] = {0};
  1081. // 每隔两秒,更新一下cpu和gpu的时间
  1082. // 或者超出了报警阈值
  1083. if (_accumDt4CpuGpu > CPU_GPU_STAT_INTERVAL) {
  1084. sprintf(buffer, "CPU time: %.2f", _cpuElapsed);
  1085. _cpuTLabel->setString(buffer);
  1086. sprintf(buffer, "GPU time: %.2f", _gpuElapsed);
  1087. _gpuTLabel->setString(buffer);
  1088. _accumDt4CpuGpu = 0;
  1089. bool co = _cpuElapsed > (CPU_GPU_MIN_TIME_ECLAPSED * 1000.0);
  1090. bool go = _gpuElapsed > (CPU_GPU_MIN_TIME_ECLAPSED * 1000.0);
  1091. if ((co != _cpuOverload) || (go != _gpuOverload)) {
  1092. _isStatusLabelUpdated = true;
  1093. }
  1094. _cpuOverload = co;
  1095. _gpuOverload = go;
  1096. }
  1097. // Probably we don't need this anymore since
  1098. // the framerate is using a low-pass filter
  1099. // to make the FPS stable
  1100. if (_accumDt > CC_DIRECTOR_STATS_INTERVAL)
  1101. {
  1102. sprintf(buffer, "%.1f / %.3f", _frames / _accumDt, _secondsPerFrame);
  1103. _FPSLabel->setString(buffer);
  1104. _accumDt = 0;
  1105. _frames = 0;
  1106. }
  1107. auto currentCalls = (unsigned long)_renderer->getDrawnBatches();
  1108. auto currentVerts = (unsigned long)_renderer->getDrawnVertices();
  1109. if( currentCalls != prevCalls ) {
  1110. sprintf(buffer, "GL calls:%6lu", currentCalls);
  1111. _drawnBatchesLabel->setString(buffer);
  1112. prevCalls = currentCalls;
  1113. }
  1114. if( currentVerts != prevVerts) {
  1115. sprintf(buffer, "GL verts:%6lu", currentVerts);
  1116. _drawnVerticesLabel->setString(buffer);
  1117. prevVerts = currentVerts;
  1118. }
  1119. const Mat4& identity = Mat4::IDENTITY;
  1120. _drawnVerticesLabel->visit(_renderer, identity, 0);
  1121. _drawnBatchesLabel->visit(_renderer, identity, 0);
  1122. _FPSLabel->visit(_renderer, identity, 0);
  1123. _cpuTLabel->visit(_renderer, identity, 0);
  1124. _gpuTLabel->visit(_renderer, identity, 0);
  1125. }
  1126. }
  1127. void Director::calculateMPF()
  1128. {
  1129. static float prevSecondsPerFrame = 0;
  1130. static const float MPF_FILTER = 0.10f;
  1131. _secondsPerFrame = _deltaTime * MPF_FILTER + (1-MPF_FILTER) * prevSecondsPerFrame;
  1132. prevSecondsPerFrame = _secondsPerFrame;
  1133. }
  1134. // returns the FPS image data pointer and len
  1135. void Director::getFPSImageData(unsigned char** datapointer, ssize_t* length)
  1136. {
  1137. // FIXME: fixed me if it should be used
  1138. *datapointer = cc_fps_images_png;
  1139. *length = cc_fps_images_len();
  1140. }
  1141. void Director::createStatsLabel()
  1142. {
  1143. Texture2D *texture = nullptr;
  1144. std::string cpuString = "000";
  1145. std::string gpuString = "000";
  1146. std::string fpsString = "00.0";
  1147. std::string drawBatchString = "000";
  1148. std::string drawVerticesString = "00000";
  1149. if (_FPSLabel)
  1150. {
  1151. cpuString = _cpuTLabel->getString();
  1152. gpuString = _gpuTLabel->getString();
  1153. fpsString = _FPSLabel->getString();
  1154. drawBatchString = _drawnBatchesLabel->getString();
  1155. drawVerticesString = _drawnVerticesLabel->getString();
  1156. CC_SAFE_RELEASE_NULL(_cpuTLabel);
  1157. CC_SAFE_RELEASE_NULL(_gpuTLabel);
  1158. CC_SAFE_RELEASE_NULL(_FPSLabel);
  1159. CC_SAFE_RELEASE_NULL(_drawnBatchesLabel);
  1160. CC_SAFE_RELEASE_NULL(_drawnVerticesLabel);
  1161. _textureCache->removeTextureForKey("/cc_fps_images");
  1162. FileUtils::getInstance()->purgeCachedEntries();
  1163. }
  1164. Texture2D::PixelFormat currentFormat = Texture2D::getDefaultAlphaPixelFormat();
  1165. Texture2D::setDefaultAlphaPixelFormat(Texture2D::PixelFormat::RGBA4444);
  1166. unsigned char *data = nullptr;
  1167. ssize_t dataLength = 0;
  1168. getFPSImageData(&data, &dataLength);
  1169. Image* image = new (std::nothrow) Image();
  1170. bool isOK = image->initWithImageData(data, dataLength);
  1171. if (! isOK) {
  1172. CCLOGERROR("%s", "Fails: init fps_images");
  1173. return;
  1174. }
  1175. texture = _textureCache->addImage(image, "/cc_fps_images");
  1176. CC_SAFE_RELEASE(image);
  1177. /*
  1178. We want to use an image which is stored in the file named ccFPSImage.c
  1179. for any design resolutions and all resource resolutions.
  1180. To achieve this, we need to ignore 'contentScaleFactor' in 'AtlasNode' and 'LabelAtlas'.
  1181. So I added a new method called 'setIgnoreContentScaleFactor' for 'AtlasNode',
  1182. this is not exposed to game developers, it's only used for displaying FPS now.
  1183. */
  1184. float scaleFactor = 1 / CC_CONTENT_SCALE_FACTOR();
  1185. _cpuTLabel = LabelAtlas::create();
  1186. _cpuTLabel->retain();
  1187. _cpuTLabel->setIgnoreContentScaleFactor(true);
  1188. _cpuTLabel->initWithString(cpuString, texture, 12, 32 , '.');
  1189. _cpuTLabel->setScale(scaleFactor);
  1190. if (_cpuOverload) {
  1191. _cpuTLabel->setColor(Color3B(255,0,0));
  1192. }
  1193. _gpuTLabel = LabelAtlas::create();
  1194. _gpuTLabel->retain();
  1195. _gpuTLabel->setIgnoreContentScaleFactor(true);
  1196. _gpuTLabel->initWithString(gpuString, texture, 12, 32 , '.');
  1197. _gpuTLabel->setScale(scaleFactor);
  1198. if (_gpuOverload) {
  1199. _gpuTLabel->setColor(Color3B(255,0,0));
  1200. }
  1201. _FPSLabel = LabelAtlas::create();
  1202. _FPSLabel->retain();
  1203. _FPSLabel->setIgnoreContentScaleFactor(true);
  1204. _FPSLabel->initWithString(fpsString, texture, 12, 32 , '.');
  1205. _FPSLabel->setScale(scaleFactor);
  1206. _drawnBatchesLabel = LabelAtlas::create();
  1207. _drawnBatchesLabel->retain();
  1208. _drawnBatchesLabel->setIgnoreContentScaleFactor(true);
  1209. _drawnBatchesLabel->initWithString(drawBatchString, texture, 12, 32, '.');
  1210. _drawnBatchesLabel->setScale(scaleFactor);
  1211. _drawnVerticesLabel = LabelAtlas::create();
  1212. _drawnVerticesLabel->retain();
  1213. _drawnVerticesLabel->setIgnoreContentScaleFactor(true);
  1214. _drawnVerticesLabel->initWithString(drawVerticesString, texture, 12, 32, '.');
  1215. _drawnVerticesLabel->setScale(scaleFactor);
  1216. Texture2D::setDefaultAlphaPixelFormat(currentFormat);
  1217. const int height_spacing = 22 / CC_CONTENT_SCALE_FACTOR();
  1218. _drawnVerticesLabel->setPosition(Vec2(0, height_spacing*4) + CC_DIRECTOR_STATS_POSITION);
  1219. _drawnBatchesLabel->setPosition(Vec2(0, height_spacing*3) + CC_DIRECTOR_STATS_POSITION);
  1220. _FPSLabel->setPosition(Vec2(0, height_spacing*2)+CC_DIRECTOR_STATS_POSITION);
  1221. _cpuTLabel->setPosition(Vec2(0, height_spacing*1)+CC_DIRECTOR_STATS_POSITION);
  1222. _gpuTLabel->setPosition(Vec2(0, height_spacing*0)+CC_DIRECTOR_STATS_POSITION);
  1223. }
  1224. #endif // #if !CC_STRIP_FPS
  1225. void Director::setContentScaleFactor(float scaleFactor)
  1226. {
  1227. if (scaleFactor != _contentScaleFactor)
  1228. {
  1229. _contentScaleFactor = scaleFactor;
  1230. _isStatusLabelUpdated = true;
  1231. }
  1232. }
  1233. void Director::setNotificationNode(Node *node)
  1234. {
  1235. if (_notificationNode != nullptr){
  1236. _notificationNode->onExitTransitionDidStart();
  1237. _notificationNode->onExit();
  1238. _notificationNode->cleanup();
  1239. }
  1240. CC_SAFE_RELEASE(_notificationNode);
  1241. _notificationNode = node;
  1242. if (node == nullptr)
  1243. return;
  1244. _notificationNode->onEnter();
  1245. _notificationNode->onEnterTransitionDidFinish();
  1246. CC_SAFE_RETAIN(_notificationNode);
  1247. }
  1248. void Director::setScheduler(Scheduler* scheduler)
  1249. {
  1250. if (_scheduler != scheduler)
  1251. {
  1252. CC_SAFE_RETAIN(scheduler);
  1253. CC_SAFE_RELEASE(_scheduler);
  1254. _scheduler = scheduler;
  1255. }
  1256. }
  1257. void Director::setActionManager(ActionManager* actionManager)
  1258. {
  1259. if (_actionManager != actionManager)
  1260. {
  1261. CC_SAFE_RETAIN(actionManager);
  1262. CC_SAFE_RELEASE(_actionManager);
  1263. _actionManager = actionManager;
  1264. }
  1265. }
  1266. void Director::setEventDispatcher(EventDispatcher* dispatcher)
  1267. {
  1268. if (_eventDispatcher != dispatcher)
  1269. {
  1270. CC_SAFE_RETAIN(dispatcher);
  1271. CC_SAFE_RELEASE(_eventDispatcher);
  1272. _eventDispatcher = dispatcher;
  1273. }
  1274. }
  1275. void Director::startAnimation()
  1276. {
  1277. startAnimation(SetIntervalReason::BY_ENGINE);
  1278. }
  1279. void Director::startAnimation(SetIntervalReason reason)
  1280. {
  1281. _lastUpdate = std::chrono::steady_clock::now();
  1282. _invalid = false;
  1283. _cocos2d_thread_id = std::this_thread::get_id();
  1284. Application::getInstance()->setAnimationInterval(_animationInterval, reason);
  1285. // fix issue #3509, skip one fps to avoid incorrect time calculation.
  1286. setNextDeltaTimeZero(true);
  1287. }
  1288. void Director::mainLoop()
  1289. {
  1290. if (_purgeDirectorInNextLoop)
  1291. {
  1292. _purgeDirectorInNextLoop = false;
  1293. purgeDirector();
  1294. }
  1295. else if (_restartDirectorInNextLoop)
  1296. {
  1297. _restartDirectorInNextLoop = false;
  1298. restartDirector();
  1299. }
  1300. else if (! _invalid)
  1301. {
  1302. drawScene();
  1303. // release the objects
  1304. PoolManager::getInstance()->getCurrentPool()->clear();
  1305. }
  1306. }
  1307. void Director::mainLoop(float dt)
  1308. {
  1309. _deltaTime = dt;
  1310. _deltaTimePassedByCaller = true;
  1311. mainLoop();
  1312. }
  1313. void Director::stopAnimation()
  1314. {
  1315. _invalid = true;
  1316. }
  1317. void Director::setAnimationInterval(float interval)
  1318. {
  1319. setAnimationInterval(interval, SetIntervalReason::BY_GAME);
  1320. }
  1321. void Director::setAnimationInterval(float interval, SetIntervalReason reason)
  1322. {
  1323. _animationInterval = interval;
  1324. if (! _invalid)
  1325. {
  1326. stopAnimation();
  1327. startAnimation(reason);
  1328. }
  1329. }
  1330. NS_CC_END