screen_width : () -> I32
Width of the WASM-4 screen in pixels.
W4 :: # (opaque)
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.
screen_width : () -> I32
Width of the WASM-4 screen in pixels.
screen_height : () -> I32
Height of the WASM-4 screen in pixels.
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.
Draw text to the screen.
W4.text!("Hello, World", { x: 0, y: 0 })
Text color is the Primary draw color. Background color is the Secondary draw color.
Draw a rectangle to the screen.
W4.rect!({ x: 0, y: 10, width: 40, height: 60 })
Fill color is the Primary draw color. Border color is the Secondary draw color.
Draw an oval to the screen.
W4.oval!({ x: 10, y: 20, width: 20, height: 30 })
Fill color is the Primary draw color. Border color is the Secondary draw color.
Draw a line between two points to the screen.
W4.line!({ x: 0, y: 0 }, { x: 10, y: 10 })
Line color is the Primary draw color.
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.
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.
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.
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 })
Prints a message to the debug console.
W4.trace!("Hello, World")
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.
load_from_disk! : () => List(U8)
Gets all saved data from persistent storage.
data = W4.load_from_disk!()
Games can persist up to 1024 bytes of data.
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.
clear_frame_buffer_each_update! : () => {}
Set a flag to clear the framebuffer between frames.
hide_gamepad_overlay! : () => {}
Set a flag to hide the gamepad overlay.
show_gamepad_overlay! : () => {}
Set a flag to show the gamepad overlay.
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.
convert_mode : [Eighth, Quarter, Half, ThreeQuarters] -> U8
to_color_flags : DrawColors -> U16
extract_color : U8 -> Palette
from_color_flags : U16 -> DrawColors