diff --git a/cli/assets/templates/assemblyscript/src/wasm4.ts b/cli/assets/templates/assemblyscript/src/wasm4.ts index e29ed870..968f763b 100644 --- a/cli/assets/templates/assemblyscript/src/wasm4.ts +++ b/cli/assets/templates/assemblyscript/src/wasm4.ts @@ -48,6 +48,43 @@ export const SYSTEM_HIDE_GAMEPAD_OVERLAY = 2; // │ │ // └───────────────────────────────────────────────────────────────────────────┘ +/** Set single pixel to the framebuffer with specific color without bounds checking. */ +// @ts-ignore: decorator +@inline +export function setPixelUnsafe(color: u8, x: i32, y: i32): void { + let idx = y * (SCREEN_SIZE >>> 2) + (x >>> 2); + let shift = ((x & 3) << 1); + let mask = 3 << shift; + let data = (color << shift) | (load(FRAMEBUFFER + idx) & ~mask); + store(FRAMEBUFFER + idx, data); +} + +/** Set single pixel to the framebuffer with specific color with bounds checking. */ +export function setPixel(color: u8, x: i32, y: i32): void { + if (x < SCREEN_SIZE && y < SCREEN_SIZE) { + setPixelUnsafe(color & 3, x, y); + } +} + +/** Get single pixel from the framebuffer without bounds checking. */ +// @ts-ignore: decorator +@inline +export function getPixelUnsafe(x: i32, y: i32): u8 { + let idx = y * (SCREEN_SIZE >>> 2) + (x >>> 2); + let shift = ((x & 3) << 1); + let color = load(FRAMEBUFFER + idx); + return (color >> shift) & 3; +} + +/** Get single pixel from the framebuffer with bounds checking. */ +export function getPixel(x: i32, y: i32): u8 { + if (x < SCREEN_SIZE && y < SCREEN_SIZE) { + return getPixelUnsafe(x, y); + } else { + return 0; + } +} + /** Copies pixels to the framebuffer. */ // @ts-ignore: decorator @external("env", "blit")