Skip to content

Commit 70c9165

Browse files
committed
Fix crash with disabled winit (#3330)
# Objective This PR fixes a crash when winit is enabled when there is a camera in the world. Part of #3155 ## Solution In this PR, I removed two unwraps and added an example for regression testing.
1 parent c061ec3 commit 70c9165

File tree

5 files changed

+27
-5
lines changed

5 files changed

+27
-5
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ path = "examples/app/return_after_run.rs"
228228
name = "thread_pool_resources"
229229
path = "examples/app/thread_pool_resources.rs"
230230

231+
[[example]]
232+
name = "without_winit"
233+
path = "examples/app/without_winit.rs"
234+
231235
# Assets
232236
[[example]]
233237
name = "asset_loading"

crates/bevy_core_pipeline/src/main_pass_3d.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ impl Node for MainPass3dNode {
4747
world: &World,
4848
) -> Result<(), NodeRunError> {
4949
let view_entity = graph.get_input_entity(Self::IN_VIEW)?;
50-
let (opaque_phase, alpha_mask_phase, transparent_phase, target, depth) = self
51-
.query
52-
.get_manual(world, view_entity)
53-
.expect("view entity should exist");
50+
let (opaque_phase, alpha_mask_phase, transparent_phase, target, depth) =
51+
match self.query.get_manual(world, view_entity) {
52+
Ok(query) => query,
53+
Err(_) => return Ok(()), // No window
54+
};
5455

5556
{
5657
// Run the opaque pass, sorted front-to-back

crates/bevy_pbr/src/light.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,10 @@ pub fn add_clusters(
341341
cameras: Query<(Entity, &Camera), Without<Clusters>>,
342342
) {
343343
for (entity, camera) in cameras.iter() {
344-
let window = windows.get(camera.window).unwrap();
344+
let window = match windows.get(camera.window) {
345+
Some(window) => window,
346+
None => continue,
347+
};
345348
let clusters = Clusters::from_screen_size_and_z_slices(
346349
UVec2::new(window.physical_width(), window.physical_height()),
347350
Z_SLICES,

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ Example | File | Description
122122
`plugin_group` | [`app/plugin_group.rs`](./app/plugin_group.rs) | Demonstrates the creation and registration of a custom plugin group
123123
`return_after_run` | [`app/return_after_run.rs`](./app/return_after_run.rs) | Show how to return to main after the Bevy app has exited
124124
`thread_pool_resources` | [`app/thread_pool_resources.rs`](./app/thread_pool_resources.rs) | Creates and customizes the internal thread pool
125+
`without_winit` | [`app/without_winit.rs`](./app/without_winit.rs) | Create an application without winit (runs single time, no event loop)
125126

126127
## Assets
127128

examples/app/without_winit.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use bevy::prelude::*;
2+
use bevy::winit::WinitPlugin;
3+
4+
fn main() {
5+
App::new()
6+
.add_plugins_with(DefaultPlugins, |group| group.disable::<WinitPlugin>())
7+
.add_system(setup_system)
8+
.run();
9+
}
10+
11+
fn setup_system(mut commands: Commands) {
12+
commands.spawn_bundle(PerspectiveCameraBundle::default());
13+
}

0 commit comments

Comments
 (0)