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//! * **Translations**: one text file per language, engine texts in English and German ([`lang`])
14//! * Optional **wxWidgets** integration through wxDragon (`audio_code::wx`, feature `wxdragon`)
15//!
16//! ## A complete game in 30 lines
17//!
18//! ```no_run
19//! use audio_code::prelude::*;
20//!
21//! struct Game {
22//!     x: f32,
23//! }
24//!
25//! impl Scene for Game {
26//!     fn start(&mut self, ctx: &mut Context) {
27//!         ctx.speech.speak("Walk with left and right. Find the beacon!");
28//!         ctx.audio.play_looped_at("beacon", vec3(10.0, 0.0, 0.0));
29//!     }
30//!
31//!     fn update(&mut self, ctx: &mut Context) {
32//!         if ctx.input.repeating(Key::Left, 0.25, 0.25) {
33//!             self.x -= 1.0;
34//!         }
35//!         if ctx.input.repeating(Key::Right, 0.25, 0.25) {
36//!             self.x += 1.0;
37//!         }
38//!         ctx.audio.set_listener(vec3(self.x, 0.0, 0.0), 0.0);
39//!         if self.x == 10.0 {
40//!             ctx.speech.speak("You found it!");
41//!             ctx.quit();
42//!         }
43//!     }
44//! }
45//!
46//! fn main() -> audio_code::Result<()> {
47//!     Engine::new("Beacon").run(Game { x: 0.0 })
48//! }
49//! ```
50//!
51//! Start with [`Engine`], [`Scene`] and [`Context`], then read the module docs. The `docs`
52//! folder of the repository contains a longer guide.
53
54#![warn(missing_docs)]
55
56pub mod audio;
57mod bundle;
58mod engine;
59mod error;
60pub mod input;
61pub mod lang;
62pub mod level;
63pub mod math;
64pub mod physics;
65mod shortcuts;
66pub mod speech;
67pub mod storage;
68pub mod time;
69pub mod ui;
70pub mod vehicle;
71mod window;
72#[cfg(feature = "wxdragon")]
73pub mod wx;
74
75pub use engine::{Context, Engine, Runner, Scene};
76
77// Compiles the code blocks in the README as doc tests, so they cannot go stale.
78#[cfg(doctest)]
79#[doc = include_str!("../README.md")]
80struct ReadmeDoctests;
81pub use error::{Error, Result};
82
83/// The quickest way to start: a whole game as one closure that runs every frame.
84///
85/// ```no_run
86/// use audio_code::prelude::*;
87///
88/// fn main() -> audio_code::Result<()> {
89///     let mut score = 0;
90///     audio_code::run("Clicker", move |ctx| {
91///         if ctx.once("intro") {
92///             ctx.speak("Press space as often as you can. Escape quits.");
93///         }
94///         if ctx.pressed(Key::Space) {
95///             score += 1;
96///             ctx.audio.play_tone(400.0 + score as f32 * 10.0, 0.05);
97///         }
98///         if ctx.pressed(Key::Escape) {
99///             ctx.speak(format!("Your score: {score}"));
100///             ctx.quit();
101///         }
102///     })
103/// }
104/// ```
105///
106/// When the game grows, move to [`Scene`]s.
107pub fn run(title: &str, update: impl FnMut(&mut Context) + 'static) -> Result<()> {
108    Engine::new(title).run_fn(update)
109}
110
111/// Everything you normally need: `use audio_code::prelude::*;`
112pub mod prelude {
113    pub use crate::audio::{Audio, Group, Sound, SoundData, Synth, Wave};
114    pub use crate::engine::{Context, Engine, Scene};
115    pub use crate::input::{Input, Key, Modifiers};
116    pub use crate::lang::Lang;
117    pub use crate::level::Level;
118    pub use crate::math::{self, Vec2, Vec3, random, vec2, vec3};
119    pub use crate::physics::{Body, BodyId, BodyKind, EARTH_GRAVITY, MoveResult, PhysicsEvent, RayHit, Shape, World};
120    pub use crate::speech::{Speech, Voice};
121    pub use crate::storage::Storage;
122    pub use crate::time::{Cooldown, Interval, Time, Timer};
123    pub use crate::ui::{Confirm, Menu, Message, TextInput, Ui, UiEvent, UiSound, UiSounds, Value, Widget};
124    pub use crate::vehicle::Vehicle;
125}