firefly

    A port of the SDK for Firefly Zero, a modern handheld game console using WebAssembly

    game
    firefly-zero
    Download zip
    Author
    Version
    0.2.0
    License
    MIT
    Last updated
    last year
    Downloads
    52

    #firefly-moon

    REUSE status ci

    An unofficial MoonBit SDK for making Firefly Zero games.

    The SDK design is heavily inspired by the firefly-go and firefly-rust SDKs. The type and function naming in this SDK is trying to stay as similar as possible to the naming in the other SDKs.

    #Prerequisites

    • moonc v0.6.26 (or later)
    • --target wasm MoonBit build target, as Firefly Zero doesn't support wasm-gc

    #Installation

    moon add applejag/firefly

    Then in your game moon.pkg.json specify this:

    { "is-main": true, "import": [ "applejag/firefly" ], "link": { "wasm": { "exports": ["boot", "update", "render", "before_exit"], "export-memory-name": "memory" } } }

    Then create a main.mbt file like so:

    ///|
    /// `main` is called immediately on startup.
    ///
    /// MoonBit requires the presence of a `main` function, but Firely Zero
    /// recommends that you use `boot` for any initialization logic.
    fn main {
    // keep this function empty
    }

    ///|
    /// `boot` is only called once, after all the memory is initialized and all
    /// runtime functions are available but before any other callback is called.
    ///
    /// This is the best place to load fonts, sprites, and other assets,
    /// initialize the default state, read configurations, etc.
    pub fn boot() -> Unit {
    @firefly.log_debug("entering boot") // placeholder for demonstration purposes
    }

    ///|
    /// `before_exit` is called before the app is closed.
    pub fn before_exit() -> Unit {
    @firefly.log_debug("entering before_exit") // placeholder for demonstration purposes
    }

    ///|
    /// `update` is called ~60 times per second.
    ///
    /// It is guaranteed to be never called more often, and it won’t be called less
    /// often if the game doesn’t consume too much resources.
    /// This is the best place to update the state of objects, position of NPCs,
    /// read and handle user input, etc.
    pub fn update() -> Unit {
    @firefly.log_debug("entering update") // placeholder for demonstration purposes
    }

    ///|
    /// `render` is called before updating the image on the screen.
    ///
    /// It might be called less often than `update` if the device sees that the game
    /// is slow and needs more resources.
    /// This is the best place to call all drawing functions.
    pub fn render() -> Unit {
    @firefly.log_debug("entering render") // placeholder for demonstration purposes
    }

    Firefly's CLI (firefly_cli) can't automatically build the project for you, so you'll need to build it manually each time with moon CLI and tell Firefly to just use the binary from target/... directory.

    To do so, configure the _bin file in your firefly.toml like so:

    author_id = "yourname" app_id = "your-app-id" author_name = "Your Name" app_name = "Your App Name" [files] _bin = { path = "./target/wasm/release/build/<moonbit app name>.wasm", copy = true }

    Building and running can then be done by running:

    moon build --target wasm firefly_cli build firefly-emulator --id yourname.your-app-id

    To reduce the clutter I suggest setting these type aliases:

    typealias @firefly.Angle
    typealias @firefly.Color
    typealias @firefly.LineStyle
    typealias @firefly.Point
    typealias @firefly.Size
    typealias @firefly.Style

    fn render() -> Unit {
    // Instead of this:
    @firefly.draw_rect(
    @firefly.Point::new(102, 78),
    @firefly.Size::new(29, 9),
    @firefly.Style::new_solid(Red),
    )
    // You can write this:
    @firefly.draw_rect(
    Point::new(102, 78),
    Size::new(29, 9),
    Style::new_solid(Red),
    )
    }

    Recommended to only set type aliases on your most commonly-used types. Typing the full name of @firefly.Image is still fine and does not require a type alias as it is not as commonly used as some of these other types.

    #Examples

    You need these to run the examples:

    Then run one of the examples by running either of these commands:

    task example/input:run task example/shapes:run

    #License

    MIT License. You can do whatever you want with the SDK, modify it, embed into any apps and games. Have fun!

    Angle

    pub type Angle Float

    An angle between two vectors, internally represented as radians.
    impl Add for Angle
    impl Compare for Angle
    impl Default for Angle
    impl Div for Angle
    impl Eq for Angle
    impl Hash for Angle
    impl Mul for Angle
    impl Neg for Angle
    impl Show for Angle
    impl Sub for Angle

    Angle::from_deg

    fn Angle::from_deg(self : Float) -> Angle

    Define an Angle in degrees (0-360°).

    Angle::from_rad

    fn Angle::from_rad(self : Float) -> Angle

    Define an Angle in radians (0-2π).

    Angle::inner

    fn Angle::inner(self : Angle) -> Float
    Convert newtype to its underlying type, automatically derived.

    Angle::normalize

    fn Angle::normalize(self : Angle) -> Angle

    Ensure the angle is within 0-2π radians, i.e within 0-360°.

    Angle::to_deg

    fn Angle::to_deg(self : Angle) -> Float

    Get the angle value in degrees (0-360°).

    Angle::to_rad

    fn Angle::to_rad(self : Angle) -> Float

    Get the angle value in radians (0-2π).

    Buttons

    pub(all) struct Buttons {
    south : Bool
    east : Bool
    west : Bool
    north : Bool
    menu : Bool
    }

    State of the buttons on the game controller.
    impl Compare for Buttons
    impl Default for Buttons
    impl Eq for Buttons
    impl Hash for Buttons
    impl Show for Buttons

    Buttons::any_pressed

    fn Buttons::any_pressed(self : Buttons) -> Bool

    Check if any button is currently pressed.

    Buttons::held

    fn Buttons::held(self : Buttons, old : Buttons) -> Buttons

    Given the old state, get buttons that were both pressed previously and are still pressed now.

    Buttons::just_pressed

    fn Buttons::just_pressed(self : Buttons, old : Buttons) -> Buttons

    Given the old state, get buttons that were not pressed previously but are pressed now.

    Buttons::just_released

    fn Buttons::just_released(self : Buttons, old : Buttons) -> Buttons

    Given the old state, get buttons that were pressed previously but are no longer pressed now.

    Buttons::read

    fn Buttons::read(peer? : Peer) -> Buttons

    Get the currently pressed buttons.

    The peer can be a player ID, or 0xFF to read the combined inputs from all active players in the multiplayer session.

    Canvas

    pub type Canvas FixedArray[Byte]

    Canvas is an Image that can be drawn upon by changing the rendering target to the canvas using set_canvas instead of to the screen.

    Compared to an Image, the Canvas is always 4 bits-per-pixel (BPP).

    Can be created using Canvas::new or Canvas::of.
    impl Default for Canvas

    Canvas::as_file

    fn Canvas::as_file(self : Canvas) -> File

    Convert the Canvas to a File.

    Canvas::as_image

    fn Canvas::as_image(self : Canvas) -> Image

    Convert the Canvas to an Image.

    Canvas::draw

    fn Canvas::draw(self : Canvas, point : Point) -> Unit

    Render the canvas.

    The point defines the upper-left corner.

    Canvas::inner

    fn Canvas::inner(self : Canvas) -> FixedArray[Byte]
    Convert newtype to its underlying type, automatically derived.

    Canvas::new

    fn Canvas::new(size : Size) -> Canvas

    Create a new blank canvas.

    Canvas::of

    fn Canvas::of(arr : FixedArray[Byte]) -> Canvas

    Creates an Canvas from a fixed-sized byte array.

    Canvas::set_as_current

    fn Canvas::set_as_current(self : Canvas) -> Unit

    Set this canvas as the current rendering target for draw calls instead of the screen.

    Color

    pub(all) enum Color {
    None
    Black
    Purple
    Red
    Orange
    Yellow
    LightGreen
    Green
    DarkGreen
    DarkBlue
    Blue
    LightBlue
    Cyan
    White
    LightGray
    Gray
    DarkGray
    }

    impl Compare for Color
    impl Default for Color
    impl Eq for Color
    impl Hash for Color
    impl Show for Color

    Color::from_byte

    fn Color::from_byte(byte : Byte) -> Color

    Color::from_int

    fn Color::from_int(int : Int) -> Color

    Color::to_byte

    fn Color::to_byte(color : Color) -> Byte

    Color::to_int

    fn Color::to_int(color : Color) -> Int

    DPad

    pub(all) struct DPad {
    left : Bool
    right : Bool
    up : Bool
    down : Bool
    }

    DPad (directional-pad) representation of the Pad, where instead of an x and y vector it is instead 4 booleans stating if the user is pressing left, right, up, or down on the pad.

    Useful for simple games and ports. The middle of the pad is a "dead zone", which will not activate any direction.

    Invariant: it is not possible for opposite directions (left and right, or down and up) to be active at the same time. However, it is possible for neighboring directions (like up and right) to be active at the same time if the player presses a diagonal.

    A DPad can be obtained from the Pad::as_dpad() method.
    impl Compare for DPad
    impl Default for DPad
    impl Eq for DPad
    impl Hash for DPad
    impl Show for DPad

    DPad::any_pressed

    fn DPad::any_pressed(self : DPad) -> Bool

    Check if any direction is currently pressed.

    DPad::held

    fn DPad::held(self : DPad, old : DPad) -> DPad

    Given the old state, get directions that were both pressed previously and are still pressed now.

    DPad::just_pressed

    fn DPad::just_pressed(self : DPad, old : DPad) -> DPad

    Given the old state, get directions that were not pressed previously but are pressed now.

    DPad::just_released

    fn DPad::just_released(self : DPad, old : DPad) -> DPad

    Given the old state, get directions that were pressed previously but are no longer pressed now.

    DPad::read

    fn DPad::read(peer? : Peer) -> DPad?

    Shorthand function for reading the pad input and converting it into a DPad.

    The peer can be a player ID, or 0xFF to read the combined inputs from all active players in the multiplayer session.

    Returns None when the pad has no input. Meaning, the user is not touching the touch pad at all.

    File

    pub type File FixedArray[Byte]

    A loaded file.

    Can be loaded using File::load
    impl Default for File

    File::as_font

    fn File::as_font(self : File) -> Font

    Convert the File to a Font.

    File::as_image

    fn File::as_image(self : File) -> Image

    Convert the File to an Image.

    File::dump

    fn File::dump(self : File, path : String) -> UInt

    Write the file to the given path in the data directory.

    • If the file exists, it will be overritten.
    • If it doesn't exist, it will be created.

    Example: writing save files.

    Returns the number of bytes written.

    File::get_size

    fn File::get_size(self : File) -> UInt

    Get the size of the file in bytes.

    File::inner

    fn File::inner(self : File) -> FixedArray[Byte]
    Convert newtype to its underlying type, automatically derived.

    File::load

    fn File::load(path : String) -> File?

    Load a file from ROM or the data directory.

    Example: loading fonts, images, or save files.

    File::of

    fn File::of(arr : FixedArray[Byte]) -> File

    Creates a File from a fixed-sized byte array.

    Font

    pub type Font FixedArray[Byte]

    A loaded font file.

    Can be loaded using Font::load
    impl Default for Font

    Font::as_file

    fn Font::as_file(self : Font) -> File

    Convert the Font to a File.

    Font::char_height

    fn Font::char_height(self : Font) -> Int

    Get the character height used in the font (in pixels).

    Font::char_width

    fn Font::char_width(self : Font) -> Int

    Get the character width used in the font (in pixels).

    Font::dump

    fn Font::dump(self : Font, path : String) -> UInt

    Write the Font to the given path in the data directory.

    • If the Font exists, it will be overritten.
    • If it doesn't exist, it will be created.

    Returns the number of bytes written.

    Font::inner

    fn Font::inner(self : Font) -> FixedArray[Byte]
    Convert newtype to its underlying type, automatically derived.

    Font::is_ascii

    fn Font::is_ascii(self : Font) -> Bool

    If the font is for ASCII encoding (English alphabet).

    Font::load

    fn Font::load(path : String) -> Font?

    Load a Font from ROM or the data directory.

    Font::of

    fn Font::of(arr : FixedArray[Byte]) -> Font

    Creates a Font from a fixed-sized byte array.

    Image

    pub type Image FixedArray[Byte]

    A loaded image file.

    Can be loaded using Image::load
    impl Default for Image

    Image::as_file

    fn Image::as_file(self : Image) -> File

    Convert the Image to a File.

    Image::bits_per_pixel

    fn Image::bits_per_pixel(self : Image) -> Byte

    Bits per pixel. One of: 1, 2, or 4

    Image::draw

    fn Image::draw(self : Image, point : Point) -> Unit

    Render the image

    The point defines the upper-left corner.

    Image::dump

    fn Image::dump(self : Image, path : String) -> UInt

    Write the Image to the given path in the data directory.

    • If the file exists, it will be overritten.
    • If it doesn't exist, it will be created.

    Returns the number of bytes written.

    Image::get_color

    fn Image::get_color(self : Image, index : Byte) -> Color

    Get a color from the image color palette.

    Palette index must be between 0-15 (includesive), otherwise this function returns Color::None

    Image::height

    fn Image::height(self : Image) -> UInt

    Returns the height of the image (in pixels).

    Image::inner

    fn Image::inner(self : Image) -> FixedArray[Byte]
    Convert newtype to its underlying type, automatically derived.

    Image::load

    fn Image::load(path : String) -> Image?

    Load a file as an image.

    Image::new

    fn Image::new(size : Size, bpp? : Byte) -> Image

    Create a new blank image.

    By default the image uses 4 bits-per-pixel (bpp). The bpp must be either 1, 2, or 4.

    Image::of

    fn Image::of(arr : FixedArray[Byte]) -> Image

    Creates an Image from a fixed-sized byte array.

    Image::pixels

    fn Image::pixels(self : Image) -> UInt

    Returns the number of pixels the image has

    Image::replace_color

    fn Image::replace_color(self : Image, old_color : Color, new_color : Color) -> Unit

    Replace one color in the image's color palette with another.

    Image::set_color

    fn Image::set_color(self : Image, index : Byte, color : Color) -> Unit

    Set color in the image color palette.

    Palette index must be between 0-15 (includesive), otherwise this function is a no-op.

    Image::set_transparency

    fn Image::set_transparency(self : Image, color : Color) -> Unit

    Set the color that should represent transparency.

    Pass Color::None to disable transparency.

    Image::size

    fn Image::size(self : Image) -> Size

    The image size in pixels

    Image::sub

    fn Image::sub(self : Image, point : Point, size : Size) -> SubImage

    Get a rectangle subregion of the image.

    The point defines the upper-left corner of the subregion within the image.

    Image::transparency

    fn Image::transparency(self : Image) -> Color

    The color used for transparency. If no transparency, returns None

    Image::width

    fn Image::width(self : Image) -> UInt

    Returns the width of the image (in pixels).

    LineStyle

    pub(all) struct LineStyle {
    color : Color
    width : Int
    }

    Style of a line used when drawing.
    impl Eq for LineStyle
    impl Hash for LineStyle
    impl Show for LineStyle

    LineStyle::draw

    fn LineStyle::draw(self : LineStyle, a : Point, b : Point) -> Unit

    Draw a line from a to b using this style.

    LineStyle::new

    fn LineStyle::new(color : Color, width : Int) -> LineStyle

    Shorthand for creating a new LineStyle.

    Pad

    pub(all) struct Pad {
    x : Int
    y : Int
    }

    A finger position on the touch pad.

    Both x and y are within the range of -1000 and 1000 (both ends included).

    • Pad.x == -1000 is on the left
    • Pad.x == 1000 is on the right
    • Pad.y == -1000 is on downwards
    • Pad.y == 1000 is on upwards
    impl Add for Pad
    impl Compare for Pad
    impl Default for Pad
    impl Eq for Pad
    impl Hash for Pad
    impl Neg for Pad
    impl Show for Pad
    impl Sub for Pad

    Pad::abs

    fn Pad::abs(self : Pad) -> Pad

    Returns a Pad with absolute (non-negative) x and y values.

    Pad::as_angle

    fn Pad::as_angle(self : Pad) -> Angle

    The angle of the polar coordinate of the touch point.

    Pad::as_dpad

    fn Pad::as_dpad(self : Pad) -> DPad

    Convert the Pad to a DPad.

    Pad::as_point

    fn Pad::as_point(self : Pad) -> Point

    Convert the Pad to a Point.

    Pad::length

    fn Pad::length(self : Pad) -> Float

    The distance from the pad center to the touch point.

    If the Pad is at {x: 0, y: 0}, then this returns 0.

    Pad::length_squared

    fn Pad::length_squared(self : Pad) -> Int

    The squared distance from the pad center to the touch point, which is easier for the computer to compute than the length.

    This also returns an Int instead of a Float as it's guaranteed that the result will be an integer.

    Pad::new

    fn Pad::new(x : Int, y : Int) -> Pad

    Shorthand for creating a new Pad.

    Pad::read

    fn Pad::read(peer? : Peer) -> Pad?

    Get the current touch pad state.

    The peer can be a player ID, or 0xFF to read the combined inputs from all active players in the multiplayer session.

    Returns None when the pad has no input. Meaning, the user is not touching the touch pad at all.

    Peer

    pub type Peer Byte

    Multi-player peer/player ID.
    impl Compare for Peer
    impl Default for Peer
    impl Eq for Peer
    impl Hash for Peer
    impl Show for Peer

    Peer::combined

    fn Peer::combined() -> Peer

    Returns a Peer with a special ID that represents a combined input of all peers.

    Useful for single-player games that want in multi-player to handle inputs from all devices as one input.

    Peer::inner

    fn Peer::inner(self : Peer) -> Byte
    Convert newtype to its underlying type, automatically derived.

    Point

    pub(all) struct Point {
    x : Int
    y : Int
    }

    A point on the screen.

    Typically, the upper-left corner of a bounding box of a shape.
    impl Add for Point
    impl Compare for Point
    impl Default for Point
    impl Eq for Point
    impl Hash for Point
    impl Neg for Point
    impl Show for Point
    impl Sub for Point

    Point::abs

    fn Point::abs(self : Point) -> Point

    Returns a Point with absolute (non-negative) x and y values.

    Point::as_pad

    fn Point::as_pad(self : Point) -> Pad

    Convert the Point to a Pad.

    Point::draw

    fn Point::draw(self : Point, color : Color) -> Unit

    Render a single pixel.

    Point::is_in_bounds

    fn Point::is_in_bounds(self : Point) -> Bool

    Point::new

    fn Point::new(x : Int, y : Int) -> Point

    Shortcut for creating a Point.

    Point::reinterpret_as_size

    fn Point::reinterpret_as_size(self : Point) -> Size

    Convert the Point to a Size.

    Note that a Size does not allow negative values. Any negative values are clamped to zero.

    Point::scale_down

    fn Point::scale_down(self : Point, divisor : Int) -> Point

    Divide a point by a scalar.

    RGB

    pub(all) struct RGB {
    r : Byte
    g : Byte
    b : Byte
    }

    The RGB (Red, Green, Blue) value of a color in the palette.
    impl Compare for RGB
    impl Default for RGB
    impl Eq for RGB
    impl Hash for RGB
    impl Show for RGB

    RGB::new

    fn RGB::new(r : Byte, g : Byte, b : Byte) -> RGB

    Shorthand for creating a new RGB.

    Size

    pub(all) struct Size {
    w : UInt
    h : UInt
    }

    Bounding box for a shape.

    The width and hight must be positive.
    impl Add for Size
    impl Compare for Size
    impl Default for Size
    impl Eq for Size
    impl Hash for Size
    impl Show for Size
    impl Sub for Size

    Size::new

    fn Size::new(w : UInt, h : UInt) -> Size

    Shortcut for creating a Size.

    Size::reinterpret_as_point

    fn Size::reinterpret_as_point(self : Size) -> Point

    Convert the Size to a Point.

    Note that Point uses signed integers, while Size uses unsigned integers. Any value above 2^31-1 are clamped to 2^31-1.

    Style

    pub(all) struct Style {
    fill : Color
    stroke : Color
    stroke_width : Int
    }

    Style of a shape used when drawing.
    impl Compare for Style
    impl Default for Style
    impl Eq for Style
    impl Hash for Style
    impl Show for Style

    Style::new_outlined

    fn Style::new_outlined(color : Color, width : Int) -> Style

    Create a style for an outlined shape (without fill).

    Style::new_solid

    fn Style::new_solid(color : Color) -> Style

    Create a style for a solid shape (without stroke).

    SubImage

    pub struct SubImage {
    image : Image
    point : Point
    size : Size
    }

    impl Default for SubImage

    SubImage::draw

    fn SubImage::draw(self : SubImage, point : Point) -> Unit

    DPAD_THRESHOLD

    let DPAD_THRESHOLD : Int

    The minimum X or Y value when converting Pad into DPad for the direction to be considered pressed. We do that to provide a dead zone in the middle of the pad.

    PAD_MAX_X

    let PAD_MAX_X : Int

    The highest possible value for Pad.x

    PAD_MAX_Y

    let PAD_MAX_Y : Int

    The highest possible value for Pad.y

    PAD_MIN_X

    let PAD_MIN_X : Int

    The lowest possible value for Pad.x

    PAD_MIN_Y

    let PAD_MIN_Y : Int

    The lowest possible value for Pad.y

    PEER_COMBINED

    let PEER_COMBINED : Byte

    Special Peer ID that represents a combined input of all peers.

    Useful for single-player games that want in multi-player to handle inputs from all devices as one input.
    let PI : Float

    Mathematical constant pi (π), but as a Float.

    SCREEN_HEIGHT

    let SCREEN_HEIGHT : Int

    The screen height in pixels.

    SCREEN_WIDTH

    let SCREEN_WIDTH : Int

    The screen width in pixels.

    TAU

    let TAU : Float

    Mathematical constant tau (τ), i.e 2*PI

    clear_screen

    fn clear_screen(color : Color) -> Unit

    Fill the whole frame with the given color.

    draw_arc

    fn draw_arc(point : Point, diameter : Int, start : Angle, sweep : Angle, style : Style) -> Unit

    Render an arc (i.e "rainbow").

    The angles start at pointing right, and continues clockwise. The start sets where the arc begins, and the sweep sets the arc lenght. Meaning, the arc goes from start to start+sweep

    Negative angle start or sweep values are allowed.

    The style.fill is ignored. The arc is always only a line.

    The sweep is clamped between 0-2π (0-360°), meaning its value does not wrap around. So a sweep of 3π is equivalent of 2π.

    draw_circle

    fn draw_circle(point : Point, radius : Int, style : Style) -> Unit

    Render a circle.

    The point specifies the upper-left corner.

    draw_ellipse

    fn draw_ellipse(point : Point, size : Size, style : Style) -> Unit

    Render an ellipse (oval).

    The point specifies the upper-left corner.

    draw_image

    fn draw_image(image : Image, point : Point) -> Unit

    Render an image.

    draw_line

    fn draw_line(a : Point, b : Point, style : LineStyle) -> Unit

    Render a line between two points.

    draw_point

    fn draw_point(x : Int, y : Int, color : Color) -> Unit

    Render a single pixel.

    draw_qr

    fn draw_qr(text : String, point : Point, black : Color, white : Color) -> Unit

    Render a QR-code.

    draw_rect

    fn draw_rect(point : Point, size : Size, style : Style) -> Unit

    Render a rectangle filling the given bounding box.

    The point specifies the upper-left corner.

    draw_rounded_rect

    fn draw_rounded_rect(point : Point, size : Size, corner : Size, style : Style) -> Unit

    Render a rectangle with rounded corners filling the given bounding box.

    The point specifies the upper-left corner.

    draw_sector

    fn draw_sector(point : Point, diameter : Int, start : Angle, sweep : Angle, style : Style) -> Unit

    Render a circle sector (i.e "pizza slice").

    The angles start at pointing right, and continues clockwise. The start sets where the arc begins, and the sweep sets the arc lenght. Meaning, the arc goes from start to start+sweep

    Negative angle start or sweep values are allowed.

    The sweep is clamped between 0-2π (0-360°), meaning it does not wrap around. So a sweep of 3π is equivalent of 2π.

    draw_sub_image

    fn draw_sub_image(image : Image, point : Point, sub_offset : Point, sub_size : Size) -> Unit

    Render a sub-image, meaning a part of an image.

    Useful for sprite sheets.

    draw_text

    fn draw_text(text : String, font : Font, point : Point, color : Color) -> Unit

    Render text using the given font.

    Unlike in the other drawing functions, here point does not represent the top-left corner, but instead the text baseline position (such as the bottom left pixel of an underscore "_").

    draw_triangle

    fn draw_triangle(a : Point, b : Point, c : Point, style : Style) -> Unit

    Render a triangle.

    The order of the points doesn't matter.

    dump_bytes_to_file

    fn dump_bytes_to_file(path : String, bytes : Bytes) -> UInt

    Write the bytes to the given path in the data directory.

    The path must not contain any path separators ("/").

    • If the file exists, it will be overritten.
    • If it doesn't exist, it will be created.

    Example: writing save files.

    Returns the number of bytes written.

    dump_file

    fn dump_file(path : String, file : File) -> UInt

    Write the file to the given path in the data directory.

    The path must not contain any path separators ("/").

    • If the file exists, it will be overritten.
    • If it doesn't exist, it will be created.

    Example: writing save files.

    Returns the number of bytes written.

    dump_font_to_file

    fn dump_font_to_file(path : String, font : Font) -> UInt

    Write the Font to the given path in the data directory.

    The path must not contain any path separators ("/").

    • If the file exists, it will be overritten.
    • If it doesn't exist, it will be created.

    Returns the number of bytes written.

    dump_image_to_file

    fn dump_image_to_file(path : String, image : Image) -> UInt

    Write the Image to the given path in the data directory.

    The path must not contain any path separators ("/").

    • If the file exists, it will be overritten.
    • If it doesn't exist, it will be created.

    Returns the number of bytes written.

    dump_string_to_file

    fn dump_string_to_file(path : String, content : String, encoding~ :
    Encoding
    ) -> UInt

    Encodes the string and writes it to the given path in the data directory.

    The path must not contain any path separators ("/").

    • If the file exists, it will be overritten.
    • If it doesn't exist, it will be created.

    Example: writing save files.

    Returns the number of bytes written.

    get_file_size

    fn get_file_size(path : String) -> UInt

    Get size (in bytes) of a file from ROM or the data directory.

    The path must not contain any path separators ("/").

    Returns 0 if the file does not exist.

    get_name

    fn get_name(peer : UInt) -> String

    Get name of the device.

    get_random

    fn get_random() -> UInt

    Get a random value.

    is_file

    fn is_file(path : String) -> Bool

    Check if the given path points to a file in ROM or the data directory.

    The path must not contain any path separators ("/").

    load_file

    fn load_file(path : String) -> File?

    Load a file from ROM or the data directory.

    The path must not contain any path separators ("/").

    Example: loading fonts, images, or save files.

    load_file_to

    fn load_file_to(path : String, output : FixedArray[Byte]) -> UInt

    Load a file into a buffer from ROM or the data directory.

    The path must not contain any path separators ("/").

    Returns the number of bytes that has been loaded.

    Example: loading fonts, images, or save files.

    load_file_to_bytes

    fn load_file_to_bytes(path : String) -> Bytes?

    Load a file from ROM or the data directory and return it as bytes.

    The path must not contain any path separators ("/").

    load_file_to_font

    fn load_file_to_font(path : String) -> Font?

    Load a file from ROM or the data directory as a Font.

    The path must not contain any path separators ("/").

    load_file_to_image

    fn load_file_to_image(path : String) -> Image?

    Load a file from ROM or the data directory as an Image.

    The path must not contain any path separators ("/").

    load_file_to_string

    fn load_file_to_string(path : String, encoding~ :
    Encoding
    ) -> String? raise
    DecodingError

    Load a file from ROM or the data directory and decode it as a string.

    The path must not contain any path separators ("/").

    log_debug

    fn log_debug(s : String) -> Unit

    Log a debug message.

    log_error

    fn log_error(s : String) -> Unit

    Log an error message.

    quit

    fn quit() -> Unit

    Exit the app after the current update is finished.

    read_buttons

    fn read_buttons(peer? : Peer) -> Buttons

    Get the currently pressed buttons.

    The peer can be a player ID, or 0xFF to read the combined inputs from all active players in the multiplayer session.

    read_pad

    fn read_pad(peer? : Peer) -> Pad?

    Get the current touch pad state.

    The peer can be a player ID, or 0xFF to read the combined inputs from all active players in the multiplayer session.

    Returns None when the pad has no input. Meaning, the user is not touching the touch pad at all.

    remove_file

    fn remove_file(path : String) -> Unit

    Removes a file (if exists) from the data directory.

    The path must not contain any path separators ("/").

    Example: removing save files.

    restart

    fn restart() -> Unit

    Restart the app after the current update is finished.

    set_canvas

    fn set_canvas(canvas : Canvas) -> Unit

    Set the target canvas for all subsequent drawing operations.

    To draw to the screen again, call unset_canvas.

    set_color

    fn set_color(color : Color, rgb : RGB) -> Unit

    Set a color value in the palette.

    set_color_palette

    fn set_color_palette(palette : FixedArray[RGB]) -> Unit

    Set all colors in the palette.

    The palette fixed array is expected to have 16 elements. If it has more, then only the first 16 values in the palette are used.

    set_seed

    fn set_seed(seed : UInt) -> Unit

    Set the random seed.

    unset_canvas

    fn unset_canvas() -> Unit

    Make all subsequent drawing operations target the screen instead of a canvas.

    This reverts the change of set_canvas.

    This may be called even if there's no current canvas set.