-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.js
174 lines (146 loc) · 3.8 KB
/
snake.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
'use strict';
let application = document.getElementById("application");
let context = application.getContext("2d");
let wasm = null;
function string_length(memory, start_address)
{
let length = 0;
let address = start_address;
while (memory[address] != 0)
{
++length;
++address;
}
return length;
}
function color_to_hex(color)
{
const r = (color >> 24) & 0xFF;
const g = (color >> 16) & 0xFF;
const b = (color >> 8) & 0xFF;
const a = (color >> 0) & 0xFF;
return "#" +
r.toString(16).padStart(2, '0') +
g.toString(16).padStart(2, '0') +
b.toString(16).padStart(2, '0') +
a.toString(16).padStart(2, '0');
}
function convert_to_string(buffer, string_address)
{
const memory = new Uint8Array(buffer);
const length = string_length(memory, string_address);
const bytes = new Uint8Array(buffer, string_address, length);
return new TextDecoder().decode(bytes);
}
function platform_throw_error(string_address)
{
const buffer = wasm.instance.exports.memory.buffer;
const string = convert_to_string(buffer, string_address);
throw new Error(string);
}
function platform_print_text(string_address)
{
const buffer = wasm.instance.exports.memory.buffer;
const string = convert_to_string(buffer, string_address)
console.log(string);
}
function platform_draw_text(string_address, x, y, size, color, fill, alignment)
{
const buffer = wasm.instance.exports.memory.buffer;
const string = convert_to_string(buffer, string_address)
const align = convert_to_string(buffer, alignment)
context.font = size + "px Iosevka Fixed Web";
context.textAlign = align;
if (fill)
{
context.fillStyle = color_to_hex(color);
context.fillText(string, x, y);
}
else
{
context.strokeStyle = color_to_hex(color);
context.strokeText(string, x, y);
}
}
function platform_draw_number(number, x, y, size, color, fill, alignment)
{
const buffer = wasm.instance.exports.memory.buffer;
const align = convert_to_string(buffer, alignment)
context.font = size + "px Iosevka Fixed Web";
context.textAlign = align;
if (fill)
{
context.fillStyle = color_to_hex(color);
context.fillText(number.toString(10), x, y);
}
else
{
context.strokeStyle = color_to_hex(color);
context.strokeText(number.toString(10), x, y);
}
}
function platform_draw_rectangle(x, y, width, height, color, fill)
{
if (fill)
{
context.fillStyle = color_to_hex(color);
context.fillRect(x, y, width, height);
}
else
{
context.strokeStyle = color_to_hex(color);
context.strokeRect(x, y, width, height);
}
}
let previous_timestamp = null;
function game_loop(timestamp)
{
if (previous_timestamp != null)
{
let delta_time = (timestamp - previous_timestamp) / 1000;
wasm.instance.exports.game_update(delta_time);
wasm.instance.exports.game_render();
}
previous_timestamp = timestamp;
window.requestAnimationFrame(game_loop);
}
WebAssembly.instantiateStreaming(
fetch("./build/snake.wasm"),
{
env:
{
platform_throw_error,
platform_print_text,
platform_draw_text,
platform_draw_number,
platform_draw_rectangle
}
}
).then((w) =>
{
wasm = w;
context.font = "14px Iosevka Fixed Web";
context.textAlign = "center";
context.fillStyle = "#FFFFFF"
context.fillText("", 0, 0);
wasm.instance.exports.game_init(application.width, application.height);
document.addEventListener(
"keydown",
function(event)
{
let key = 0;
if (event.keyCode === 37 || event.keyCode === 38 ||
event.keyCode === 39 || event.keyCode === 40)
{
key = event.keyCode;
}
else
{
key = event.key.charCodeAt();
}
wasm.instance.exports.game_key_down(key);
},
);
window.requestAnimationFrame(game_loop);
}
);