Skip to main content

examples/systems/
system_delta_time.rs

1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4
5fn main() {
6    let world = World::new();
7
8    // Create system that prints delta_time. This system doesn't query for any
9    // components which means it won't match any entities, but will still be ran
10    // once for each call to ecs_progress.
11    world.system::<()>().run(|mut it| {
12        while it.next() {
13            println!("delta_time: {}", it.delta_time());
14        }
15    });
16
17    // Call progress with 0.0f for the delta_time parameter. This will cause
18    // ecs_progress to measure the time passed since the last frame. The
19    // delta_time of the first frame is a best guess (16ms).
20    world.progress();
21
22    // The following calls should print a delta_time of approximately 100ms
23
24    let os_sleep = unsafe { flecs_ecs_sys::ecs_os_api.sleep_ }.unwrap();
25
26    unsafe { os_sleep(0, 100 * 1000 * 1000) };
27    world.progress();
28
29    unsafe { os_sleep(0, 100 * 1000 * 1000) };
30
31    world.progress();
32
33    // Output:
34    //  delta_time: 0.016666668
35    //  delta_time: 0.10155179
36    //  delta_time: 0.10091246
37}
38
39#[cfg(feature = "flecs_nightly_tests")]
40#[test]
41fn test() {
42    let output_capture = OutputCapture::capture().unwrap();
43    main();
44    assert!(output_capture.output().lock().unwrap().len() > 0);
45}