Module app
Expand description
Host-driven startup, fixed update, presentation and shutdown hooks. A renderer-independent, host-driven application lifecycle.
App owns a Game and a GameSession. The host injects elapsed seconds
into App::frame; no window, executor, renderer, or wall clock is required.
Existing async renderer games can continue using ferravex_render::game!.
Hosts needing timing and execution details can use App::frame_report
instead, or inspect App::last_frame_report after either method, even on error.
use ferravex_core::session::GameSession;
use ferravex_engine::app::{App, AppControl, Game};
use std::convert::Infallible;
#[derive(Default)]
struct Counter(u32);
impl Game for Counter {
type Error = Infallible;
fn fixed_update(&mut self, _dt: f32) -> Result<AppControl, Self::Error> {
self.0 += 1;
Ok(AppControl::Continue)
}
}
let mut app = App::new(Counter::default(), GameSession::new(64.0, 8, true));
let report = app.frame_report(2.5 / 64.0)?;
assert_eq!(report.executed_updates, 2);
assert_eq!(report.alpha, 0.5);
assert_eq!(app.game().0, 2);
app.session_mut().set_paused(true);
app.frame(10.0)?; // still renders, with no simulation catch-up
assert_eq!(app.game().0, 2);
app.stop()?; // shutdown once; later frame/stop calls do not invoke hooks§Hook and error guarantees
- Construction invokes no hooks. The first
App::startuporApp::frameattempts startup exactly once, even if the session is Ready or paused. - Each running frame performs the session’s bounded fixed updates, followed
by one render, including frames with zero updates. Ready, paused, and terminal
sessions keep rendering. Starting the application does not start a Ready
session: the host controls that separately with
App::session_mut. - A hook returning
AppControl::Stopskips remaining updates/rendering and attempts shutdown immediately.App::stopdoes the same. Once startup was attempted, shutdown is attempted exactly once, including after startup fails. Stopping/dropping an application that never started invokes neither hook. - A hook error stops the application, attempts shutdown, and returns an
AppErrorretaining both errors if cleanup also fails. Nothing is retried; later calls return Stop (orOk(())forstop). Mutations are not rolled back. - Dropping a running application attempts shutdown as a fallback and discards
its error. Call
stop()explicitly to observe cleanup errors. Panics are not caught; hooks should report recoverable failures throughResult.
The session clock schedules its whole frame before update hooks run. Its tick
therefore includes scheduled updates skipped by an early stop/error. The
AppFrameReport distinguishes scheduled updates from executed hooks. A stopped
app cannot resume; create a new app for another lifecycle. Session restart/reset
between frames affects simulation timing only and never repeats startup.
Structs§
- App
- A single application lifecycle driven by host-supplied elapsed time.
- AppError
- A hook failure and, when applicable, a separate cleanup failure.
- AppFrame
Report - Execution and timing details for one completed host frame call.
- Tick
Context - Context for one scheduled fixed update, produced by
TickBatch::ticks.
Enums§
- AppControl
- A hook’s decision about whether the application should continue.
- AppHook
- The hook that produced an
AppError. - AppState
- Application lifecycle state, independent of the simulation’s session state.