Skip to content

Commit

Permalink
Changes ScheduleRunnerPlugin RunMode::Loop to run on fixed interval (b…
Browse files Browse the repository at this point in the history
…evyengine#233)

* Changes ScheduleRunnerPlugin RunMode::Loop to run on fixed interval

* fix formatting
  • Loading branch information
mfrancis107 authored and BimDav committed Aug 26, 2020
1 parent f67be1e commit 5eb4cc8
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions crates/bevy_app/src/schedule_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use crate::{
event::{EventReader, Events},
plugin::Plugin,
};
use std::{thread, time::Duration};
use std::{
thread,
time::{Duration, Instant},
};

/// Determines the method used to run an [App]'s `Schedule`
#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -51,6 +54,8 @@ impl Plugin for ScheduleRunnerPlugin {
app.schedule.run(&mut app.world, &mut app.resources);
}
RunMode::Loop { wait } => loop {
let start_time = Instant::now();

if let Some(app_exit_events) = app.resources.get_mut::<Events<AppExit>>() {
if app_exit_event_reader.latest(&app_exit_events).is_some() {
break;
Expand All @@ -65,8 +70,13 @@ impl Plugin for ScheduleRunnerPlugin {
}
}

let end_time = Instant::now();

if let Some(wait) = wait {
thread::sleep(wait);
let exe_time = end_time - start_time;
if exe_time < wait {
thread::sleep(wait - exe_time);
}
}
},
}
Expand Down

0 comments on commit 5eb4cc8

Please sign in to comment.