Skip to content

Commit ff35f81

Browse files
authored
Improve Director stepFrame logic (#3221)
1 parent c6b0bc4 commit ff35f81

11 files changed

Lines changed: 36 additions & 39 deletions

File tree

axmol/base/Director.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ void Director::calculateDeltaTime()
424424
}
425425
else
426426
{
427-
// delta time may passed by invoke renderFrame(dt)
427+
// delta time may passed by invoke stepFrame(dt)
428428
if (!_deltaTimePassedByCaller)
429429
{
430430
auto now = std::chrono::steady_clock::now();
@@ -1580,7 +1580,7 @@ void Director::performFrameTasks(FrameTaskQueue& frameTasks)
15801580
}
15811581
}
15821582

1583-
void Director::renderFrame()
1583+
void Director::stepFrame()
15841584
{
15851585
if (_cleanupDirectorInNextLoop)
15861586
{
@@ -1591,18 +1591,19 @@ void Director::renderFrame()
15911591
{
15921592
_restartDirectorInNextLoop = false;
15931593
restartDirector();
1594+
_renderView->pollEvents();
15941595
}
15951596
else if (!_invalid)
15961597
{
15971598
processFrame();
15981599
}
15991600
}
16001601

1601-
void Director::renderFrame(float dt)
1602+
void Director::stepFrame(float dt)
16021603
{
16031604
_deltaTime = dt;
16041605
_deltaTimePassedByCaller = true;
1605-
renderFrame();
1606+
stepFrame();
16061607
}
16071608

16081609
void Director::stopAnimation()

axmol/base/Director.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,12 +424,12 @@ class AX_DLL Director
424424
void setClearColor(const Color& clearColor);
425425
const Color& getClearColor() const { return _clearColor; }
426426

427-
[[internal]] void renderFrame();
428-
/** Invoke render frame with delta time. Then `calculateDeltaTime` can just use the delta time directly.
429-
* The delta time paseed may include vsync time. See issue #17806
427+
[[internal]] void stepFrame();
428+
/** Invoke frame step with delta time. Then `calculateDeltaTime` can just use the delta time directly.
429+
* The delta time passed may include vsync time. See issue #17806
430430
* @since 3.16
431431
*/
432-
[[internal]] void renderFrame(float dt);
432+
[[internal]] void stepFrame(float dt);
433433

434434
/** The size in pixels of the surface. It could be different than the screen size.
435435
* High-res devices might have a higher surface size than the screen size.
@@ -583,8 +583,9 @@ class AX_DLL Director
583583
*/
584584
bool isValid() const { return !_invalid; }
585585

586+
protected:
586587
/**
587-
* Advance one frame of the engine loop.
588+
* Process one frame of the engine loop.
588589
*
589590
* This method is invoked automatically once per frame by the Director.
590591
* It drives both the game logic update and the rendering pipeline:
@@ -598,7 +599,6 @@ class AX_DLL Director
598599
*/
599600
[[internal]] void processFrame();
600601

601-
protected:
602602
static void performFrameTasks(FrameTaskQueue& frameTasks);
603603

604604
void performFrameBoundaryTasks();

axmol/platform/android/java/src/dev/axmol/lib/AxmolPlayer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public void onDrawFrame() {
303303
/*
304304
* Render time MUST be counted in, or the FPS will slower than appointed.
305305
*/
306-
nativeRenderFrame();
306+
nativeStepFrame();
307307
/*
308308
* No need to use algorithm in default(60,90,120... FPS) situation,
309309
* since onDrawFrame() was called by system 60 times per second by default.
@@ -746,7 +746,7 @@ public void onGetContentRect(android.view.ActionMode mode, View view, android.gr
746746
// Native methods for AxmolPlayer
747747
// ===========================================================
748748

749-
public static native void nativeRenderFrame();
749+
public static native void nativeStepFrame();
750750

751751
public static native void nativeOnResume();
752752

axmol/platform/android/jni/AxmolPlayerJni.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ JNIEXPORT void JNICALL Java_dev_axmol_lib_AxmolPlayer_nativeOnSurfaceChanged(JNI
173173
}
174174
}
175175

176-
JNIEXPORT void JNICALL Java_dev_axmol_lib_AxmolPlayer_nativeRenderFrame(JNIEnv*, jclass)
176+
JNIEXPORT void JNICALL Java_dev_axmol_lib_AxmolPlayer_nativeStepFrame(JNIEnv*, jclass)
177177
{
178-
ax::Director::getInstance()->renderFrame();
178+
ax::Director::getInstance()->stepFrame();
179179
}
180180

181181
JNIEXPORT void JNICALL

axmol/platform/ios/DirectorCaller-ios.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ - (void)doCaller:(id)sender
187187
#endif
188188
CFTimeInterval dt = ((CADisplayLink*)displayLink).timestamp - lastDisplayTime;
189189
lastDisplayTime = ((CADisplayLink*)displayLink).timestamp;
190-
director->renderFrame(dt);
190+
director->stepFrame(dt);
191191
}
192192
}
193193

axmol/platform/ios/RenderHostView-ios.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ - (void)layoutSubviews
324324

325325
// Avoid flicker. Issue #350
326326
if ([NSThread isMainThread])
327-
director->processFrame();
327+
director->stepFrame();
328328
}
329329

330330
- (void)swapBuffers

axmol/platform/linux/Application-linux.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ int Application::run()
7171

7272
director->performFrameBoundaryTasks();
7373

74-
director->renderFrame();
75-
renderView->pollEvents();
74+
director->stepFrame();
7675

7776
auto interval = std::chrono::steady_clock::now() - lastTime;
7877
if (interval < _animationInterval)
@@ -86,14 +85,14 @@ int Application::run()
8685
}
8786
}
8887
/* Only work on Desktop
89-
* Director::renderFrame is really one frame logic
88+
* Director::stepFrame is really one frame logic
9089
* when we want to close the window, we should call Director::end();
91-
* then call Director::renderFrame to do release of internal resources
90+
* then call Director::stepFrame to do release of internal resources
9291
*/
9392
if (renderView->isGfxContextReady())
9493
{
9594
director->end();
96-
director->renderFrame();
95+
director->stepFrame();
9796
director = nullptr;
9897
}
9998
renderView->release();

axmol/platform/mac/Application-mac.mm

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ of this software and associated documentation files (the "Software"), to deal
7373
lastTime = std::chrono::steady_clock::now();
7474

7575
director->performFrameBoundaryTasks();
76-
director->renderFrame();
77-
renderView->pollEvents();
76+
director->stepFrame();
7877

7978
auto interval = std::chrono::steady_clock::now() - lastTime;
8079
auto waitDuration = _animationInterval - interval - _1ms;
@@ -85,14 +84,14 @@ of this software and associated documentation files (the "Software"), to deal
8584
}
8685

8786
/* Only work on Desktop
88-
* Director::renderFrame is really one frame logic
87+
* Director::stepFrame is really one frame logic
8988
* when we want to close the window, we should call Director::end();
90-
* then call Director::renderFrame to do release of internal resources
89+
* then call Director::stepFrame to do release of internal resources
9190
*/
9291
if (renderView->isGfxContextReady())
9392
{
9493
director->end();
95-
director->renderFrame();
94+
director->stepFrame();
9695
}
9796

9897
renderView->release();

axmol/platform/wasm/Application-wasm.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static int s_targetFPS = 0; // 0 = follow browser refresh rate
102102
static double s_lastFrameTime = 0.0; // ms
103103
static double s_accumulator = 0.0; // ms
104104

105-
static void renderFrame();
105+
static void stepFrame();
106106

107107
static void updateFrame()
108108
{
@@ -112,7 +112,7 @@ static void updateFrame()
112112
// First frame: render immediately
113113
if (s_lastFrameTime <= 0.0) [[unlikely]]
114114
{
115-
renderFrame();
115+
stepFrame();
116116
s_lastFrameTime = now;
117117
return;
118118
}
@@ -124,7 +124,7 @@ static void updateFrame()
124124
if (delta > 1000.0) [[unlikely]]
125125
{
126126
s_accumulator = 0.0;
127-
renderFrame();
127+
stepFrame();
128128
return;
129129
}
130130

@@ -133,28 +133,27 @@ static void updateFrame()
133133
// If targetFPS is 0, follow browser refresh rate directly
134134
if (s_targetFPS <= 0)
135135
{
136-
renderFrame();
136+
stepFrame();
137137
return;
138138
}
139139

140140
// Frame skipping logic based on accumulator
141141
double targetInterval = 1000.0 / s_targetFPS;
142142
if (s_accumulator >= targetInterval)
143143
{
144-
renderFrame();
144+
stepFrame();
145145
s_accumulator -= targetInterval;
146146
if (s_accumulator < 0.0) [[unlikely]] // floating-point safety
147147
s_accumulator = 0.0;
148148
} // else onIdle
149149
}
150150

151-
static void renderFrame()
151+
static void stepFrame()
152152
{
153153
auto director = __director;
154154
auto renderView = director->getRenderView();
155155

156-
director->renderFrame();
157-
renderView->pollEvents();
156+
director->stepFrame();
158157

159158
if (renderView->windowShouldClose())
160159
{
@@ -164,7 +163,7 @@ static void renderFrame()
164163
if (renderView->isGfxContextReady())
165164
{
166165
director->end();
167-
director->renderFrame();
166+
director->stepFrame();
168167
}
169168
renderView->release();
170169

axmol/platform/win32/Application-win32.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ int Application::run()
111111
if (interval >= _animationInterval.QuadPart)
112112
{
113113
nLast.QuadPart = nNow.QuadPart;
114-
director->renderFrame();
115-
renderView->pollEvents();
114+
director->stepFrame();
116115
}
117116
else
118117
{
@@ -131,7 +130,7 @@ int Application::run()
131130
if (renderView->isGfxContextReady())
132131
{
133132
director->end();
134-
director->renderFrame();
133+
director->stepFrame();
135134
director = nullptr;
136135
}
137136
renderView->release();

0 commit comments

Comments
 (0)