Skip to content

Commit

Permalink
The update_frame_count system should be placed in CorePlugin (bevye…
Browse files Browse the repository at this point in the history
…ngine#6676)

# Objective

Latest Release, "bevy 0.9" move the FrameCount updater into RenderPlugin, it leads to user who only run app with Core/Minimal Plugin cannot get the right number of FrameCount, it always return 0.

As for use cases like a server app, we don't want to add render dependencies to the app.

More detail in bevyengine#6656 

## Solution

- Move the `update_frame_count` into CorePlugin
  • Loading branch information
phuocthanhdo authored and ItsDoot committed Feb 1, 2023
1 parent 0000fee commit 4a0fb5a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
17 changes: 16 additions & 1 deletion crates/bevy_core/src/lib.rs
Expand Up @@ -6,7 +6,7 @@ mod name;
mod serde;
mod task_pool_options;

use bevy_ecs::system::Resource;
use bevy_ecs::system::{ResMut, Resource};
pub use bytemuck::{bytes_of, cast_slice, Pod, Zeroable};
pub use name::*;
pub use task_pool_options::*;
Expand Down Expand Up @@ -53,6 +53,7 @@ impl Plugin for CorePlugin {
register_math_types(app);

app.init_resource::<FrameCount>();
app.add_system(update_frame_count);
}
}

Expand Down Expand Up @@ -108,6 +109,10 @@ fn register_math_types(app: &mut App) {
#[derive(Default, Resource, Clone, Copy)]
pub struct FrameCount(pub u32);

fn update_frame_count(mut frame_count: ResMut<FrameCount>) {
frame_count.0 = frame_count.0.wrapping_add(1);
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -145,4 +150,14 @@ mod tests {
compute_rx.try_recv().unwrap();
io_rx.try_recv().unwrap();
}

#[test]
fn frame_counter_update() {
let mut app = App::new();
app.add_plugin(CorePlugin::default());
app.update();

let frame_count = app.world.resource::<FrameCount>();
assert_eq!(1, frame_count.0);
}
}
5 changes: 5 additions & 0 deletions crates/bevy_render/src/globals.rs
Expand Up @@ -18,12 +18,17 @@ impl Plugin for GlobalsPlugin {
render_app
.init_resource::<GlobalsBuffer>()
.init_resource::<Time>()
.add_system_to_stage(RenderStage::Extract, extract_frame_count)
.add_system_to_stage(RenderStage::Extract, extract_time)
.add_system_to_stage(RenderStage::Prepare, prepare_globals_buffer);
}
}
}

fn extract_frame_count(mut commands: Commands, frame_count: Extract<Res<FrameCount>>) {
commands.insert_resource(**frame_count);
}

fn extract_time(mut commands: Commands, time: Extract<Res<Time>>) {
commands.insert_resource(time.clone());
}
Expand Down
23 changes: 1 addition & 22 deletions crates/bevy_render/src/lib.rs
Expand Up @@ -19,7 +19,6 @@ mod spatial_bundle;
pub mod texture;
pub mod view;

use bevy_core::FrameCount;
use bevy_hierarchy::ValidParentCheckPlugin;
pub use extract_param::Extract;

Expand Down Expand Up @@ -333,8 +332,7 @@ impl Plugin for RenderPlugin {
.add_plugin(CameraPlugin)
.add_plugin(ViewPlugin)
.add_plugin(MeshPlugin)
.add_plugin(GlobalsPlugin)
.add_plugin(FrameCountPlugin);
.add_plugin(GlobalsPlugin);
}
}

Expand Down Expand Up @@ -368,22 +366,3 @@ fn extract(app_world: &mut World, render_app: &mut App) {
// see <https://github.com/bevyengine/bevy/issues/5082>
extract.apply_buffers(running_world);
}

pub struct FrameCountPlugin;
impl Plugin for FrameCountPlugin {
fn build(&self, app: &mut bevy_app::App) {
app.add_system(update_frame_count);

if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
render_app.add_system_to_stage(RenderStage::Extract, extract_frame_count);
}
}
}

fn update_frame_count(mut frame_count: ResMut<FrameCount>) {
frame_count.0 = frame_count.0.wrapping_add(1);
}

fn extract_frame_count(mut commands: Commands, frame_count: Extract<Res<FrameCount>>) {
commands.insert_resource(**frame_count);
}

0 comments on commit 4a0fb5a

Please sign in to comment.