nadir-platform
The OS layer of the Nadir engine: a window and its events, gamepads, OpenGL contexts, audio output, the user's folders, an error dialog and a precise sleep, with one safe API on Linux, Windows and macOS.
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. It wraps the raw declarations of
nadir-sys. Games use it through
nadir_engine, which opens the
window and runs the loop with it, and re-exports its Event as
nadir::Event; depend on it directly to use it on its own, for a window and
its events without the rest of the engine.
Status
Version 0.4: the API will change before 1.0.
Built today:
- A window (
Platform): borderless fullscreen (native fullscreen on macOS), the title, hiding the pointer, size limits, and the scale factor. - Its events, polled without waiting: close requests, resizes, scale factors, focus, physical keys with repeats, typed text, mouse motion, buttons and the wheel. When the window loses the focus, every key and button still down gets a release right after.
- On Linux, gamepads (
poll_gamepads,gamepad_state), read from the kernel's event devices: pads plugged in and unplugged while the game runs, their buttons by their place on the pad, their sticks from -1 to 1 and their triggers from 0 to 1, with no dead zone. - Typed text (
Event::Text), a character at a time after the key press that typed it, composed characters included: on Linux from an X input method, on Windows fromWM_CHAR, on macOS from a key press'scharacters, with no input method there yet. - Frames while Windows drags or resizes the window, or macOS resizes it
(
set_modal_frame_callback). - On Linux, an OpenGL 3.3 core context for the window, and one that draws
offscreen with no display (
create_headless_gl). - On Linux, audio output (
open_audio): an audio thread that feeds ALSA's default device from a callback, recovers from underruns, and opens the device again when it is lost. user_dirs, the folders for settings, saves and logs;show_error_dialog;precise_sleep, for a frame limiter.- With the
devfeature, an allocation counter.
Not yet: OpenGL on Windows and macOS (roadmap tasks M2.6 and M2.7), audio output on Windows and macOS (M6.4 and M6.5), gamepads on Windows and macOS (M8.2 and M8.3), Metal (M10.1), showing a text being composed, an input method on macOS, noticing that the window was minimized, holding the pointer inside the window, and native Wayland.
Usage
[]
= "0.4"
= "0.4"
nadir-core has the UVec2 of the window's size, the Key of a key
event, the GamepadButton of a pad's and the log. Open a window and run
until it is closed, or Escape or a pad's Start is pressed:
use ;
use info;
use ;
use ;
Find the folders where a game keeps what it writes. Nothing is created: make a folder before writing into it.
use ;
Features
dev: the allocation counter. The global allocator of every binary that links this crate counts each thread's allocations and the process's, andthread_alloc_countsandprocess_alloc_countsread them. No other crate in the binary may declare a global allocator. Turn it on only from a[features]table or a[dev-dependencies]entry, so that no release build carries it.nadir_engine's owndevfeature turns it on.headless-testis for the workspace's own tests, which need an EGL driver.
Platforms
- Linux (X11): all of the above works, and was checked by hand on
XWayland. Xlib (
libX11.so.6) and EGL (libEGL.so.1) are loaded while the program runs, not linked, so a machine without them gets an error that says which library or function is missing. A Wayland session runs the window through XWayland; without XWayland, the error says how to turn it on. The window draws with OpenGL 3.3 core through EGL, andcreate_headless_glneeds the surfaceless platform (EGL_MESA_platform_surfaceless), which Mesa and NVIDIA have. The scale factor is read once, fromXft.dpi, when the window opens. - Windows 10 and later: a Win32 window, aware of each monitor's DPI,
with scale-factor changes, borderless fullscreen and an error message
box. It builds and passes its tests in CI, but still awaits its checks by
hand (roadmap task M1.5). It has no OpenGL context yet (M2.6): its GPU
surface has no functions, and swapping fails with
PlatformError::Unsupported, as doescreate_headless_gl. - macOS 12 and later: an AppKit window, opened on the main thread only, with Retina scale factors, native fullscreen and an error alert. It builds and passes its tests in CI, but still awaits its checks by hand (M1.6). It has no OpenGL context yet (M2.7), as on Windows.
On any other system, opening a window, user_dirs and create_headless_gl
return PlatformError::Unsupported.
Documentation
- API reference: https://docs.rs/nadir-platform
- Design: https://github.com/jabbott-iii/Nadir/blob/main/docs/subsystems/sys-and-platform.md
- Each system's details: https://github.com/jabbott-iii/Nadir/blob/main/docs/03-platforms.md
- The engine: https://github.com/jabbott-iii/Nadir