Skip to content

Commit

Permalink
[client] Tuck sprite_pool under WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Oct 5, 2024
1 parent d39a32b commit 399fed9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 25 deletions.
23 changes: 10 additions & 13 deletions client.c3
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,9 @@ struct SpritePool {
Sprite*[SPRITE_POOL_CAPACITY] visible_items;
int visible_length;
}
SpritePool sprite_pool;

fn SpritePool *allocate_sprite_pool() @extern("allocate_sprite_pool") @wasm {
return mem::new(SpritePool);
}

fn void reset_sprite_pool(SpritePool *sprite_pool) {
fn void reset_sprite_pool() {
sprite_pool.length = 0;
sprite_pool.visible_length = 0;
}
Expand Down Expand Up @@ -845,21 +842,21 @@ fn void key_up(uint key_code) @extern("key_up") @wasm {
}
}

fn void render_game(Image *display, float *zbuffer, SpritePool *sprite_pool, ParticlePool *particle_pool, Image *key_image, Image *bomb_image, Image *particle_image, Image *wall_image, Image *player_image, float delta_time, float time) @extern("render_game") @wasm {
fn void render_game(Image *display, float *zbuffer, ParticlePool *particle_pool, Image *key_image, Image *bomb_image, Image *particle_image, Image *wall_image, Image *player_image, float delta_time, float time) @extern("render_game") @wasm {
update_all_players(common::scene, delta_time);
update_items(sprite_pool, time, &common::items, key_image, bomb_image);
update_bombs_on_client_side(sprite_pool, particle_pool, bomb_image, common::scene, delta_time, &common::bombs);
update_particles(particle_image, sprite_pool, delta_time, common::scene, particle_pool);
update_items(&sprite_pool, time, &common::items, key_image, bomb_image);
update_bombs_on_client_side(&sprite_pool, particle_pool, bomb_image, common::scene, delta_time, &common::bombs);
update_particles(particle_image, &sprite_pool, delta_time, common::scene, particle_pool);

render_other_players(sprite_pool, player_image);
render_other_players(&sprite_pool, player_image);

render_floor_and_ceiling(display);
render_walls(display, zbuffer, wall_image, common::scene);
cull_and_sort_sprites(sprite_pool);
render_sprites(display, zbuffer, sprite_pool);
cull_and_sort_sprites(&sprite_pool);
render_sprites(display, zbuffer, &sprite_pool);

ping_server_if_needed();
reset_sprite_pool(sprite_pool);
reset_sprite_pool();
common::reset_temp_mark();
}

Expand Down
6 changes: 2 additions & 4 deletions client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ async function instantiateWasmClient(url) {
return {
...wasmCommon,
allocate_zbuffer: wasm.instance.exports.allocate_zbuffer,
allocate_sprite_pool: wasm.instance.exports.allocate_sprite_pool,
render_minimap: wasm.instance.exports.render_minimap,
allocate_particle_pool: wasm.instance.exports.allocate_particle_pool,
allocate_image: wasm.instance.exports.allocate_image,
Expand Down Expand Up @@ -198,13 +197,12 @@ async function createGame() {
bombBlastSound,
};
const particlesPtr = wasmClient.allocate_particle_pool();
const spritePoolPtr = wasmClient.allocate_sprite_pool();
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const ws = new WebSocket(`${protocol}//${window.location.hostname}:${SERVER_PORT}`);
if (window.location.hostname === 'tsoding.github.io')
ws.close();
const display = createDisplay(wasmClient, SCREEN_WIDTH, SCREEN_HEIGHT);
const game = { ws, particlesPtr, assets, spritePoolPtr, dts: [], wasmClient, display };
const game = { ws, particlesPtr, assets, dts: [], wasmClient, display };
ws.binaryType = 'arraybuffer';
ws.addEventListener("close", (event) => {
console.log("WEBSOCKET CLOSE", event);
Expand Down Expand Up @@ -245,7 +243,7 @@ async function createGame() {
const deltaTime = (timestamp - prevTimestamp) / 1000;
const time = timestamp / 1000;
prevTimestamp = timestamp;
game.wasmClient.render_game(game.display.backImagePtr, game.display.zBufferPtr, game.spritePoolPtr, game.particlesPtr, game.assets.keyImagePtr, game.assets.bombImagePtr, game.assets.particleImagePtr, game.assets.wallImagePtr, game.assets.playerImagePtr, deltaTime, time);
game.wasmClient.render_game(game.display.backImagePtr, game.display.zBufferPtr, game.particlesPtr, game.assets.keyImagePtr, game.assets.bombImagePtr, game.assets.particleImagePtr, game.assets.wallImagePtr, game.assets.playerImagePtr, deltaTime, time);
displaySwapBackImageData(game.display, game.wasmClient);
renderDebugInfo(game.display.ctx, deltaTime, game);
window.requestAnimationFrame(frame);
Expand Down
12 changes: 4 additions & 8 deletions client.mts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ interface Display {

interface WasmClient extends common.WasmCommon {
allocate_zbuffer: (width: number) => number,
allocate_sprite_pool: () => number,
render_minimap: (display: number, sprite_pool: number) => void;
allocate_particle_pool: () => number,
allocate_image: (width: number, height: number) => number,
Expand All @@ -68,7 +67,7 @@ interface WasmClient extends common.WasmCommon {
key_down: (key_code: number) => void,
key_up: (key_code: number) => void,
// TODO: render_game() should be actually called something like tick() cause that's what it is
render_game: (display: number, zbuffer: number, sprite_pool: number, particle_pool: number, key_image: number, bomb_image: number, particle_image: number, wall_image: number, player_image: number, delta_time: number, time: number) => void,
render_game: (display: number, zbuffer: number, particle_pool: number, key_image: number, bomb_image: number, particle_image: number, wall_image: number, player_image: number, delta_time: number, time: number) => void,
ping_msecs: () => number,
process_message: (message: number, particle_pool: number) => boolean,
}
Expand Down Expand Up @@ -120,7 +119,6 @@ interface Assets {

interface Game {
ws: WebSocket,
spritePoolPtr: number,
particlesPtr: number,
assets: Assets,
dts: number[],
Expand Down Expand Up @@ -216,7 +214,6 @@ async function instantiateWasmClient(url: string): Promise<WasmClient> {
return {
...wasmCommon,
allocate_zbuffer: wasm.instance.exports.allocate_zbuffer as (width: number) => number,
allocate_sprite_pool: wasm.instance.exports.allocate_sprite_pool as () => number,
render_minimap: wasm.instance.exports.render_minimap as (display: number, sprite_pool: number) => void,
allocate_particle_pool: wasm.instance.exports.allocate_particle_pool as () => number,
allocate_image: wasm.instance.exports.allocate_image as (width: number, height: number) => number,
Expand All @@ -227,7 +224,7 @@ async function instantiateWasmClient(url: string): Promise<WasmClient> {
unregister_all_other_players: wasm.instance.exports.unregister_all_other_players as () => void,
key_down: wasm.instance.exports.key_down as (key_code: number) => void,
key_up: wasm.instance.exports.key_up as (key_code: number) => void,
render_game: wasm.instance.exports.render_game as (display: number, zbuffer: number, sprite_pool: number, particle_pool: number, key_image: number, bomb_image: number, particle_image: number, wall_image: number, player_image: number, delta_time: number, time: number) => void,
render_game: wasm.instance.exports.render_game as (display: number, zbuffer: number, particle_pool: number, key_image: number, bomb_image: number, particle_image: number, wall_image: number, player_image: number, delta_time: number, time: number) => void,
ping_msecs: wasm.instance.exports.ping_msecs as () => number,
process_message: wasm.instance.exports.process_message as (message: number) => boolean,
};
Expand Down Expand Up @@ -268,7 +265,6 @@ async function createGame(): Promise<Game> {
}

const particlesPtr = wasmClient.allocate_particle_pool();
const spritePoolPtr = wasmClient.allocate_sprite_pool();

const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const ws = new WebSocket(`${protocol}//${window.location.hostname}:${SERVER_PORT}`);
Expand All @@ -282,7 +278,7 @@ async function createGame(): Promise<Game> {
// tsoding.github.io we just instantly close the connection.
if (window.location.hostname === 'tsoding.github.io') ws.close();
const display = createDisplay(wasmClient, SCREEN_WIDTH, SCREEN_HEIGHT);
const game: Game = {ws, particlesPtr, assets, spritePoolPtr, dts: [], wasmClient, display};
const game: Game = {ws, particlesPtr, assets, dts: [], wasmClient, display};

ws.binaryType = 'arraybuffer';
ws.addEventListener("close", (event) => {
Expand Down Expand Up @@ -330,7 +326,7 @@ async function createGame(): Promise<Game> {
const time = timestamp/1000;
prevTimestamp = timestamp;

game.wasmClient.render_game(game.display.backImagePtr, game.display.zBufferPtr, game.spritePoolPtr, game.particlesPtr, game.assets.keyImagePtr, game.assets.bombImagePtr, game.assets.particleImagePtr, game.assets.wallImagePtr, game.assets.playerImagePtr, deltaTime, time);
game.wasmClient.render_game(game.display.backImagePtr, game.display.zBufferPtr, game.particlesPtr, game.assets.keyImagePtr, game.assets.bombImagePtr, game.assets.particleImagePtr, game.assets.wallImagePtr, game.assets.playerImagePtr, deltaTime, time);

displaySwapBackImageData(game.display, game.wasmClient);
renderDebugInfo(game.display.ctx, deltaTime, game);
Expand Down
Binary file modified client.wasm
Binary file not shown.

0 comments on commit 399fed9

Please sign in to comment.