Skip to content

Commit 60599a9

Browse files
authored
Merge pull request #121 from 1492083648/fix/fps-getfps-nan-and-smoothing
fix: GetFPS NaN/unstable at high SetFPS, use 60-frame average
2 parents e0cb83c + 6dd1ea7 commit 60599a9

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

LuaSTG/LuaSTG/AppFrame.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,18 @@ void AppFrame::onBeforeUpdate() {
395395
m_frame_rate_controller->update();
396396
}
397397

398-
m_fFPS = 1.0 / m_frame_rate_controller->getStatistics()->getDuration(0);
399-
m_fAvgFPS = 1.0 / m_frame_rate_controller->getStatistics()->getAverage(10);
398+
// Avoid NaN/inf: only invert positive finite duration; use minimum duration to cap FPS and avoid 1/0
399+
constexpr double min_duration = 1e-6; // cap reported FPS at 1e6
400+
const auto* stats = m_frame_rate_controller->getStatistics();
401+
double d0 = stats->getDuration(0);
402+
if (d0 == d0 && d0 > 0.0) {
403+
m_fFPS = 1.0 / (d0 < min_duration ? min_duration : d0);
404+
}
405+
// Use 60-frame average so GetFPS is stable at high FPS (10-frame window was too noisy at 2000+ FPS)
406+
double avg = stats->getAverage(60);
407+
if (avg == avg && avg > 0.0) {
408+
m_fAvgFPS = 1.0 / (avg < min_duration ? min_duration : avg);
409+
}
400410
m_frame_rate_controller->setFrameRate(m_target_fps);
401411
m_message_timer = core::ScopeTimer(&m_message_time);
402412
}

engine/window-system/core/FrameRateController.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ namespace core {
2121
l += 1;
2222
}
2323
}
24+
if (l == 0) {
25+
return 0.0;
26+
}
2427
return result / static_cast<double>(l);
2528
}
2629

0 commit comments

Comments
 (0)