-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.html
More file actions
189 lines (168 loc) · 5.16 KB
/
index.html
File metadata and controls
189 lines (168 loc) · 5.16 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>ZigZag</title>
</head>
<body>
<p>The buttons are a, s and the arrow keys.</p>
<canvas id="canvas" width="512" height="512"></canvas>
<script>
class FPSCounter {
init() {
this.last = 0;
}
tick() {
const now = Date.now();
const diff = now - this.last;
this.last = now;
return diff;
}
}
const newInputState = () => ({
a_pressed: false,
b_pressed: false,
up_pressed: false,
down_pressed: false,
left_pressed: false,
right_pressed: false,
});
(async () => {
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.imageSmoothingEnabled = false;
ctx.scale(4, 4);
const img = await new Promise((resolve) => {
const img = new Image(); // Create new img element
img.src = "assets/sprites.png"; // Set source path
img.addEventListener(
"load",
() => {
resolve(img);
},
false
);
});
const wasmPromise = new Promise((resolve) => {
const importObject = {
module: {},
env: {
memory: new WebAssembly.Memory({ initial: 256 }),
consoleLog: () => {},
wasmSprite: (dx, dy, dw, dh, sx, sy, sw, sh) => {
// Don't draw the image if its off screen.
ctx.drawImage(img, sx, sy, sw, sh, dx, dy, dw, dh);
},
},
};
fetch("zig-out/lib/wasm.wasm")
.then((response) => response.arrayBuffer())
.then((bytes) => WebAssembly.instantiate(bytes, importObject))
.then((results) => {
resolve(results);
});
});
const wasm = await wasmPromise;
const exports = wasm.instance.exports;
exports.setup();
// Setup the keyboard
const inputState = {
a_pressed: false,
b_pressed: false,
up_pressed: false,
down_pressed: false,
left_pressed: false,
right_pressed: false,
a_down: false,
b_down: false,
up_down: false,
down_down: false,
left_down: false,
right_down: false,
};
document.addEventListener("keydown", (e) => {
if (e.key === "a") {
inputState.a_pressed = true;
inputState.a_down = true;
}
if (e.key === "s") {
inputState.b_pressed = true;
inputState.b_down = true;
}
if (e.key === "ArrowDown") {
inputState.down_pressed = true;
inputState.down_down = true;
}
if (e.key === "ArrowUp") {
inputState.up_pressed = true;
inputState.up_down = true;
}
if (e.key === "ArrowLeft") {
inputState.left_pressed = true;
inputState.left_down = true;
}
if (e.key === "ArrowRight") {
inputState.right_pressed = true;
inputState.right_down = true;
}
});
document.addEventListener("keyup", (e) => {
if (e.key === "a") {
inputState.a_down = false;
}
if (e.key === "s") {
inputState.b_down = false;
}
if (e.key === "ArrowDown") {
inputState.down_down = false;
}
if (e.key === "ArrowUp") {
inputState.up_down = false;
}
if (e.key === "ArrowLeft") {
inputState.left_down = false;
}
if (e.key === "ArrowRight") {
inputState.right_down = false;
}
});
const setup = wasm.instance.exports.setup;
setup();
const fps = new FPSCounter();
const mainLoop = () => {
fps.tick();
const before = Date.now();
// clear the screen
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Let's figure out how long it takes to run loop.
exports.loop(
inputState.right_down,
inputState.right_pressed,
inputState.up_down,
inputState.up_pressed,
inputState.down_down,
inputState.down_pressed,
inputState.left_down,
inputState.left_pressed,
inputState.a_down,
inputState.a_pressed,
inputState.b_down,
inputState.b_pressed
);
inputState.a_pressed = false;
inputState.b_pressed = false;
inputState.up_pressed = false;
inputState.down_pressed = false;
inputState.left_pressed = false;
inputState.right_pressed = false;
const after = Date.now();
// Attempt to loop at 30fps.
// TODO: Come up with a better scheme for this.
const wait = Math.max(0, 33.0 - (after - before));
setTimeout(mainLoop, wait);
};
mainLoop();
})();
</script>
</body>
</html>