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, 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_levelloads 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. 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,libEGLand OpenGL at run time, and ALSA'slibasoundfor sound, so it needs no development packages to build. Withoutlibasoundthe 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).
Usage
[]
= "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:
use *;
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) from these files:
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
use Error;
use Scope;
use *;
/// What the player does.
/// The hero's clips: the tags of their Aseprite export.
/// The ground's tile layer.
;
/// 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;
/// 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.
/// 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.
/// Draws the ground, behind every sprite.
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:
use Duration;
use *;
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:
- hands the window's events to
Input; - 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;
- runs the frame stages once, which fill the
RenderFrame; - 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 withapp.world_mut().register::<MyType>()before use. A reflected one, defined withnadir::scene::reflect_struct!,reflect_enum!orreflect_newtype!, is registered withApp::register_componentinstead: the world hash covers the reflected components. - A system is a plain function,
fn(&World, &Tick)for a stage of each tick orfn(&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>()andworld.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>(), thenworld.resource_mut::<Events<T>>().send(event). Tick systems readtick_events(), the tick's own; frame systems readframe_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
AudioEmitterplays 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::loopedmakes one for an ambient loop; - the listener is the first entity with an
AudioListener, or else the camera; - a
MusicAreais 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_nowmakes the asset before it returns, for what the game needs to set up, such as anAtlas. 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'satlas/<group>.kbj,nadir::render::Font, the cook'satlas/<font>.font.kbj, whichFont::load_now_inloads with its atlas and pages,nadir::audio'sSound, a.kpcmor.qoa, andMusic, a.qoa, andnadir::scene'sLevelandPrefabAsset, whichApp::load_leveland thePrefabLibraryload. Asset paths are lowercase. - Assets load in a
Scope,Global,LevelorTransient, andunload_scopefrees 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:
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>.qoiand the indexatlas/<group>.kbj, with each font's glyphs inatlas/<font>.font.kbj.sprites/hero.pngis the spritesprites/heroin the groupsprites. Anatlas.jsoncin a folder can name another group, and an image directly inSOURCEis 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.jsoncbeside an image can cut it into frames, set its pivot, or make it a tile set:{ "tiles": [16, 16] }. - Each JSONC file below
data/becomesdata/<name>.kbj; a prefab,data/prefabs/<name>.jsonc, is checked as one. - Each LDtk 1.5.3 project,
.ldtk, with its.ldtkllevel 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.wavthe sound fileaudio/sfx/step.kpcm, and one belowaudio/music/a QOA track,audio/music/field.qoa; a sidecar<name>.sound.jsonccan 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
FrameAllocsholds what the last frame allocated, andnadir::thread_alloc_countsandnadir::process_alloc_countsgive the counts so far. With it on, no other crate in the program may declare a#[global_allocator]. App::runreloads what changes while it runs: the loaded files, such as thosecargo xtask cook --watchcooks 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 thePrefabLibrary; 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::offscreennever reloads.- The debug UI shows the windows of
App::add_debug_windowand 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'sprofilefeature 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 inApp::run; its "Write trace" button writes them, fromApp::run, totrace-<time>.jsonbeside the log file.
- The assets are the loose files of
Turn it on from your game's own feature, so that a release build leaves it out:
[]
= "0.4"
[]
= ["nadir_engine/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, whosedefaultdevice 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::runlogs the error and runs the game without drawing, and so without reloading;App::offscreenreturns the error. Nor is there audio output yet (M6.4 and M6.5):App::runlogs that, and the game is silent. - Call
App::runon 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
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:
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