New timer struct.
Fix frame rate for animated sprites.
This commit is contained in:
22
src/timer.rs
22
src/timer.rs
@@ -1,5 +1,21 @@
|
||||
extern crate time;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
pub fn current_time() -> f64 {
|
||||
(time::precise_time_ns() / 1_000_000) as f64
|
||||
|
||||
pub struct Timer {
|
||||
time: Instant
|
||||
}
|
||||
|
||||
impl Timer {
|
||||
pub fn new() -> Timer {
|
||||
Timer {
|
||||
time: Instant::now()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_time(&self) -> f64 {
|
||||
let secs = self.time.elapsed().as_secs() as f64;
|
||||
let nanos = self.time.elapsed().subsec_nanos() as f64;
|
||||
|
||||
secs * 1_000.0 + nanos / 1_000_000.0
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user