Skip to content

Commit

Permalink
Replace pipeline status code with bevy_pipelines_ready
Browse files Browse the repository at this point in the history
  • Loading branch information
rparrett committed Dec 21, 2023
1 parent 8d2cb77 commit b86bb0c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 59 deletions.
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ bevy-inspector-egui = { version = "0.21", optional = true }
leafwing-input-manager = "0.11"
bevy-ui-navigation = "0.33.0"
bevy_mod_debugdump = { version = "0.9.0", optional = true }
bevy_pipelines_ready = "0.2.0"

bevy-tnua = "0.13.0"
bevy-tnua-physics-integration-layer = "0.1.0"
Expand All @@ -26,7 +27,6 @@ bevy-tnua-rapier3d = "0.1.0"
rand = "0.8.5"
serde = "*"
ron = "0.8.0"
crossbeam-channel = "0.5.8"

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = { version = "*", features = ["console", "Window", "Storage"] }
79 changes: 22 additions & 57 deletions src/loading.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
use bevy::{
pbr::CascadeShadowConfigBuilder,
prelude::*,
render::{
render_resource::{CachedPipelineState, PipelineCache},
Render, RenderApp, RenderSet,
},
};
use bevy::{pbr::CascadeShadowConfigBuilder, prelude::*};
use bevy_asset_loader::prelude::*;
use crossbeam_channel::Receiver;
use bevy_pipelines_ready::{PipelinesReady, PipelinesReadyPlugin};

use crate::GameState;

Expand Down Expand Up @@ -70,57 +63,27 @@ pub struct Images {
pub heart: Handle<Image>,
}

#[derive(Resource)]
struct PipelineStatus(Receiver<bool>);

#[cfg(not(target_arch = "wasm32"))]
const EXPECTED_PIPELINES: usize = 14;
const EXPECTED_PIPELINES: usize = 15;
#[cfg(target_arch = "wasm32")]
const EXPECTED_PIPELINES: usize = 12;
const EXPECTED_PIPELINES: usize = 13;

impl Plugin for LoadingPlugin {
fn build(&self, app: &mut App) {
let (tx, rx) = crossbeam_channel::bounded(1);

app.insert_resource(PipelineStatus(rx));

app.add_loading_state(
LoadingState::new(GameState::Loading).continue_to_state(GameState::Pipelines),
)
.add_collection_to_loading_state::<_, Models>(GameState::Loading)
.add_collection_to_loading_state::<_, Fonts>(GameState::Loading)
.add_collection_to_loading_state::<_, Images>(GameState::Loading)
.add_collection_to_loading_state::<_, Sounds>(GameState::Loading)
.add_systems(
Update,
pipelines_done.run_if(in_state(GameState::Pipelines)),
)
.add_systems(OnExit(GameState::Pipelines), cleanup)
.add_systems(OnEnter(GameState::Pipelines), setup_pipelines);

let renderer_app = app.sub_app_mut(RenderApp);
let mut done = false;
renderer_app.add_systems(
Render,
(move |cache: Res<PipelineCache>| {
if done {
return;
}

let ready = cache
.pipelines()
.filter(|pipeline| matches!(pipeline.state, CachedPipelineState::Ok(_)))
.count();

debug!("pipelines ready: {}/{}", ready, EXPECTED_PIPELINES);

if ready >= EXPECTED_PIPELINES {
let _ = tx.send(true);
done = true
}
})
.in_set(RenderSet::Cleanup),
);
app.add_plugins(PipelinesReadyPlugin)
.add_loading_state(
LoadingState::new(GameState::Loading).continue_to_state(GameState::Pipelines),
)
.add_collection_to_loading_state::<_, Models>(GameState::Loading)
.add_collection_to_loading_state::<_, Fonts>(GameState::Loading)
.add_collection_to_loading_state::<_, Images>(GameState::Loading)
.add_collection_to_loading_state::<_, Sounds>(GameState::Loading)
.add_systems(
Update,
pipelines_done.run_if(in_state(GameState::Pipelines)),
)
.add_systems(OnExit(GameState::Pipelines), cleanup)
.add_systems(OnEnter(GameState::Pipelines), setup_pipelines);
}
}

Expand Down Expand Up @@ -181,8 +144,10 @@ fn setup_pipelines(
));
}

fn pipelines_done(status: Res<PipelineStatus>, mut next_state: ResMut<NextState<GameState>>) {
if status.0.try_recv().unwrap_or_default() {
fn pipelines_done(ready: Res<PipelinesReady>, mut next_state: ResMut<NextState<GameState>>) {
info!("Pipelines Ready: {}/{}", ready.get(), EXPECTED_PIPELINES);

if ready.get() >= EXPECTED_PIPELINES {
next_state.set(GameState::MainMenu);
}
}
Expand Down

0 comments on commit b86bb0c

Please sign in to comment.