Skip to content

Commit

Permalink
Prefix all the extern platform functions with platform_
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Sep 27, 2024
1 parent bfd388d commit 728fcbd
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 32 deletions.
24 changes: 12 additions & 12 deletions client.c3
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 4 additions & 5 deletions client.mts
Original file line number Diff line number Diff line change
Expand Up @@ -236,20 +236,19 @@ enum AssetSound {

async function instantiateWasmClient(url: string): Promise<WasmClient> {
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);
Expand Down
Binary file modified client.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion common.c3
Original file line number Diff line number Diff line change
Expand Up @@ -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>];
Expand Down
8 changes: 4 additions & 4 deletions server.c3
Original file line number Diff line number Diff line change
Expand Up @@ -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(<float, AVERAGE_CAPACITY>);
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
}
6 changes: 3 additions & 3 deletions server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions server.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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<WasmServer> {
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();
Expand Down
Binary file modified server.wasm
Binary file not shown.

0 comments on commit 728fcbd

Please sign in to comment.