| 51 | } |
| 52 | |
| 53 | int Application::run() |
| 54 | { |
| 55 | initGfxContextAttrs(); |
| 56 | // Initialize instance and cocos2d. |
| 57 | if (!applicationDidFinishLaunching()) |
| 58 | { |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | std::chrono::steady_clock::time_point lastTime{}; |
| 63 | |
| 64 | auto director = Director::getInstance(); |
| 65 | auto renderView = director->getRenderView(); |
| 66 | |
| 67 | // Retain renderView to avoid renderView being released in the while loop |
| 68 | renderView->retain(); |
| 69 | |
| 70 | while (!renderView->windowShouldClose()) |
| 71 | { |
| 72 | lastTime = std::chrono::steady_clock::now(); |
| 73 | |
| 74 | director->mainLoop(); |
| 75 | renderView->pollEvents(); |
| 76 | |
| 77 | auto interval = std::chrono::steady_clock::now() - lastTime; |
| 78 | if (interval < _animationInterval) |
| 79 | { |
| 80 | auto waitDuration = _animationInterval - interval; |
| 81 | std::this_thread::sleep_for(waitDuration); |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | std::this_thread::yield(); |
| 86 | } |
| 87 | } |
| 88 | /* Only work on Desktop |
| 89 | * Director::mainLoop is really one frame logic |
| 90 | * when we want to close the window, we should call Director::end(); |
| 91 | * then call Director::mainLoop to do release of internal resources |
| 92 | */ |
| 93 | if (renderView->isGfxContextReady()) |
| 94 | { |
| 95 | director->end(); |
| 96 | director->mainLoop(); |
| 97 | director = nullptr; |
| 98 | } |
| 99 | renderView->release(); |
| 100 | return EXIT_SUCCESS; |
| 101 | } |
| 102 | |
| 103 | void Application::setAnimationInterval(float interval) |
| 104 | { |
nothing calls this directly
no test coverage detected