-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
136 lines (119 loc) · 4.24 KB
/
main.js
File metadata and controls
136 lines (119 loc) · 4.24 KB
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
'use strict';
var memory = new WebAssembly.Memory({ initial : 108 });
/*stdout and stderr goes here*/
const output = document.getElementById("output");
function readWasmString(offset, length) {
const bytes = new Uint8Array(memory.buffer, offset, length);
return new TextDecoder('utf8').decode(bytes);
}
function appendOutput(style) {
return function(offset, length) {
const lines = readWasmString(offset, length).split('\n');
for (var i=0; i<lines.length; ++i) {
if (lines[i].length == 0) {
continue;
}
var t = document.createElement("span");
t.classList.add(style);
t.appendChild(document.createTextNode(lines[i]));
t.scrollIntoView({behavior: "smooth", block: "end", inline: "nearest"}); /*smooth scrolling is experimental according to MDN*/
}
}
}
/*stats about how often doom polls the time*/
const getmsps_stats = document.getElementById("getmsps_stats");
const getms_stats = document.getElementById("getms_stats");
var getms_calls_total = 0;
var getms_calls = 0; // in current second
window.setInterval(function() {
getms_calls_total += getms_calls;
getms_calls = 0;
}, 1000);
function getMilliseconds() {
++getms_calls;
return performance.now();
}
/*doom is rendered here*/
const canvas = document.getElementById('screen');
const doom_screen_width = 320*2;
const doom_screen_height = 200*2;
function drawCanvas(ptr) {
var doom_screen = new Uint8ClampedArray(memory.buffer, ptr, doom_screen_width*doom_screen_height*4)
var render_screen = new ImageData(doom_screen, doom_screen_width, doom_screen_height)
var ctx = canvas.getContext('2d');
ctx.putImageData(render_screen, 0, 0);
}
/*These functions will be available in WebAssembly. We also share the memory to share larger amounts of data with javascript, e.g. strings of the video output.*/
var importObject = {
js: {
js_console_log: appendOutput("log"),
js_stdout: appendOutput("stdout"),
js_stderr: appendOutput("stderr"),
js_milliseconds_since_start: getMilliseconds,
js_draw_screen: drawCanvas,
},
env: {
memory: memory
}
};
WebAssembly.instantiateStreaming(fetch('doom.wasm'), importObject)
.then(obj => {
/*Initialize Doom*/
obj.instance.exports.main();
/*input handling*/
let doomKeyCode = function(keyCode) {
// Doom seems to use mostly the same keycodes, except for the following (maybe I'm missing a few.)
switch (keyCode) {
// Custom remapping for Vizio remote
// Rebind ok/enter to ctrl/fire
case 13:
return (0x80+0x1d);
// Rebind back/backspace to space/open
case 8:
return 32;
// Rebind exit/escape to confirm/enter
case 27:
return 13;
case 8:
return 127; // KEY_BACKSPACE
case 17:
return (0x80+0x1d); // KEY_RCTRL
case 18:
return (0x80+0x38); // KEY_RALT
case 37:
return 0xac; // KEY_LEFTARROW
case 38:
return 0xad; // KEY_UPARROW
case 39:
return 0xae; // KEY_RIGHTARROW
case 40:
return 0xaf; // KEY_DOWNARROW
default:
if (keyCode >= 65 /*A*/ && keyCode <= 90 /*Z*/) {
return keyCode + 32; // ASCII to lower case
}
if (keyCode >= 112 /*F1*/ && keyCode <= 123 /*F12*/ ) {
return keyCode + 75; // KEY_F1
}
return keyCode;
}
};
let keyDown = function(keyCode) {obj.instance.exports.add_browser_event(0 /*KeyDown*/, keyCode);};
let keyUp = function(keyCode) {obj.instance.exports.add_browser_event(1 /*KeyUp*/, keyCode);};
/*keyboard input*/
canvas.addEventListener('keydown', function(event) {
keyDown(doomKeyCode(event.keyCode));
event.preventDefault();
}, false);
canvas.addEventListener('keyup', function(event) {
keyUp(doomKeyCode(event.keyCode));
event.preventDefault();
}, false);
canvas.focus();
/*Main game loop*/
function step(timestamp) {
obj.instance.exports.doom_loop_step();
window.requestAnimationFrame(step);
}
window.requestAnimationFrame(step);
});