Skip to main content

audio_code/
lib.rs

1//! # audio-code
2//!
3//! A simple, high level game engine for **audio games**: games played by ear, without
4//! graphics. Everything a blind or visually impaired player needs is built in:
5//!
6//! * **Speech** through the player's screen reader or system voice ([`speech`], via prism)
7//! * **Sound**: one-line playback, 3D positioned sounds, music with cross fades ([`audio`])
8//! * **Virtual UI**: spoken menus, sliders, checkboxes, text fields and dialogs ([`ui`])
9//! * **Physics**: walls, floors, gravity, triggers and ray casts ([`physics`])
10//! * **Vehicles**: cars and trucks with weight, speed, gears, brakes and fuel ([`vehicle`])
11//! * **Time**: frame timing, a scheduler, timers, cooldowns and intervals ([`time`])
12//! * **Input**: pressed / held / released, custom key repeat, typed text ([`input`])
13//! * Optional **wxWidgets** integration through wxDragon (`audio_code::wx`, feature `wxdragon`)
14//!
15//! ## A complete game in 30 lines
16//!
17//! ```no_run
18//! use audio_code::prelude::*;
19//!
20//! struct Game {
21//!     x: f32,
22//! }
23//!
24//! impl Scene for Game {
25//!     fn start(&mut self, ctx: &mut Context) {
26//!         ctx.speech.speak("Walk with left and right. Find the beacon!");
27//!         ctx.audio.play_looped_at("beacon", vec3(10.0, 0.0, 0.0));
28//!     }
29//!
30//!     fn update(&mut self, ctx: &mut Context) {
31//!         if ctx.input.repeating(Key::Left, 0.25, 0.25) {
32//!             self.x -= 1.0;
33//!         }
34//!         if ctx.input.repeating(Key::Right, 0.25, 0.25) {
35//!             self.x += 1.0;
36//!         }
37//!         ctx.audio.set_listener(vec3(self.x, 0.0, 0.0), 0.0);
38//!         if self.x == 10.0 {
39//!             ctx.speech.speak("You found it!");
40//!             ctx.quit();
41//!         }
42//!     }
43//! }
44//!
45//! fn main() -> audio_code::Result<()> {
46//!     Engine::new("Beacon").run(Game { x: 0.0 })
47//! }
48//! ```
49//!
50//! Start with [`Engine`], [`Scene`] and [`Context`], then read the module docs. The `docs`
51//! folder of the repository contains a longer guide.
52
53#![warn(missing_docs)]
54
55pub mod audio;
56mod bundle;
57mod engine;
58mod error;
59pub mod input;
60pub mod level;
61pub mod math;
62pub mod physics;
63mod shortcuts;
64pub mod speech;
65pub mod storage;
66pub mod time;
67pub mod ui;
68pub mod vehicle;
69mod window;
70#[cfg(feature = "wxdragon")]
71pub mod wx;
72
73pub use engine::{Context, Engine, Runner, Scene};
74
75// Compiles the code blocks in the README as doc tests, so they cannot go stale.
76#[cfg(doctest)]
77#[doc = include_str!("../README.md")]
78struct ReadmeDoctests;
79pub use error::{Error, Result};
80
81/// The quickest way to start: a whole game as one closure that runs every frame.
82///
83/// ```no_run
84/// use audio_code::prelude::*;
85///
86/// fn main() -> audio_code::Result<()> {
87///     let mut score = 0;
88///     audio_code::run("Clicker", move |ctx| {
89///         if ctx.once("intro") {
90///             ctx.speak("Press space as often as you can. Escape quits.");
91///         }
92///         if ctx.pressed(Key::Space) {
93///             score += 1;
94///             ctx.audio.play_tone(400.0 + score as f32 * 10.0, 0.05);
95///         }
96///         if ctx.pressed(Key::Escape) {
97///             ctx.speak(format!("Your score: {score}"));
98///             ctx.quit();
99///         }
100///     })
101/// }
102/// ```
103///
104/// When the game grows, move to [`Scene`]s.
105pub fn run(title: &str, update: impl FnMut(&mut Context) + 'static) -> Result<()> {
106    Engine::new(title).run_fn(update)
107}
108
109/// Everything you normally need: `use audio_code::prelude::*;`
110pub mod prelude {
111    pub use crate::audio::{Audio, Group, Sound, SoundData, Synth, Wave};
112    pub use crate::engine::{Context, Engine, Scene};
113    pub use crate::input::{Input, Key, Modifiers};
114    pub use crate::level::Level;
115    pub use crate::math::{self, Vec2, Vec3, random, vec2, vec3};
116    pub use crate::physics::{Body, BodyId, BodyKind, EARTH_GRAVITY, MoveResult, PhysicsEvent, RayHit, Shape, World};
117    pub use crate::speech::{Speech, Voice};
118    pub use crate::storage::Storage;
119    pub use crate::time::{Cooldown, Interval, Time, Timer};
120    pub use crate::ui::{Confirm, Menu, Message, TextInput, Ui, UiEvent, UiSound, UiSounds, Value, Widget};
121    pub use crate::vehicle::Vehicle;
122}