1
Fork 0

stuff on-screen

also made a full justfile for web3 (wrong three) codegolfing (no node)

blitting works thanks to http://cliffle.com/blog/bare-metal-wasm/
This commit is contained in:
Andy Killorin 2023-08-25 20:57:26 -05:00
parent 822cef9916
commit 63f6a46482
Signed by: ank
GPG key ID: B6241CA3B552BCA4
5 changed files with 117 additions and 7 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
pirates/target
build

11
front/index.html Normal file
View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<script type="module" src="index.js">
</script>
</head>
<body>
<canvas id="window"></canvas>
</body>
</html>

35
front/index.js Normal file
View file

@ -0,0 +1,35 @@
async function init() {
const { instance } = await WebAssembly.instantiateStreaming(
fetch("./pirates.wasm")
);
const width = 600;
const height = 600;
const canvas = document.getElementById("window");
canvas.width = width;
canvas.height = height;
const buffer_address = instance.exports.BUFFER.value;
const image = new ImageData(
new Uint8ClampedArray(
instance.exports.memory.buffer,
buffer_address,
4 * width * height,
),
width,
);
const ctx = canvas.getContext("2d");
const render = () => {
instance.exports.frame_entry();
ctx.putImageData(image, 0, 0);
requestAnimationFrame(render);
}
render();
}
init();

View file

@ -1,7 +1,42 @@
alias b := build
alias r := run
alias c := check-zip
build: minify-js minify-rust minify-html
build-rust:
cargo build --manifest-path pirates/Cargo.toml --target wasm32-unknown-unknown --release
minify-rust: build-rust
minify-rust: build-rust build-dir
wasm-strip pirates/target/wasm32-unknown-unknown/release/pirates.wasm
wasm-opt -o pirates/target/wasm32-unknown-unknown/release/pirates-opt.wasm -Oz pirates/target/wasm32-unknown-unknown/release/pirates.wasm
wasm-opt -o build/pirates.wasm -Oz pirates/target/wasm32-unknown-unknown/release/pirates.wasm
minify-js: build-dir
#minify-js -m module --output build/index.js front/index.js
minify front/index.js > build/index.js
minify-html: build-dir
minify front/index.html > build/index.html
[private]
build-dir:
mkdir -p build
check-size: build
dust -s build
check-zip: zip
unzip -v build/release.zip | awk '{printf ("%5s\t%s\n", $3, $8)}'
@cat build/release.zip | wc -c | xargs -I {} python3 -c "print(str(round({}/(13*1024),2))+'%')"
zip: build
zip -r build/release.zip build -x release.zip
run: build
python3 -m http.server &
firefox http://0.0.0.0:8080/build/index.html
clean:
cargo clean --manifest-path pirates/Cargo.toml
rm -r build

View file

@ -1,5 +1,33 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
#![no_std]
use core::sync::atomic::{AtomicU32, Ordering};
#[panic_handler]
fn handle_panic(_: &core::panic::PanicInfo) -> ! {
loop {}
}
const WIDTH: usize = 600;
const HEIGHT: usize = 600;
#[no_mangle]
static mut BUFFER: [u32; WIDTH * HEIGHT] = [0; WIDTH * HEIGHT];
static FRAME: AtomicU32 = AtomicU32::new(0);
#[no_mangle]
pub unsafe extern fn frame_entry() {
// calling from multiple threads is ub
render_frame(&mut BUFFER)
}
fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
let frame = FRAME.fetch_add(1, Ordering::Relaxed);
for y in 0..HEIGHT {
for x in 0..WIDTH {
buffer[y*WIDTH + x] = frame.wrapping_add((x^y) as u32) | 0xFF000000;
}
}
}
#[cfg(test)]
@ -8,7 +36,7 @@ mod tests {
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
let result = number();
assert_eq!(result, 64);
}
}