clock.rs 959 Bytes
Newer Older
1
2
3
4
5
use crate::utils;

pub struct Clock {
    perf: web_sys::Performance,
    start: f64,
Simon Goller's avatar
Simon Goller committed
6
7
8
    last_tick: f64,
    frame_delta: f32,
    pub max_delta: f32,
9
10
11
12
13
14
15
16
17
}

impl Clock {
    pub fn new() -> Clock {
        let perf = utils::window()
            .performance()
            .expect("cannot access `window.performance`");
        let start = perf.now();

Simon Goller's avatar
Simon Goller committed
18
19
20
21
22
23
24
        Clock {
            perf,
            start,
            frame_delta: 0.03,
            last_tick: start,
            max_delta: 0.1,
        }
25
26
    }

27
    pub fn frame_delta(&self) -> f32 {
Simon Goller's avatar
Simon Goller committed
28
29
30
31
32
33
34
35
        self.frame_delta
    }

    pub fn update(&mut self, timestamp: f64) {
        let timestamp = timestamp / 1000.0;
        self.frame_delta = self.max_delta.min((timestamp - self.last_tick) as f32);
        self.last_tick = timestamp;
        crate::console_log!("Frame delta: {}", self.frame_delta());
36
37
38
39
40
41
    }

    pub fn wall_time(&self) -> f64 {
        (self.perf.now() - self.start) / 1000.0
    }
}