audio-code 0.2.1

A simple, high level game engine for audio games: 3D sound, speech, virtual menus, physics and timing. No graphics required.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# Engine, Scene and Runner


Starting a game, configuring the engine, writing scenes, and driving the engine yourself.
See also [context.md](context.md) for what a scene can do with `ctx`.

```rust
use audio_code::prelude::*;   // Engine, Scene, Context and everything else
```

## Starting a game


### `audio_code::run(title: &str, update: impl FnMut(&mut Context) + 'static) -> Result<()>`


The quickest start: the whole game is one closure that is called every frame. The closure
keeps its state in captured variables. Same as `Engine::new(title).run_fn(update)`. Opens
a window and returns when the game quits.

```rust
use audio_code::prelude::*;

fn main() -> audio_code::Result<()> {
    let mut score = 0;
    audio_code::run("Clicker", move |ctx| {
        if ctx.once("intro") {
            ctx.speak("Press space. Escape quits.");
        }
        if ctx.pressed(Key::Space) {
            score += 1;
            ctx.audio.play_tone(400.0 + score as f32 * 10.0, 0.05);
        }
        if ctx.pressed(Key::Escape) {
            ctx.speak(format!("Score: {score}"));
            ctx.quit();
        }
    })
}
```

## Engine


`Engine` is a builder that holds the configuration. Create it with `Engine::new`, chain the
settings you need, and finish with one of the `run*` or `build*` methods.

### `Engine::new(title: impl Into<String>) -> Engine`


Creates a configuration with defaults: 60 fps, 60 physics steps per second, sound folder
`"sounds"`, no gravity, window 640×480, save file `<title>.save`. The title is the window
title, which the screen reader announces when the window gets focus.

```rust
use audio_code::prelude::*;

struct MainMenu;

impl Scene for MainMenu {
    fn update(&mut self, ctx: &mut Context) {}
}

fn main() -> audio_code::Result<()> {
    Engine::new("My game").run(MainMenu)
}
```

### `engine.fps(fps: u32) -> Engine`


Target frames per second, clamped to 1..=1000. Default 60. `Scene::update` runs once per
frame.

```rust
let engine = Engine::new("Game").fps(120);
```

### `engine.physics_rate(rate: u32) -> Engine`


Physics steps (and `Scene::fixed_update` calls) per second, clamped to 1..=1000.
Default 60.

```rust
let engine = Engine::new("Game").physics_rate(120);
```

### `engine.gravity(gravity: Vec3) -> Engine`


Gravity of the physics world in m/s². Default `Vec3::ZERO` (no gravity, right for top down
games). Use `EARTH_GRAVITY` (`vec3(0.0, 0.0, -9.81)`) for platformers. Can be changed
later with `ctx.physics.set_gravity`.

```rust
let engine = Engine::new("Jumper").gravity(EARTH_GRAVITY);
let moon = Engine::new("Moon").gravity(vec3(0.0, 0.0, -1.62));
```

### `engine.save_file(path: impl Into<PathBuf>) -> Engine`


File used by `ctx.storage`. Default `<title>.save` in the working directory, with every
character that is not a letter, digit, `-` or `_` replaced by `_`.

```rust
let engine = Engine::new("Game").save_file("saves/profile1.save");
```

### `engine.encrypt_save(secret: impl Into<String>) -> Engine`


Encrypts the save file of `ctx.storage` with a key made from `secret` (optional, default
off). Players can no longer read or edit it; a changed or damaged file is detected, kept as
`<file>.broken`, and reported by `ctx.storage.load_error()`. An existing plain text save
still loads and is written encrypted on the next save. See
[storage.md](storage.md#encryption).

```rust
let engine = Engine::new("Game").encrypt_save("my secret word");
```

### `engine.sound_dir(dir: impl Into<PathBuf>) -> Engine`


Folder searched for sounds by name. Default `"sounds"`, so `ctx.play("step")` finds
`sounds/step.ogg`. See [audio.md](audio.md#how-sound-names-are-found).

```rust
let engine = Engine::new("Game").sound_dir("assets/audio");
```

### `engine.lang_dir(dir: impl Into<PathBuf>) -> Engine`


Folder with the translation files `<code>.txt`. Default `"lang"`. See [lang.md](lang.md).

```rust
let engine = Engine::new("Game").lang_dir("assets/lang");
```

### `engine.language(code: impl Into<String>) -> Engine`


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 `ctx.set_language` still
wins. English and German engine texts are built in, so a German-only game needs no files.

```rust
let engine = Engine::new("Spiel").language("de");
```

### `engine.echo_speech(echo: bool) -> Engine`


Also prints everything spoken to the console. Default `false`. Handy while developing.

```rust
let engine = Engine::new("Game").echo_speech(true);
```

### `engine.window_size(width: u32, height: u32) -> Engine`


Window size in pixels. Default 640×480. The window draws nothing; it exists so the game
receives keyboard input and can be found and focused.

```rust
let engine = Engine::new("Game").window_size(800, 600);
```

### `engine.headless() -> Engine`


No audio device (silent `Audio`), speech printed to the console, storage kept in memory.
Meant for automated tests and tools; combine with `Engine::build`.

```rust
let mut runner = Engine::new("test").headless().build_fn(|ctx| {
    ctx.speak("hello");
});
runner.frame();
```

### `engine.run(scene: impl Scene) -> Result<()>`


Opens the window (winit backend) and runs the game until it quits. Returns an
`Error::Window` if no window can be created.

```rust
use audio_code::prelude::*;

struct Game;

impl Scene for Game {
    fn update(&mut self, ctx: &mut Context) {
        if ctx.pressed(Key::Escape) {
            ctx.quit();
        }
    }
}

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

### `engine.run_with<S: Scene>(build: impl FnOnce(&mut Context) -> S + 'static) -> Result<()>`


Like `run`, but builds the first scene with access to the `Context`. Use it when the
scene's constructor needs to add bodies, load sounds or read storage. `build` runs once,
before the scene's `start`.

```rust
use audio_code::prelude::*;

struct Level {
    player: BodyId,
}

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

impl Scene for Level {
    fn update(&mut self, ctx: &mut Context) {
        let step = ctx.input.movement() * 3.0 * ctx.time.delta();
        ctx.physics.move_by(self.player, step.extend(0.0));
    }
}

fn main() -> audio_code::Result<()> {
    Engine::new("Game").gravity(Vec3::ZERO).run_with(Level::new)
}
```

### `engine.run_fn(update: impl FnMut(&mut Context) + 'static) -> Result<()>`


Runs a game written as a single closure called every frame. `audio_code::run` is a
shortcut for this.

```rust
use audio_code::prelude::*;

fn main() -> audio_code::Result<()> {
    Engine::new("Tiny").echo_speech(true).run_fn(|ctx| {
        if ctx.pressed(Key::Escape) {
            ctx.quit();
        }
    })
}
```

### `engine.build(scene: impl Scene) -> Runner`


Creates the context and a `Runner` without opening a window. You call `runner.frame()`
yourself. Used by window backends, tests and custom loops.

```rust
use audio_code::prelude::*;

struct Game;

impl Scene for Game {
    fn update(&mut self, ctx: &mut Context) {
        if ctx.pressed(Key::Space) {
            ctx.speak("Space");
        }
    }
}

fn main() {
    let mut runner = Engine::new("test").headless().build(Game);
    runner.frame();                       // first frame: Scene::start
    runner.key_down(Key::Space, false);
    runner.frame();                       // Scene::update sees Space pressed
    assert_eq!(runner.ctx.speech.last(), Some("Space"));
}
```

### `engine.build_fn(update: impl FnMut(&mut Context) + 'static) -> Runner`


Like `build` for a closure game.

```rust
let mut runner = Engine::new("test").headless().build_fn(|ctx| {
    if ctx.pressed(Key::Enter) {
        ctx.quit();
    }
});
runner.frame();
runner.key_down(Key::Enter, false);
assert!(!runner.frame());   // false: the game quit
```

`engine.run_wx(..)` and `engine.run_wx_with(..)` (feature `wxdragon`) are described in
[wx.md](wx.md).

## Scene


```rust
pub trait Scene: 'static {
    fn start(&mut self, ctx: &mut Context) {}
    fn update(&mut self, ctx: &mut Context);           // required
    fn fixed_update(&mut self, ctx: &mut Context) {}
    fn on_ui(&mut self, event: &UiEvent, ctx: &mut Context) {}
    fn pause(&mut self, ctx: &mut Context) {}
    fn resume(&mut self, ctx: &mut Context) {}
    fn end(&mut self, ctx: &mut Context) {}
}
```

One screen or state of the game: main menu, level, pause screen, game over. Only `update`
is required. Scenes are changed with `ctx.switch_scene`, `ctx.push_scene` and
`ctx.pop_scene` (see [context.md](context.md#scene-changes)).

Order inside one frame: UI update → `on_ui` for each UI event → zero or more physics steps,
each followed by `fixed_update` → `update` → listener and attached sounds follow their
bodies → pending scene change is applied.

### `fn start(&mut self, ctx: &mut Context)`


Called once when the scene becomes active for the first time (game start, switch or push).
Speak the intro, open the menu, start music here.

```rust
use audio_code::prelude::*;

struct Intro;

impl Scene for Intro {
    fn start(&mut self, ctx: &mut Context) {
        ctx.speak("Welcome. Press Enter to begin.");
        ctx.audio.play_music("music/intro");
    }
    fn update(&mut self, ctx: &mut Context) {}
}
```

### `fn update(&mut self, ctx: &mut Context)`


Called every frame. Game logic goes here. Multiply speeds by `ctx.time.delta()` to stay
independent of the frame rate.

```rust
use audio_code::prelude::*;

struct Walk {
    x: f32,
}

impl Scene for Walk {
    fn update(&mut self, ctx: &mut Context) {
        self.x += ctx.input.axis(Key::Left, Key::Right) * 2.0 * ctx.time.delta();
        ctx.audio.set_listener(vec3(self.x, 0.0, 0.0), 0.0);
    }
}
```

### `fn fixed_update(&mut self, ctx: &mut Context)`


Called at a fixed rate (`Engine::physics_rate`, default 60 per second), right after each
physics step. Use it for logic that must not depend on the frame rate, such as applying
forces. It can run 0 to 8 times per frame.

```rust
use audio_code::prelude::*;

struct Windy {
    leaf: BodyId,
}

impl Scene for Windy {
    fn fixed_update(&mut self, ctx: &mut Context) {
        ctx.physics.apply_force(self.leaf, vec3(2.0, 0.0, 0.0));
    }
    fn update(&mut self, ctx: &mut Context) {}
}
```

### `fn on_ui(&mut self, event: &UiEvent, ctx: &mut Context)`


Called for every UI event of the frame, before `update`. An alternative to polling
`ctx.ui.clicked(..)` in `update`.

```rust
use audio_code::prelude::*;

struct MainMenu;

impl Scene for MainMenu {
    fn start(&mut self, ctx: &mut Context) {
        ctx.ui.open(Menu::new("main", "Main menu").button("play", "Play").button("quit", "Quit"));
    }
    fn on_ui(&mut self, event: &UiEvent, ctx: &mut Context) {
        if let UiEvent::Activated { item, .. } = event {
            match item.as_str() {
                "quit" => ctx.quit(),
                _ => ctx.speak("Not yet"),
            }
        }
    }
    fn update(&mut self, ctx: &mut Context) {}
}
```

### `fn pause(&mut self, ctx: &mut Context)`


Called when another scene is pushed on top of this one. Good place to pause loops.

```rust
use audio_code::prelude::*;

struct Level {
    engine_sound: Sound,
}

impl Scene for Level {
    fn pause(&mut self, ctx: &mut Context) {
        self.engine_sound.pause();
        ctx.time.pause();
    }
    fn resume(&mut self, ctx: &mut Context) {
        self.engine_sound.resume();
        ctx.time.resume();
    }
    fn update(&mut self, ctx: &mut Context) {}
}
```

### `fn resume(&mut self, ctx: &mut Context)`


Called when this scene is on top again after the scene above it was popped. See the
example under `pause`.

### `fn end(&mut self, ctx: &mut Context)`


Called when the scene is removed: popped, switched away from, or when the game quits.
Clean up bodies and sounds here.

```rust
use audio_code::prelude::*;

struct Level;

impl Scene for Level {
    fn end(&mut self, ctx: &mut Context) {
        ctx.physics.clear();
        ctx.audio.stop_all();
    }
    fn update(&mut self, ctx: &mut Context) {}
}
```

## Runner


`audio_code::Runner` drives scenes frame by frame. Window backends feed it key events and
call `frame()`. You only need it for tests, tools or your own backend. Create it with
`Engine::build` or `Engine::build_fn`.

### `runner.ctx: Context`


The public context. Tests read and change it directly.

```rust
let mut runner = Engine::new("t").headless().build_fn(|_| {});
let p = runner.ctx.physics.add(Body::kinematic(Vec3::ZERO));
runner.frame();
assert_eq!(runner.ctx.physics.position(p), Vec3::ZERO);
```

### `runner.frame() -> bool`

Runs one frame now. The very first call only runs `Scene::start`. Returns `false` once the
game has quit (and then storage has been saved and all scenes ended).

```rust
let mut runner = Engine::new("t").headless().build_fn(|ctx| ctx.quit());
runner.frame();              // start
assert!(!runner.frame());    // update calls quit
```

### `runner.key_down(key: Key, repeat: bool)`

Reports a key going down. `repeat` is `true` for key repeats sent by the operating system.
The key is visible in the next `frame()`.

```rust
let mut runner = Engine::new("t").headless().build_fn(|_| {});
runner.frame();
runner.key_down(Key::Up, false);
runner.frame();
assert!(runner.ctx.input.held(Key::Up));
```

### `runner.key_up(key: Key)`

Reports a key going up.

```rust
let mut runner = Engine::new("t").headless().build_fn(|_| {});
runner.key_down(Key::Up, false);
runner.frame();
runner.key_up(Key::Up);
runner.frame();
```

### `runner.text(text: &str)`

Reports typed text (control characters are dropped). Text fields read it through
`ctx.input.text()`.

```rust
let mut runner = Engine::new("t").headless().build_fn(|_| {});
runner.frame();
runner.text("hello");
runner.frame();
assert_eq!(runner.ctx.input.text(), "hello");
```

### `runner.focus_lost()`

Releases all held keys. Call it when the window loses focus, so no key stays stuck.

```rust
let mut runner = Engine::new("t").headless().build_fn(|_| {});
runner.focus_lost();
```

### `runner.next_frame_time() -> Instant`

When the next frame is due according to `Engine::fps`. Backends wait until then.

```rust
let mut runner = Engine::new("t").headless().build_fn(|_| {});
let due = runner.next_frame_time();
```

### `runner.should_quit() -> bool`

Whether the game wants to quit.

```rust
let mut runner = Engine::new("t").headless().build_fn(|_| {});
if runner.should_quit() {
    runner.shutdown();
}
```

### `runner.shutdown()`

Ends all scenes (`Scene::end`) and saves `ctx.storage`. Called automatically when the game
quits; call it yourself when you stop driving the runner early.

```rust
let mut runner = Engine::new("t").headless().build_fn(|_| {});
runner.frame();
runner.shutdown();
```

## Error and Result

### `audio_code::Result<T>`

Shorthand for `std::result::Result<T, audio_code::Error>`. `main` usually returns
`audio_code::Result<()>`.

### `audio_code::Error`

```rust
pub enum Error {
    Io(std::io::Error),   // a file could not be read
    Decode(String),       // a sound file could not be decoded
    Audio(String),        // the audio device could not be opened
    Window(String),       // the window or event loop could not be created
}
```

Implements `Display`, `std::error::Error` and `From<std::io::Error>`. Most of the engine
never returns errors (missing sounds only warn). You see them from `Engine::run`,
`Audio::preload`, `Audio::preload_dir`, `SoundData::load` and `Storage::save`.

```rust
match ctx.audio.preload("music/boss") {
    Ok(()) => {}
    Err(audio_code::Error::Io(e)) => eprintln!("missing: {e}"),
    Err(e) => eprintln!("{e}"),
}
```

## Prelude


`use audio_code::prelude::*;` imports:

- `Context`, `Engine`, `Scene`
- `Audio`, `Group`, `Sound`, `SoundData`, `Synth`, `Wave`
- `Input`, `Key`, `Modifiers`
- `Level`
- `math` (the module), `Vec2`, `Vec3`, `vec2`, `vec3`, `random`
- `Body`, `BodyId`, `BodyKind`, `EARTH_GRAVITY`, `MoveResult`, `PhysicsEvent`, `RayHit`,
  `Shape`, `World`
- `Speech`, `Storage`
- `Cooldown`, `Interval`, `Time`, `Timer`
- `Confirm`, `Menu`, `Message`, `TextInput`, `Ui`, `UiEvent`, `UiSound`, `UiSounds`,
  `Value`, `Widget`
- `Vehicle`

Not in the prelude (import by path): `audio_code::Runner`, `audio_code::Error`,
`audio_code::audio::SoundBuilder`, `audio_code::input::RawInput`,
`audio_code::ui::{Status, UiContext, MenuItem, ItemKind}`, `audio_code::wx::GameHost`.