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 4669684..02a11c5 100755 Binary files a/client.wasm and b/client.wasm differ