[UI] Use ticks as frame time instead of file time.

File time is not as accurate as its resolution suggests.
This results in a zero time delta between frames for high fps.
This commit is contained in:
Joel Linn 2019-12-01 21:37:52 +01:00 committed by Rick Gibbed
parent c3f3bd8701
commit 2c99228041
2 changed files with 14 additions and 10 deletions

View File

@ -173,12 +173,15 @@ void Window::OnPaint(UIEvent* e) {
++frame_count_; ++frame_count_;
++fps_frame_count_; ++fps_frame_count_;
uint64_t now_ns = xe::Clock::QueryHostSystemTime(); static auto tick_frequency = Clock::QueryHostTickFrequency();
if (now_ns > fps_update_time_ns_ + 1000 * 10000) { auto now_ticks = Clock::QueryHostTickCount();
// Average fps over 1 second.
if (now_ticks > fps_update_time_ticks_ + tick_frequency * 1) {
fps_ = static_cast<uint32_t>( fps_ = static_cast<uint32_t>(
fps_frame_count_ / fps_frame_count_ /
(static_cast<double>(now_ns - fps_update_time_ns_) / 10000000.0)); (static_cast<double>(now_ticks - fps_update_time_ticks_) /
fps_update_time_ns_ = now_ns; tick_frequency));
fps_update_time_ticks_ = now_ticks;
fps_frame_count_ = 0; fps_frame_count_ = 0;
} }
@ -186,12 +189,13 @@ void Window::OnPaint(UIEvent* e) {
// Prepare ImGui for use this frame. // Prepare ImGui for use this frame.
auto& io = imgui_drawer_->GetIO(); auto& io = imgui_drawer_->GetIO();
if (!last_paint_time_ns_) { if (!last_paint_time_ticks_) {
io.DeltaTime = 0.0f; io.DeltaTime = 0.0f;
last_paint_time_ns_ = now_ns; last_paint_time_ticks_ = now_ticks;
} else { } else {
io.DeltaTime = (now_ns - last_paint_time_ns_) / 10000000.0f; io.DeltaTime = (now_ticks - last_paint_time_ticks_) /
last_paint_time_ns_ = now_ns; static_cast<float>(tick_frequency);
last_paint_time_ticks_ = now_ticks;
} }
io.DisplaySize = ImVec2(static_cast<float>(scaled_width()), io.DisplaySize = ImVec2(static_cast<float>(scaled_width()),
static_cast<float>(scaled_height())); static_cast<float>(scaled_height()));

View File

@ -180,9 +180,9 @@ class Window {
uint32_t frame_count_ = 0; uint32_t frame_count_ = 0;
uint32_t fps_ = 0; uint32_t fps_ = 0;
uint64_t fps_update_time_ns_ = 0; uint64_t fps_update_time_ticks_ = 0;
uint64_t fps_frame_count_ = 0; uint64_t fps_frame_count_ = 0;
uint64_t last_paint_time_ns_ = 0; uint64_t last_paint_time_ticks_ = 0;
bool modifier_shift_pressed_ = false; bool modifier_shift_pressed_ = false;
bool modifier_cntrl_pressed_ = false; bool modifier_cntrl_pressed_ = false;