Skip to content

Commit

Permalink
Delegate frame rate measuring to nannou
Browse files Browse the repository at this point in the history
  • Loading branch information
palant committed Jun 7, 2022
1 parent 2603d27 commit ffdb9cc
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ struct Model<T> {
image: nannou::image::DynamicImage,
window_width: u32,
window_height: u32,
measure_framerate: bool,
measure_start: std::time::Instant,
counter: usize,
display_framerate: bool,
}

fn model<T: 'static + Fighter + GenerateRandomly>(app: &App) -> Model<T> {
Expand Down Expand Up @@ -91,9 +89,7 @@ fn model<T: 'static + Fighter + GenerateRandomly>(app: &App) -> Model<T> {
)),
window_width: img_width as u32,
window_height: img_height as u32,
measure_framerate: args.framerate,
measure_start: std::time::Instant::now(),
counter: 0,
display_framerate: args.framerate,
}
}

Expand All @@ -109,18 +105,6 @@ fn update<T: Fighter + Colored>(_app: &App, model: &mut Model<T>, _update: Updat
}
}
}

if model.measure_framerate {
const MEASURE_FRAMES_COUNT: usize = 100;

model.counter += 1;
if model.counter % MEASURE_FRAMES_COUNT == 0 {
let framerate =
MEASURE_FRAMES_COUNT as f32 / model.measure_start.elapsed().as_secs_f32();
println!("Frame rate: {:.2}", framerate);
model.measure_start = std::time::Instant::now();
}
}
}

fn view<T>(app: &App, model: &Model<T>, frame: Frame) {
Expand All @@ -144,6 +128,10 @@ fn view<T>(app: &App, model: &Model<T>, frame: Frame) {
.height(ratio * image_height as f32);
}
draw.to_frame(app, &frame).unwrap();

if model.display_framerate && app.elapsed_frames() % 100 == 99 {
println!("Frame rate: {:.2}", app.fps());
}
}

fn event<T>(app: &App, model: &mut Model<T>, ev: nannou::event::WindowEvent) {
Expand Down

0 comments on commit ffdb9cc

Please sign in to comment.