CCDirector.cpp 40 KB

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