This commit is contained in:
krzosa
2024-12-31 14:48:19 +01:00
parent 3f9f90466b
commit 05d49eefe8
19 changed files with 262 additions and 195 deletions

View File

@@ -79,8 +79,16 @@ const mem = new memory_t(new WebAssembly['Memory']({ initial: 2000, maximum: 655
const request = await fetch('main.wasm');
const binary = await request.arrayBuffer();
const wasm_imports = {
// core
memory: mem.mem,
wasm_write_to_console: (str, len) => console.log(mem.read_cstr(str, len)),
wasm_parse_float: (str, len) => { return parseFloat(mem.read_cstr(str, len)); },
wasm_alert: (str, len) => { alert(mem.read_cstr(str,len)); },
wasm_trap: () => { throw new Error(); },
wasm_write_to_console: (str, len) => { console.log(mem.read_cstr(str, len)); },
wasm_get_milliseconds: () => { return performance.now(); },
// gfx
wasm_draw_text: (str, len, x, y, font_str, font_len, font_size, r, g, b, a) => {
ctx2d.font = `${font_size}px ${mem.read_cstr(font_str, font_len)}`;
ctx2d.fillStyle = `rgba(${r}, ${g}, ${b}, ${a})`;
@@ -114,15 +122,7 @@ const mem = new memory_t(new WebAssembly['Memory']({ initial: 2000, maximum: 655
ctx2d.rect(x, y, w, h);
ctx2d.clip();
},
wasm_parse_float: (str, len) => {
return parseFloat(mem.read_cstr(str, len));
},
wasm_trap: () => { throw new Error() },
wasm_alert: (str, len) => {
const string = mem.read_cstr(str,len);
console.log(string);
alert(string);
},
};
const program = await WebAssembly['instantiate'](binary, { "env": wasm_imports });
@@ -154,7 +154,7 @@ const mem = new memory_t(new WebAssembly['Memory']({ initial: 2000, maximum: 655
const dpr = window.devicePixelRatio;
canvas.width = canvas.getBoundingClientRect().width * dpr;
canvas.height = canvas.getBoundingClientRect().height * dpr;
wasm_exports['wasm_update'](time / 1000, canvas.width, canvas.height, dpr);
wasm_exports['wasm_update'](canvas.width, canvas.height, dpr);
requestAnimationFrame(update);
});
})()