C: what Basthon adds
This page describes the headers specific to Basthon and the differences with a C compiler on a computer. To discover the kernel through examples, see C in Basthon.
| Header | To… |
|---|---|
<image.h> | show an image file (PPM, PNG…) |
<canvas.h> | draw in a drawing area |
<basthon.h> | download a file of the filesystem |
The errors of these functions (missing file, NULL pointer, invalid size…) stop the program with a Runtime error: <function>: … message.
<image.h>
void image_show(const char *filename);Shows the image file filename: PNG, or Netpbm image (PBM, PGM or PPM, text formats P1 to P3 or binary ones P4 to P6, on 8 or 16 bits). The file is read in the filesystem: written by the program (fopen, fprintf, fwrite…) or opened in Basthon.
<canvas.h>
A drawing area (a 2D canvas of the browser), with functions modelled on the Canvas 2D API. The origin is at the top left, the y axis points down, angles are in radians.
The drawing is shown, as an image, at the end of the run if it changed since it was last shown, and at each call of canvas_show. It is not animated nor interactive (keyboard, mouse) yet.
Drawing area
| Function | Role |
|---|---|
void canvas_create(int w, int h) | creates a white drawing area of w × h pixels (1 to 4096), where the next drawings go |
void canvas_show(void) | shows the drawing in its current state |
void canvas_clear(void) | clears the drawing (in white) |
int canvas_width(void) | width of the drawing |
int canvas_height(void) | height of the drawing |
void canvas_download(const char *name) | downloads the drawing: PNG, or JPEG if name ends with .jpg or .jpeg |
Colours and lines
A colour is a CSS colour ("red", "#ff8000", "hsl(200, 80%, 50%)", "rgba(0, 0, 255, 0.5)"…) or its red, green, blue components (from 0 to 255).
| Function | Role |
|---|---|
void canvas_fill_color(const char *colour) | fill colour |
void canvas_fill_rgb(int r, int g, int b) | fill colour, in RGB |
void canvas_stroke_color(const char *colour) | line colour |
void canvas_stroke_rgb(int r, int g, int b) | line colour, in RGB |
void canvas_line_width(double w) | line width (in pixels) |
Shapes
| Function | Role |
|---|---|
void canvas_fill_rect(double x, double y, double w, double h) | filled rectangle |
void canvas_stroke_rect(double x, double y, double w, double h) | outline of a rectangle |
void canvas_clear_rect(double x, double y, double w, double h) | clears a rectangle (in white) |
void canvas_line(double x1, double y1, double x2, double y2) | segment |
void canvas_fill_circle(double x, double y, double r) | disk of centre (x, y) and radius r |
void canvas_stroke_circle(double x, double y, double r) | circle |
Paths
A path is built point by point, then filled or stroked.
| Function | Role |
|---|---|
void canvas_begin_path(void) | starts a new path |
void canvas_move_to(double x, double y) | moves the pen without drawing |
void canvas_line_to(double x, double y) | adds a segment up to the point (x, y) |
void canvas_arc(double x, double y, double r, double start, double end) | adds a circular arc, from the angle start to the angle end |
void canvas_close_path(void) | closes the path |
void canvas_fill(void) | fills the path |
void canvas_stroke(void) | strokes the path |
Text
| Function | Role |
|---|---|
void canvas_font(const char *font) | CSS font, for instance "bold 20px sans-serif" |
void canvas_fill_text(const char *text, double x, double y) | writes text from the point (x, y) (baseline) |
Pixels
| Function | Role |
|---|---|
void canvas_set_pixel(int x, int y, int r, int g, int b) | colours a pixel (fast: use it to draw pixel by pixel) |
void canvas_put_rgb(const unsigned char *pixels, int x, int y, int w, int h) | draws at (x, y) an image of w × h pixels of 3 bytes (red, green, blue), row by row |
A structure of three unsigned char takes exactly 3 bytes: an array pixel image[H][W] is drawn with canvas_put_rgb(&image[0][0].r, 0, 0, W, H).
<basthon.h>
void basthon_download(const char *filename);Downloads the file filename of the filesystem (written by the program or opened in Basthon).
Differences with a C compiler
Basthon's C is compiled to WebAssembly by xcc, with its own standard library:
- no need for
main, and declarations stay available from one cell to the next; the last expression without;is shown with its type; longis 32 bits (likeint): uselong longfor 64 bits;- array overflows (of known size),
NULLdereferences and the formats ofprintfandscanfare checked; ascanfwith an argument that is not a pointer (missing&) prevents the run; scanf,getchar… read the user's inputs, likeinputin Python;<time.h>does not provideclock;- a function used before it is defined in a later cell is not found.