Galactica/crates/util/src/timing.rs

99 lines
2.3 KiB
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.
2024-01-10 19:47:30 -08:00
#[derive(Debug)]
2024-01-10 18:53:19 -08:00
pub struct Timing {
2024-01-10 19:47:30 -08:00
/// The time we spent processing the whole frame
pub frame: f32,
frame_timer: Instant,
2024-01-10 18:53:19 -08:00
/// The time we spent simulating game state
pub galaxy: f32,
2024-01-10 19:47:30 -08:00
galaxy_timer: Instant,
2024-01-10 18:53:19 -08:00
/// The time we spent simulating physics
2024-01-10 19:47:30 -08:00
pub physics_sim: f32,
physics_sim_timer: Instant,
/// The time we spent updating physics ships
pub physics_ship: f32,
physics_ship_timer: Instant,
/// The time we spent simulating physics
pub render: f32,
render_timer: Instant,
2024-01-10 18:53:19 -08:00
}
2024-01-10 19:47:30 -08:00
// TODO: macros
// TODO: document each duration
2024-01-10 18:53:19 -08:00
impl Timing {
/// Create a new timing struct
pub fn new() -> Self {
Self {
2024-01-10 19:47:30 -08:00
frame: f32::NAN,
2024-01-10 18:53:19 -08:00
galaxy: f32::NAN,
2024-01-10 19:47:30 -08:00
physics_sim: f32::NAN,
physics_ship: f32::NAN,
render: f32::NAN,
galaxy_timer: Instant::now(),
physics_sim_timer: Instant::now(),
physics_ship_timer: Instant::now(),
render_timer: Instant::now(),
frame_timer: Instant::now(),
2024-01-10 18:53:19 -08:00
}
}
2024-01-10 19:47:30 -08:00
/// Start frame timer
pub fn start_frame(&mut self) {
self.frame_timer = Instant::now();
}
/// Start galaxy timer
pub fn start_galaxy(&mut self) {
self.galaxy_timer = Instant::now();
}
/// Start physics sim timer
pub fn start_physics_sim(&mut self) {
self.physics_sim_timer = Instant::now();
}
/// Start physics ship timer
pub fn start_physics_ships(&mut self) {
self.physics_ship_timer = Instant::now();
}
/// Start render timer
pub fn start_render(&mut self) {
self.render_timer = Instant::now();
}
/// Record galaxy simulation time.
pub fn mark_frame(&mut self) {
self.frame = self.frame_timer.elapsed().as_secs_f32();
2024-01-10 18:53:19 -08:00
}
2024-01-10 19:47:30 -08:00
/// Record galaxy simulation time.
2024-01-10 18:53:19 -08:00
pub fn mark_galaxy(&mut self) {
2024-01-10 19:47:30 -08:00
self.galaxy = self.galaxy_timer.elapsed().as_secs_f32();
}
/// Record physics simulation time
pub fn mark_physics_sim(&mut self) {
self.physics_sim = self.physics_sim_timer.elapsed().as_secs_f32();
}
/// Record physics ship update time
pub fn mark_physics_ships(&mut self) {
self.physics_ship = self.physics_ship_timer.elapsed().as_secs_f32();
2024-01-10 18:53:19 -08:00
}
2024-01-10 19:47:30 -08:00
/// Record physics simulation time
pub fn mark_render(&mut self) {
self.render = self.render_timer.elapsed().as_secs_f32();
2024-01-10 18:53:19 -08:00
}
}