-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
154 lines (139 loc) · 4.02 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<!DOCTYPE html>
<html lang="en">
<head>
<style>
body {
background-color: #cfcfcf;
}
* {
margin: 0;
padding: 0;
}
canvas {
cursor: pointer;
}
.wrapper {
display: flex;
align-items: center;
height: 100vh;
justify-content: center;
}
</style>
<script type="module">
// TODO: export from C?
const WEFX_KEYDOWN = 0;
const WEFX_KEYPRESS = 1;
const WEFX_KEYUP = 2;
const WEFX_MOUSEMOVE = 3;
const WEFX_MOUSEDOWN = 4;
const WEFX_MOUSEUP = 5;
const WEFX_CLICK = 6;
const SIZE_OF_INT = 4;
const canvas = document.getElementById("screenCanvas");
const ctx = canvas.getContext("2d");
ctx.imageSmoothingEnabled = false;
function relativeXY(e, canvas) {
const rect = canvas.getBoundingClientRect();
return {
x: (e.clientX - rect.left) | 0,
y: (e.clientY - rect.top) | 0,
};
}
async function bootstrap() {
const env = {
print: function(strptr) {
// Because C will only send pointers into the
// memory, we need to grab the char values out
// of memory. C strings are terminated by a
// null character by default.
// Super unsafe code here - which is half the fun.
let mptr = strptr;
const memory = getMemory();
const view = new Uint8ClampedArray(memory)
let out = "";
let c = view[mptr];
while(c != 0) {
out += String.fromCharCode(c);
c = view[++mptr];
}
console.log(out);
}
};
const { instance } = await WebAssembly.instantiateStreaming(
fetch("./wefx.wasm"),
{env: env},
);
const {
memory,
screen,
init,
main_loop,
wefx_log,
wefx_draw,
wefx_xsize,
wefx_ysize,
wefx_add_queue_event,
wefx_q,
} = instance.exports;
const err = init();
if (err) {
console.error("Init failed");
return;
}
const width = wefx_xsize();
const height = wefx_ysize();
canvas.width = width;
canvas.height = height;
const offset = screen.value;
const imgArray = new Uint8ClampedArray(
memory.buffer,
offset,
SIZE_OF_INT * width * height
);
const getMemory = () => {
return memory.buffer;
}
const toWefxEvent = (t, e, canvas) => {
const xy = relativeXY(e, canvas);
const k = e.key ? e.key.charCodeAt(0) : 0;
wefx_add_queue_event(t, e.button, e.timeStamp, k, xy.x, xy.y);
};
document.addEventListener("keypress", (e) =>
toWefxEvent(WEFX_KEYPRESS, e, canvas)
);
document.addEventListener("keydown", (e) =>
toWefxEvent(WEFX_KEYDOWN, e, canvas)
);
document.addEventListener("keyup", (e) =>
toWefxEvent(WEFX_KEYUP, e, canvas)
);
canvas.addEventListener("mousedown", (e) =>
toWefxEvent(WEFX_MOUSEDOWN, e, canvas)
);
canvas.addEventListener("mouseup", (e) =>
toWefxEvent(WEFX_MOUSEUP, e, canvas)
);
canvas.addEventListener("mousemove", (e) =>
toWefxEvent(WEFX_MOUSEMOVE, e, canvas)
);
let start = Date.now();
const image = new ImageData(imgArray, width);
const loop = (t) => {
const current = Date.now();
const delta = current - start;
main_loop(delta * 0.05, wefx_q);
wefx_draw(offset);
ctx.putImageData(image, 0, 0);
requestAnimationFrame(loop);
};
requestAnimationFrame(loop);
}
bootstrap();
</script>
</head>
<body>
<div class="wrapper">
<canvas id="screenCanvas"></canvas>
</div>
</body>
</html>