Galactica/crates/util/src/timing.rs

45 lines
1005 B
Rust
Raw Normal View History

2024-01-10 18:53:19 -08:00
//! Keep track of the time we spent in each part of the game loop.
use std::time::Instant;
/// Utility struct.
/// Keeps track of the time we spent in each part of the game loop.
pub struct Timing {
timer: Option<Instant>,
/// The time we spent simulating game state
pub galaxy: f32,
/// The time we spent simulating physics
pub physics: f32,
}
impl Timing {
/// Create a new timing struct
pub fn new() -> Self {
Self {
timer: None,
galaxy: f32::NAN,
physics: f32::NAN,
}
}
/// Start the timer
pub fn start(&mut self) {
self.timer = Some(Instant::now());
}
/// Clear timer and record galaxy simulation time.
/// Assumes timer has been started
pub fn mark_galaxy(&mut self) {
self.galaxy = self.timer.unwrap().elapsed().as_secs_f32();
self.timer = None;
}
/// Clear timer and record physics simulation time
/// Asumes timer has been started
pub fn mark_physics(&mut self) {
self.physics = self.timer.unwrap().elapsed().as_secs_f32();
self.timer = None;
}
}