-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
214 lines (165 loc) · 6.16 KB
/
script.js
File metadata and controls
214 lines (165 loc) · 6.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import init, { key_pressed, update_speed, get_fps } from './out/game_engine.js';
const canvas = document.getElementById("my_canvas");
const gl = canvas.getContext("webgl");
if (!gl) {
console.error("WebGL is not supported");
} else {
const vertexShaderSource = `
attribute vec2 a_position; // Input vertex positions
void main() {
gl_Position = vec4(a_position, 0, 1); // Map to clip space
}
`;
const fragmentShaderSource = `
precision mediump float;
uniform vec4 u_color; // Input color
void main() {
gl_FragColor = u_color; // Set fragment color
}
`;
const createShader = (type, source) => {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
console.error("Shader compilation error:", gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
return shader;
};
const vertexShader = createShader(gl.VERTEX_SHADER, vertexShaderSource);
const fragmentShader = createShader(gl.FRAGMENT_SHADER, fragmentShaderSource);
const program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
console.error("Program linking error:", gl.getProgramInfoLog(program));
gl.deleteProgram(program);
} else {
gl.program = program;
gl.useProgram(program);
const resolutionUniformLocation = gl.getUniformLocation(program, "u_resolution");
gl.uniform2f(resolutionUniformLocation, canvas.width, canvas.height);
}
}
window.addEventListener("keydown", (event) => {
let keyCode = 0;
switch (event.key) {
case "ArrowUp": keyCode = 3; break;
case "ArrowDown": keyCode = 4; break;
case "ArrowLeft": keyCode = 1; break;
case "ArrowRight": keyCode = 2; break;
case " ": keyCode = 5; break;
}
if (keyCode !== 0) {
key_pressed(keyCode);
}
});
function change_screen_color(red, green, blue, alpha) {
const canvas = document.getElementById("my_canvas");
const gl = canvas.getContext("webgl");
if (!gl) {
console.error("WebGL is not supported");
return;
}
gl.clearColor(red, green, blue, alpha);
gl.clear(gl.COLOR_BUFFER_BIT);
}
window.change_screen_color = change_screen_color;
function js_draw_rectangle(x, y, width, height, red, green, blue, alpha) {
const canvas = document.getElementById("my_canvas");
const gl = canvas.getContext("webgl");
if (!gl) {
console.error("WebGL not supported");
return;
}
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const toClipSpace = (coord, size) => (coord / size) * 2 - 1;
const x1 = toClipSpace(x, canvasWidth);
const y1 = toClipSpace(canvasHeight - (y + height), canvasHeight);
const x2 = toClipSpace(x + width, canvasWidth);
const y2 = toClipSpace(canvasHeight - y, canvasHeight);
// console.log(`Drawing rect at (${x}, ${y}) -> NDC: (${x1}, ${y1}), (${x2}, ${y2})`);
const vertices = new Float32Array([
x1, y1,
x2, y1,
x1, y2,
x1, y2,
x2, y1,
x2, y2
]);
if (!gl.program) {
console.error("WebGL program not initialized");
return;
}
gl.useProgram(gl.program);
const positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
const positionAttributeLocation = gl.getAttribLocation(gl.program, "a_position");
gl.enableVertexAttribArray(positionAttributeLocation);
gl.vertexAttribPointer(positionAttributeLocation, 2, gl.FLOAT, false, 0, 0);
const colorUniformLocation = gl.getUniformLocation(gl.program, "u_color");
gl.uniform4f(colorUniformLocation, red, green, blue, alpha);
gl.drawArrays(gl.TRIANGLES, 0, 6);
}
window.js_draw_rectangle = js_draw_rectangle;
function update_score(number) {
// console.log("Score:", number);
const scoreElement = document.getElementById("score");
if (scoreElement) {
scoreElement.textContent = `Score: ${number}`;
}
}
window.update_score = update_score;
window.js_draw_text = (text, x, y, size, r, g, b, a) => {
const ctx = canvas.getContext("2d");
ctx.font = `${size}px Arial`;
ctx.fillStyle = `rgba(${r * 255}, ${g * 255}, ${b * 255}, ${a})`;
ctx.fillText(text, x, y);
};
window.js_draw_sprite = (x, y, width, height, src) => {
const img = new Image();
img.src = src;
img.onload = () => {
gl.drawImage(img, x, y, width, height);
};
};
window.play_sound = (src) => {
const audio = new Audio(src);
audio.play();
};
init().then(() => {
console.log("WASM initialized");
// js_draw_rectangle(100, 100, 50, 50, 1.0, 0.0, 0.0, 1.0);
// js_draw_rectangle(200, 200, 50, 50, 0.0, 1.0, 0.0, 1.0);
function updateFPSDisplay() {
const fpsElement = document.getElementById("fps");
if (fpsElement) {
const fps = get_fps();
fpsElement.textContent = `FPS: ${fps.toFixed(2)}`;
}
}
setInterval(updateFPSDisplay, 250)
function render() {
// console.log("Batched Rectangles:", window.batched_rectangles);
if (window.batched_rectangles) {
window.batched_rectangles.forEach(rect => {
const { x, y, width, height, r, g, b, a } = rect;
js_draw_rectangle(x, y, width, height, r, g, b, a)
});
}
requestAnimationFrame(render);
}
requestAnimationFrame(render);
});
document.getElementById("speed_btn").addEventListener("click", (event) => {
const currentSpeed = parseFloat(event.target.getAttribute("data-speed"));
update_speed(currentSpeed);
const newSpeed = currentSpeed === 0.5 ? 1.0 : 0.5;
event.target.setAttribute("data-speed", newSpeed);
event.target.textContent = newSpeed === 0.5 ? "Increase Speed" : "Normal Speed";
});