Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get/set pixel routines #476

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions cli/assets/templates/assemblyscript/src/wasm4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <u8>((x & 3) << 1);
let mask = 3 << shift;
let data = (color << shift) | (load<u8>(FRAMEBUFFER + idx) & ~mask);
store<u8>(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 (<u32>x < SCREEN_SIZE && <u32>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 = <u8>((x & 3) << 1);
let color = load<u8>(FRAMEBUFFER + idx);
return (color >> shift) & 3;
}

/** Get single pixel from the framebuffer with bounds checking. */
export function getPixel(x: i32, y: i32): u8 {
if (<u32>x < SCREEN_SIZE && <u32>y < SCREEN_SIZE) {
return getPixelUnsafe(x, y);
} else {
return 0;
}
}

/** Copies pixels to the framebuffer. */
// @ts-ignore: decorator
@external("env", "blit")
Expand Down