Skip to main content

speedy2d/
time.rs

1/*
2 *  Copyright 2021 QuantumBadger
3 *
4 *  Licensed under the Apache License, Version 2.0 (the "License");
5 *  you may not use this file except in compliance with the License.
6 *  You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *  Unless required by applicable law or agreed to in writing, software
11 *  distributed under the License is distributed on an "AS IS" BASIS,
12 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 *  See the License for the specific language governing permissions and
14 *  limitations under the License.
15 */
16
17#[cfg(not(target_arch = "wasm32"))]
18use std::time::Instant;
19
20use crate::error::{BacktraceError, ErrorMessage};
21#[cfg(target_arch = "wasm32")]
22use crate::web::{WebPerformance, WebWindow};
23
24/// Measures the amount of time elapsed since its creation.
25pub struct Stopwatch
26{
27    clock: TimeClock,
28    start: TimeInstant
29}
30
31impl Stopwatch
32{
33    /// Creates a new Stopwatch, starting at the current time.
34    #[inline]
35    pub fn new() -> Result<Self, BacktraceError<ErrorMessage>>
36    {
37        let clock = TimeClock::new()?;
38        let start = clock.now();
39
40        Ok(Self { clock, start })
41    }
42
43    /// Returns the number of seconds since the Stopwatch was created.
44    #[inline]
45    pub fn secs_elapsed(&self) -> f64
46    {
47        self.clock.secs_elapsed_since(&self.start)
48    }
49}
50
51/// Allows access to the system clock.
52#[derive(Clone)]
53struct TimeClock
54{
55    #[cfg(target_arch = "wasm32")]
56    performance: WebPerformance
57}
58
59impl TimeClock
60{
61    /// Creates a new TimeClock.
62    pub fn new() -> Result<Self, BacktraceError<ErrorMessage>>
63    {
64        #[cfg(target_arch = "wasm32")]
65        return Ok(Self {
66            performance: WebWindow::new()?.performance()?
67        });
68
69        #[cfg(not(target_arch = "wasm32"))]
70        return Ok(Self {});
71    }
72
73    /// Returns a [TimeInstant] representing the current time.
74    #[inline]
75    pub fn now(&self) -> TimeInstant
76    {
77        #[cfg(target_arch = "wasm32")]
78        return TimeInstant {
79            value: self.performance.now()
80        };
81
82        #[cfg(not(target_arch = "wasm32"))]
83        return TimeInstant {
84            value: Instant::now()
85        };
86    }
87
88    /// Returns the difference in seconds between the current time, and the
89    /// provided [TimeInstant].
90    #[inline]
91    pub fn secs_elapsed_since(&self, start: &TimeInstant) -> f64
92    {
93        #[cfg(target_arch = "wasm32")]
94        return (self.now().value - start.value) / 1000.0;
95
96        #[cfg(not(target_arch = "wasm32"))]
97        return start.value.elapsed().as_secs_f64();
98    }
99}
100
101/// Represents an instant in time.
102struct TimeInstant
103{
104    #[cfg(target_arch = "wasm32")]
105    value: f64,
106
107    #[cfg(not(target_arch = "wasm32"))]
108    value: Instant
109}