Skip to content

Commit

Permalink
Add global time scaling (bevyengine#5752)
Browse files Browse the repository at this point in the history
# Objective

- Make `Time` API more consistent.
- Support time accel/decel/pause.

## Solution

This is just the `Time` half of bevyengine#3002. I was told that part isn't controversial.

- Give the "delta time" and "total elapsed time" methods `f32`, `f64`, and `Duration` variants with consistent naming.
- Implement accelerating / decelerating the passage of time.
- Implement stopping time.

---

## Changelog

- Changed `time_since_startup` to `elapsed` because `time.time_*` is just silly.
- Added `relative_speed` and `set_relative_speed` methods.
- Added `is_paused`, `pause`, `unpause` , and methods. (I'd prefer `resume`, but `unpause` matches `Timer` API.)
- Added `raw_*` variants of the "delta time" and "total elapsed time" methods.
- Added `first_update` method because there's a non-zero duration between startup and the first update.

## Migration Guide

- `time.time_since_startup()` -> `time.elapsed()`
- `time.seconds_since_startup()` -> `time.elapsed_seconds_f64()`
- `time.seconds_since_startup_wrapped_f32()` -> `time.elapsed_seconds_wrapped()`

If you aren't sure which to use, most systems should continue to use "scaled" time (e.g. `time.delta_seconds()`). The realtime "unscaled" time measurements (e.g. `time.raw_delta_seconds()`) are mostly for debugging and profiling.
  • Loading branch information
maniwani committed Oct 22, 2022
1 parent cb5e2d8 commit 7989cb2
Show file tree
Hide file tree
Showing 24 changed files with 609 additions and 166 deletions.
7 changes: 4 additions & 3 deletions crates/bevy_diagnostic/src/frame_time_diagnostics_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ impl FrameTimeDiagnosticsPlugin {
) {
diagnostics.add_measurement(Self::FRAME_COUNT, || frame_count.0 as f64);

if time.delta_seconds_f64() == 0.0 {
let delta_seconds = time.raw_delta_seconds_f64();
if delta_seconds == 0.0 {
return;
}

diagnostics.add_measurement(Self::FRAME_TIME, || time.delta_seconds_f64() * 1000.);
diagnostics.add_measurement(Self::FRAME_TIME, || delta_seconds * 1000.0);

diagnostics.add_measurement(Self::FPS, || 1.0 / time.delta_seconds_f64());
diagnostics.add_measurement(Self::FPS, || 1.0 / delta_seconds);
}
}
4 changes: 2 additions & 2 deletions crates/bevy_diagnostic/src/log_diagnostics_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl LogDiagnosticsPlugin {
time: Res<Time>,
diagnostics: Res<Diagnostics>,
) {
if state.timer.tick(time.delta()).finished() {
if state.timer.tick(time.raw_delta()).finished() {
if let Some(ref filter) = state.filter {
for diagnostic in filter.iter().flat_map(|id| {
diagnostics
Expand All @@ -110,7 +110,7 @@ impl LogDiagnosticsPlugin {
time: Res<Time>,
diagnostics: Res<Diagnostics>,
) {
if state.timer.tick(time.delta()).finished() {
if state.timer.tick(time.raw_delta()).finished() {
if let Some(ref filter) = state.filter {
for diagnostic in filter.iter().flat_map(|id| {
diagnostics
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn prepare_globals_buffer(
frame_count: Res<FrameCount>,
) {
let buffer = globals_buffer.buffer.get_mut();
buffer.time = time.seconds_since_startup_wrapped_f32();
buffer.time = time.elapsed_seconds_wrapped();
buffer.delta_time = time.delta_seconds();
buffer.frame_count = frame_count.0;

Expand Down
Loading

0 comments on commit 7989cb2

Please sign in to comment.