@@ -2,6 +2,7 @@ use std::{
22 env,
33 fs:: File ,
44 io:: BufReader ,
5+ num:: NonZeroU32 ,
56 ops:: Deref ,
67 process, thread,
78 time:: { Duration , Instant } ,
@@ -26,28 +27,33 @@ mod multi_window;
2627pub use multi_window:: MultiWindow ;
2728
2829pub ( crate ) struct FpsLimiter {
29- max_fps : u32 ,
30+ max_fps : Option < NonZeroU32 > ,
3031 frame_start : Instant ,
3132}
3233
3334impl FpsLimiter {
3435 pub ( crate ) fn new ( ) -> Self {
3536 Self {
36- max_fps : 60 ,
37+ max_fps : NonZeroU32 :: new ( 60 ) ,
3738 frame_start : Instant :: now ( ) ,
3839 }
3940 }
4041
41- fn desired_loop_duration ( & self ) -> Duration {
42- Duration :: from_secs_f32 ( 1.0 / self . max_fps as f32 )
42+ fn desired_loop_duration ( max_fps : NonZeroU32 ) -> Duration {
43+ Duration :: from_secs_f32 ( 1.0 / max_fps. get ( ) as f32 )
4344 }
4445
4546 fn sleep ( & mut self ) {
46- let sleep_duration = ( self . frame_start + self . desired_loop_duration ( ) )
47- . saturating_duration_since ( Instant :: now ( ) ) ;
48- thread:: sleep ( sleep_duration) ;
47+ match self . max_fps {
48+ Some ( max_fps) => {
49+ let sleep_duration = ( self . frame_start + Self :: desired_loop_duration ( max_fps) )
50+ . saturating_duration_since ( Instant :: now ( ) ) ;
51+ thread:: sleep ( sleep_duration) ;
4952
50- self . frame_start = Instant :: now ( ) ;
53+ self . frame_start = Instant :: now ( ) ;
54+ }
55+ None => { }
56+ }
5157 }
5258}
5359
@@ -204,6 +210,6 @@ impl Window {
204210
205211 /// Sets the FPS limit of the window.
206212 pub fn set_max_fps ( & mut self , max_fps : u32 ) {
207- self . fps_limiter . max_fps = max_fps;
213+ self . fps_limiter . max_fps = NonZeroU32 :: new ( max_fps) ;
208214 }
209215}
0 commit comments