Forráskód Böngészése

added fps smoothing, plus reduction of data

Ghastrod 1 éve
szülő
commit
ea05bab4c3
1 módosított fájl, 19 hozzáadás és 5 törlés
  1. 19 5
      src/lib.rs

+ 19 - 5
src/lib.rs

@@ -37,11 +37,11 @@ struct State<'a> {
     last_frame: Instant,
     framecount: u64,
 
-
     frametime: f64,
     old_frame_times: Vec<f64>,
     max_history_size: usize,
     //sky_pipeline: wgpu::RenderPipeline,
+    last_fps: Instant,
 }
 
 impl<'a> State<'a> {
@@ -181,7 +181,7 @@ impl<'a> State<'a> {
         let framecount: u64 = 0;
         let old_frame_times : Vec<f64> = vec![];
         let max_history: usize = 60;
-
+        let last_fps = Instant::now();
         Self {
             surface,
             vertex_buffer,
@@ -202,7 +202,8 @@ impl<'a> State<'a> {
             //sky_pipeline: sky_pipeline
             framecount,
             old_frame_times: old_frame_times,
-            max_history_size: max_history
+            max_history_size: max_history,
+            last_fps: last_fps
         }
     }
 
@@ -233,6 +234,14 @@ impl<'a> State<'a> {
             0,
             bytemuck::cast_slice(&[self.camera_uniform]),
         );
+
+
+
+        // self.queue.write_buffer(
+        //     &self.vertex_buffer,
+        //     0,
+        //     bytemuck::cast_slice(todo!())
+        // );  
     }
     fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
         let output = self.surface.get_current_texture()?;
@@ -281,9 +290,12 @@ impl<'a> State<'a> {
         self.queue.submit(iter::once(encoder.finish()));
         output.present();
 
+
+        //FPS logic
         self.frametime = (self.last_frame.elapsed().as_secs() as f64) + (self.last_frame.elapsed().as_nanos() as f64 / 1_000_000_000.0);
         self.last_frame = Instant::now();
         
+        let interval_fps = Duration::from_millis(100);
 
         self.old_frame_times.push(self.frametime);
         if self.old_frame_times.len() > self.max_history_size {
@@ -292,8 +304,10 @@ impl<'a> State<'a> {
         let smoothed_frame_time = self.old_frame_times.iter().sum::<f64>() / self.old_frame_times.len() as f64;
         let smoothed_fps = 1.0 / smoothed_frame_time;
 
-        println!("{:?}", smoothed_fps);
-
+        if self.last_fps.elapsed() > interval_fps{
+            self.last_fps = Instant::now();
+            println!("{:?}", smoothed_fps.ceil());
+        }
         Ok(())
     }
 }