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
impl Engine
Sourcepub fn new(title: impl Into<String>) -> Self
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?
More examples
Sourcepub fn physics_rate(self, rate: u32) -> Self
pub fn physics_rate(self, rate: u32) -> Self
Physics and Scene::fixed_update steps per second (default 60).
Sourcepub fn gravity(self, gravity: Vec3) -> Self
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.
Sourcepub fn save_file(self, path: impl Into<PathBuf>) -> Self
pub fn save_file(self, path: impl Into<PathBuf>) -> Self
File used by Context::storage (default <title>.save in the working directory).
Sourcepub fn encrypt_save(self, secret: impl Into<String>) -> Self
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.
Sourcepub fn sound_dir(self, dir: impl Into<PathBuf>) -> Self
pub fn sound_dir(self, dir: impl Into<PathBuf>) -> Self
Folder searched for sounds (default "sounds"). See crate::audio.
Sourcepub fn lang_dir(self, dir: impl Into<PathBuf>) -> Self
pub fn lang_dir(self, dir: impl Into<PathBuf>) -> Self
Folder with the translation files <code>.txt (default "lang"). See
crate::lang.
Sourcepub fn language(self, code: impl Into<String>) -> Self
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.
Sourcepub fn echo_speech(self, echo: bool) -> Self
pub fn echo_speech(self, echo: bool) -> Self
Print everything spoken to the console as well (default false).
Examples found in repository?
More examples
Sourcepub fn window_size(self, width: u32, height: u32) -> Self
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.
Sourcepub fn headless(self) -> Self
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.
Sourcepub fn run(self, scene: impl Scene) -> Result<()>
pub fn run(self, scene: impl Scene) -> Result<()>
Opens the window and runs the game until it quits.
Examples found in repository?
More examples
Sourcepub fn run_with<S: Scene>(
self,
build: impl FnOnce(&mut Context) -> S + 'static,
) -> Result<()>
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?
More examples
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}Sourcepub fn run_fn(self, update: impl FnMut(&mut Context) + 'static) -> Result<()>
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.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Engine
impl RefUnwindSafe for Engine
impl Send for Engine
impl Sync for Engine
impl Unpin for Engine
impl UnsafeUnpin for Engine
impl UnwindSafe for Engine
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.