Skip to content

Commit

Permalink
Clean up frametime calculation and UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Yatekii committed Nov 8, 2024
1 parent c695069 commit 3b42e9b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
10 changes: 5 additions & 5 deletions src/bin/drawing/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,14 @@ impl HudUi {
});

ui.label(&format!(
"Mouse Position: ({:.1},{:.1})",
pointer_position[0], pointer_position[1]
"Frametime {:.2?} at zoom {:.2}",
app_state.stats.get_average(),
app_state.zoom
));

ui.label(&format!(
"Frametime {:.2} at zoom {:.2}",
app_state.stats.get_average(),
app_state.zoom
"Mouse Position: ({:.1},{:.1})",
pointer_position[0], pointer_position[1]
));
});
});
Expand Down
4 changes: 1 addition & 3 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ fn main() {
let mut last_pos = winit::dpi::LogicalPosition::new(0.0, 0.0);

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;

let ui_event = hud.interact(&event);
match event {
Event::WindowEvent { event, .. } => match event {
Expand Down Expand Up @@ -157,7 +155,7 @@ fn main() {
app_state.stats.capture_frame();
if CONFIG.general.display_framerate {
println!(
"Frametime {:.2} at zoom {:.2}",
"Frametime {:.2?} at zoom {:.2}",
app_state.stats.get_average(),
app_state.zoom
);
Expand Down
19 changes: 8 additions & 11 deletions src/bin/stats.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::time::Duration;

pub struct Stats {
stamp: std::time::Instant,
last_frametimes: std::collections::VecDeque<u64>,
last_frametimes: std::collections::VecDeque<Duration>,
frames: u64,
}

Expand All @@ -10,8 +12,8 @@ impl Stats {
stamp: std::time::Instant::now(),
last_frametimes: {
let mut dq = std::collections::VecDeque::new();
for i in 0..30 {
dq.push_back(i);
for _ in 0..30 {
dq.push_back(Duration::default());
}
dq
},
Expand All @@ -21,17 +23,12 @@ impl Stats {

pub fn capture_frame(&mut self) {
self.last_frametimes.pop_front();
self.last_frametimes
.push_back(self.stamp.elapsed().as_micros() as u64);
self.last_frametimes.push_back(self.stamp.elapsed());
self.frames += 1;
self.stamp = std::time::Instant::now();
}

pub fn get_average(&self) -> f64 {
self.last_frametimes.iter().sum::<u64>() as f64 / 30.0
}

pub fn _get_last_delta(&self) -> f32 {
(self.last_frametimes[29] as f64 / 1_000_000f64) as f32
pub fn get_average(&self) -> Duration {
self.last_frametimes.iter().sum::<Duration>() / 30
}
}

0 comments on commit 3b42e9b

Please sign in to comment.