-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblit.js
88 lines (81 loc) · 1.77 KB
/
blit.js
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
const canvas = document.getElementById("screen");
const ctx = canvas.getContext("2d");
const imageData = ctx.createImageData(16, 16);
function blit() {
for (let i = 0; i < msm.output().length; i++) {
let p = msm.output()[i];
/* red */ imageData.data[i*4] = p ? 255 : 0;
/* green */ imageData.data[i*4+1] = p ? 255 : 0;
/* blue */ imageData.data[i*4+2] = p ? 255 : 0;
/* alpha */ imageData.data[i*4+3] = 255;
}
ctx.putImageData(imageData, 0, 0);
}
function gameloop() {
msm.exec_screen_vector();
blit();
new Promise(display_update);
setTimeout(() => {
requestAnimationFrame(gameloop);
}, 1000 / 60);
}
function start_gameloop() {
requestAnimationFrame(gameloop);
}
canvas.addEventListener("keyup", (event) => {
switch(event.key) {
case 'ArrowUp':
msm.keyup(0b11111110);
break;
case 'ArrowDown':
msm.keyup(0b11111101);
break;
case 'ArrowLeft':
msm.keyup(0b11111011);
break;
case 'ArrowRight':
msm.keyup(0b11110111);
break
case 'x':
msm.keyup(0b11101111);
break;
case 'c':
msm.keyup(0b11011111);
break;
default:
msm.keyup(0);
}
});
canvas.addEventListener("keydown", (event) => {
switch(event.key) {
case 'ArrowUp':
msm.keydown(0b00000001);
msm.exec_key_vector();
break;
case 'ArrowDown':
msm.keydown(0b00000010);
msm.exec_key_vector();
break;
case 'ArrowLeft':
msm.keydown(0b00000100);
msm.exec_key_vector();
break;
case 'ArrowRight':
msm.keydown(0b00001000);
msm.exec_key_vector();
break;
case 'x':
msm.keydown(0b00010000);
msm.exec_key_vector();
break;
case 'c':
msm.keydown(0b00100000);
msm.exec_key_vector();
break;
default:
msm.keydown(0);
}
});
start_gameloop();
msm.on_executed(blit);
blit();