reinit repo after broken git

This commit is contained in:
krzosa
2024-12-29 10:10:09 +01:00
commit a30a897272
40 changed files with 13769 additions and 0 deletions

153
package/index.html Normal file
View File

@@ -0,0 +1,153 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<canvas class="fill-screen" id="canvas"></canvas>
</body>
<style>
@font-face {
font-family: fira;
src: url(FiraCode-Regular.ttf);
}
* {
margin: 0;
padding: 0;
overflow: hidden;
}
.fill-screen {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
</style>
</html>
<script>
class memory_t {
exports = null; // this is set after wasm module created
constructor(wasm_memory) {
this.mem = wasm_memory;
this.u8 = new Uint8Array(this.mem.buffer);
this.data_view = new DataView(this.mem.buffer);
this.utf8decoder = new TextDecoder("utf-8");
this.utf8encoder = new TextEncoder("utf-8");
}
read_cstr(str, len) {
const arr = this.u8.subarray(str, str+len);
const text = this.utf8decoder.decode(arr);
return text;
}
write_string_into_cmemory(ptr, ptr_len, string) {
const bytes = this.utf8encoder.encode(string);
let i = 0;
for (; i < bytes.length && i < (ptr_len-1); i += 1) {
this.u8[ptr + i] = bytes[i];
}
this.u8[ptr + i] = 0;
}
write_string(temp, string) {
const ptr = this.exports[temp].value;
const len = this.data_view.getInt32(this.exports[temp + "_len"].value, true);
this.write_string_into_cmemory(ptr, len, string);
return ptr;
}
}
const canvas = document.getElementById("canvas");
const ctx2d = canvas.getContext('2d');
const mem = new memory_t(new WebAssembly['Memory']({ initial: 2000, maximum: 65536 }));
(async function main() {
if (!ctx2d) {
alert('Outdated browser, cant draw :(');
return;
}
const request = await fetch('main.wasm');
const binary = await request.arrayBuffer();
const wasm_imports = {
memory: mem.mem,
wasm_write_to_console: (str, len) => console.log(mem.read_cstr(str, len)),
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})`;
ctx2d.textBaseline = "top"
ctx2d.fillText(mem.read_cstr(str, len), x, y)
},
wasm_measure_text: (str, len, font_str, font_len, font_size) => {
ctx2d.font = `${font_size}px ${mem.read_cstr(font_str, font_len)}`;
ctx2d.textBaseline = "top";
const metrics = ctx2d.measureText(mem.read_cstr(str, len));
return metrics.width;
},
wasm_get_font_height: (font_str, font_len, font_size) => {
ctx2d.font = `${font_size}px ${mem.read_cstr(font_str, font_len)}`;
ctx2d.textBaseline = "top";
return ctx2d.measureText('NothinBelowTheBaseline').actualBoundingBoxDescent;
},
wasm_draw_rect: (x, y, w, h, r, g, b, a) => {
ctx2d.beginPath();
ctx2d.rect(x, y, w, h);
ctx2d.fillStyle = `rgba(${r}, ${g}, ${b}, ${a})`;
ctx2d.fill();
},
wasm_set_clip: (x, y, w, h) => {
ctx2d.restore();
ctx2d.save();
ctx2d.beginPath();
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() },
};
const program = await WebAssembly['instantiate'](binary, { "env": wasm_imports });
const instance = program['instance'];
const wasm_exports = instance['exports'];
mem.exports = wasm_exports;
wasm_exports['wasm_init']();
addEventListener("keydown", (event) => {
wasm_exports["wasm_key_down"](mem.write_string("wasm_temp_buff1", event.key), event.ctrlKey, event.shiftKey, event.altKey, event.metaKey);
});
addEventListener("keyup", (event) => {
wasm_exports["wasm_key_up"](mem.write_string("wasm_temp_buff1", event.key), event.ctrlKey, event.shiftKey, event.altKey, event.metaKey);
});
addEventListener("mousemove", (event) => {
wasm_exports["wasm_mouse_move"](event.clientX, event.clientY, event.ctrlKey, event.shiftKey, event.altKey, event.metaKey);
});
addEventListener("mousedown", (event) => {
wasm_exports["wasm_mouse_down"](event.clientX, event.clientY, event.button, event.ctrlKey, event.shiftKey, event.altKey, event.metaKey);
});
addEventListener("mouseup", (event) => {
wasm_exports["wasm_mouse_up"](event.clientX, event.clientY, event.button, event.ctrlKey, event.shiftKey, event.altKey, event.metaKey);
});
addEventListener("wheel", (event) => {
wasm_exports["wasm_mouse_wheel"](event.deltaX, event.deltaY, event.deltaZ, event.ctrlKey, event.shiftKey, event.altKey, event.metaKey);
});
requestAnimationFrame(function update(time) {
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);
requestAnimationFrame(update);
});
})()
</script>