Frame stats

master
Mark 2023-12-22 14:13:36 -08:00
parent 3356895353
commit d0e40965d4
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
1 changed files with 29 additions and 9 deletions

View File

@ -62,9 +62,29 @@ impl<'a> DrawContext<'a> {
}
}
// Frame timing:
// <start>
// Input
// Draw
// <draw>
// Physics
// <phys>
// Wait
// <wait>
// Apply input
// <total>
#[derive(Debug, Default)]
struct PerformanceStats {
last_frame_draw: f64,
last_frame_phys: f64,
last_frame_wait: f64,
last_frame_total: f64,
}
fn main() -> Result<(), String> {
let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?;
let mut stats = PerformanceStats::default();
let window = video_subsystem
.window("SDL2", 640, 480)
@ -82,20 +102,15 @@ fn main() -> Result<(), String> {
let sa = SpriteAtlas::new(&texture_creator)?;
let mut event_pump = sdl_context.event_pump()?;
let mut system = System::new();
let mut dc = DrawContext::new(&mut canvas, Camera::new());
let mut i = InputStatus::new();
let mut system = System::new();
let mut s = Ship::new(ShipKind::Gypsum, Cartesian::new(0.0, 0.0));
//let mut last_frame_time = 0f64;
let mut frame_start;
let mut running = true;
let mut dc = DrawContext::new(&mut canvas, Camera::new());
while running {
frame_start = Instant::now();
dc.update();
@ -120,19 +135,22 @@ fn main() -> Result<(), String> {
system.draw(&mut dc, &sa)?;
s.draw(&mut dc, &sa)?;
dc.canvas.present();
stats.last_frame_draw = frame_start.elapsed().as_secs_f64();
// Physics goes here
let frame_time = frame_start.elapsed().as_secs_f64();
stats.last_frame_phys = frame_time;
// Wait
// (limit frame rate)
if frame_time < FTL {
std::thread::sleep(Duration::from_secs_f64(FTL - frame_time));
}
stats.last_frame_wait = frame_start.elapsed().as_secs_f64();
// Apply input
let frame_time = frame_start.elapsed().as_secs_f64();
//last_frame_time = frame_time;
s.body.tick(frame_time);
system.tick(frame_time);
@ -148,6 +166,8 @@ fn main() -> Result<(), String> {
if i.key_left {
s.body.rot(1.0 * frame_time);
}
stats.last_frame_total = frame_start.elapsed().as_secs_f64();
}
Ok(())