Frame stats
parent
3356895353
commit
d0e40965d4
38
src/main.rs
38
src/main.rs
|
@ -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> {
|
fn main() -> Result<(), String> {
|
||||||
let sdl_context = sdl2::init()?;
|
let sdl_context = sdl2::init()?;
|
||||||
let video_subsystem = sdl_context.video()?;
|
let video_subsystem = sdl_context.video()?;
|
||||||
|
let mut stats = PerformanceStats::default();
|
||||||
|
|
||||||
let window = video_subsystem
|
let window = video_subsystem
|
||||||
.window("SDL2", 640, 480)
|
.window("SDL2", 640, 480)
|
||||||
|
@ -82,20 +102,15 @@ fn main() -> Result<(), String> {
|
||||||
|
|
||||||
let sa = SpriteAtlas::new(&texture_creator)?;
|
let sa = SpriteAtlas::new(&texture_creator)?;
|
||||||
let mut event_pump = sdl_context.event_pump()?;
|
let mut event_pump = sdl_context.event_pump()?;
|
||||||
|
let mut dc = DrawContext::new(&mut canvas, Camera::new());
|
||||||
let mut system = System::new();
|
|
||||||
|
|
||||||
let mut i = InputStatus::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 s = Ship::new(ShipKind::Gypsum, Cartesian::new(0.0, 0.0));
|
||||||
|
|
||||||
//let mut last_frame_time = 0f64;
|
|
||||||
let mut frame_start;
|
let mut frame_start;
|
||||||
|
|
||||||
let mut running = true;
|
let mut running = true;
|
||||||
|
|
||||||
let mut dc = DrawContext::new(&mut canvas, Camera::new());
|
|
||||||
|
|
||||||
while running {
|
while running {
|
||||||
frame_start = Instant::now();
|
frame_start = Instant::now();
|
||||||
dc.update();
|
dc.update();
|
||||||
|
@ -120,19 +135,22 @@ fn main() -> Result<(), String> {
|
||||||
system.draw(&mut dc, &sa)?;
|
system.draw(&mut dc, &sa)?;
|
||||||
s.draw(&mut dc, &sa)?;
|
s.draw(&mut dc, &sa)?;
|
||||||
dc.canvas.present();
|
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();
|
let frame_time = frame_start.elapsed().as_secs_f64();
|
||||||
|
stats.last_frame_phys = frame_time;
|
||||||
|
|
||||||
// Wait
|
// Wait
|
||||||
// (limit frame rate)
|
// (limit frame rate)
|
||||||
if frame_time < FTL {
|
if frame_time < FTL {
|
||||||
std::thread::sleep(Duration::from_secs_f64(FTL - frame_time));
|
std::thread::sleep(Duration::from_secs_f64(FTL - frame_time));
|
||||||
}
|
}
|
||||||
|
stats.last_frame_wait = frame_start.elapsed().as_secs_f64();
|
||||||
|
|
||||||
// Apply input
|
// Apply input
|
||||||
|
|
||||||
let frame_time = frame_start.elapsed().as_secs_f64();
|
let frame_time = frame_start.elapsed().as_secs_f64();
|
||||||
//last_frame_time = frame_time;
|
|
||||||
|
|
||||||
s.body.tick(frame_time);
|
s.body.tick(frame_time);
|
||||||
system.tick(frame_time);
|
system.tick(frame_time);
|
||||||
|
@ -148,6 +166,8 @@ fn main() -> Result<(), String> {
|
||||||
if i.key_left {
|
if i.key_left {
|
||||||
s.body.rot(1.0 * frame_time);
|
s.body.rot(1.0 * frame_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stats.last_frame_total = frame_start.elapsed().as_secs_f64();
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in New Issue