Draw a line and read a key in Rust
The program of "Draw a line and read a key", in Rust: one line, whose top end Left and Right swing; Esc quits.
The program
line.rs:
#![no_std]
#![no_main]
use terse::*;
entry!(main);
fn main() {
let mut buf = [0u8; 64];
let mut x: i16 = 320;
loop {
while let Some(e) = next_event() {
if e.kind != EV_KEY_DOWN {
continue;
}
match e.key() {
KEY_LEFT => x -= 10,
KEY_RIGHT => x += 10,
KEY_ESCAPE => exit(0),
_ => {}
}
}
let mut list = VecList::new(&mut buf);
list.move_to(320 * VEC_UNIT, 400 * VEC_UNIT);
list.line_to(x * VEC_UNIT, 80 * VEC_UNIT);
list.submit();
wait_frame();
}
}
Build it with terse build line.rs and run it with terse-host line.terse.
What is different from C
- The list is built by a
VecList, which writes the commands into a buffer the program owns andsubmitends and hands over. The buffer is a local array, so it is on the stack, which is in RAM, as a display list must be. - Coordinates are
i16sixteenths of a screen unit, andVEC_UNITis 16:320 * VEC_UNITis 320 units. - The keys are a
matchone.key(), with the same names as in C.exitnever returns, so it can stand as an arm of thematch.
Test it without a window
With the keys.txt of the last page (Left at frames 5 and 6),
terse run line.terse --frames 10 --events keys.txt \
--dump --dump-vectors
prints the same list as the C program, byte for byte:
-- vectors: 11 list bytes -- line 5120 6400 4800 1280 w 16 c ffffffff
What you have now
A Rust program that draws and reacts to keys. It is examples/program/rust/line.rs in the Tersenet sources. Next, a small game: Make a small game, in C, then in Rust.
VecList and the other drawing calls of the runtime are in Rust programs; every command of the display in The 2D vector display.