Expand description
Easy, efficient drawing: draw into a Canvas in your program’s memory,
then Canvas::present sends only what changed to the GPU, in as few
calls as it can.
use wackasm_opencomputers::{draw::{Canvas, Rgb, Shapes}, prelude::*};
let mut canvas = Canvas::new(Gpu::first().ok_or(Error::NoSuchComponent)?)?;
canvas.clear(Rgb::BLACK);
canvas.text(1, 0, "hello", Rgb::WHITE, Rgb::BLACK);
// Half-block pixels: two square-ish pixels per character cell.
let mut px = canvas.pixels();
px.fill_circle(20, 12, 6, Rgb::hsv(0.12, 1.0, 1.0));
px.line(0, 2, 40, 20, Rgb::new(0x33, 0xCC, 0xFF));
// Braille dots: 2x4 per cell, one colour per cell.
let mut dots = canvas.dots();
dots.circle(60, 30, 10, Rgb::WHITE);
let calls = canvas.present()?; // later presents only send changes§Coordinates
Everything here counts from 0 at the top left (OpenComputers’ own GPU
calls count from 1): cells are 0..width() x 0..height(), half-block
pixels 0..width() x 0..height() * 2, and braille dots
0..width() * 2 x 0..height() * 4. Drawing outside is clipped.
§How drawing modes mix
A cell holds one character with a foreground and a background colour.
Half-block pixels turn a cell into ▀ (top pixel in front, bottom pixel
behind); braille dots turn it into a braille character in one colour,
over the cell’s background. Writing one kind of graphics over another
replaces it, keeping the cell’s background colour.
§What present sends
It compares the canvas with what it last sent, and for each row writes
the changed cells as runs: one gpu.set per run of cells sharing colours
(a space matches any foreground), plus a colour change only when the
colour differs from the last one set. So redrawing a moving sprite costs a
few calls, not a screen’s worth. With Canvas::double_buffered, changes
go to a GPU buffer first and reach the screen in one bitblt, which
avoids tearing. If something else draws on the screen, call
Canvas::invalidate so the next present redraws everything.
Structs§
- Canvas
- A framebuffer for one GPU’s screen; see the module docs.
- Dots
- The canvas as braille dots, 2x4 per cell; see
Canvas::dots. A cell’s dots share one colour: the last one drawn there. - Pixels
- The canvas as half-block pixels, two per cell (top and bottom); see
Canvas::pixels. - Rgb
- A 24-bit colour. The GPU shows the nearest colour its tier supports.
Traits§
- Shapes
- Lines, rectangles and circles, for anything that can set a point.