2024-01-10 18:53:19 -08:00
|
|
|
//! Keep track of the time we spent in each part of the game loop.
|
2024-01-11 20:21:07 -08:00
|
|
|
use std::time::{Duration, Instant};
|
2024-01-10 18:53:19 -08:00
|
|
|
|
|
|
|
/// Utility struct.
|
|
|
|
/// Keeps track of the time we spent in each part of the game loop.
|
2024-01-11 20:21:07 -08:00
|
|
|
#[derive(Debug, Clone)]
|
2024-01-10 18:53:19 -08:00
|
|
|
pub struct Timing {
|
2024-01-11 20:21:07 -08:00
|
|
|
/// The time we spent on all frame computations
|
|
|
|
pub frame: Duration,
|
2024-01-10 19:47:30 -08:00
|
|
|
frame_timer: Instant,
|
2024-01-10 18:53:19 -08:00
|
|
|
|
2024-01-23 19:03:42 -08:00
|
|
|
/// The total time we spent simulating physics
|
2024-01-11 20:21:07 -08:00
|
|
|
pub physics_sim: Duration,
|
2024-01-10 19:47:30 -08:00
|
|
|
physics_sim_timer: Instant,
|
|
|
|
|
2024-01-23 19:03:42 -08:00
|
|
|
/// The time we spent updating physics state
|
|
|
|
pub physics_step: Duration,
|
|
|
|
physics_step_timer: Instant,
|
|
|
|
|
2024-01-10 19:47:30 -08:00
|
|
|
/// The time we spent updating physics ships
|
2024-01-11 20:21:07 -08:00
|
|
|
pub physics_ship: Duration,
|
2024-01-10 19:47:30 -08:00
|
|
|
physics_ship_timer: Instant,
|
2024-01-23 19:03:42 -08:00
|
|
|
|
|
|
|
/// The time we spent updating physics projectiles
|
|
|
|
pub physics_proj: Duration,
|
|
|
|
physics_proj_timer: Instant,
|
|
|
|
|
|
|
|
/// The time we spent updating effects
|
|
|
|
pub physics_effect: Duration,
|
|
|
|
physics_effect_timer: Instant,
|
2024-01-10 18:53:19 -08:00
|
|
|
}
|
|
|
|
|
2024-01-10 19:47:30 -08:00
|
|
|
// 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-11 20:21:07 -08:00
|
|
|
frame: Duration::ZERO,
|
|
|
|
physics_sim: Duration::ZERO,
|
|
|
|
physics_ship: Duration::ZERO,
|
2024-01-23 19:03:42 -08:00
|
|
|
physics_effect: Duration::ZERO,
|
|
|
|
physics_proj: Duration::ZERO,
|
|
|
|
physics_step: Duration::ZERO,
|
|
|
|
physics_effect_timer: Instant::now(),
|
|
|
|
physics_proj_timer: Instant::now(),
|
|
|
|
physics_step_timer: Instant::now(),
|
2024-01-10 19:47:30 -08:00
|
|
|
physics_sim_timer: Instant::now(),
|
|
|
|
physics_ship_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();
|
|
|
|
}
|
|
|
|
|
2024-01-23 19:03:42 -08:00
|
|
|
/// Record total frame compute time
|
|
|
|
pub fn mark_frame(&mut self) {
|
|
|
|
self.frame = self.frame_timer.elapsed();
|
|
|
|
}
|
|
|
|
|
2024-01-10 19:47:30 -08:00
|
|
|
/// Start physics sim timer
|
|
|
|
pub fn start_physics_sim(&mut self) {
|
|
|
|
self.physics_sim_timer = Instant::now();
|
|
|
|
}
|
|
|
|
|
2024-01-23 19:03:42 -08:00
|
|
|
/// Record physics simulation time
|
|
|
|
pub fn mark_physics_sim(&mut self) {
|
|
|
|
self.physics_sim = self.physics_sim_timer.elapsed();
|
|
|
|
}
|
|
|
|
|
2024-01-10 19:47:30 -08:00
|
|
|
/// Start physics ship timer
|
|
|
|
pub fn start_physics_ships(&mut self) {
|
|
|
|
self.physics_ship_timer = Instant::now();
|
|
|
|
}
|
|
|
|
|
2024-01-23 19:03:42 -08:00
|
|
|
/// Record physics ship update time
|
|
|
|
pub fn mark_physics_ships(&mut self) {
|
|
|
|
self.physics_ship = self.physics_ship_timer.elapsed();
|
2024-01-10 18:53:19 -08:00
|
|
|
}
|
|
|
|
|
2024-01-23 19:03:42 -08:00
|
|
|
/// Start physics step timer
|
|
|
|
pub fn start_physics_step(&mut self) {
|
|
|
|
self.physics_step_timer = Instant::now();
|
2024-01-10 19:47:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Record physics ship update time
|
2024-01-23 19:03:42 -08:00
|
|
|
pub fn mark_physics_step(&mut self) {
|
|
|
|
self.physics_step = self.physics_step_timer.elapsed();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Start physics ship timer
|
|
|
|
pub fn start_physics_proj(&mut self) {
|
|
|
|
self.physics_proj_timer = Instant::now();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Record physics ship update time
|
|
|
|
pub fn mark_physics_proj(&mut self) {
|
|
|
|
self.physics_proj = self.physics_proj_timer.elapsed();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Start physics ship timer
|
|
|
|
pub fn start_physics_effects(&mut self) {
|
|
|
|
self.physics_effect_timer = Instant::now();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Record physics ship update time
|
|
|
|
pub fn mark_physics_effects(&mut self) {
|
|
|
|
self.physics_effect = self.physics_effect_timer.elapsed();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get current timing values as a pretty string
|
|
|
|
pub fn get_string(&self) -> String {
|
|
|
|
let mut s = String::new();
|
|
|
|
//s.push_str(&format!(
|
|
|
|
// "Overall {:6.00} fps\n\n",
|
|
|
|
// 1.0 / self.frame.as_secs_f32()
|
|
|
|
//));
|
|
|
|
|
|
|
|
let f = self.physics_sim.as_secs_f32();
|
|
|
|
s.push_str(&format!("Phys {:6.00} fps\n", 1.0 / f));
|
|
|
|
s.push_str(&format!(
|
2024-01-23 21:21:31 -08:00
|
|
|
"Step {:5.02}%\n",
|
2024-01-23 19:03:42 -08:00
|
|
|
100.0 * self.physics_step.as_secs_f32() / f
|
|
|
|
));
|
|
|
|
s.push_str(&format!(
|
|
|
|
"Proj {:05.02}%\n",
|
|
|
|
100.0 * self.physics_proj.as_secs_f32() / f
|
|
|
|
));
|
|
|
|
s.push_str(&format!(
|
|
|
|
"Ships {:05.02}%\n",
|
|
|
|
100.0 * self.physics_ship.as_secs_f32() / f
|
|
|
|
));
|
|
|
|
s.push_str(&format!(
|
|
|
|
"Efcts {:05.02}%",
|
|
|
|
100.0 * self.physics_effect.as_secs_f32() / f
|
|
|
|
));
|
|
|
|
|
|
|
|
return s;
|
2024-01-10 18:53:19 -08:00
|
|
|
}
|
|
|
|
}
|