From c81d53fa47863d80436ce808b16c836ea6d3e16c Mon Sep 17 00:00:00 2001 From: pommicket Date: Sun, 18 Dec 2022 17:40:43 -0500 Subject: framerate cap --- src/main.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 38898a2..f77a3b7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,5 @@ /* @TODO: -- options for: - - max framerate - come up with twisty lipschitz continuous function, & add it - (slightly) more interesting constants - Params instead of depth for GenRandom @@ -418,6 +416,15 @@ impl State { // returns false if we should quit fn frame(&mut self) -> bool { + if let Some(max_framerate) = self.settings.get_f32("max-framerate") { + if max_framerate > 0.0 { + let dt = self.frame_time.elapsed().as_secs_f32(); + let sleep_millis = 1000.0 * (1.0 / max_framerate - dt); + if sleep_millis >= 1.0 { + std::thread::sleep(std::time::Duration::from_millis(sleep_millis as u64)); + } + } + } let frame_dt = self.frame_time.elapsed().as_secs_f32(); self.frame_time = Instant::now(); -- cgit v1.2.3