Skip to main content

App

Trait App 

Source
pub trait App<B>
where B: Backend,
{ // Required method fn update(&mut self, term: &mut Terminal<B>, frame: &Frame) -> Flow; // Provided method fn init(&mut self, _term: &mut Terminal<B>) { ... } }
Expand description

The per-frame update contract for a game.

Implement this once, generically over the backend, to run everywhere:

use retroglyph_core::app::{App, Flow, Frame};
use retroglyph_core::backend::Backend;
use retroglyph_core::color::Style;
use retroglyph_core::terminal::Terminal;

struct MyGame;
impl<B: Backend> App<B> for MyGame {
    fn update(&mut self, term: &mut Terminal<B>, _frame: &Frame) -> Flow {
        term.surface().put((0, 0), '@', Style::default());
        Flow::Exit
    }
}

Required Methods§

Source

fn update(&mut self, term: &mut Terminal<B>, frame: &Frame) -> Flow

Advance and render one frame.

Draw into term, read input via term, and return Flow::Exit to stop the loop.

Draw via term.surface() or term.draw() (though draw presents itself, which usually conflicts with the driver’s own automatic present below; prefer surface() inside update). Every driver (run_on and retroglyph-window’s windowed drivers) presents the frame automatically right after this method returns, unless it returned Flow::Idle, in which case the driver skips present entirely. Calling present yourself inside update remains fine (the driver detects it already ran via present_count and skips its own call) but is never required. run_on and run_on_with link back here rather than restating this contract.

Provided Methods§

Source

fn init(&mut self, _term: &mut Terminal<B>)

Runs once, before the first update call, with the first live term for whichever backend is actually running.

The hook for anything whose initial state depends on the real starting grid size (term.backend().size()), which varies by backend: crossterm reports the actual terminal size, a windowed backend reports whatever grid it was configured with. Every driver in this crate (run_on_with, TestHarness::step, retroglyph-recorder’s replay_live) and retroglyph-window’s windowed driver call this exactly once, before the first update.

Default: does nothing. An app with no backend-dependent startup state never needs to override this; one that does can build its state here instead of deferring construction behind an Option<State> field lazily initialized on the first update call.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§