Trait Game

pub trait Game {
    type Error;

    // Required method
    fn fixed_update(&mut self, dt: f32) -> Result<AppControl, Self::Error>;

    // Provided methods
    fn startup(&mut self) -> Result<AppControl, Self::Error> { ... }
    fn fixed_update_context(
        &mut self,
        context: TickContext,
    ) -> Result<AppControl, Self::Error> { ... }
    fn render(&mut self, _alpha: f32) -> Result<AppControl, Self::Error> { ... }
    fn shutdown(&mut self) -> Result<(), Self::Error> { ... }
}
Expand description

Game hooks called synchronously by App. Only fixed update is required.

Keep current and previous simulation poses in the game, shift the current pose to previous before each update, and interpolate them in render(alpha). The host may sample input through App::game_mut before each frame and control start/pause/retry through App::session_mut. See the module example for a headless implementation and the app_lifecycle example for interpolation.

Required Associated Types§

type Error

Application-defined error; Infallible is useful for headless simulations.

Required Methods§

fn fixed_update(&mut self, dt: f32) -> Result<AppControl, Self::Error>

Advance simulation by the session’s constant timestep in seconds.

The default Self::fixed_update_context calls this once per executed tick. Existing implementations need no changes.

Provided Methods§

fn startup(&mut self) -> Result<AppControl, Self::Error>

Initialize once. Shutdown will run even if initialization returns an error, so cleanup must handle partially initialized resources.

fn fixed_update_context( &mut self, context: TickContext, ) -> Result<AppControl, Self::Error>

Advance one scheduled tick with its absolute ID and index within the batch.

App invokes this hook instead of directly invoking fixed_update. The default forwards to Self::fixed_update with context.dt. An override owns the update and may call the old hook itself; the runner never calls both. Stop/error handling and AppHook::FixedUpdate are unchanged. Reports count invocations, including Stop/Err, not commits.

IDs begin at 1, wrap through 0, and are reused after session reset/restart. They count issued work, including manual or skipped updates, and are not an executed-update total. See TickContext and ferravex_core::TickBatch.

use ferravex_engine::app::{App, AppControl, Game, TickContext};
use ferravex_engine::session::GameSession;
use std::convert::Infallible;
#[derive(Default)]
struct Counter { position: f32, last_tick: u64 }
impl Game for Counter {
    type Error = Infallible;
    fn fixed_update(&mut self, dt: f32) -> Result<AppControl, Self::Error> {
        self.position += 2.0 * dt;
        Ok(AppControl::Continue)
    }
    fn fixed_update_context(&mut self, tick: TickContext) -> Result<AppControl, Self::Error> {
        self.last_tick = tick.tick;
        self.fixed_update(tick.dt)
    }
}
let mut app = App::new(Counter::default(), GameSession::new(64.0, 4, true));
let report = app.frame_report(2.0 / 64.0)?;
assert_eq!(report.executed_updates, 2);
assert_eq!(app.game().last_tick, 2);
assert_eq!(app.game().position, 4.0 / 64.0);

fn render(&mut self, _alpha: f32) -> Result<AppControl, Self::Error>

Present once after updates, with the session’s interpolation factor.

alpha is the fractional remainder in 0.0..=1.0; inactive sessions supply zero. This hook is also called when no fixed updates are due.

fn shutdown(&mut self) -> Result<(), Self::Error>

Release application resources once after startup was attempted.

Implementors§