W4

W4 :: # (opaque)

roc-wasm4

Build WASM-4 games using Roc.

This module provides the high-level, ergonomic API for the WASM-4 platform. Internally it wraps the low-level effects exposed by Host.

set_palette! : { color1 : U32, color2 : U32, color3 : U32, color4 : U32 } => {}

Set the color Palette for your game.

W4.set_palette!({
    color1: 0xffffff,
    color2: 0xff0000,
    color3: 0x00ff00,
    color4: 0x0000ff,
})

Warning: this will overwrite the existing Palette, changing all colors on the screen.

get_palette! : () => { color1 : U32, color2 : U32, color3 : U32, color4 : U32 }

Get the color Palette for your game.

{ color1, color2, color3, color4 } = W4.get_palette!()
set_draw_colors! : DrawColors => {}

Set the draw colors for the next draw command.

blue = Color1
white = Color4
W4.set_draw_colors!({
    primary: blue,
    secondary: white,
    tertiary: None,
    quaternary: None,
})

Warning: this will overwrite any existing draw colors that are set.

get_draw_colors! : () => DrawColors

Get the currently set draw colors.

{ primary, secondary, tertiary, quaternary } = W4.get_draw_colors!()
set_primary_color! : Palette => {}

Helper for setting the primary drawing color.

blue = Color1
W4.set_primary_color!(blue)

Warning: this will overwrite any existing draw colors, and sets the secondary, tertiary and quaternary values to None.

set_text_colors! : { fg : Palette, bg : Palette } => {}

Helper for setting the draw colors for text.

blue = Color1
white = Color4
W4.set_text_colors!({ fg: blue, bg: white })

Warning: this will overwrite any existing draw colors, and sets the tertiary and quaternary values to None.

set_shape_colors! : { border : Palette, fill : Palette } => {}

Helper for colors when drawing a shape.

blue = Color1
white = Color4
W4.set_shape_colors!({ border: blue, fill: white })

Warning: this will overwrite any existing draw colors, and sets the tertiary and quaternary values to None.

hline! : { x : I32, y : I32, len : U32 } => {}

Draw a horizontal line starting at (x, y) with len to the screen.

W4.hline!({ x: 10, y: 20, len: 30 })

Line color is the Primary draw color.

vline! : { x : I32, y : I32, len : U32 } => {}

Draw a vertical line starting at (x, y) with len to the screen.

W4.vline!({ x: 10, y: 20, len: 30 })

Line color is the Primary draw color.

get_gamepad! : Player => Gamepad

Get the controls for a Gamepad.

{ button1, button2, left, right, up, down } = W4.get_gamepad!(Player1)
get_mouse! : () => Mouse

Get the current Mouse position and button state.

{ x, y, left, right, middle } = W4.get_mouse!()
get_netplay! : () => Netplay

Get the Netplay status.

netplay = W4.get_netplay!()
match netplay {
    Enabled(Player1) => # ..
    Enabled(Player2) => # ..
    Enabled(Player3) => # ..
    Enabled(Player4) => # ..
    Disabled => # ..
}

Note: All WASM-4 games that support local multiplayer automatically support netplay.

Refer to the WASM-4 docs for more information

seed_rand! : U64 => {}

Seeds the global pseudo-random number generator.

W4.seed_rand!(frames_since_start)

Wasm4 exposes no way to seed a random number generator. To work around this, it is suggested to count the number of frames the user is on the title screen before starting the game and use that to seed the prng.

rand! : () => I32

Generate a pseudo-random number between Num.min_i32 and Num.max_i32 (inclusive).

i = W4.rand!()
rand_between! : { start : I32, before : I32 } => I32

Generate a pseudo-random number in the range [start, before).

# random number in the range 0-99
i = W4.rand_between!({ start: 0, before: 100 })
save_to_disk! : List(U8) => Try({}, [SaveFailed])

Saves data to persistent storage. Any previously saved data on the disk is replaced.

Returns Err(SaveFailed) on failure.

result = W4.save_to_disk!([0x10])
match result {
    Ok({}) => # success
    Err(SaveFailed) => # handle failure
}

Games can persist up to 1024 bytes of data.

Refer to the WASM-4 docs for more information

preserve_frame_buffer! : () => {}

Set a flag to keep the framebuffer between frames.

This can be helpful if you only want to update part of the screen.

get_pixel! : { x : U8, y : U8 } => Palette

Get the color for an individual pixel in the framebuffer.

set_pixel! : { x : U8, y : U8 }, Palette => {}

Set the color for an individual pixel in the framebuffer.

run_shader! : Shader => {}

Run a fragment Shader on the raw framebuffer.

The shader is invoked for every pixel (x, y) on the screen, and its return value is written back to the framebuffer.

tone! : {
    start_freq : U16,
    end_freq : U16,
    channel : [
        Pulse1([Eighth, Quarter, Half, ThreeQuarters]),
        Pulse2([Eighth, Quarter, Half, ThreeQuarters]),
        Triangle,
        Noise,
    ],
    pan : [Center, Left, Right],
    sustain_time : U8,
    release_time : U8,
    decay_time : U8,
    attack_time : U8,
    volume : U8,
    peak_volume : U8,
} => {}
bit_is_set : U8, U8 -> Bool

Returns Bool.True if bit n (0-indexed from LSB) is set in byte.