wasm4fun_time/ticker.rs
1// Copyright Claudio Mattera 2022.
2//
3// Distributed under the MIT License or the Apache 2.0 License at your option.
4// See the accompanying files License-MIT.txt and License-Apache-2.0.txt, or
5// online at
6// https://opensource.org/licenses/MIT
7// https://opensource.org/licenses/Apache-2.0
8
9/// A ticker to keep track of the current frame from start
10///
11/// WASM-4 console runs at 60 frames per second.
12/// This object can be used to find out what is the current frame number since
13/// the console startup, or within the current second.
14pub struct Ticker;
15
16static mut TICKER_COUNTER: u64 = 0;
17
18impl Ticker {
19 /// Update the ticker
20 ///
21 /// This function must be called at the end of each frame.
22 #[inline]
23 pub fn update(&mut self) {
24 unsafe {
25 TICKER_COUNTER += 1;
26 }
27 }
28
29 /// Get the current frame number since console startup
30 #[inline]
31 pub fn since_startup(&self) -> u64 {
32 unsafe { TICKER_COUNTER }
33 }
34
35 /// Get the current frame number within current second
36 ///
37 /// The frame number will be in range [0; 60).
38 #[inline]
39 pub fn within_second(&self) -> u8 {
40 unsafe { (TICKER_COUNTER % 60) as u8 }
41 }
42}