ferravex_render/
demo.rs

1//! Shared native smoke-run support for small interactive examples.
2//! Each game supplies its own autoplay inputs and uses the same simulation as a human.
3use crate::prelude::*;
4
5/// Native flags: `--autoplay`, `--smoke-frames N`, `--screenshot path.png`.
6/// Browser and mobile launches default to interactive play.
7#[derive(Default)]
8pub struct Demo {
9    pub autoplay: bool,
10    pub frame: u64,
11    /// Optional local authoring file, watched by games with ContentSource support.
12    pub content: Option<String>,
13    limit: Option<u64>,
14    #[cfg(not(target_arch = "wasm32"))]
15    screenshot: Option<String>,
16}
17
18impl Demo {
19    pub fn new() -> Self {
20        Self::from_args(false)
21    }
22    /// Enables --content for a game that installs a ContentSource.
23    pub fn with_content() -> Self {
24        Self::from_args(true)
25    }
26    fn from_args(supports_content: bool) -> Self {
27        #[cfg(target_arch = "wasm32")]
28        let _ = supports_content;
29        #[cfg(not(target_arch = "wasm32"))]
30        {
31            let mut demo = Self::default();
32            let mut args = std::env::args().skip(1);
33            while let Some(arg) = args.next() {
34                match arg.as_str() {
35                    "--autoplay" => demo.autoplay = true,
36                    "--content" => {
37                        assert!(supports_content, "This game does not support --content");
38                        demo.content = Some(args.next().expect("--content requires a path"))
39                    }
40                    "--smoke-frames" => {
41                        demo.limit = Some(
42                            args.next()
43                                .and_then(|s| s.parse::<u64>().ok())
44                                .filter(|n| *n > 0)
45                                .expect("--smoke-frames requires a positive integer"),
46                        );
47                    }
48                    "--screenshot" => {
49                        demo.screenshot = Some(args.next().expect("--screenshot requires a path"))
50                    }
51                    "--help" => {
52                        println!(
53                            "Options: --autoplay --smoke-frames N --screenshot path.png --content path.fxscene (supported games only)"
54                        );
55                        std::process::exit(0);
56                    }
57                    _ => panic!("Unknown option: {arg}"),
58                }
59            }
60            demo
61        }
62        #[cfg(target_arch = "wasm32")]
63        Self {
64            autoplay: crate::autoplay_enabled(),
65            ..Self::default()
66        }
67    }
68
69    /// Smoke runs use a reproducible render timestep; simulation should still use FixedClock.
70    pub fn dt(&self) -> f32 {
71        if self.limit.is_some() {
72            1.0 / 60.0
73        } else {
74            get_frame_time().clamp(0.0, 0.1)
75        }
76    }
77
78    pub fn time(&self) -> f32 {
79        if self.limit.is_some() {
80            self.frame as f32 / 60.0
81        } else {
82            get_time() as f32
83        }
84    }
85
86    /// Call after all drawing and before next_frame. Returns true when the run is finished.
87    pub fn finish_frame(&mut self) -> bool {
88        self.frame += 1;
89        let finished = self.limit.is_some_and(|limit| self.frame >= limit);
90        #[cfg(not(target_arch = "wasm32"))]
91        if finished && let Some(path) = &self.screenshot {
92            get_screen_data().export_png(path);
93        }
94        finished
95    }
96}