diff --git a/client.c3 b/client.c3 index bbb2470..73a3122 100644 --- a/client.c3 +++ b/client.c3 @@ -33,9 +33,9 @@ enum AssetSound { } // TODO: Use std::math::random instead (when you finally figure out how to use it) -extern fn float js_random(); -extern fn void play_sound(AssetSound sound, float player_position_x, float player_position_y, float object_position_x, float object_position_y); -extern fn bool is_offline_mode(); +extern fn float platform_random(); +extern fn void platform_play_sound(AssetSound sound, float player_position_x, float player_position_y, float object_position_x, float object_position_y); +extern fn bool platform_is_offline_mode(); struct Image { Color *pixels; @@ -483,12 +483,12 @@ fn void emit_particle(float source_x, float source_y, float source_z, ParticlePo particle.position.y = source_y; particle.position_z = source_z; - float angle = js_random()*2.0f*(float)math::PI; + float angle = platform_random()*2.0f*(float)math::PI; particle.velocity.x = math::cos(angle); particle.velocity.y = math::sin(angle); - particle.velocity_z = js_random()*0.5f + 0.5f; + particle.velocity_z = platform_random()*0.5f + 0.5f; - float velocity_mag = PARTICLE_MAX_SPEED*js_random(); + float velocity_mag = PARTICLE_MAX_SPEED*platform_random(); particle.velocity *= velocity_mag; particle.velocity_z *= velocity_mag; break; @@ -566,7 +566,7 @@ fn bool apply_items_collected_batch_message_to_level_items(ItemsCollectedBatchMe if (item.alive) { item.alive = false; - play_sound(ITEM_PICKUP, player_position_x, player_position_y, item.position.x, item.position.y); + platform_play_sound(ITEM_PICKUP, player_position_x, player_position_y, item.position.x, item.position.y); } } @@ -628,13 +628,13 @@ fn void render_items(SpritePool *sprite_pool, Item[]* items, float time, fn void update_items_offline(Item[]* items, float player_position_x, float player_position_y) @extern("update_items_offline") @wasm { foreach (item_index, item: *items) { if (common::collect_item(player_position_x, player_position_y, items, item_index)) { - play_sound(ITEM_PICKUP, player_position_x, player_position_y, item.position.x, item.position.y); + platform_play_sound(ITEM_PICKUP, player_position_x, player_position_y, item.position.x, item.position.y); } } } fn void explode_bomb(float bomb_position_x, float bomb_position_y, float bomb_position_z, float player_position_x, float player_position_y, ParticlePool *particle_pool) { - play_sound(BOMB_BLAST, player_position_x, player_position_y, bomb_position_x, bomb_position_y); + platform_play_sound(BOMB_BLAST, player_position_x, player_position_y, bomb_position_x, bomb_position_y); for (int i = 0; i < BOMB_PARTICLE_COUNT; ++i) { emit_particle(bomb_position_x, bomb_position_y, bomb_position_z, particle_pool); } @@ -646,10 +646,10 @@ fn void update_bombs_on_client_side(SpritePool *sprite_pool, ParticlePool *parti push_sprite(sprite_pool, bomb_image_pixels, bomb_image_width, bomb_image_height, bomb.position.x, bomb.position.y, bomb.position_z, common::BOMB_SCALE, 0, 0, bomb_image_width, bomb_image_height); if (common::update_bomb(bomb, scene, delta_time)) { - play_sound(BOMB_RICOCHET, player_position_x, player_position_y, bomb.position.x, bomb.position.y); + platform_play_sound(BOMB_RICOCHET, player_position_x, player_position_y, bomb.position.x, bomb.position.y); } - if (is_offline_mode() && bomb.lifetime <= 0) { + if (platform_is_offline_mode() && bomb.lifetime <= 0) { explode_bomb(bomb.position.x, bomb.position.y, bomb.position_z, player_position_x, player_position_y, particle_pool); } } @@ -718,7 +718,7 @@ fn void entry() @init(2048) @private { // message. But since js_write() in server.mts is implemented as a // single console.log(), that newline is added implicitly anyway. os::native_fwrite_fn = fn usz!(void* f, char[] buffer) { - common::js_write(&buffer[0], buffer.len); + common::platform_write(&buffer[0], buffer.len); return buffer.len; }; common::temp_mark = allocator::temp().used; diff --git a/client.mjs b/client.mjs index 1c8f261..b655984 100644 --- a/client.mjs +++ b/client.mjs @@ -154,12 +154,12 @@ async function instantiateWasmClient(url) { "fmodf": (x, y) => x % y, "fminf": Math.min, "fmaxf": Math.max, - "js_random": Math.random, - "js_write": (buffer, buffer_len) => { + "platform_random": Math.random, + "platform_write": (buffer, buffer_len) => { console.log(new TextDecoder().decode(new Uint8ClampedArray(game.wasmClient.memory.buffer, buffer, buffer_len))); }, - "is_offline_mode": () => game.ws.readyState != WebSocket.OPEN, - "play_sound": (sound, player_position_x, player_position_y, object_position_x, object_position_y) => { + "platform_is_offline_mode": () => game.ws.readyState != WebSocket.OPEN, + "platform_play_sound": (sound, player_position_x, player_position_y, object_position_x, object_position_y) => { const maxVolume = 1; const objectPosition = new Vector2(object_position_x, object_position_y); const playerPosition = new Vector2(player_position_x, player_position_y); diff --git a/client.mts b/client.mts index be46c88..dc086fa 100644 --- a/client.mts +++ b/client.mts @@ -236,20 +236,19 @@ enum AssetSound { async function instantiateWasmClient(url: string): Promise { const wasm = await WebAssembly.instantiateStreaming(fetch(url), { - // TODO: add js_write "env": { "fmodf": (x: number, y: number) => x%y, "fminf": Math.min, "fmaxf": Math.max, - "js_random": Math.random, + "platform_random": Math.random, // NOTE: This implicitly adds newline, but given how we using this // function in client.c3 it's actually fine. This function is called // once per io::printn() anyway. - "js_write": (buffer: number, buffer_len: number) => { + "platform_write": (buffer: number, buffer_len: number) => { console.log(new TextDecoder().decode(new Uint8ClampedArray(game.wasmClient.memory.buffer, buffer, buffer_len))); }, - "is_offline_mode": () => game.ws.readyState != WebSocket.OPEN, - "play_sound": (sound: number, player_position_x: number, player_position_y: number, object_position_x: number, object_position_y: number) => { + "platform_is_offline_mode": () => game.ws.readyState != WebSocket.OPEN, + "platform_play_sound": (sound: number, player_position_x: number, player_position_y: number, object_position_x: number, object_position_y: number) => { const maxVolume = 1; const objectPosition = new Vector2(object_position_x, object_position_y); const playerPosition = new Vector2(player_position_x, player_position_y); diff --git a/client.wasm b/client.wasm index b9acff3..d58c408 100755 Binary files a/client.wasm and b/client.wasm differ diff --git a/common.c3 b/common.c3 index 3b42e49..27c4bf2 100644 --- a/common.c3 +++ b/common.c3 @@ -9,7 +9,7 @@ const float BOMB_DAMP = 0.8; const float BOMB_SCALE = 0.25; const float PLAYER_RADIUS = 0.5; -extern fn void js_write(void *buffer, usz buffer_sz); +extern fn void platform_write(void *buffer, usz buffer_sz); def Vector2 = float[<2>]; def IVector2 = int[<2>]; diff --git a/server.c3 b/server.c3 index 2720fc3..bb0da83 100644 --- a/server.c3 +++ b/server.c3 @@ -6,7 +6,7 @@ import common; /// Stats ////////////////////////////// -extern fn int js_now_secs(); +extern fn int platform_now_secs(); const usz AVERAGE_CAPACITY = 30; def Samples = RingBuffer(); @@ -141,7 +141,7 @@ fn String get_stat(Stat *stat) { switch (stat.kind) { case COUNTER: return string::tformat("%d", stat.counter.value); case AVERAGE: return string::tformat("%f", stat.average.samples.average()); - case TIMER: return display_time_interval(js_now_secs() - stat.timer.started_at); + case TIMER: return display_time_interval(platform_now_secs() - stat.timer.started_at); } } @@ -297,9 +297,9 @@ fn void entry() @init(2048) @private { // message. But since js_write() in server.mts is implemented as a // single console.log(), that newline is added implicitly anyway. os::native_fwrite_fn = fn usz!(void* f, char[] buffer) { - common::js_write(&buffer[0], buffer.len); + common::platform_write(&buffer[0], buffer.len); return buffer.len; }; - stats[StatEntry.UPTIME].timer.started_at = js_now_secs(); + stats[StatEntry.UPTIME].timer.started_at = platform_now_secs(); common::temp_mark = allocator::temp().used; } diff --git a/server.mjs b/server.mjs index c5a3fb0..f5821a9 100644 --- a/server.mjs +++ b/server.mjs @@ -332,15 +332,15 @@ function tick() { wasmServer.reset_temp_mark(); setTimeout(tick, Math.max(0, 1000 / SERVER_FPS - tickTime)); } -function js_now_secs() { +function platform_now_secs() { return Math.floor(Date.now() / 1000); } -function js_write(buffer, buffer_len) { +function platform_write(buffer, buffer_len) { console.log(new TextDecoder().decode(new Uint8ClampedArray(wasmServer.memory.buffer, buffer, buffer_len))); } async function instantiateWasmServer(path) { const wasm = await WebAssembly.instantiate(readFileSync(path), { - "env": { js_now_secs, js_write }, + "env": { platform_now_secs, platform_write }, }); const wasmCommon = common.makeWasmCommon(wasm); wasmCommon._initialize(); diff --git a/server.mts b/server.mts index fb7f78b..cdce0d5 100644 --- a/server.mts +++ b/server.mts @@ -406,20 +406,20 @@ interface WasmServer extends common.WasmCommon { exploded_bombs_as_batch_message: (exploded_bombs: number, bombs: number) => number, } -function js_now_secs(): number { +function platform_now_secs(): number { return Math.floor(Date.now()/1000); } // NOTE: This implicitly adds newline, but given how we using this // function in server.c3 it's actually fine. This function is called // once per io::printn() anyway. -function js_write(buffer: number, buffer_len: number) { +function platform_write(buffer: number, buffer_len: number) { console.log(new TextDecoder().decode(new Uint8ClampedArray(wasmServer.memory.buffer, buffer, buffer_len))); } async function instantiateWasmServer(path: string): Promise { const wasm = await WebAssembly.instantiate(readFileSync(path), { - "env": {js_now_secs, js_write}, + "env": {platform_now_secs, platform_write}, }); const wasmCommon = common.makeWasmCommon(wasm); wasmCommon._initialize(); diff --git a/server.wasm b/server.wasm index 3ca0292..44b5284 100755 Binary files a/server.wasm and b/server.wasm differ