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, collision, assets and reflection as nadir::ecs,
nadir::input, nadir::render, nadir::collision, nadir::assets and
nadir::scene, so a game needs nothing else.
Status
Version 0.2: 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, 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;
- prefabs, which extend one another, spawned through the
PrefabLibrary, with the engine's sprites, colliders and animators reflected for them; - assets loaded on worker threads, and an offline cook that packs images, Aseprite exports and bitmap fonts into atlases, and makes level files of LDtk projects;
- 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);
- audio (M6), text (M7.1), the debug tools (M7), gamepads (M8);
- loading levels, which the cook makes of LDtk projects (M5.6), pack files (M5.7), hot reload (M5.8);
- 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, so it needs no development packages to build. - 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.2"
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 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 and the
window, 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), and the folder the assets
load from (assets). 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::extract_sprites |
Draws each Sprite into the RenderFrame, between the last two ticks |
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.
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, andAtlas, the cook'satlas/<group>.kbj. 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.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.
A job whose files did not change since the last cook into OUT is skipped,
unless --force. Point Config::assets at OUT. Prefabs and level files
are nadir::scene's PrefabAsset and Level, and loading a level into
the world comes with roadmap task M5.6. The engine has no asset type for
the cooked fonts and other data files yet: text comes with roadmap task
M7.1.
Features
dev: for development builds.- 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].
Turn it on from your game's own feature, so that a release build leaves it out:
[]
= "0.2"
[]
= ["nadir_engine/dev"]
Platforms
- Linux: everything above works, under X11, or under Wayland through XWayland. Drawing needs OpenGL 3.3, through EGL.
- 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;App::offscreenreturns the error. - 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