From ddb064cdf4fbedf56153abebeffea812f5a2d582 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Sat, 26 Aug 2023 17:15:15 -0500 Subject: [PATCH] text from rust --- front/index.js | 51 ++++++++++++++++++++++++++++++++-------------- justfile | 7 ++++--- pirates/src/lib.rs | 21 +++++++++++++++++-- 3 files changed, 59 insertions(+), 20 deletions(-) diff --git a/front/index.js b/front/index.js index f891afe..2d55c6b 100644 --- a/front/index.js +++ b/front/index.js @@ -1,34 +1,55 @@ +var ctx; +var image; +var memory; +const width = 256; +const height = 224; + +function blit_frame() { + ctx.putImageData(image, 0, 0); +} + +function blit_text(text, len, x, y, size) { + let decoded = (new TextDecoder()) + .decode(new Uint8Array(memory.buffer, text, len)); + ctx.font = size +'vh serif' + ctx.fillText(decoded,x,y); +} + async function init() { - const { instance } = await WebAssembly.instantiateStreaming( - fetch("./index.wasm") - ); - - const width = 600; - const height = 600; - const canvas = document.getElementById("window"); canvas.width = width; canvas.height = height; + ctx = canvas.getContext("2d"); + + const { instance } = await WebAssembly.instantiateStreaming( + fetch("./index.wasm"), + { + "env" : { + "js_sin": Math.sin, + "blit_frame": blit_frame, + "blit_text": blit_text, + }, + } + ); + + memory = instance.exports.memory const buffer_address = instance.exports.BUFFER.value; - const image = new ImageData( + image = new ImageData( new Uint8ClampedArray( - instance.exports.memory.buffer, + memory.buffer, buffer_address, 4 * width * height, ), width, ); - const ctx = canvas.getContext("2d"); + instance.exports.frame_entry(); + ctx.textBaseline = 'top' + ctx.textAlign = 'left'; const render = () => { instance.exports.frame_entry(); - ctx.putImageData(image, 0, 0); - ctx.font = '84px sans-serif' - ctx.textBaseline = 'top' - ctx.textAlign = 'left'; - ctx.fillText("demo",12,12); requestAnimationFrame(render); } diff --git a/justfile b/justfile index eb5392d..94a1a71 100644 --- a/justfile +++ b/justfile @@ -7,10 +7,11 @@ build: minify-js minify-rust minify-html build-rust: cargo build --manifest-path pirates/Cargo.toml --target wasm32-unknown-unknown --release + cp pirates/target/wasm32-unknown-unknown/release/pirates.wasm front/index.wasm minify-rust: build-rust build-dir - wasm-strip pirates/target/wasm32-unknown-unknown/release/pirates.wasm - wasm-opt -o build/index.wasm -Oz pirates/target/wasm32-unknown-unknown/release/pirates.wasm + wasm-strip front/index.wasm + wasm-opt -o build/index.wasm -Oz front/index.wasm minify-js: build-dir #minify-js -m module --output build/index.js front/index.js @@ -29,7 +30,7 @@ check-size: 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))+'%')" + @cat build/release.zip | wc -c | xargs -I {} python3 -c "print(str(round({}/(13*1024)*100,2))+'%')" zip: build zip -r build/release.zip build -x release.zip diff --git a/pirates/src/lib.rs b/pirates/src/lib.rs index 24d86e8..008a9e3 100644 --- a/pirates/src/lib.rs +++ b/pirates/src/lib.rs @@ -7,8 +7,22 @@ fn handle_panic(_: &core::panic::PanicInfo) -> ! { loop {} } -const WIDTH: usize = 600; -const HEIGHT: usize = 600; +extern { + fn blit_frame(); + fn blit_text(text: *const u8, len: u32, x: i32, y: i32, size: u8); +} + +fn draw_text(text: &str, x: i32, y: i32, size: u8) { + unsafe { + blit_text( + (*text).as_ptr(), + text.len() as u32, + x, y, size) + } +} + +const WIDTH: usize = 256; +const HEIGHT: usize = 224; #[no_mangle] static mut BUFFER: [u32; WIDTH * HEIGHT] = [0; WIDTH * HEIGHT]; @@ -28,6 +42,9 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) { buffer[y*WIDTH + x] = frame.wrapping_add((x^y) as u32) | 0xFF000000; } } + unsafe { blit_frame(); } + + draw_text("hi from rust", 0,100,1); } #[cfg(test)]