ferravex/
prelude.rs

1//! A deliberately small set of common engine entry points.
2//!
3//! Import with `use ferravex::prelude::*;`. These are the original types, so they
4//! interoperate with the full [`crate::core`], [`crate::math`], [`crate::input`]
5//! and [`crate::render`] APIs without conversions.
6//!
7//! - [`GameSession`] coordinates start, pause, restart and fixed simulation time.
8//!   [`SessionConfig`] validates reusable settings; setup and inspector errors
9//!   stay at [`crate::core::FixedClockError`] and [`crate::session::SessionStepError`].
10//! - [`TickBatch`] yields [`TickContext`] values with each scheduled update's ID
11//!   and delta; scheduled IDs are distinct from executed-update counts.
12//! - [`Vec2`], [`Vec3`], [`Mat4`] and the other math types are shared by all engines.
13//! - [`ActionMap`] handles platform-independent actions; [`PlayerInput`] connects
14//!   those actions to the renderer's platform input and controls UI.
15//! - [`Camera2D`], [`Camera3D`] and [`Conf`] configure the renderer. Drawing,
16//!   frame scheduling and low-level input polling stay qualified as `render::…`.
17//!
18//! There is no generic `Camera`, `Mesh` or `Action` alias: choose the appropriate
19//! subsystem explicitly when reaching beyond these entry points.
20//! In particular, use [`crate::app::AppFrameReport`] for lifecycle diagnostics,
21//! [`crate::time::SimulationTimer`] for timers and [`crate::feedback`] for
22//! accepted-event presentation. These systems do not expand wildcard imports.
23//!
24//! ```
25//! use ferravex::prelude::*;
26//!
27//! let mut actions = ActionMap::default();
28//! let right = actions.action(vec![Binding::digital(
29//!     InputCode::Key(KeyCode::Right as u16),
30//! )]);
31//! let config = SessionConfig::try_new(60.0, 8, true)?;
32//! let mut session = GameSession::from_config(config);
33//! actions.feed(InputCode::Key(KeyCode::Right as u16), 1.0);
34//! let mut position = Vec2::ZERO;
35//! let batch: TickBatch = session.advance_batch(1.0 / 60.0);
36//! for TickContext { tick, dt, .. } in batch.ticks() {
37//!     // Consume edges per simulation tick, including catch-up ticks.
38//!     let action = actions.consume_tick(right);
39//!     position += vec2(action.value, 0.0) * 120.0 * dt;
40//!     assert_eq!(tick, 1);
41//! }
42//! assert!((position.x - 2.0).abs() < 0.0001);
43//! # Ok::<(), ferravex::core::FixedClockError>(())
44//! ```
45
46pub use crate::input::{ActionKind, ActionMap, Binding, InputCode};
47pub use crate::math::{Color, Mat4, Quat, Rect, Vec2, Vec3, vec2, vec3};
48pub use crate::render;
49pub use crate::render::input::PlayerInput;
50pub use crate::render::{Camera2D, Camera3D, Conf, KeyCode, MouseButton};
51pub use crate::{
52    Entity, FixedClock, FrameStep, GameSession, SessionConfig, SessionState, TickBatch,
53    TickContext, World,
54};
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn checked_config_reexport_keeps_core_identity_and_error_types() {
62        let config: crate::core::session::SessionConfig =
63            SessionConfig::try_new(64.0, 4, false).unwrap();
64        let mut session = GameSession::from_config(config);
65        let independent = GameSession::from_config(config);
66        assert!(session.start());
67        assert!(session.set_paused(true));
68        let frame: FrameStep = session.step_paused().unwrap();
69        assert_eq!(frame.steps, 1);
70        assert_eq!(session.tick(), 1);
71        assert_eq!(independent.tick(), 0);
72        assert_eq!(independent.state(), SessionState::Ready);
73        assert_eq!(
74            SessionConfig::try_new(60.0, 0, false),
75            Err(crate::core::FixedClockError::ZeroCatchUpBudget)
76        );
77        session.finish(true);
78        assert_eq!(
79            session.step_paused().unwrap_err(),
80            crate::session::SessionStepError::NotPaused {
81                state: SessionState::Won
82            }
83        );
84    }
85
86    #[test]
87    fn session_exports_share_core_state_and_do_not_catch_up_after_pause() {
88        let mut session: crate::core::session::GameSession = GameSession::new(60.0, 4, false);
89        assert_eq!(session.state(), SessionState::Ready);
90        assert_eq!(session.advance(10.0).steps, 0);
91        assert!(session.start());
92        assert_eq!(session.advance(1.0 / 60.0).steps, 1);
93        assert!(session.set_paused(true));
94        assert_eq!(session.advance(10.0).steps, 0);
95        assert!(session.set_paused(false));
96        let frame: FrameStep = session.advance(0.5 / 60.0);
97        assert_eq!(frame.steps, 0);
98        assert!((frame.alpha - 0.5).abs() < 0.0001);
99        assert_eq!(session.tick(), 1);
100    }
101
102    #[test]
103    fn input_edges_survive_zero_step_frames_and_are_consumed_once() {
104        let mut input: crate::input::ActionMap = ActionMap::default();
105        let code = InputCode::Key(KeyCode::Space as u16);
106        let jump = input.action(vec![Binding::digital(code)]);
107        let mut session = GameSession::new(60.0, 4, true);
108        input.feed(code, 1.0);
109        input.feed(code, 0.0);
110        assert_eq!(session.advance(0.5 / 60.0).steps, 0);
111
112        let frame = session.advance(2.0 / 60.0);
113        assert_eq!(frame.steps, 2);
114        let mut accepted = 0;
115        for _ in 0..frame.steps {
116            accepted += u32::from(input.consume_tick(jump).pressed);
117        }
118        assert_eq!(accepted, 1);
119        assert!(!input.consume_tick(jump).pressed);
120    }
121
122    #[test]
123    fn math_exports_work_directly_with_renderer_cameras() {
124        let camera = Camera2D::from_display_rect(Rect::new(0.0, 0.0, 640.0, 360.0));
125        let matrix: Mat4 = render::Camera::matrix(&camera, 640.0, 360.0);
126        let origin: Vec3 = matrix.project_point3(Vec3::ZERO);
127        assert!(origin.abs_diff_eq(vec3(-1.0, 1.0, 0.0), 0.0001));
128        assert!(
129            matrix
130                .project_point3(vec3(640.0, 360.0, 0.0))
131                .abs_diff_eq(vec3(1.0, -1.0, 0.0), 0.0001)
132        );
133    }
134}