nadir_engine 0.4.0

A lightweight, cross-platform Rust engine.
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
# nadir_engine

The engine crate of Nadir: the `App` and its main loop, the engine's
components and systems, and the subsystems a game uses, in one crate. Code
names it `nadir`.

It is part of [Nadir](https://github.com/jabbott-iii/Nadir), a 2D engine for
top-down games on Windows, macOS and Linux, written in Rust with no
third-party crates. This is the crate a game depends on: it re-exports the
ECS, input, rendering, sound, collision, assets and reflection as
`nadir::ecs`, `nadir::input`, `nadir::render`, `nadir::audio`,
`nadir::collision`, `nadir::assets` and `nadir::scene`, so a game needs
nothing else.

## Status

Version 0.4: the API will change before 1.0.

Built:

- a window, with keyboard and mouse input, on Linux (X11);
- a fixed 60 Hz simulation, which drawing interpolates between ticks;
- an ECS of entities, components, resources and events, with systems in
  stages;
- input actions, with bindings, latched once a tick, and replays of them;
- an OpenGL 3.3 renderer: y-sorted sprite batches, tile layers with
  animated tiles, bitmap text in the world, and a pixel-perfect camera;
- kinematic collision: boxes and circles, a grid of solid tiles,
  move-and-slide, triggers, and queries;
- sprite animation, from clips made in code or from Aseprite's tags;
- sound on Linux, through ALSA: a mixer of voices on four buses, sounds
  that play at entities and pan and fade by where they are from the
  listener, and music that crossfades as the listener goes from one area
  to another;
- prefabs, which extend one another, spawned through the
  `PrefabLibrary`, with the engine's sprites, colliders and animators
  reflected for them;
- levels made in LDtk and cooked, which `App::load_level` loads whole, in
  place of the last: their tile layers drawn, their grid collided with,
  their tall tiles and entities spawned;
- assets loaded on worker threads, and an offline cook that packs images,
  Aseprite exports and bitmap fonts into atlases, makes level files of
  LDtk projects, turns WAVs into the game's sounds and music, and puts
  all it cooks in one pack file, which release builds read;
- hot reload in development builds: textures, atlases and their clips,
  prefabs, the current level and shaders, reloaded while the game runs,
  behind the handles it holds;
- a debug UI in development builds: windows the game adds, each shown by a
  key of its own, with widgets from `nadir::debug`, drawn over the game in
  the engine's own pixel font, and keeping their places across runs; and
  the engine's own, a stats overlay on F1 and a profiler on F3, whose
  graphs of the last 300 frames' processor and GPU times show a frame's
  scopes as a tree, and which writes them as a Chrome trace for Perfetto
  UI; an inspector on F2, of the world's entities and their components,
  each field edited through reflection, components added and removed, an
  entity despawned or written as a prefab; a console on the backtick,
  over the log, with history, Tab completion, the engine's commands and
  the game's; debug drawing on F4
  (colliders, triggers, the collision grid, the spatial hash, tile chunks,
  the camera); and time controls, F5 to pause, F6 to step a tick, F7 to
  scale the game's time;
- a world hash, to check that a replay gives the same simulation.

Not built yet:

- drawing on Windows and macOS: the window stays empty until the OpenGL
  contexts there land (roadmap tasks M2.6 and M2.7), and sound there
  (M6.4 and M6.5);
- gamepads (M8);
- game states (M9.1), settings (M9.3), saves (M9.4), pathfinding (M9.5);
- the Metal backend for macOS (M10).

## What you need

- Rust, from [rustup]https://rustup.rs. The engine is built and tested
  with the stable toolchain its repository pins (`rust-toolchain.toml`);
  older versions are not tested.
- On Linux: an X11 session, or Wayland with XWayland, and OpenGL 3.3. The
  engine loads `libX11`, `libEGL` and OpenGL at run time, and ALSA's
  `libasound` for sound, so it needs no development packages to build.
  Without `libasound` the game runs, silent.
- On Windows: the MSVC build tools and the Windows SDK. On macOS: the
  Xcode command-line tools. A game builds and runs on both, but its window
  stays empty for now (see [Platforms]#platforms).

## Usage

```toml
[dependencies]
nadir_engine = "0.4"
```

The package is `nadir_engine`, since `nadir` was taken on crates.io, and its
library is `nadir`. A game needs only this crate:

```rust,no_run
use nadir::prelude::*;

fn main() -> Result<(), nadir::Error> {
    App::new(Config::default())
        .add_fixed_system(FixedStage::Gameplay, "hello", |_, tick| {
            if tick.index == 0 {
                nadir::log::info!("the first tick");
            }
        })
        .run()
}
```

`App::run` opens the window and the audio output, and runs the game until
the window is closed.

### A walking hero

This game has a hero who walks with WASD or the arrow keys, animated and
facing the way they go, on a field of grass walled in by stone that they
collide with, and a camera that follows them. Its art is cooked by
`nadir-cook` (see [Cooking assets](#cooking-assets)) from these files:

```text
assets/sprites/hero.png      the hero's sheet, exported from Aseprite
assets/sprites/hero.json     its frames and its tags, `stand` and `walk`
assets/sprites/tiles.png     a tile set: grass is tile 0, stone tile 1
assets/sprites/tiles.sprite.jsonc
                             { "tiles": [16, 16] }: cut it into 16-pixel tiles
```

```rust,no_run
use std::error::Error;

use nadir::assets::Scope;
use nadir::prelude::*;

/// What the player does.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Act {
    Move,
}

impl ActionSet for Act {
    const ALL: &'static [Self] = &[Act::Move];

    fn index(self) -> usize {
        self as usize
    }

    fn name(self) -> &'static str {
        "move"
    }

    fn kind(self) -> ActionKind {
        ActionKind::Axis2
    }
}

/// The hero's clips: the tags of their Aseprite export.
struct Gait {
    stand: Handle<Clip>,
    walk: Handle<Clip>,
}

/// The ground's tile layer.
struct Ground(Handle<TileLayer>);

/// How fast the hero walks, in pixels a second.
const SPEED: f32 = 120.0;
/// The field's size in tiles, and a tile's side in pixels.
const WIDTH: u32 = 30;
const HEIGHT: u32 = 17;
const TILE: f32 = 16.0;

fn main() -> Result<(), Box<dyn Error>> {
    let mut map = ActionMap::<Act>::new()?;
    let keys = |up, down, left, right| Binding::KeyComposite2 { up, down, left, right };
    map.bind(Act::Move, 0, keys(Key::W, Key::S, Key::A, Key::D))?;
    map.bind(Act::Move, 1, keys(Key::Up, Key::Down, Key::Left, Key::Right))?;

    let mut app = App::new(Config {
        title: "My game".into(),
        game: "My game".into(),
        assets: "cooked".into(),
        ..Config::default()
    })
    .add_actions(map)
    .add_fixed_system(FixedStage::Gameplay, "walk", walk)
    .add_frame_system(FrameStage::Camera, "follow", follow)
    .add_frame_system(FrameStage::Extract, "draw_ground", draw_ground);

    let world = app.world_mut();
    // The cooked atlas, made now, and its pages, which load in the
    // background and are drawn once they have.
    let (atlas, pages) = {
        let mut server = world.resource_mut::<AssetServer>();
        let atlas = server.load_now::<Atlas>("atlas/sprites.kbj", &mut FinalizeCtx::new())?;
        let atlas = server.get(atlas).cloned().ok_or("the atlas did not load")?;
        let pages = atlas.load_pages(&mut server, Scope::Global);
        (atlas, pages)
    };
    let page = |sprite: &AtlasSprite| {
        let first = sprite.frames().first().ok_or("a sprite with no frame")?;
        pages.get(first.page()).copied().ok_or("a frame on no page")
    };
    let gait = {
        let mut clips = world.resource_mut::<Clips>();
        clips.add_atlas(&atlas, &pages)?;
        Gait {
            stand: clips.find("sprites/hero/stand").ok_or("no clip `stand`")?,
            walk: clips.find("sprites/hero/walk").ok_or("no clip `walk`")?,
        }
    };
    let hero = atlas.sprite("sprites/hero").ok_or("no sprite `sprites/hero`")?;
    let hero = Sprite::from_atlas(page(hero)?, hero, 0).ok_or("the hero has no frame")?;
    let tiles = atlas.sprite("sprites/tiles").ok_or("no sprite `sprites/tiles`")?;
    let set = tiles.tile_set(page(tiles)?).ok_or("`sprites/tiles` is not a tile set")?;

    // The ground to draw, and the grid of solid cells to collide with.
    let mut ground = TileLayer::new(set, UVec2::new(WIDTH, HEIGHT), TILE, Vec2::ZERO);
    let mut grid = TileGrid::new(WIDTH, HEIGHT, TILE, Vec2::ZERO);
    for y in 0..HEIGHT {
        for x in 0..WIDTH {
            let cell = IVec2::new(x as i32, y as i32);
            let stone = x == 0 || y == 0 || x == WIDTH - 1 || y == HEIGHT - 1;
            ground.set(cell, Some(Tile::new(if stone { 1 } else { 0 })));
            grid.set_solid(cell, stone);
        }
    }
    let ground = world.resource_mut::<TileLayers>().add(ground);
    world.resource_mut::<Collisions>().set_grid(grid);

    world
        .spawn()
        .with(Player)
        .with(Transform::at(Vec2::new(240.0, 136.0)))
        .with(Velocity::default())
        .with(hero)
        .with(Animator::new(gait.stand))
        .with(Facing::Right)
        // A box at the hero's feet, which their Transform is.
        .with(Collider::solid(Shape::Aabb { half: Vec2::new(5.0, 3.0) }));
    world.insert_resource(gait);
    world.insert_resource(Ground(ground));

    app.run()?;
    Ok(())
}

/// Walks the hero by the `Move` action, faces them the way they go, and
/// plays their walk while they go. The engine's `nadir::movement`, after
/// this in the tick, moves them and slides them along the walls.
fn walk(world: &World, _tick: &Tick) {
    let direction = world.resource::<ActionState<Act>>().axis2(Act::Move);
    let gait = world.resource::<Gait>();
    let players = world.read::<Player>();
    let mut velocities = world.write::<Velocity>();
    let mut facings = world.write::<Facing>();
    let mut animators = world.write::<Animator>();
    for (player, _) in players.iter() {
        if let Some(velocity) = velocities.get_mut(player) {
            velocity.0 = direction * SPEED;
        }
        if let Some(facing) = facings.get_mut(player) {
            if direction.x < 0.0 {
                *facing = Facing::Left;
            } else if direction.x > 0.0 {
                *facing = Facing::Right;
            }
        }
        if let Some(animator) = animators.get_mut(player) {
            animator.play(if direction == Vec2::ZERO { gait.stand } else { gait.walk });
        }
    }
}

/// Centres the camera on the hero, where this frame draws them: part of the
/// way from where the last tick started to where it left them.
fn follow(world: &World, frame: &Frame) {
    let Some(player) = world.read::<Player>().entities().first().copied() else {
        return;
    };
    let Some(now) = world.read::<Transform>().get(player).map(|it| it.position) else {
        return;
    };
    let then = world.read::<PrevTransform>().get(player).map_or(now, |it| it.0.position);
    world.resource_mut::<RenderFrame>().camera.position = then.lerp(now, frame.alpha);
}

/// Draws the ground, behind every sprite.
fn draw_ground(world: &World, _frame: &Frame) {
    let ground = world.resource::<Ground>().0;
    world.resource_mut::<RenderFrame>().tile_layers.push(TileLayerDraw::new(ground));
}
```

### Running frames without a window

`App::update` runs one frame, with the window's events you give it, and
draws nothing. Tests use it to drive a game tick by tick:

```rust
use std::time::Duration;

use nadir::prelude::*;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Act {
    Move,
}

impl ActionSet for Act {
    const ALL: &'static [Self] = &[Act::Move];

    fn index(self) -> usize {
        self as usize
    }

    fn name(self) -> &'static str {
        "move"
    }

    fn kind(self) -> ActionKind {
        ActionKind::Axis2
    }
}

fn walk(world: &World, _tick: &Tick) {
    let direction = world.resource::<ActionState<Act>>().axis2(Act::Move);
    let players = world.read::<Player>();
    let mut velocities = world.write::<Velocity>();
    for (player, _) in players.iter() {
        if let Some(velocity) = velocities.get_mut(player) {
            velocity.0 = direction * 120.0;
        }
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut map = ActionMap::<Act>::new()?;
    let wasd = Binding::KeyComposite2 { up: Key::W, down: Key::S, left: Key::A, right: Key::D };
    map.bind(Act::Move, 0, wasd)?;
    let mut app = App::new(Config::default())
        .add_actions(map)
        .add_fixed_system(FixedStage::Gameplay, "walk", walk);
    let player = app
        .world_mut()
        .spawn()
        .with(Player)
        .with(Transform::at(Vec2::ZERO))
        .with(Velocity::default())
        .id();

    // D goes down, and a frame of one tick runs: 120 pixels a second is 2
    // a tick.
    let tick = Duration::from_nanos(16_666_667);
    let d = nadir::Event::Key { key: Key::D, pressed: true, repeat: false };
    app.update(tick, &[d]);
    let at = app.world().read::<Transform>().get(player).map(|it| it.position);
    assert_eq!(at, Some(Vec2::new(2.0, 0.0)));
    Ok(())
}
```

## How a game fits together

### The app and the main loop

A game makes an `App` from a `Config`, adds its resources, actions and
systems with the builder methods, sets its world up through
`App::world_mut`, and calls `App::run`. That opens the log file, the
window and the audio output, and then each frame:

1. hands the window's events to `Input`;
2. runs the simulation ticks that are due, 60 a second however fast the
   frames come; each tick latches the game's actions, then runs the tick
   stages;
3. runs the frame stages once, which fill the `RenderFrame`;
4. draws it, and makes the assets the workers have loaded.

A frame counts for at most 250 ms, so after a stall the game falls behind
rather than running a burst of ticks to catch up. `Config` sets the window
(title, size, resizing, fullscreen), the pacing (`frame_rate_limit`, 60 by
default, and `vsync`), the size of the image the world is drawn into
(`virtual_resolution`, 480 × 270 by default), the folder the assets
load from (`assets`), and each sound bus's volume as the game starts
(`volumes`). `studio` and `game` name the folders of the log file.

### The world, systems and stages

The game's state is a `World` (`nadir::ecs`): entities, their components,
and resources, which are values of any type, one of each.

- A component is a type with `impl Component for MyType {}`, registered
  with `app.world_mut().register::<MyType>()` before use. A reflected one,
  defined with `nadir::scene::reflect_struct!`, `reflect_enum!` or
  `reflect_newtype!`, is registered with `App::register_component` instead:
  the world hash covers the reflected components.
- A system is a plain function, `fn(&World, &Tick)` for a stage of each
  tick or `fn(&World, &Frame)` for a stage of each frame. A closure that
  captures nothing will do too; systems keep their state in resources.
- Systems read and write through the world: `world.read::<T>()`,
  `world.write::<T>()`, `world.resource::<R>()` and
  `world.resource_mut::<R>()`. These are borrowed at run time: borrowing
  the same storage mutably twice at once panics, naming the system and the
  type.
- Inside a system, spawn and despawn through `world.commands()`. Commands
  apply after the tick's stages, or after the frame's. During setup,
  `world.spawn()` spawns at once.
- Events are resources too: `App::add_event::<T>()`, then
  `world.resource_mut::<Events<T>>().send(event)`. Tick systems read
  `tick_events()`, the tick's own; frame systems read `frame_events()`, all
  the ticks' since the last frame.

The stages of a tick are `Input`, `Gameplay`, `Movement`, `PostMovement`
and `Cleanup`; those of a frame `Animate`, `Camera`, `Ui` and `Extract`.
Systems run in stage order, then in the order they were added. The engine
adds its own first in their stages:

| Stage | Engine system | What it does |
| :- | :- | :- |
| tick `Input` | `nadir::snapshot` | Copies each `Transform` into its `PrevTransform` |
| tick `Movement` | `nadir::movement` | Moves each entity with a `Velocity`; one with a `Collider` slides along what it meets |
| tick `PostMovement` | `nadir::parent_offsets` | Puts each entity with a `Parent` at its parent's position, plus its offset |
| tick `PostMovement` | `nadir::triggers` | Sends `TriggerEnter` and `TriggerExit` |
| frame `Animate` | `nadir::animate` | Advances each `Animator`, writes its frame into its `Sprite`, and mirrors the sprites that face left |
| frame `Extract` | `nadir::sound` | Puts the listener at the `AudioListener` or the camera, plays each `AudioEmitter` at its entity, and sends the track of the `MusicArea` the listener is in |
| frame `Extract` | `nadir::extract_sprites` | Draws each `Sprite` into the `RenderFrame`, between the last two ticks |
| frame `Extract` | `nadir::extract_level` | Draws the `CurrentLevel`'s tile layers, with its background as the clear colour |

So a game sets velocities in `Gameplay`, and reacts to the moves after the
engine's, in `Movement` or `PostMovement`. Gameplay goes in tick systems,
which run with a fixed step of 1/60 s; the camera and other things that
only show go in frame systems.

### Input actions

Gameplay reads actions, never keys. The game names its actions with an
enum that implements `ActionSet`, binds keys and mouse buttons to them in an
`ActionMap` (up to four bindings an action), and adds it with
`App::add_actions`. Each tick, before the tick stages, the input is
latched into the resource `ActionState<A>`: `pressed`, `just_pressed`,
`just_released`, `value` and `axis2`. A key pressed and released between
two ticks is still seen. Read edges such as `just_pressed` in tick systems:
a frame system sees only the last tick's. The resource `Input` also holds
the pointer and the wheel.

### Sprites and the camera

An entity with a `Transform` and a `Sprite` is drawn. Its position is the
sprite's pivot, the middle of its bottom edge unless set otherwise: its
feet. Sprites are sorted by layer, higher in front, and within a layer by
the pivot's y, so that what is lower on screen is in front.
`Sprite::new(texture, size)` shows a whole texture, and
`Sprite::from_atlas` a frame of a cooked atlas.

Frames draw each sprite part of the way from where it was at the start of
the last tick to where the tick left it. The engine keeps the
`PrevTransform` that this needs. To move an entity at once, with nothing
drawn on the way, write both its `Transform` and its `PrevTransform`.

The world is drawn into an image of `Config::virtual_resolution` pixels,
scaled up into the window by the largest whole factor that fits, with bars
of `RenderFrame::clear`'s color around it. The camera is the
`RenderFrame`'s `camera`: a frame system in the `Camera` stage sets its
`position`, the world point at the centre of the view, and it keeps it from
one frame to the next.

### Tile layers

A `TileLayer` is a grid of cells showing tiles of a `TileSet`, a texture cut
into columns and rows; `Tile::animated` cycles through tiles with no work on
the CPU. Add the layer to the resource `TileLayers`, and push a
`TileLayerDraw` of its handle into `RenderFrame::tile_layers` each frame, in
the `Extract` stage. Tiles do not collide by themselves: the collision grid
is a `TileGrid`, set with `Collisions::set_grid`.

### Collision

Collision is kinematic: no forces, no rigid bodies. A `Collider` is a box or
a circle (`Shape`) on collision layers (`LayerMask`), offset from its
entity's `Transform`. `Collider::solid` stops movers; `Collider::trigger`
only notices them, and the engine sends `TriggerEnter` and `TriggerExit`
events when a collider starts and stops overlapping it.

The resource `Collisions` holds the colliders, rebuilt each tick. Its
`world()` answers queries (`point`, `aabb`, `circle`, `nearest`, and the
casts `segment`, `sweep_aabb` and `sweep_circle`) with collider indices,
which `Collisions::entity` turns into entities. Pathfinding comes with
roadmap task M9.5.

### Animation

A `Clip` is a list of frames, each a region of a texture shown for a time,
which loops or plays once. The resource `Clips` keeps them: `add`,
`add_named`, or `add_atlas` for every clip of a cooked atlas, named like
`sprites/hero/walk` from Aseprite's tags. An `Animator` plays a clip on its
entity's `Sprite`; `Animator::play` changes the clip, and does nothing if
it is the one playing, so a tick system can call it every tick. An entity
whose `Facing` is `Left` has its sprite mirrored. A clip that plays once
sends `AnimationFinished` when it ends.

Animators move in frames, by how long the simulation ran. Their time is
frame state: decide nothing in a tick system by it.

### Sound

The resource `Audio` (`nadir::audio`) is the game's handle on the mixer,
which runs on the audio output's thread. `audio.play(&assets, sound,
params)` plays a `Sound`, a cooked `.kpcm` or `.qoa`, on a `Bus` (music,
sound effects, the interface or ambience), and `play_at` plays one at a
position in the world, which pans and fades it by where it is from the
listener. `play_music` crossfades from one `Music` track to another.
`Config::volumes` sets each bus's volume as the game starts, in percent,
heard as its square: 50 is −12 dB.

Entities make sound through the engine's `nadir::sound`, each frame:

- an `AudioEmitter` plays its sound at its entity, following it, from
  when it is within 800 pixels of the listener; it stops a little past
  that, or when its entity or the emitter goes. `AudioEmitter::looped`
  makes one for an ambient loop;
- the listener is the first entity with an `AudioListener`, or else the
  camera;
- a `MusicArea` is a box around its entity whose track plays when the
  listener comes into it; outside every area, the music plays on.

A sound played once, as something happens, plays only if it has loaded:
load it before with `AssetServer::load_now`. `App::update` and
`App::offscreen` play nothing, so tests and recordings are silent;
`App::take_mixer` hands a test the mixer to render itself.

### Assets

The resource `AssetServer` loads files from `Config::assets`. A relative
path there is found from the folder the game runs in.

- `load::<Texture>(path)` returns a handle at once. A worker thread reads
  and decodes the file, and the main loop makes the texture, a couple of
  milliseconds' worth a frame. A sprite whose texture has not loaded yet is
  not drawn.
- `load_now` makes the asset before it returns, for what the game needs to
  set up, such as an `Atlas`. Before the window opens there is no GPU, so
  only an asset that needs none, an atlas but not a texture, can be made
  then.
- The asset types so far are `Texture`, a QOI image, `Atlas`, the cook's
  `atlas/<group>.kbj`, `nadir::render::Font`, the cook's
  `atlas/<font>.font.kbj`, which `Font::load_now_in` loads with its atlas
  and pages, `nadir::audio`'s `Sound`, a `.kpcm` or `.qoa`, and
  `Music`, a `.qoa`, and `nadir::scene`'s `Level` and `PrefabAsset`,
  which `App::load_level` and the `PrefabLibrary` load. Asset paths are
  lowercase.
- Assets load in a `Scope`, `Global`, `Level` or `Transient`, and
  `unload_scope` frees those of one.
- Loads finish only in drawn frames, at no frame known in advance, so tick
  systems must not depend on whether an asset has loaded.

### Logs and errors

Log with `nadir::log::{error, warn, info, debug, trace}`. Records go to
standard error, and `App::run` also writes them to `nadir.log` in the
game's logs folder, keeping the last three runs' files beside it. On Linux
that folder is `$XDG_DATA_HOME/<game>` (`~/.local/share/<game>` by
default), on Windows `%LOCALAPPDATA%\<studio>\<game>`, and on macOS
`~/Library/Logs/<game>`. If the file cannot be opened, the game goes on
without it.

`App::run` returns a `nadir::Error` when the window cannot open, the
renderer cannot start, or a frame cannot be drawn. It logs the error and
shows it to the player first: on standard error on Linux, and in a message
box on Windows or an alert on macOS.

### Replays

`nadir::input::InputRecorder` records the game's `ActionState` tick by
tick, from a tick system, and `App::play_replay` plays a recording in place
of the live input. `App::world_hash` hashes the reflected components. After
the same ticks of the same replay, from the same start, it is the same
however the frames fell, as long as frame systems change no reflected
component and spawn or despawn nothing, and tick systems decide nothing by
what frames set, by whether an asset has loaded, or by the pointer or the
wheel.

## Cooking assets

The engine loads cooked assets. `nadir-cook` makes them from source files:

```bash
cargo install nadir-cook
nadir-cook assets cooked
```

`nadir-cook [--force] SOURCE OUT` cooks the files below `SOURCE` into
`OUT`:

- Each folder's PNG images, Aseprite exports and BMFont fonts go into an
  atlas named after the folder, its group: pages `atlas/<group>_<n>.qoi`
  and the index `atlas/<group>.kbj`, with each font's glyphs in
  `atlas/<font>.font.kbj`. `sprites/hero.png` is the sprite
  `sprites/hero` in the group `sprites`. An `atlas.jsonc` in a folder can
  name another group, and an image directly in `SOURCE` is an error.
- An Aseprite export is the JSON of
  `aseprite -b hero.aseprite --sheet hero.png --data hero.json --format json-array --list-tags`;
  its tags become the sprite's clips.
- A sidecar `<name>.sprite.jsonc` beside an image can cut it into frames,
  set its pivot, or make it a tile set: `{ "tiles": [16, 16] }`.
- Each JSONC file below `data/` becomes `data/<name>.kbj`; a prefab,
  `data/prefabs/<name>.jsonc`, is checked as one.
- Each LDtk 1.5.3 project, `.ldtk`, with its `.ldtkl` level files, becomes
  a level file of each level, `levels/<name>.klvl`, whose entities spawn
  the prefabs of their names.
- Each WAV below `audio/` becomes a sound: `audio/sfx/step.wav` the sound
  file `audio/sfx/step.kpcm`, and one below `audio/music/` a QOA track,
  `audio/music/field.qoa`; a sidecar `<name>.sound.jsonc` can set its
  format, its rate and whether it loops.
- Every file above goes into one pack too, `game.kpak`, each file of the
  same bytes stored once, compressed where that makes it smaller.

A job whose files did not change since the last cook into `OUT` is skipped,
unless `--force`. Point `Config::assets` at `OUT`. A build without `dev`
reads the pack there when there is one, and the loose files otherwise, so
a release of the game needs only `game.kpak`; a pack that does not open
fails every load, with its error. A build with `dev` reads the loose
files. `App::load_level`
loads a level file into the world, its prefabs with it. A cooked font is
`nadir::render::Font`: `Font::load_now_in` loads it with its atlas and
pages, and a system pushes texts into `RenderFrame::texts`, drawn in the
world (M7.1), or into `RenderFrame::ui` with the UI's sprites, drawn on the
window after it (M7.8). The engine has no asset type for the other data files yet.

## Features

- `dev`: for development builds.
  - The assets are the loose files of `Config::assets`, not its pack.
  - A texture that failed to load shows a placeholder, a magenta and black
    checker, rather than nothing.
  - The global allocator counts allocations. The resource `FrameAllocs`
    holds what the last frame allocated, and `nadir::thread_alloc_counts`
    and `nadir::process_alloc_counts` give the counts so far. With it on,
    no other crate in the program may declare a `#[global_allocator]`.
  - `App::run` reloads what changes while it runs: the loaded files, such
    as those `cargo xtask cook --watch` cooks again, and the engine's
    shaders. A texture draws its new texels behind its handle; an atlas's
    clips are replaced by name; a prefab is replaced in the
    `PrefabLibrary`; the current level is made again, behind the handles
    of its assets, tile layers and clips, its entities spawned anew; and a
    shader rebuilds its program. Whatever fails to reload is logged, and
    what it would replace stays. What the game copied out of an asset
    itself, such as an atlas's regions, does not change.
    `App::offscreen` never reloads.
  - The debug UI shows the windows of `App::add_debug_window` and the
    engine's: the stats overlay on F1, the inspector on F2, the profiler
    on F3, the debug drawing on F4 and the console on the backtick, with
    F5 to F7 for the game's time. nadir-core's
    `profile` feature is on, so each system and the engine's steps record
    their scopes, which the profiler keeps for the last 300 frames, with
    the GPU's time over each frame's drawing, which the renderer times in
    `App::run`; its "Write trace" button writes them, from `App::run`, to
    `trace-<time>.json` beside the log file.

Turn it on from your game's own feature, so that a release build leaves it
out:

```toml
[dependencies]
nadir_engine = "0.4"

[features]
dev = ["nadir_engine/dev"]
```

```bash
cargo run --features dev
```

## Platforms

- **Linux:** everything above works, under X11, or under Wayland through
  XWayland. Drawing needs OpenGL 3.3, through EGL, and sound ALSA's
  `libasound.so.2`, whose `default` device a sound server, PipeWire or
  PulseAudio, usually provides.
- **Windows and macOS:** a game builds, and the engine's tests pass, but its
  window stays empty: there is no OpenGL context there yet (roadmap tasks
  M2.6 and M2.7). `App::run` logs the error and runs the game without
  drawing, and so without reloading; `App::offscreen` returns the error.
  Nor is there audio output yet (M6.4 and M6.5): `App::run` logs that, and
  the game is silent.
- Call `App::run` on the main thread: macOS opens windows only there.

## The sandbox

The repository's sandbox game is the full example, and the one each
feature is built for. Its
[`main.rs`](https://github.com/jabbott-iii/Nadir/blob/main/games/sandbox/src/main.rs)
has a tile map with animated water and flowers, a person cooked from an
Aseprite export who walks and collides, a camera that follows them, stress
scenes of thousands of sprites and colliders, recorded and replayed input,
and frames drawn with no window for the demo page. To run it:

```bash
git clone https://github.com/jabbott-iii/Nadir.git
cd Nadir
cargo xtask cook
cargo run -p sandbox
```

## Documentation

- API reference: https://docs.rs/nadir_engine
- Design: https://github.com/jabbott-iii/Nadir/blob/main/docs/01-architecture.md
  (the main loop, stages and threads) and
  https://github.com/jabbott-iii/Nadir/blob/main/docs/subsystems/ecs.md
- The engine: https://github.com/jabbott-iii/Nadir

## License

Apache License 2.0: see [LICENSE](LICENSE) and [NOTICE](NOTICE).