Skip to main content

examples/systems/
system_target_fps.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    // Set target FPS to 1 frame per second
18    world.set_target_fps(1.0);
19
20    // Run 3 frames
21    for _ in 0..3 {
22        // To make sure the frame doesn't run faster than the specified target
23        // FPS ecs_progress will insert a sleep if the measured delta_time is
24        // smaller than 1 / target_fps.
25        //
26        // By default ecs_progress uses the sleep function provided by the OS
27        // which is not always very accurate. If more accuracy is required the
28        // sleep function of the OS API can be overridden with a custom one.
29        //
30        // If a value other than 0 is provided to the delta_time argument of
31        // ecs_progress, this value will be used to calculate the length of
32        // the sleep to insert.
33        world.progress();
34    }
35
36    // Output:
37    //  Delta time: 1
38    //  Delta time: 1.0182016
39    //  Delta time: 1.0170991
40}
41
42#[cfg(feature = "flecs_nightly_tests")]
43#[test]
44fn test() {
45    let output_capture = OutputCapture::capture().unwrap();
46    main();
47    assert!(output_capture.output().lock().unwrap().len() > 0);
48}