renderbug: first implementation of surface-based rendering
This commit is contained in:
30
src/time.rs
Normal file
30
src/time.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use std::time::{Instant, Duration};
|
||||
|
||||
pub struct Periodically {
|
||||
last_run: Instant,
|
||||
duration: Duration
|
||||
}
|
||||
|
||||
impl Periodically {
|
||||
pub fn new(duration: Duration) -> Self {
|
||||
Self {
|
||||
last_run: Instant::now(),
|
||||
duration: duration
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_every_n_seconds(seconds: u64) -> Self {
|
||||
Self::new(Duration::new(seconds, 0))
|
||||
}
|
||||
|
||||
pub fn new_every_n_ms(milliseconds: u32) -> Self {
|
||||
Self::new(Duration::new(0, milliseconds*1000))
|
||||
}
|
||||
|
||||
pub fn run<F>(&mut self, f: F) where F: FnOnce() {
|
||||
if self.last_run.elapsed() >= self.duration {
|
||||
f();
|
||||
self.last_run = Instant::now();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user