-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.html
236 lines (206 loc) · 6.99 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=0" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<title>Zero Man</title>
<link rel="icon" type="image/x-icon" href="img/favicon.ico">
<style type="text/css">
* {
margin: 0;
padding: 0;
touch-action: none;
user-select: none;
-webkit-user-select: none;
}
body {
width: 100vw;
height: 100vh;
overflow: hidden;
background-color: black;
}
canvas {
display: block;
width: 100%;
height: 100%;
object-position: top;
object-fit: contain;
image-rendering: pixelated;
}
.touchcontrol {
display: none;
}
.touchcontrol-enabled {
display: block;
}
</style>
</head>
<body>
<canvas id="canvasgl" width="256" height="240"></canvas>
<svg class="touchcontrol" style="position:absolute;height:min(45vw,45vh);bottom:5%;left:5%;" viewBox="0 0 30 30">
<path d="M0 10 V20 H9 L14 15 9 10 Z" fill="#fff4" onpointerenter="pressedKeys[37]=true"
onpointerleave="pressedKeys[37]=false" />
<path d="M20 0 H10 V9 L15 14 20 9 Z" fill="#fff4" onpointerenter="pressedKeys[38]=true"
onpointerleave="pressedKeys[38]=false" />
<path d="M30 20 V10 H21 L16 15 21 20 Z" fill="#fff4" onpointerenter="pressedKeys[39]=true"
onpointerleave="pressedKeys[39]=false" />
<path d="M10 30 H20 V21 L15 16 10 21 Z" fill="#fff4" onpointerenter="pressedKeys[40]=true"
onpointerleave="pressedKeys[40]=false" />
</svg>
<svg class="touchcontrol" style="position:absolute;height:min(45vw,45vh);bottom:5%;right:5%;" viewBox="0 0 30 30">
<circle cx="10" cy="22" r="7" fill="#fff4" onpointerenter="pressedKeys[16]=true"
onpointerleave="pressedKeys[16]=false" />
<circle cx="22" cy="8" r="7" fill="#fff4" onpointerenter="pressedKeys[32]=true"
onpointerleave="pressedKeys[32]=false" />
</svg>
<script>
var $canvasgl = document.getElementById("canvasgl");
</script>
<!-- <script src="js/dom.js"></script> -->
<!-- <script src="js/audio.js"></script> -->
<!-- <script src="js/canvas.js"></script> -->
<script src="js/webgl.js"></script>
<!-- <script src="js/wasm.js"></script> -->
<script>
document.ondblclick = function(e) {
e.preventDefault();
}
let logString = '';
const jsLogWrite = (ptr, len) => {
logString += readCharStr(ptr, len)
}
const jsLogFlush = () => {
console.log(logString)
logString = ''
}
function jsStorageSetString(keyPtr, keyLen, valuePtr, valueLen) {
const key = readCharStr(keyPtr, keyLen);
const value = readCharStr(valuePtr, valueLen);
localStorage.setItem(key, value);
}
function jsStorageGetString(keyPtr, keyLen, valuePtr, valueLen) {
const key = readCharStr(keyPtr, keyLen);
const value = localStorage.getItem(key);
return writeCharStr(value, valuePtr, valueLen);
}
function hasLoadSnapshot() {
const params = new URLSearchParams(location.search);
return params.has('loadSnapshot');
}
const readCharStr = (ptr, len) => {
const array = new Uint8Array(memory.buffer, ptr, len);
const decoder = new TextDecoder();
return decoder.decode(array);
}
const writeCharStr = (str, ptr, len) => {
const dest = new Uint8Array(memory.buffer, ptr, len)
const encoder = new TextEncoder()
const array = encoder.encode(str);
len = Math.min(array.length, len);
for (let i = 0; i < len; i++) {
dest[i] = array[i];
}
return len;
}
const env = {
// ...wasm,
// ...audio,
// ...canvas,
// ...zigdom,
...webgl,
jsLogWrite,
jsLogFlush,
jsStorageSetString,
jsStorageGetString,
hasLoadSnapshot,
isKeyDown,
isButtonDown,
}
let pressedKeys = {};
function isKeyDown(keyCode) {
return pressedKeys[keyCode] == true;
}
function isButtonDown(buttonIndex) {
const gamepads = navigator.getGamepads ? navigator.getGamepads() : [];
if (gamepads.length > 0) {
const gamepad = gamepads[0];
if (gamepad) {
if (buttonIndex < gamepad.buttons.length) {
return gamepad.buttons[buttonIndex].pressed;
}
}
}
return false;
}
fetchAndInstantiate('zig-out/bin/main.wasm', { env }).then(instance => {
memory = instance.exports.memory;
instance.exports.onInit();
const onAnimationFrame = instance.exports.onAnimationFrame;
function onKeyDown(e) {
// e.repeat
disableTouchControls();
pressedKeys[e.keyCode] = true;
instance.exports.onKeyDown(e.keyCode);
}
function onKeyUp(e) {
pressedKeys[e.keyCode] = false;
}
function onBeforeUnload(e) {
instance.exports.onBeforeUnload();
}
document.addEventListener("touchstart", e => e.preventDefault());
document.addEventListener("touchend", e => e.preventDefault());
document.addEventListener('keydown', onKeyDown);
document.addEventListener('keyup', onKeyUp);
addEventListener("beforeunload", onBeforeUnload);
function step(timestamp) {
onAnimationFrame(timestamp);
window.requestAnimationFrame(step);
}
window.requestAnimationFrame(step);
});
function fetchAndInstantiate(url, importObject) {
return fetch(url)
.then(response => {
const contentType = response.headers.get("content-type");
if (contentType == "application/wasm" || contentType == "application/octet-stream")
return response.arrayBuffer();
response.text().then(text => {
var html = '<h1>Fetch ' + url + ' failed:</h1>';
html += '<pre>' + text + '</pre>';
document.body.innerHTML = html;
});
throw Error("failed to fetch wasm file");
})
.then(bytes => WebAssembly.instantiate(bytes, importObject))
.then(results => results.instance);
}
let touchControlsEnabled = false;
function enableTouchControls() {
if (touchControlsEnabled) return;
touchControlsEnabled = true;
const touchControls = document.querySelectorAll('.touchcontrol');
for (const touchControl of touchControls) {
touchControl.classList.add('touchcontrol-enabled');
}
}
function disableTouchControls() {
if (!touchControlsEnabled) return;
touchControlsEnabled = false;
const touchControls = document.querySelectorAll('.touchcontrol');
for (const touchControl of touchControls) {
touchControl.classList.remove('touchcontrol-enabled');
}
}
document.addEventListener("touchstart", function (e) {
e.preventDefault();
enableTouchControls();
}, false);
document.addEventListener("contextmenu", function (e) {
e.preventDefault();
}, false);
</script>
</body>
</html>