Skip to content

Commit

Permalink
refactor: Get gizmos to run on the main schedule when fixed_time is d…
Browse files Browse the repository at this point in the history
…isabled
  • Loading branch information
Lubba-64 committed Aug 25, 2024
1 parent 553ca5a commit 6e5c510
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 64 deletions.
10 changes: 5 additions & 5 deletions crates/bevy_app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ keywords = ["bevy"]
[features]
trace = []
bevy_debug_stepping = []
default = ["bevy_reflect"]
default = ["bevy_reflect", "fixed_time"]
bevy_reflect = ["dep:bevy_reflect", "bevy_ecs/bevy_reflect"]
reflect_functions = [
"bevy_reflect",
"bevy_reflect/functions",
"bevy_ecs/reflect_functions",
"bevy_reflect",
"bevy_reflect/functions",
"bevy_ecs/reflect_functions",
]
fixed_update = []
fixed_time = []

[dependencies]
# bevy
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub use terminal_ctrl_c_handler::*;

#[allow(missing_docs)]
pub mod prelude {
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
pub use crate::main_schedule::{
FixedFirst, FixedLast, FixedPostUpdate, FixedPreUpdate, FixedUpdate, RunFixedMainLoop,
};
Expand Down
28 changes: 14 additions & 14 deletions crates/bevy_app/src/main_schedule.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{App, Plugin};
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
use bevy_ecs::schedule::IntoSystemSetConfigs;
use bevy_ecs::{
schedule::{ExecutorKind, InternedScheduleLabel, Schedule, ScheduleLabel, SystemSet},
Expand Down Expand Up @@ -83,23 +83,23 @@ pub struct PreUpdate;
/// [`RunFixedMainLoop`] will *not* be parallelized between each other.
///
/// See the [`Main`] schedule for some details about how schedules are run.
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
#[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash)]
pub struct RunFixedMainLoop;

/// Runs first in the [`FixedMain`] schedule.
///
/// See the [`FixedMain`] schedule for details on how fixed updates work.
/// See the [`Main`] schedule for some details about how schedules are run.
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
#[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash)]
pub struct FixedFirst;

/// The schedule that contains logic that must run before [`FixedUpdate`].
///
/// See the [`FixedMain`] schedule for details on how fixed updates work.
/// See the [`Main`] schedule for some details about how schedules are run.
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
#[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash)]
pub struct FixedPreUpdate;

Expand All @@ -115,7 +115,7 @@ pub struct FixedPreUpdate;
/// See the [`Update`] schedule for examples of systems that *should not* use this schedule.
/// See the [`FixedMain`] schedule for details on how fixed updates work.
/// See the [`Main`] schedule for some details about how schedules are run.
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
#[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash)]
pub struct FixedUpdate;

Expand All @@ -124,15 +124,15 @@ pub struct FixedUpdate;
///
/// See the [`FixedMain`] schedule for details on how fixed updates work.
/// See the [`Main`] schedule for some details about how schedules are run.
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
#[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash)]
pub struct FixedPostUpdate;

/// The schedule that runs last in [`FixedMain`]
///
/// See the [`FixedMain`] schedule for details on how fixed updates work.
/// See the [`Main`] schedule for some details about how schedules are run.
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
#[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash)]
pub struct FixedLast;

Expand All @@ -145,7 +145,7 @@ pub struct FixedLast;
/// See [this example](https://github.com/bevyengine/bevy/blob/latest/examples/time/time.rs).
///
/// See the [`Main`] schedule for some details about how schedules are run.
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
#[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash)]
pub struct FixedMain;

Expand Down Expand Up @@ -201,7 +201,7 @@ impl Default for MainScheduleOrder {
labels: vec![
First.intern(),
PreUpdate.intern(),
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
RunFixedMainLoop.intern(),
Update.intern(),
SpawnScene.intern(),
Expand Down Expand Up @@ -296,7 +296,7 @@ impl Plugin for MainSchedulePlugin {
.init_resource::<MainScheduleOrder>()
.add_systems(Main, Main::run_main);

#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
{
let mut fixed_main_schedule = Schedule::new(FixedMain);
fixed_main_schedule.set_executor_kind(ExecutorKind::SingleThreaded);
Expand Down Expand Up @@ -326,14 +326,14 @@ impl Plugin for MainSchedulePlugin {

/// Defines the schedules to be run for the [`FixedMain`] schedule, including
/// their order.
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
#[derive(Resource, Debug)]
pub struct FixedMainScheduleOrder {
/// The labels to run for the [`FixedMain`] schedule (in the order they will be run).
pub labels: Vec<InternedScheduleLabel>,
}

#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
impl Default for FixedMainScheduleOrder {
fn default() -> Self {
Self {
Expand All @@ -348,7 +348,7 @@ impl Default for FixedMainScheduleOrder {
}
}

#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
impl FixedMainScheduleOrder {
/// Adds the given `schedule` after the `after` schedule
pub fn insert_after(&mut self, after: impl ScheduleLabel, schedule: impl ScheduleLabel) {
Expand All @@ -371,7 +371,7 @@ impl FixedMainScheduleOrder {
}
}

#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
impl FixedMain {
/// A system that runs the fixed timestep's "main schedule"
pub fn run_fixed_main(world: &mut World) {
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_gizmos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ license = "MIT OR Apache-2.0"
keywords = ["bevy"]

[features]
default = ["fixed_time"]
webgl = []
webgpu = []
fixed_update = ["bevy_app/fixed_update"]
fixed_time = ["bevy_app/fixed_time"]
bevy_render = ["dep:bevy_render", "bevy_core_pipeline"]

[dependencies]
Expand Down
67 changes: 43 additions & 24 deletions crates/bevy_gizmos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ pub mod prelude {
pub use crate::light::{LightGizmoColor, LightGizmoConfigGroup, ShowLightGizmo};
}

use bevy_app::{App, Last, Plugin};
#[cfg(feature = "fixed_update")]
use bevy_app::{App, First, Last, Plugin};
#[cfg(feature = "fixed_time")]
use bevy_app::{FixedFirst, FixedLast, RunFixedMainLoop};
use bevy_asset::{Asset, AssetApp, Assets, Handle};
use bevy_color::LinearRgba;
Expand Down Expand Up @@ -242,28 +242,47 @@ impl AppGizmoBuilder for App {
handles.list.insert(TypeId::of::<Config>(), None);
handles.strip.insert(TypeId::of::<Config>(), None);

self.init_resource::<GizmoStorage<Config, ()>>()
.init_resource::<GizmoStorage<Config, Fixed>>()
.init_resource::<GizmoStorage<Config, Swap<Fixed>>>()
.add_systems(
RunFixedMainLoop,
start_gizmo_context::<Config, Fixed>
.in_set(bevy_app::RunFixedMainLoopSystem::BeforeFixedMainLoop),
)
.add_systems(FixedFirst, clear_gizmo_context::<Config, Fixed>)
.add_systems(FixedLast, collect_requested_gizmos::<Config, Fixed>)
.add_systems(
RunFixedMainLoop,
end_gizmo_context::<Config, Fixed>
.in_set(bevy_app::RunFixedMainLoopSystem::AfterFixedMainLoop),
)
.add_systems(
Last,
(
propagate_gizmos::<Config, Fixed>.before(UpdateGizmoMeshes),
update_gizmo_meshes::<Config>.in_set(UpdateGizmoMeshes),
),
);
#[cfg(feature = "fixed_time")]
{
self.init_resource::<GizmoStorage<Config, ()>>()
.init_resource::<GizmoStorage<Config, Fixed>>()
.init_resource::<GizmoStorage<Config, Swap<Fixed>>>()
.add_systems(
RunFixedMainLoop,
start_gizmo_context::<Config, Fixed>
.in_set(bevy_app::RunFixedMainLoopSystem::BeforeFixedMainLoop),
)
.add_systems(FixedFirst, clear_gizmo_context::<Config, Fixed>)
.add_systems(FixedLast, collect_requested_gizmos::<Config, Fixed>)
.add_systems(
RunFixedMainLoop,
end_gizmo_context::<Config, Fixed>
.in_set(bevy_app::RunFixedMainLoopSystem::AfterFixedMainLoop),
)
.add_systems(
Last,
(
propagate_gizmos::<Config, Fixed>.before(UpdateGizmoMeshes),
update_gizmo_meshes::<Config>.in_set(UpdateGizmoMeshes),
),
);
}
#[cfg(not(feature = "fixed_time"))]
{
self.init_resource::<GizmoStorage<Config, ()>>()
.init_resource::<GizmoStorage<Config, ()>>()
.init_resource::<GizmoStorage<Config, Fixed>>()
.init_resource::<GizmoStorage<Config, Swap<Fixed>>>()
.add_systems(First, clear_gizmo_context::<Config, Fixed>)
.add_systems(
Last,
(
collect_requested_gizmos::<Config, Fixed>,
propagate_gizmos::<Config, Fixed>.before(UpdateGizmoMeshes),
update_gizmo_meshes::<Config>.in_set(UpdateGizmoMeshes),
),
);
}

self
}
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_time/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ license = "MIT OR Apache-2.0"
keywords = ["bevy"]

[features]
default = ["bevy_reflect"]
default = ["bevy_reflect", "fixed_time"]
serialize = ["serde"]
fixed_update = ["bevy_app/fixed_update"]
fixed_time = ["bevy_app/fixed_time"]

[dependencies]
# bevy
bevy_app = { path = "../bevy_app", version = "0.15.0-dev" }
bevy_ecs = { path = "../bevy_ecs", version = "0.15.0-dev", features = [
"bevy_reflect",
"bevy_reflect",
] }
bevy_reflect = { path = "../bevy_reflect", version = "0.15.0-dev", features = [
"bevy",
"bevy",
], optional = true }
bevy_utils = { path = "../bevy_utils", version = "0.15.0-dev" }

Expand Down
16 changes: 8 additions & 8 deletions crates/bevy_time/src/fixed.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
use bevy_app::FixedMain;
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
use bevy_ecs::world::World;
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;
use bevy_utils::Duration;

use crate::time::Time;
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
use crate::virt::Virtual;

/// The fixed timestep game clock following virtual time.
///
/// A specialization of the [`Time`] structure. **For method documentation, see
/// [`Time<Fixed>#impl-Time<Fixed>`].**
///
///
/// It is automatically inserted as a resource by
/// [`TimePlugin`](crate::TimePlugin) and updated based on
/// [`Time<Virtual>`](Virtual). The fixed clock is automatically set as the
Expand Down Expand Up @@ -209,12 +209,12 @@ impl Time<Fixed> {
self.context().overstep.as_secs_f64() / self.context().timestep.as_secs_f64()
}

#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
fn accumulate(&mut self, delta: Duration) {
self.context_mut().overstep += delta;
}

#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
fn expend(&mut self) -> bool {
let timestep = self.timestep();
if let Some(new_value) = self.context_mut().overstep.checked_sub(timestep) {
Expand Down Expand Up @@ -242,7 +242,7 @@ impl Default for Fixed {
/// [`Time<Virtual>`](Virtual) and [`Time::overstep`].
/// You can order your systems relative to this by using
/// [`RunFixedMainLoopSystem`](bevy_app::prelude::RunFixedMainLoopSystem).
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
pub(super) fn run_fixed_main_schedule(world: &mut World) {
let delta = world.resource::<Time<Virtual>>().delta();
world.resource_mut::<Time<Fixed>>().accumulate(delta);
Expand All @@ -258,7 +258,7 @@ pub(super) fn run_fixed_main_schedule(world: &mut World) {
*world.resource_mut::<Time>() = world.resource::<Time<Virtual>>().as_generic();
}

#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
#[cfg(test)]
mod test {
use super::*;
Expand Down
14 changes: 7 additions & 7 deletions crates/bevy_time/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ pub mod prelude {
}

use bevy_app::prelude::*;
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
use bevy_app::RunFixedMainLoop;
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
use bevy_ecs::event::signal_event_update_system;
use bevy_ecs::event::{event_update_system, EventRegistry, ShouldUpdateEvents};
use bevy_ecs::prelude::*;
Expand Down Expand Up @@ -72,7 +72,7 @@ impl Plugin for TimePlugin {
.in_set(TimeSystem)
.ambiguous_with(event_update_system),
);
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
{
app.add_systems(
RunFixedMainLoop,
Expand Down Expand Up @@ -160,7 +160,7 @@ pub fn time_system(
#[cfg(test)]
mod tests {
use crate::{Fixed, Time, TimePlugin, TimeUpdateStrategy, Virtual};
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
use bevy_app::FixedUpdate;
use bevy_app::{App, Startup, Update};
use bevy_ecs::{
Expand Down Expand Up @@ -189,7 +189,7 @@ mod tests {
#[derive(Resource, Default)]
struct FixedUpdateCounter(u8);

#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
fn count_fixed_updates(mut counter: ResMut<FixedUpdateCounter>) {
counter.0 += 1;
}
Expand Down Expand Up @@ -222,7 +222,7 @@ mod tests {
let time_step = fixed_update_timestep / 2 + Duration::from_millis(1);

let mut app = App::new();
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
app.add_systems(FixedUpdate, count_fixed_updates);

app.add_plugins(TimePlugin)
Expand Down Expand Up @@ -325,7 +325,7 @@ mod tests {
}

let mut app = App::new();
#[cfg(feature = "fixed_update")]
#[cfg(feature = "fixed_time")]
app.add_systems(FixedUpdate, count_fixed_updates);

app.add_plugins(TimePlugin)
Expand Down

0 comments on commit 6e5c510

Please sign in to comment.