You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
951 B

  1. #![allow(dead_code)]
  2. use std::mem::take;
  3. use std::time::Instant;
  4. use specs::{System, Write};
  5. use crate::resources::Global;
  6. pub struct Process {
  7. last_frame: Instant,
  8. time: f32,
  9. count: usize,
  10. }
  11. impl Default for Process {
  12. fn default() -> Self {
  13. Self {
  14. last_frame: Instant::now(),
  15. time: 0.0,
  16. count: 0,
  17. }
  18. }
  19. }
  20. impl<'a> System<'a> for Process {
  21. type SystemData = Write<'a, Global>;
  22. fn run(&mut self, mut global: Self::SystemData) {
  23. let now = Instant::now();
  24. global.delta = (now - self.last_frame).as_secs_f32();
  25. self.last_frame = now;
  26. self.time += global.delta;
  27. self.count += 1;
  28. if self.time >= 2.0 {
  29. self.time = 0.0;
  30. self.count = 0;
  31. global.fps = 0;
  32. } else if self.time >= 1.0 {
  33. self.time -= 1.0;
  34. global.fps = take(&mut self.count);
  35. }
  36. }
  37. }