use std::num; use std::sync; use std::sync::atomic; use std::time; fn main() { let stop = sync::Arc::new(atomic::AtomicBool::new(false)); let is_stopped = stop.clone(); ctrlc::set_handler(move || stop.store(true, atomic::Ordering::SeqCst)) .expect("Error setting Ctrl-C handler"); println!("Press Ctrl-C to quit."); let start = time::Instant::now(); let updates_per_second = num::NonZeroU32::new(10).unwrap(); let frames_per_second = num::NonZeroU32::new(6).unwrap(); let clock = chron::Clock::new(updates_per_second) .max_frame_rate(frames_per_second) .max_updates_per_frame(10); let mut updates = 0; let mut renders = 0; for tick in clock { if is_stopped.load(atomic::Ordering::SeqCst) { break; } match tick { chron::Tick::Update => { updates += 1; println!("update {:?}", start.elapsed()); } chron::Tick::Render { interpolation } => { renders += 1; println!("render {:?}, {interpolation}", start.elapsed()); // We can simulate some rendering busy work by sleeping: // // let sleep_time = std::time::Duration::from_millis(16); // std::thread::sleep(sleep_time); // // If we set the `sleep_time` to a value that is higher than the frame time set by // `frames_per_second`, we can see that frames are dropped to maintain the // specified `updates_per_second`. Example: Set `max_frame_rate` to 60 and // `sleep_time` to MORE than 16 ms. } } } let elapsed = start.elapsed(); println!(); println!("elapsed: {:?}", elapsed); println!( "updates/s: {:?}, total: {:?}", updates as f32 / elapsed.as_secs_f32(), updates, ); println!( "frames/s: {:?}, total: {:?}", renders as f32 / elapsed.as_secs_f32(), renders, ); }