Skip to content

Commit bbe6482

Browse files
authored
Remove unused Rust WASM artifact (#23)
1 parent 887e063 commit bbe6482

File tree

2 files changed

+157
-49
lines changed

2 files changed

+157
-49
lines changed

wasm/game.js

Lines changed: 92 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,90 @@
11
const canvas = document.getElementById('game-canvas');
2-
const context = canvas.getContext('2d');
2+
const context =
3+
canvas.getContext('2d', { alpha: false, desynchronized: true }) ||
4+
canvas.getContext('2d');
35
const fpsDisplay = document.getElementById('fps-counter');
46
let wasmExports = null;
57
let lastTime = null;
68
let fpsFrameCount = 0;
79
let fpsLastTimestamp = 0;
10+
let ballStatePtr = 0;
11+
let ballStateView = null;
12+
const MAX_PIXEL_RATIO = 1.5;
13+
14+
function getBallStateView() {
15+
if (!wasmExports || !wasmExports.memory) {
16+
return null;
17+
}
18+
19+
const memoryBuffer = wasmExports.memory.buffer;
20+
if (!ballStatePtr && typeof wasmExports.get_ball_state_ptr === 'function') {
21+
ballStatePtr = wasmExports.get_ball_state_ptr();
22+
}
23+
24+
if (!ballStatePtr) {
25+
return null;
26+
}
27+
28+
if (
29+
!ballStateView ||
30+
ballStateView.byteOffset !== ballStatePtr ||
31+
ballStateView.buffer !== memoryBuffer
32+
) {
33+
ballStateView = new Float32Array(memoryBuffer, ballStatePtr, 3);
34+
}
35+
36+
return ballStateView;
37+
}
838

939
function resizeCanvas() {
10-
canvas.width = window.innerWidth;
11-
canvas.height = window.innerHeight;
40+
const deviceRatio = window.devicePixelRatio || 1;
41+
const pixelRatio = Math.min(deviceRatio, MAX_PIXEL_RATIO);
42+
const displayWidth = Math.floor(window.innerWidth * pixelRatio);
43+
const displayHeight = Math.floor(window.innerHeight * pixelRatio);
44+
45+
canvas.width = displayWidth;
46+
canvas.height = displayHeight;
47+
canvas.style.width = `${window.innerWidth}px`;
48+
canvas.style.height = `${window.innerHeight}px`;
49+
50+
if (context && typeof context.resetTransform === 'function') {
51+
context.resetTransform();
52+
}
53+
54+
ballStateView = null;
1255

1356
if (wasmExports) {
1457
wasmExports.set_canvas_size(canvas.width, canvas.height);
1558
}
1659
}
1760

1861
function drawFrame() {
19-
if (!wasmExports) {
62+
if (!wasmExports || !context) {
2063
return;
2164
}
2265

23-
context.clearRect(0, 0, canvas.width, canvas.height);
66+
const state = getBallStateView();
67+
let x;
68+
let y;
69+
let radius;
70+
71+
if (state) {
72+
x = state[0];
73+
y = state[1];
74+
radius = state[2];
75+
} else {
76+
if (typeof wasmExports.get_ball_radius !== 'function') {
77+
return;
78+
}
79+
80+
radius = wasmExports.get_ball_radius();
81+
x = wasmExports.get_ball_x();
82+
y = wasmExports.get_ball_y();
83+
}
84+
2485
context.fillStyle = '#0d47a1';
2586
context.fillRect(0, 0, canvas.width, canvas.height);
2687

27-
const radius = wasmExports.get_ball_radius();
28-
const x = wasmExports.get_ball_x();
29-
const y = wasmExports.get_ball_y();
30-
3188
context.fillStyle = '#ffca28';
3289
context.beginPath();
3390
context.arc(x, y, radius, 0, Math.PI * 2);
@@ -40,10 +97,24 @@ function updateAndRender(timestamp) {
4097
fpsLastTimestamp = timestamp;
4198
}
4299

43-
const deltaSeconds = (timestamp - lastTime) / 1000;
100+
const deltaSecondsRaw = (timestamp - lastTime) / 1000;
101+
const deltaSeconds = Math.min(deltaSecondsRaw, 0.25);
44102
lastTime = timestamp;
45103

46-
wasmExports.update(deltaSeconds);
104+
if (wasmExports) {
105+
let ptr = 0;
106+
if (typeof wasmExports.update_and_get_state === 'function') {
107+
ptr = wasmExports.update_and_get_state(deltaSeconds);
108+
} else if (typeof wasmExports.update === 'function') {
109+
wasmExports.update(deltaSeconds);
110+
}
111+
112+
if (ptr && ptr !== ballStatePtr) {
113+
ballStatePtr = ptr;
114+
ballStateView = null;
115+
}
116+
}
117+
47118
drawFrame();
48119

49120
fpsFrameCount += 1;
@@ -67,10 +138,18 @@ async function start() {
67138
wasmExports = instance.exports;
68139

69140
resizeCanvas();
70-
wasmExports.reset_ball();
141+
142+
if (typeof wasmExports.get_ball_state_ptr === 'function') {
143+
ballStatePtr = wasmExports.get_ball_state_ptr();
144+
}
145+
146+
if (typeof wasmExports.reset_ball === 'function') {
147+
wasmExports.reset_ball();
148+
}
149+
71150
drawFrame();
72151
requestAnimationFrame(updateAndRender);
73152
}
74153

75-
window.addEventListener('resize', resizeCanvas);
154+
window.addEventListener('resize', resizeCanvas, { passive: true });
76155
start();

wasm/hello.c

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,62 @@
11
#include <emscripten/emscripten.h>
2+
#include <stdint.h>
3+
4+
typedef struct BallState {
5+
float x;
6+
float y;
7+
float radius;
8+
} BallState;
29

310
static float canvas_width = 640.0f;
411
static float canvas_height = 480.0f;
5-
static float ball_x = 320.0f;
6-
static float ball_y = 240.0f;
7-
static float ball_radius = 16.0f;
12+
static BallState ball = {320.0f, 240.0f, 16.0f};
813
static float velocity_x = 180.0f;
914
static float velocity_y = 140.0f;
1015

1116
static void clamp_ball_inside_canvas(void)
1217
{
13-
if (ball_x < ball_radius) {
14-
ball_x = ball_radius;
15-
} else if (ball_x > canvas_width - ball_radius) {
16-
ball_x = canvas_width - ball_radius;
18+
if (ball.x < ball.radius) {
19+
ball.x = ball.radius;
20+
} else {
21+
const float max_x = canvas_width - ball.radius;
22+
if (ball.x > max_x) {
23+
ball.x = max_x;
24+
}
25+
}
26+
27+
if (ball.y < ball.radius) {
28+
ball.y = ball.radius;
29+
} else {
30+
const float max_y = canvas_height - ball.radius;
31+
if (ball.y > max_y) {
32+
ball.y = max_y;
33+
}
34+
}
35+
}
36+
37+
static void simulate_ball(float delta_seconds)
38+
{
39+
if (delta_seconds <= 0.0f) {
40+
return;
41+
}
42+
43+
ball.x += velocity_x * delta_seconds;
44+
ball.y += velocity_y * delta_seconds;
45+
46+
if (ball.x - ball.radius < 0.0f) {
47+
ball.x = ball.radius;
48+
velocity_x = -velocity_x;
49+
} else if (ball.x + ball.radius > canvas_width) {
50+
ball.x = canvas_width - ball.radius;
51+
velocity_x = -velocity_x;
1752
}
1853

19-
if (ball_y < ball_radius) {
20-
ball_y = ball_radius;
21-
} else if (ball_y > canvas_height - ball_radius) {
22-
ball_y = canvas_height - ball_radius;
54+
if (ball.y - ball.radius < 0.0f) {
55+
ball.y = ball.radius;
56+
velocity_y = -velocity_y;
57+
} else if (ball.y + ball.radius > canvas_height) {
58+
ball.y = canvas_height - ball.radius;
59+
velocity_y = -velocity_y;
2360
}
2461
}
2562

@@ -40,8 +77,8 @@ void set_canvas_size(int width, int height)
4077
EMSCRIPTEN_KEEPALIVE
4178
void reset_ball(void)
4279
{
43-
ball_x = canvas_width * 0.5f;
44-
ball_y = canvas_height * 0.5f;
80+
ball.x = canvas_width * 0.5f;
81+
ball.y = canvas_height * 0.5f;
4582
velocity_x = 180.0f;
4683
velocity_y = 140.0f;
4784
clamp_ball_inside_canvas();
@@ -50,44 +87,36 @@ void reset_ball(void)
5087
EMSCRIPTEN_KEEPALIVE
5188
void update(float delta_seconds)
5289
{
53-
if (delta_seconds <= 0.0f) {
54-
return;
55-
}
56-
57-
ball_x += velocity_x * delta_seconds;
58-
ball_y += velocity_y * delta_seconds;
90+
simulate_ball(delta_seconds);
91+
}
5992

60-
if (ball_x - ball_radius < 0.0f) {
61-
ball_x = ball_radius;
62-
velocity_x = -velocity_x;
63-
} else if (ball_x + ball_radius > canvas_width) {
64-
ball_x = canvas_width - ball_radius;
65-
velocity_x = -velocity_x;
66-
}
93+
EMSCRIPTEN_KEEPALIVE
94+
uintptr_t update_and_get_state(float delta_seconds)
95+
{
96+
simulate_ball(delta_seconds);
97+
return (uintptr_t)&ball;
98+
}
6799

68-
if (ball_y - ball_radius < 0.0f) {
69-
ball_y = ball_radius;
70-
velocity_y = -velocity_y;
71-
} else if (ball_y + ball_radius > canvas_height) {
72-
ball_y = canvas_height - ball_radius;
73-
velocity_y = -velocity_y;
74-
}
100+
EMSCRIPTEN_KEEPALIVE
101+
uintptr_t get_ball_state_ptr(void)
102+
{
103+
return (uintptr_t)&ball;
75104
}
76105

77106
EMSCRIPTEN_KEEPALIVE
78107
float get_ball_x(void)
79108
{
80-
return ball_x;
109+
return ball.x;
81110
}
82111

83112
EMSCRIPTEN_KEEPALIVE
84113
float get_ball_y(void)
85114
{
86-
return ball_y;
115+
return ball.y;
87116
}
88117

89118
EMSCRIPTEN_KEEPALIVE
90119
float get_ball_radius(void)
91120
{
92-
return ball_radius;
121+
return ball.radius;
93122
}

0 commit comments

Comments
 (0)