From 399fed9241bdcf18768072eab3f631c939de7a1f Mon Sep 17 00:00:00 2001 From: rexim Date: Sun, 6 Oct 2024 05:24:11 +0700 Subject: [PATCH] [client] Tuck sprite_pool under WASM --- client.c3 | 23 ++++++++++------------- client.mjs | 6 ++---- client.mts | 12 ++++-------- client.wasm | Bin 71313 -> 71153 bytes 4 files changed, 16 insertions(+), 25 deletions(-) diff --git a/client.c3 b/client.c3 index 7612134..3cd24b7 100644 --- a/client.c3 +++ b/client.c3 @@ -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; } @@ -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(); } diff --git a/client.mjs b/client.mjs index 2ad734c..2577c9e 100644 --- a/client.mjs +++ b/client.mjs @@ -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, @@ -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); @@ -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); diff --git a/client.mts b/client.mts index 63d5cae..9a279a4 100644 --- a/client.mts +++ b/client.mts @@ -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, @@ -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, } @@ -120,7 +119,6 @@ interface Assets { interface Game { ws: WebSocket, - spritePoolPtr: number, particlesPtr: number, assets: Assets, dts: number[], @@ -216,7 +214,6 @@ async function instantiateWasmClient(url: string): Promise { 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, @@ -227,7 +224,7 @@ async function instantiateWasmClient(url: string): Promise { 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, }; @@ -268,7 +265,6 @@ async function createGame(): Promise { } 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}`); @@ -282,7 +278,7 @@ async function createGame(): Promise { // 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) => { @@ -330,7 +326,7 @@ async function createGame(): Promise { 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); diff --git a/client.wasm b/client.wasm index 4669684fccd016ba9e2e0ddbac1df4d04357a1f0..02a11c54d0ca5a90c5ad5409bdd607a310f2ce3f 100755 GIT binary patch delta 6050 zcma(#33L=ywsotjm-LcULOL4|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