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.
This commit is contained in:
Hennadii Chernyshchyk 2021-12-15 00:15:47 +00:00
parent c061ec33c8
commit 70c9165886
5 changed files with 27 additions and 5 deletions

View file

@ -228,6 +228,10 @@ path = "examples/app/return_after_run.rs"
name = "thread_pool_resources"
path = "examples/app/thread_pool_resources.rs"
[[example]]
name = "without_winit"
path = "examples/app/without_winit.rs"
# Assets
[[example]]
name = "asset_loading"

View file

@ -47,10 +47,11 @@ impl Node for MainPass3dNode {
world: &World,
) -> Result<(), NodeRunError> {
let view_entity = graph.get_input_entity(Self::IN_VIEW)?;
let (opaque_phase, alpha_mask_phase, transparent_phase, target, depth) = self
.query
.get_manual(world, view_entity)
.expect("view entity should exist");
let (opaque_phase, alpha_mask_phase, transparent_phase, target, depth) =
match self.query.get_manual(world, view_entity) {
Ok(query) => query,
Err(_) => return Ok(()), // No window
};
{
// Run the opaque pass, sorted front-to-back

View file

@ -341,7 +341,10 @@ pub fn add_clusters(
cameras: Query<(Entity, &Camera), Without<Clusters>>,
) {
for (entity, camera) in cameras.iter() {
let window = windows.get(camera.window).unwrap();
let window = match windows.get(camera.window) {
Some(window) => window,
None => continue,
};
let clusters = Clusters::from_screen_size_and_z_slices(
UVec2::new(window.physical_width(), window.physical_height()),
Z_SLICES,

View file

@ -122,6 +122,7 @@ Example | File | Description
`plugin_group` | [`app/plugin_group.rs`](./app/plugin_group.rs) | Demonstrates the creation and registration of a custom plugin group
`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
`thread_pool_resources` | [`app/thread_pool_resources.rs`](./app/thread_pool_resources.rs) | Creates and customizes the internal thread pool
`without_winit` | [`app/without_winit.rs`](./app/without_winit.rs) | Create an application without winit (runs single time, no event loop)
## Assets

View file

@ -0,0 +1,13 @@
use bevy::prelude::*;
use bevy::winit::WinitPlugin;
fn main() {
App::new()
.add_plugins_with(DefaultPlugins, |group| group.disable::<WinitPlugin>())
.add_system(setup_system)
.run();
}
fn setup_system(mut commands: Commands) {
commands.spawn_bundle(PerspectiveCameraBundle::default());
}