Skip to content

Commit

Permalink
fix: cannot move when no gl context
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximetinu committed Feb 12, 2024
1 parent 7f09dac commit 7679db8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
24 changes: 19 additions & 5 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub struct App {
/// This is initially set to [`Main`].
pub main_schedule_label: InternedScheduleLabel,
sub_apps: HashMap<InternedAppLabel, SubApp>,
run_sub_apps: bool,
plugin_registry: Vec<Box<dyn Plugin>>,
plugin_name_added: HashSet<String>,
/// A private counter to prevent incorrect calls to `App::run()` from `Plugin::build()`
Expand Down Expand Up @@ -238,6 +239,7 @@ impl App {
world,
runner: Box::new(run_once),
sub_apps: HashMap::default(),
run_sub_apps: true,
plugin_registry: Vec::default(),
plugin_name_added: Default::default(),
main_schedule_label: Main.intern(),
Expand Down Expand Up @@ -265,11 +267,13 @@ impl App {
let _bevy_main_update_span = info_span!("main app").entered();
self.world.run_schedule(self.main_schedule_label);
}
for (_label, sub_app) in &mut self.sub_apps {
#[cfg(feature = "trace")]
let _sub_app_span = info_span!("sub app", name = ?_label).entered();
sub_app.extract(&mut self.world);
sub_app.run();
if self.run_sub_apps {
for (_label, sub_app) in &mut self.sub_apps {
#[cfg(feature = "trace")]
let _sub_app_span = info_span!("sub app", name = ?_label).entered();
sub_app.extract(&mut self.world);
sub_app.run();
}
}

self.world.clear_trackers();
Expand Down Expand Up @@ -815,6 +819,16 @@ impl App {
self.sub_apps.insert(label.intern(), sub_app);
}

/// Resumes sub apps
pub fn resume_sub_apps(&mut self) {
self.run_sub_apps = true;
}

/// Pauses sub apps from running.
pub fn pause_sub_apps(&mut self) {
self.run_sub_apps = false;
}

/// Removes a sub app from the app. Returns [`None`] if the label doesn't exist.
pub fn remove_sub_app(&mut self, label: impl AppLabel) -> Option<SubApp> {
self.sub_apps.remove(&label.intern())
Expand Down
6 changes: 4 additions & 2 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ pub fn winit_runner(mut app: App) {
}
let (config, windows) = focused_windows_state.get(&app.world);
let focused = windows.iter().any(|(_, window)| window.focused);
let mut should_update = match config.update_mode(focused) {
let should_update = match config.update_mode(focused) {
UpdateMode::Continuous | UpdateMode::Reactive { .. } => {
// `Reactive`: In order for `event_handler` to have been called, either
// we received a window or raw input event, the `wait` elapsed, or a
Expand Down Expand Up @@ -861,7 +861,9 @@ pub fn winit_runner(mut app: App) {
{
if !has_gl_context(&window) {
app.world.send_event(WindowGlContextLost { window: entity });
should_update = false;
// HEP: pause sub-apps to stop WGPU from crashing when there's no OpenGL context.
// Ensures that the rest of the systems in the main app keep running (i.e. physics).
app.pause_sub_apps();
}
}
}
Expand Down

0 comments on commit 7679db8

Please sign in to comment.