Skip to main content

Engine

Struct Engine 

Source
pub struct Engine { /* private fields */ }
Expand description

Configures and starts the game.

use audio_code::prelude::*;

struct Hello;

impl Scene for Hello {
    fn start(&mut self, ctx: &mut Context) {
        ctx.speech.speak("Hello! Press space to beep, escape to quit.");
    }

    fn update(&mut self, ctx: &mut Context) {
        if ctx.input.pressed(Key::Space) {
            ctx.audio.play_tone(440.0, 0.2);
        }
        if ctx.input.pressed(Key::Escape) {
            ctx.quit();
        }
    }
}

fn main() -> audio_code::Result<()> {
    Engine::new("Hello").run(Hello)
}

Implementations§

Source§

impl Engine

Source

pub fn new(title: impl Into<String>) -> Self

Creates a new engine configuration. The title is used for the window and is what the screen reader announces when the window gets focus.

Examples found in repository?
examples/driving.rs (line 149)
148fn main() -> audio_code::Result<()> {
149    Engine::new("Driving").run_with(Town::new)
150}
More examples
Hide additional examples
examples/menus.rs (line 91)
90fn main() -> audio_code::Result<()> {
91    Engine::new("Menu demo").echo_speech(true).run(MainMenu)
92}
examples/hello.rs (line 30)
29fn main() -> audio_code::Result<()> {
30    Engine::new("Hello audio-code").echo_speech(true).run(Hello)
31}
examples/beacon.rs (line 158)
157fn main() -> audio_code::Result<()> {
158    Engine::new("Beacon").echo_speech(true).run_with(Maze::new)
159}
examples/flight.rs (line 147)
146fn main() -> audio_code::Result<()> {
147    Engine::new("Flight").echo_speech(true).run_with(Flight::new)
148}
examples/platformer.rs (line 109)
108fn main() -> audio_code::Result<()> {
109    Engine::new("Platformer").gravity(EARTH_GRAVITY * 2.0).echo_speech(true).run_with(Platformer::new)
110}
Source

pub fn fps(self, fps: u32) -> Self

Target frames per second (default 60).

Source

pub fn physics_rate(self, rate: u32) -> Self

Physics and Scene::fixed_update steps per second (default 60).

Source

pub fn gravity(self, gravity: Vec3) -> Self

Gravity of the physics world, e.g. EARTH_GRAVITY. Default is none (top down games). Can be changed later with ctx.physics.set_gravity.

Examples found in repository?
examples/platformer.rs (line 109)
108fn main() -> audio_code::Result<()> {
109    Engine::new("Platformer").gravity(EARTH_GRAVITY * 2.0).echo_speech(true).run_with(Platformer::new)
110}
Source

pub fn save_file(self, path: impl Into<PathBuf>) -> Self

File used by Context::storage (default <title>.save in the working directory).

Source

pub fn encrypt_save(self, secret: impl Into<String>) -> Self

Encrypts the save file of Context::storage with a key made from secret (optional, default off). Players can no longer read or edit it, and a changed or damaged file is detected and kept as <file>.broken. An existing plain text save is converted on the next save. See crate::storage for what this protects against.

Source

pub fn sound_dir(self, dir: impl Into<PathBuf>) -> Self

Folder searched for sounds (default "sounds"). See crate::audio.

Source

pub fn lang_dir(self, dir: impl Into<PathBuf>) -> Self

Folder with the translation files <code>.txt (default "lang"). See crate::lang.

Source

pub fn language(self, code: impl Into<String>) -> Self

The language the game starts in, e.g. "de" (default: the system language if the game has texts for it, else English). A language the player chose with Context::set_language still wins.

Source

pub fn echo_speech(self, echo: bool) -> Self

Print everything spoken to the console as well (default false).

Examples found in repository?
examples/menus.rs (line 91)
90fn main() -> audio_code::Result<()> {
91    Engine::new("Menu demo").echo_speech(true).run(MainMenu)
92}
More examples
Hide additional examples
examples/hello.rs (line 30)
29fn main() -> audio_code::Result<()> {
30    Engine::new("Hello audio-code").echo_speech(true).run(Hello)
31}
examples/beacon.rs (line 158)
157fn main() -> audio_code::Result<()> {
158    Engine::new("Beacon").echo_speech(true).run_with(Maze::new)
159}
examples/flight.rs (line 147)
146fn main() -> audio_code::Result<()> {
147    Engine::new("Flight").echo_speech(true).run_with(Flight::new)
148}
examples/platformer.rs (line 109)
108fn main() -> audio_code::Result<()> {
109    Engine::new("Platformer").gravity(EARTH_GRAVITY * 2.0).echo_speech(true).run_with(Platformer::new)
110}
Source

pub fn window_size(self, width: u32, height: u32) -> Self

Window size in pixels (default 640×480). The window shows nothing, but sighted helpers can still find and focus it.

Source

pub fn headless(self) -> Self

Use no audio device and print speech to the console instead of using prism. Meant for automated tests and tools.

Source

pub fn run(self, scene: impl Scene) -> Result<()>

Opens the window and runs the game until it quits.

Examples found in repository?
examples/menus.rs (line 91)
90fn main() -> audio_code::Result<()> {
91    Engine::new("Menu demo").echo_speech(true).run(MainMenu)
92}
More examples
Hide additional examples
examples/hello.rs (line 30)
29fn main() -> audio_code::Result<()> {
30    Engine::new("Hello audio-code").echo_speech(true).run(Hello)
31}
Source

pub fn run_with<S: Scene>( self, build: impl FnOnce(&mut Context) -> S + 'static, ) -> Result<()>

Like Engine::run, but builds the first scene with access to the Context. Handy when the scene needs to create bodies or load sounds in its constructor:

struct Level { player: BodyId }

impl Level {
    fn new(ctx: &mut Context) -> Self {
        Self { player: ctx.physics.add(Body::kinematic(Vec3::ZERO)) }
    }
}

fn main() -> audio_code::Result<()> {
    Engine::new("Game").run_with(Level::new)
}
Examples found in repository?
examples/driving.rs (line 149)
148fn main() -> audio_code::Result<()> {
149    Engine::new("Driving").run_with(Town::new)
150}
More examples
Hide additional examples
examples/beacon.rs (line 158)
157fn main() -> audio_code::Result<()> {
158    Engine::new("Beacon").echo_speech(true).run_with(Maze::new)
159}
examples/flight.rs (line 147)
146fn main() -> audio_code::Result<()> {
147    Engine::new("Flight").echo_speech(true).run_with(Flight::new)
148}
examples/platformer.rs (line 109)
108fn main() -> audio_code::Result<()> {
109    Engine::new("Platformer").gravity(EARTH_GRAVITY * 2.0).echo_speech(true).run_with(Platformer::new)
110}
examples/bell_catcher.rs (lines 23-42)
22fn main() -> audio_code::Result<()> {
23    Engine::new("Bell Catcher").run_with(|ctx| {
24        ctx.audio.register("bell", Synth::sine(1568.0).with(Synth::sine(3136.0).volume(0.3)).length(0.35).decay(9.0).volume(0.6));
25        ctx.audio.register("step", Synth::noise().length(0.05).decay(80.0).volume(0.5));
26        ctx.audio.register("edge", Synth::sine(110.0).length(0.12).decay(30.0).volume(0.8));
27        ctx.audio.register(
28            "catch",
29            Synth::sine(784.0).length(0.08).then(Synth::sine(1047.0).length(0.08)).then(Synth::sine(1568.0).length(0.2).decay(12.0)).volume(0.4),
30        );
31        ctx.audio.register("miss", Synth::saw(220.0).sweep_to(80.0).length(0.5).decay(5.0).volume(0.35));
32
33        // Restore the speech settings from the last session.
34        if let Some(voice) = ctx.storage.get::<String>("voice") {
35            ctx.speech.set_voice(voice);
36        }
37        let rate: f32 = ctx.storage.get_or("rate", 50.0);
38        if rate != 50.0 {
39            ctx.speech.set_rate(rate / 100.0);
40        }
41        Title
42    })
43}
Source

pub fn run_fn(self, update: impl FnMut(&mut Context) + 'static) -> Result<()>

Runs a game written as a single closure that is called every frame. The closure can keep its own state in captured variables. See crate::run.

Source

pub fn build_fn(self, update: impl FnMut(&mut Context) + 'static) -> Runner

Like Engine::build for a closure game.

Source

pub fn build(self, scene: impl Scene) -> Runner

Creates the Context and runner without a window. Window backends use this; you can use it for tests or to drive the engine from your own loop.

Trait Implementations§

Source§

impl Clone for Engine

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Engine

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync> ⓘ

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more