-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
79 lines (73 loc) · 2.12 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
<style>
body {
color: white;
font-family: arial;
flex-direction: column;
background-color: black;
display: flex;
align-items: center;
justify-content: center;
}
</style>
<body>
<div>This is supposed to be zork.</div>
</body>
<script>
var width = 100;
var height = 43;
var charWidth = 10;
var charHeight = 20;
var pressedKeys = [];
var c = document.createElement('canvas');
c.width = width * charWidth;
c.height = height * charHeight;
document.body.append(c);
var ctx = c.getContext('2d');
ctx.textAlign = "left";
ctx.font = '12px arial';
var machine_ptr;
var wasm_instance;
fetch('target/wasm32-unknown-unknown/release/rustzork.wasm')
.then(response => response.arrayBuffer())
.then(bytes => WebAssembly.instantiate(bytes, {
env: {
debug_trace: function(x) {
console.log( "debug trace", x );
},
terminal_height: function () {
return height;
},
clear: function() {
ctx.clearRect(0, 0, width*charWidth, height*charHeight);
},
put_line: function(x,y,ptr,len) {
ctx.fillStyle = `rgb(255,255,255)`;
var text = "";
var memory = new Uint8Array(wasm_instance.exports.memory.buffer, ptr, len);
for( let n of memory.values() )
{
text += String.fromCharCode( n );
}
ctx.fillText(text, x*charWidth + charWidth / 2, y*charHeight + charHeight);
}
}
})
.then( results => {
wasm_instance = results.instance;
machine_ptr = wasm_instance.exports.initialize();
function update() {
window.requestAnimationFrame(update);
for( let n of pressedKeys )
{
wasm_instance.exports.key_pressed(machine_ptr, n.keyCode);
}
pressedKeys = [];
wasm_instance.exports.update(machine_ptr);
}
document.addEventListener('keydown', (event) => {
//console.log(event);
pressedKeys.push(event);
});
update();
}));
</script>