← screen · API reference · version 0.2.2
2 file(s), 189 lines. This is the source the published version ships, and it is loft — the library is written in the same language you call it from.
Public items
A worked example is a call site the author chose as the thing to read first. Call sites are every line naming the item, found mechanically over the source with comments excluded — so an item with no worked example is not an item nobody uses.
| Item | Defined | Worked example | Call sites |
|---|---|---|---|
open fn | 51 | — | 15 25 33 43 48 |
show fn | 60 | — | 18 35 45 |
poll fn | 66 | — | 25 |
has_keys fn | 71 | — | 24 |
program_keys fn | 80 | — | none in this package |
keys fn | 87 | — | 24 |
shown fn | 95 | — | 19 20 21 22 23 27 … (9 total) |
width fn | 100 | — | 17 21 51 53 60 61 … (7 total) |
height fn | 104 | — | 17 21 51 53 60 61 … (7 total) |
close fn | 109 | — | 26 38 47 |
has_pointer fn | 115 | — | none in this package |
pointer fn | 120 | — | none in this package |
buttons fn | 125 | — | none in this package |
wheel fn | 130 | — | none in this package |
typed fn | 136 | — | none in this package |
src/screen.loft
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// screen — show a small frame on the machine's screen, scaled up by a whole number, and read
// the keys of the window it is shown in. One API on every machine; the C part (c/screen.c)
// has one section per platform:
//
// - a desktop with X11: a window of `width * scale` by `height * scale`;
// - a Tanmatsu: the board's display, the frame rotated into its portrait panel and centred;
// - elsewhere (a browser, Windows, macOS): none yet, so `open` answers false.
//
// A frame is RGB565 values, one per pixel, row by row — what `tiles::pixels` answers — so a
// game draws with `tiles` and shows the result here. A screen without keys of its own (a
// Tanmatsu, whose keys `input::input_tick_device` reads) answers `has_keys()` false, and a
// game then reads its keys there: one program, asking what the machine has.
fn sc_open(width: integer, height: integer, scale: integer, mode: integer, title: text) -> integer;
#c "sc_open" "int64_t(int64_t, int64_t, int64_t, int64_t, const char*)"
fn sc_close();
#c "sc_close" "void(void)"
fn sc_show(pixels: const vector<i32>, width: integer, height: integer);
#c "sc_show" "void(const int32_t*, int64_t, int64_t, int64_t)"
fn sc_poll(wait_ms: integer) -> integer;
#c "sc_poll" "int64_t(int64_t)"
fn sc_has_keys() -> integer;
#c "sc_has_keys" "int64_t(void)"
fn sc_key(code: integer) -> integer;
#c "sc_key" "int64_t(int64_t)"
fn sc_shown(x: integer, y: integer) -> integer;
#c "sc_shown" "int64_t(int64_t, int64_t)"
fn sc_width() -> integer;
#c "sc_width" "int64_t(void)"
fn sc_height() -> integer;
#c "sc_height" "int64_t(void)"
fn sc_pointer(which: integer) -> integer;
#c "sc_pointer" "int64_t(int64_t)"
fn sc_text() -> text;
#c "sc_text" "const char*(void)"
fn sc_text_taken();
#c "sc_text_taken" "void(void)"
fn sc_program_keys() -> integer;
#c "sc_program_keys" "int64_t(void)"
/// Open the screen for frames of `width` by `height` pixels, each shown as a `scale` by
/// `scale` block. On a desktop that is a window of that size titled `title`; on a device it is
/// the whole display, the frame centred. `fill` asks for the whole display where there is a
/// choice: in a page the canvas fills the window and follows it when it is resized (read
/// `width()` and `height()` each frame to draw at its size). `offscreen` keeps only the scaled
/// image in memory — for tests, and for a machine with no screen. False where this machine has
/// no screen (no display). Opening again closes the screen that was open.
pub fn open(width: integer, height: integer, scale: integer = 1, title: text = "loft",
offscreen: boolean = false, fill: boolean = false) -> boolean {
sc_open(width, height, scale, if offscreen { 1 } else if fill { 2 } else { 0 }, title) != 0
}
/// Show a frame: `width * height` RGB565 values, row by row, as `tiles::pixels` answers them.
/// It is shown at the scale `open` asked for, or the largest whole scale that fits: a frame of
/// the screen's own size (`width()` x `height()`, drawn at the screen's resolution) is shown as
/// it is. A frame with fewer values than that shows nothing.
pub fn show(pixels: const vector<i32>, width: integer, height: integer) {
sc_show(pixels, width, height);
}
/// Take what happened since the last call — keys, the window being closed — waiting up to
/// `wait_ms` milliseconds for something to happen. False once the window was closed.
pub fn poll(wait_ms: integer = 0) -> boolean {
sc_poll(wait_ms) != 0
}
/// Does this screen have keys of its own (a desktop window)? A device's keys are `input`'s.
pub fn has_keys() -> boolean {
sc_has_keys() != 0
}
/// True where the keys this screen reads are the program's, whichever of its windows is in
/// front (macOS: one event queue for the application). False where every window has its own
/// (X11): a second window in front then takes the keys, and this screen reads none. A game
/// that opens a second window — a GPU view beside its frame — asks this before it relies on
/// `keys()` while that window is in front.
pub fn program_keys() -> boolean {
sc_program_keys() != 0
}
/// Which of the window's keys are down, by graphics' key codes (`KEY_W` = 119, `KEY_UP` =
/// 128, ...): 256 flags, the form `input::input_tick_from_state` takes. All false where the
/// screen has no keys.
pub fn keys() -> vector<boolean> {
out: vector<boolean> = [];
for k in 0..256 { out += [sc_key(k) != 0]; }
out
}
/// The RGB565 value shown at screen pixel (x, y), or -1 outside the screen — what a test
/// reads back.
pub fn shown(x: integer, y: integer) -> integer {
sc_shown(x, y)
}
/// The size of the screen in pixels: the window's, or the device's display turned landscape.
pub fn width() -> integer {
sc_width()
}
pub fn height() -> integer {
sc_height()
}
/// Close the screen.
pub fn close() {
sc_close();
}
/// Whether this screen has a pointer: a desktop window's mouse, a page's mouse or touch. The
/// Tanmatsu has none — a program for it works from the keyboard alone.
pub fn has_pointer() -> boolean {
sc_pointer(4) != 0
}
/// Where the pointer is, in the screen's pixels (`width()` x `height()`), as `poll` last saw it.
pub fn pointer() -> (integer, integer) {
(sc_pointer(0), sc_pointer(1))
}
/// The pointer's buttons held down: 1 the left, 2 the right, 4 the middle, added together.
pub fn buttons() -> integer {
sc_pointer(2)
}
/// The wheel's notches since this was last asked (up is positive).
pub fn wheel() -> integer {
sc_pointer(3)
}
/// The text typed since this was last asked — what the keys wrote, with the keyboard's layout
/// and shift applied, as opposed to which keys are down.
pub fn typed() -> text {
t = "{sc_text()}";
sc_text_taken();
t
}
tests/scale.loft
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// The scaled image, read back off-screen, so it runs with no display. The frame is 2x2,
// RGB565: red 63488 (31 << 11), green 2016 (63 << 5), blue 31, white 65535, row by row
// [R G / B W]. At scale 3 every frame pixel is a 3x3 block of a 6x6 screen.
use screen;
const R = 63488;
const G = 2016;
const B = 31;
const W = 65535;
fn main() {
assert(screen::open(2, 2, 3, "test", true), "off-screen opens anywhere");
assert(screen::width() == 6 && screen::height() == 6, "6x6");
frame: vector<i32> = [R as i32, G as i32, B as i32, W as i32];
screen::show(frame, 2, 2);
assert(screen::shown(0, 0) == R && screen::shown(2, 2) == R, "the red block's corners");
assert(screen::shown(3, 0) == G && screen::shown(5, 2) == G, "green to its right");
assert(screen::shown(0, 3) == B && screen::shown(2, 5) == B, "blue below");
assert(screen::shown(5, 5) == W, "white at the far corner");
assert(screen::shown(6, 0) == -1 && screen::shown(-1, 0) == -1, "outside reads -1");
assert(!screen::has_keys(), "off-screen has no keys");
assert(screen::poll(0), "still open");
screen::close();
assert(screen::shown(0, 0) == -1, "closed: nothing shown");
}
// A frame smaller than the screen is centred, the rest black: a 1x1 frame at scale 2 shown on
// a screen opened for 2x2 frames (4x4) covers (1, 1) .. (2, 2).
fn test_a_smaller_frame_is_centred() {
assert(screen::open(2, 2, 2, "test", true), "opens");
one: vector<i32> = [W as i32];
screen::show(one, 1, 1);
assert(screen::shown(0, 0) == 0 && screen::shown(3, 3) == 0, "black round it");
assert(screen::shown(1, 1) == W && screen::shown(2, 2) == W, "the frame in the middle");
screen::close();
}
// A frame with fewer values than its size shows nothing, and a closed screen shows nothing.
fn test_a_short_frame_shows_nothing() {
assert(screen::open(2, 2, 1, "test", true), "opens");
short: vector<i32> = [R as i32];
screen::show(short, 2, 2);
assert(screen::shown(0, 0) == 0, "nothing drawn");
screen::close();
assert(!screen::open(0, 2, 1, "test", true), "an empty frame size is refused");
}