-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
414 lines (344 loc) · 13 KB
/
index.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import init, { Gamebuino } from "./pkg/wasm_gamebuino.js";
import background1 from "./background1.js";
import background2 from "./background2.js";
let memory;
let loadWasmPromise = init().then(wasm => {
memory = wasm.memory;
});
const keymap = [
[83, 40], // down
[65, 81, 37], // left
[68, 39], // right
[87, 90, 38], // up
[74], // A
[75], // B
[85], // MENU
[73] // HOME
];
const dpadX = 109;
const dpadY = 206;
const dpadDist = 87;
const aX = 639;
const aY = 223;
const bX = 716;
const bY = 189;
const menuX = 287;
const menuY = 372;
const homeX = 495;
const homeY = 372;
const btnDist = 40;
class GamebuinoEmulator extends HTMLElement {
constructor() {
super();
this.root = this.attachShadow({ mode: "open" });
this.root.innerHTML = `
<style>
</style>
<div id="console">
<canvas id="gbscreen" width="320" height="256"></canvas>
</div>
`;
this.updateStyle();
this.canvas = this.root.getElementById("gbscreen");
this.ctx = this.canvas.getContext("2d");
this.ctx.scale(2, 2);
this.ctx.imageSmoothingEnabled = false;
this.imageData = this.ctx.getImageData(0, 0, 160, 128);
this.buttonData = 0b11111111;
this.lastTimestamp = 0;
this.requestId;
this.pointerPresses = {};
this.nextAudioStart = 0;
document.addEventListener("keydown", event => {
this.initAudio();
for (var i = 0; i < keymap.length; i++) {
for (var code of keymap[i]) {
if (code == event.keyCode) {
event.preventDefault();
this.buttonData &= ~(1 << i);
return;
}
}
}
});
document.addEventListener("keyup", event => {
for (var i = 0; i < keymap.length; i++) {
for (var code of keymap[i]) {
if (code == event.keyCode) {
this.buttonData |= 1 << i;
return;
}
}
}
});
window.addEventListener("resize", event => {
this.setScale();
});
const controls = this.root.getElementById("console");
controls.addEventListener("pointerdown", event => {
this.initAudio();
this.handlePointerDown(event)
});
controls.addEventListener("pointermove", event =>
this.handlePointerMove(event)
);
document.addEventListener("pointerup", event =>
this.handlePointerUp(event)
);
document.addEventListener("pointercancel", event =>
this.handlePointerUp(event)
);
this.start();
}
get src() {
return this.getAttribute("src");
}
set src(value) {
return this.setAttribute("src", value);
}
get background() {
return this.getAttribute("background");
}
set background(value) {
return this.setAttribute("background", value);
}
get fullscreen() {
return this.getAttribute("fullscreen");
}
set fullscreen(value) {
return this.setAttribute("fullscreen", value);
}
isFullscreen() {
return this.fullscreen !== null && this.fullscreen !== undefined;
}
static get observedAttributes() {
return ["src", "background", "fullscreen"];
}
get buttonState() {
if (!navigator || !navigator.getGamepads) return this.buttonData;
const gamepad = navigator.getGamepads()[0];
if (!gamepad) return this.buttonData;
let gamepadData = 0b11111111;
if (gamepad.axes[0] < -.9 || gamepad.buttons[14].pressed) gamepadData &= 0b11111101;
if (gamepad.axes[0] > .9 || gamepad.buttons[15].pressed) gamepadData &= 0b11111011;
if (gamepad.axes[1] < -.9 || gamepad.buttons[12].pressed) gamepadData &= 0b11110111;
if (gamepad.axes[1] > .9 || gamepad.buttons[13].pressed) gamepadData &= 0b11111110;
if (gamepad.buttons[1].pressed || gamepad.buttons[3].pressed) gamepadData &= 0b11011111;
if (gamepad.buttons[0].pressed || gamepad.buttons[2].pressed) gamepadData &= 0b11101111;
if (gamepad.buttons[8].pressed) gamepadData &= 0b10111111;
if (gamepad.buttons[9].pressed) gamepadData &= 0b01111111;
return this.buttonData & gamepadData;
}
attributeChangedCallback(name, oldValue, newValue) {
switch(name) {
case "background":
case "fullscreen":
this.updateStyle();
break;
case "src":
this.start();
break;
}
}
setScale() {
const elem = this.root.getElementById("console");
if (!this.isFullscreen()) {
elem.style = "";
return;
}
const windowWidth = window.innerWidth && document.documentElement.clientWidth ? Math.min(window.innerWidth, document.documentElement.clientWidth) : window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName('body')[0].clientWidth;
const windowHeight = window.innerHeight && document.documentElement.clientHeight ? Math.min(window.innerHeight, document.documentElement.clientHeight) : window.innerHeight || document.documentElement.clientHeight || document.getElementsByTagName('body')[0].clientHeight;
let scale = windowWidth / elem.clientWidth;
if (elem.clientHeight * scale > windowHeight)
scale = windowHeight / elem.clientHeight;
const margintop = ((windowHeight - (elem.clientHeight * scale)) / 2);
const marginleft = ((windowWidth - (elem.clientWidth * scale)) / 2);
elem.style = "position: abosolute; transform-origin: 0 0; top:" + margintop + "px; left:" + marginleft + "px; transform: scale(" + scale + ")";
}
updateStyle() {
if (this.background === "none") {
if (this.isFullscreen()) {
this.root.querySelector("style").textContent = `
#console {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: black;
display: flex;
justify-content: center;
align-items: center;
}
#gbscreen {
object-fit: contain;
image-rendering: pixelated;
height: 100%;
width: 100%;
}`;
} else {
this.root.querySelector("style").textContent = "";
}
} else {
this.root.querySelector("style").textContent = `
:host {
display: inline-block;
width: 788px;
height: 428px;
}
#console {
width: 788px;
height: 428px;
background-image: url('${this.background === "2" ? background2 : background1}');
position: relative;
image-rendering: pixelated;
}
#gbscreen {
position: absolute;
top: 80px;
left: 232px;
}`;
}
this.setScale();
}
start(program) {
let arrayBufferPromise;
if (program) {
arrayBufferPromise = Promise.resolve(program);
} else {
arrayBufferPromise = fetch(this.src)
.then(response => response.arrayBuffer());
}
Promise.all([arrayBufferPromise, loadWasmPromise])
.then(([buffer]) => {
if (this.requestId) cancelAnimationFrame(this.requestId);
if (this.gamebuino) this.gamebuino.free();
this.gamebuino = Gamebuino.new();
this.gamebuino.load_program(new Uint8Array(buffer), 0x4000);
if (this.audioCtx) {
this.audioCtx.close();
this.audioCtx = undefined;
}
this.nextAudioStart = 0;
this.step();
}
);
}
step(timestamp) {
const goalTicksPerSecond = 20000000;
const maxIterations = goalTicksPerSecond / 30;
const delta = timestamp - this.lastTimestamp;
this.lastTimestamp = timestamp;
let iterations = (delta * goalTicksPerSecond) / 1000;
if (iterations > maxIterations) iterations = maxIterations;
this.gamebuino.run(iterations, this.buttonState);
const buf8 = new Uint8ClampedArray(
memory.buffer,
this.gamebuino.image_pointer(),
160 * 128 * 4
);
this.imageData.data.set(buf8);
this.ctx.putImageData(this.imageData, 0, 0);
this.ctx.drawImage(this.canvas, 0, 0);
this.handleAudio();
this.requestId = requestAnimationFrame(t => this.step(t));
}
squareDist(touch, x, y) {
return (
(touch.offsetX - x) * (touch.offsetX - x) +
(touch.offsetY - y) * (touch.offsetY - y)
);
}
handlePointerDown(event) {
// Ignore if no console is displayed
if (this.background === "none") return;
event.preventDefault();
this.pointerPresses[event.pointerId] = 0b11111111;
this.handlePointerMove(event);
this.updateButtonData();
}
handlePointerMove(event) {
// Ignore if no console is displayed
if (this.background === "none") return;
if (this.pointerPresses.hasOwnProperty(event.pointerId)) {
this.pointerPresses[event.pointerId] = this.handlePointer(event);
}
this.updateButtonData();
}
handlePointerUp(event) {
// Ignore if no console is displayed
if (this.background === "none") return;
delete this.pointerPresses[event.pointerId];
this.updateButtonData();
}
updateButtonData() {
this.buttonData = 0b11111111;
for (let prop in this.pointerPresses) {
this.buttonData &= this.pointerPresses[prop];
}
}
handlePointer(event) {
if (this.squareDist(event, dpadX, dpadY) < dpadDist * dpadDist) {
var angle = Math.atan2(
dpadY - event.offsetY,
event.offsetX - dpadX
);
if (angle < (-7 * Math.PI) / 8) {
return 0b11111101;
} else if (angle < (-5 * Math.PI) / 8) {
return 0b11111100;
} else if (angle < (-3 * Math.PI) / 8) {
return 0b11111110;
} else if (angle < -Math.PI / 8) {
return 0b11111010;
} else if (angle < Math.PI / 8) {
return 0b11111011;
} else if (angle < (3 * Math.PI) / 8) {
return 0b11110011;
} else if (angle < (5 * Math.PI) / 8) {
return 0b11110111;
} else if (angle < (7 * Math.PI) / 8) {
return 0b11110101;
} else {
return 0b11111101;
}
} else if (this.squareDist(event, aX, aY) < btnDist * btnDist) {
return 0b11101111;
} else if (this.squareDist(event, bX, bY) < btnDist * btnDist) {
return 0b11011111;
} else if (this.squareDist(event, menuX, menuY) < btnDist * btnDist) {
return 0b10111111;
} else if (this.squareDist(event, homeX, homeY) < btnDist * btnDist) {
return 0b01111111;
}
return 0b11111111;
}
initAudio() {
const AudioContext = window.AudioContext || window.webkitAudioContext;
if (!this.audioCtx && AudioContext) {
console.log("Audio sample rate = ", this.gamebuino.sample_rate);
this.audioCtx = new AudioContext({ sampleRate: this.gamebuino.sample_rate });
this.audioCtx.resume();
}
}
handleAudio() {
if (!this.audioCtx) return;
const frameCount = this.gamebuino.sound_samples;
if (frameCount === 0) return;
const raw = new Uint16Array(memory.buffer, this.gamebuino.sound_data_pointer(), frameCount);
const audioBuffer = this.audioCtx.createBuffer(1, frameCount, this.audioCtx.sampleRate);
const channelBuffer = audioBuffer.getChannelData(0);
const source = this.audioCtx.createBufferSource();
for (let i = 0; i < frameCount; i++) {
channelBuffer[i] = raw[i] / 1024;
}
source.buffer = audioBuffer;
source.connect(this.audioCtx.destination);
if (this.audioCtx.currentTime > this.nextAudioStart) {
this.nextAudioStart = this.audioCtx.currentTime;
}
source.start(this.nextAudioStart);
this.nextAudioStart += audioBuffer.duration;
}
}
customElements.define("gamebuino-emulator", GamebuinoEmulator);