Skip to content

Commit 013d852

Browse files
author
minggo
authored
make fps stable on iOS (#17852)
1 parent a857ebe commit 013d852

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

cocos/base/CCDirector.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ Director* Director::getInstance()
108108
Director::Director()
109109
: _isStatusLabelUpdated(true)
110110
, _invalid(true)
111+
, _deltaTimePassedByCaller(false)
111112
{
112113
}
113114

@@ -346,7 +347,12 @@ void Director::calculateDeltaTime()
346347
}
347348
else
348349
{
349-
_deltaTime = std::chrono::duration_cast<std::chrono::microseconds>(now - _lastUpdate).count() / 1000000.0f;
350+
// delta time may passed by invoke mainLoop(dt)
351+
if (!_deltaTimePassedByCaller)
352+
{
353+
_deltaTime = std::chrono::duration_cast<std::chrono::microseconds>(now - _lastUpdate).count() / 1000000.0f;
354+
_lastUpdate = now;
355+
}
350356
_deltaTime = MAX(0, _deltaTime);
351357
}
352358

@@ -357,8 +363,6 @@ void Director::calculateDeltaTime()
357363
_deltaTime = 1 / 60.0f;
358364
}
359365
#endif
360-
361-
_lastUpdate = now;
362366
}
363367
float Director::getDeltaTime() const
364368
{
@@ -1447,6 +1451,13 @@ void Director::mainLoop()
14471451
}
14481452
}
14491453

1454+
void Director::mainLoop(float dt)
1455+
{
1456+
_deltaTime = dt;
1457+
_deltaTimePassedByCaller = true;
1458+
mainLoop();
1459+
}
1460+
14501461
void Director::stopAnimation()
14511462
{
14521463
_invalid = true;

cocos/base/CCDirector.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,11 @@ class CC_DLL Director : public Ref
386386
void setDepthTest(bool on);
387387

388388
void mainLoop();
389+
/** Invoke main loop with delta time. Then `calculateDeltaTime` can just use the delta time directly.
390+
* The delta time paseed may include vsync time. See issue #17806
391+
* @since 3.16
392+
*/
393+
void mainLoop(float dt);
389394

390395
/** The size in pixels of the surface. It could be different than the screen size.
391396
* High-res devices might have a higher surface size than the screen size.
@@ -615,6 +620,7 @@ class CC_DLL Director : public Ref
615620

616621
/* delta time since last tick to main loop */
617622
float _deltaTime;
623+
bool _deltaTimePassedByCaller;
618624

619625
/* The _openGLView, where everything is rendered, GLView is a abstract class,cocos2d-x provide GLViewImpl
620626
which inherit from it as default renderer context,you can have your own by inherit from it*/

cocos/platform/ios/CCDirectorCaller-ios.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
id displayLink;
3333
int interval;
3434
BOOL isAppActive;
35+
CFTimeInterval lastDisplayTime;
3536
}
3637
@property (readwrite) int interval;
3738
-(void) startMainLoop;

cocos/platform/ios/CCDirectorCaller-ios.mm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ -(void) doCaller: (id) sender
136136
glFlush();
137137

138138
[EAGLContext setCurrentContext: cocos2dxContext];
139-
director->mainLoop();
139+
140+
CFTimeInterval dt = ((CADisplayLink*)displayLink).timestamp - lastDisplayTime;
141+
lastDisplayTime = ((CADisplayLink*)displayLink).timestamp;
142+
director->mainLoop(dt);
140143
}
141144
}
142145

0 commit comments

Comments
 (0)