Change update_delta_time to accept a Duration

Fixes #333
This commit is contained in:
Joonas Javanainen 2020-07-07 23:07:56 +03:00
parent 2e8f95fb09
commit c575dec15b
No known key found for this signature in database
GPG Key ID: D39CCA5CB19B9179
3 changed files with 10 additions and 7 deletions

View File

@ -95,7 +95,11 @@ impl System {
let mut last_frame = Instant::now();
event_loop.run(move |event, _, control_flow| match event {
Event::NewEvents(_) => last_frame = imgui.io_mut().update_delta_time(last_frame),
Event::NewEvents(_) => {
let now = Instant::now();
imgui.io_mut().update_delta_time(now - last_frame);
last_frame = now;
}
Event::MainEventsCleared => {
let gl_window = display.gl_window();
platform

View File

@ -94,7 +94,9 @@ impl System {
platform
.prepare_frame(io, render_sys.window())
.expect("Failed to start frame");
last_frame = io.update_delta_time(last_frame);
let now = Instant::now();
io.update_delta_time(now - last_frame);
last_frame = now;
let mut ui = imgui.frame();
run_ui(&mut run, &mut ui);

View File

@ -2,7 +2,7 @@ use bitflags::bitflags;
use std::f32;
use std::ops::{Index, IndexMut};
use std::os::raw::{c_char, c_int, c_void};
use std::time::Instant;
use std::time::Duration;
use crate::fonts::atlas::FontAtlas;
use crate::fonts::font::Font;
@ -327,9 +327,7 @@ impl Io {
sys::ImGuiIO_ClearInputCharacters(self.raw_mut());
}
}
pub fn update_delta_time(&mut self, previous: Instant) -> Instant {
let now = Instant::now();
let delta = now - previous;
pub fn update_delta_time(&mut self, delta: Duration) {
let delta_s = delta.as_secs() as f32 + delta.subsec_nanos() as f32 / 1_000_000_000.0;
if delta_s > 0.0 {
self.delta_time = delta_s;
@ -337,7 +335,6 @@ impl Io {
self.delta_time = f32::MIN_POSITIVE;
}
self.delta_time = delta_s;
now
}
}