Skip to content

Commit

Permalink
[client] Tuck some game loop functions under render_game()
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Oct 5, 2024
1 parent 344c140 commit d39a32b
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 103 deletions.
79 changes: 6 additions & 73 deletions client.c3
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn SpritePool *allocate_sprite_pool() @extern("allocate_sprite_pool") @wasm {
return mem::new(SpritePool);
}

fn void reset_sprite_pool(SpritePool *sprite_pool) @extern("reset_sprite_pool") @wasm {
fn void reset_sprite_pool(SpritePool *sprite_pool) {
sprite_pool.length = 0;
sprite_pool.visible_length = 0;
}
Expand Down Expand Up @@ -309,77 +309,6 @@ fn void render_walls(Image *display, float *zbuffer, Image *wall, Scene *scene)
}
}

// TODO: @translate
// It's not really needed right now so it can be translated later
fn void render_minimap(Image *display, SpritePool *sprite_pool) @extern("render_minimap") @wasm {
// ctx.save();

// // A couple of temporary vectors
// const p1 = new Vector2();
// const p2 = new Vector2();

// const cellSize = ctx.canvas.width*MINIMAP_SCALE;

// ctx.translate(ctx.canvas.width*0.03, ctx.canvas.height*0.03);
// ctx.scale(cellSize, cellSize);

// ctx.fillStyle = "#181818";
// ctx.fillRect(0, 0, scene.width, scene.height);

// ctx.lineWidth = 0.05;
// const walls = new Uint8ClampedArray(wasmCommon.memory.buffer, scene.wallsPtr, scene.width*scene.height);
// for (let y = 0; y < scene.height; ++y) {
// for (let x = 0; x < scene.width; ++x) {
// if (sceneGetTile(walls, scene, p1.set(x, y))) {
// ctx.fillStyle = "blue";
// ctx.fillRect(x, y, 1, 1);
// }
// }
// }

// // Grid
// ctx.strokeStyle = "#303030";
// for (let x = 0; x <= scene.width; ++x) {
// strokeLine(ctx, p1.set(x, 0), p2.set(x, scene.height));
// }
// for (let y = 0; y <= scene.height; ++y) {
// strokeLine(ctx, p1.set(0, y), p2.set(scene.width, y));
// }

// ctx.fillStyle = "magenta";
// ctx.fillRect(player.position.x - PLAYER_SIZE*0.5,
// player.position.y - PLAYER_SIZE*0.5,
// PLAYER_SIZE, PLAYER_SIZE);

// ctx.strokeStyle = "magenta";
// strokeLine(ctx, camera.fovLeft, camera.fovRight);
// strokeLine(ctx, camera.position, camera.fovLeft);
// strokeLine(ctx, camera.position, camera.fovRight);

// if (MINIMAP_SPRITES) {
// ctx.strokeStyle = "yellow";
// ctx.fillStyle = "white"
// for (let i = 0; i < spritePool.length; ++i) {
// const sprite = spritePool.items[i];
// ctx.fillRect(sprite.position.x - MINIMAP_SPRITE_SIZE*0.5,
// sprite.position.y - MINIMAP_SPRITE_SIZE*0.5,
// MINIMAP_SPRITE_SIZE, MINIMAP_SPRITE_SIZE);

// }

// const sp = new Vector2();
// for (let sprite of visibleSprites) {
// strokeLine(ctx, player.position, sprite.position);
// sp.copy(sprite.position).sub(player.position).norm().scale(sprite.dist).add(player.position);
// ctx.fillRect(sp.x - MINIMAP_SPRITE_SIZE*0.5,
// sp.y - MINIMAP_SPRITE_SIZE*0.5,
// MINIMAP_SPRITE_SIZE, MINIMAP_SPRITE_SIZE);
// }
// }

// ctx.restore();
}

fn void cull_and_sort_sprites(SpritePool *sprite_pool) {
Camera camera = { .position = {me.position.x, me.position.y}, .direction = me.direction };
camera.update();
Expand Down Expand Up @@ -928,10 +857,14 @@ fn void render_game(Image *display, float *zbuffer, SpritePool *sprite_pool, Par
render_walls(display, zbuffer, wall_image, common::scene);
cull_and_sort_sprites(sprite_pool);
render_sprites(display, zbuffer, sprite_pool);

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

uint ping_cooldown = PING_COOLDOWN;
fn void ping_server_if_needed() @extern("ping_server_if_needed") @wasm {
fn void ping_server_if_needed() {
if (!platform_is_offline_mode()) {
ping_cooldown -= 1;
if (ping_cooldown == 0) {
Expand Down
12 changes: 0 additions & 12 deletions client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { SERVER_PORT } from './common.mjs';
const SCREEN_FACTOR = 30;
const SCREEN_WIDTH = Math.floor(16 * SCREEN_FACTOR);
const SCREEN_HEIGHT = Math.floor(9 * SCREEN_FACTOR);
const MINIMAP = false;
let game;
function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
Expand Down Expand Up @@ -56,9 +55,6 @@ function createDisplay(wasmClient, backImageWidth, backImageHeight) {
if (ctx === null)
throw new Error("2D context is not supported");
ctx.imageSmoothingEnabled = false;
const minimapWidth = backImageWidth * 0.03;
const minimapHeight = backImageHeight * 0.03;
const minimapPtr = wasmClient.allocate_image(minimapWidth, minimapHeight);
const backImagePtr = wasmClient.allocate_image(backImageWidth, backImageHeight);
const zBufferPtr = wasmClient.allocate_zbuffer(backImageWidth);
const backCanvas = new OffscreenCanvas(backImageWidth, backImageHeight);
Expand All @@ -70,7 +66,6 @@ function createDisplay(wasmClient, backImageWidth, backImageHeight) {
ctx,
backCtx,
backImagePtr,
minimapPtr,
zBufferPtr,
};
}
Expand Down Expand Up @@ -163,7 +158,6 @@ async function instantiateWasmClient(url) {
...wasmCommon,
allocate_zbuffer: wasm.instance.exports.allocate_zbuffer,
allocate_sprite_pool: wasm.instance.exports.allocate_sprite_pool,
reset_sprite_pool: wasm.instance.exports.reset_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 All @@ -175,7 +169,6 @@ async function instantiateWasmClient(url) {
key_down: wasm.instance.exports.key_down,
key_up: wasm.instance.exports.key_up,
render_game: wasm.instance.exports.render_game,
ping_server_if_needed: wasm.instance.exports.ping_server_if_needed,
ping_msecs: wasm.instance.exports.ping_msecs,
process_message: wasm.instance.exports.process_message,
};
Expand Down Expand Up @@ -253,11 +246,6 @@ async function createGame() {
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.ping_server_if_needed();
game.wasmClient.reset_sprite_pool(game.spritePoolPtr);
game.wasmClient.reset_temp_mark();
if (MINIMAP)
game.wasmClient.render_minimap(game.display.minimapPtr, game.spritePoolPtr);
displaySwapBackImageData(game.display, game.wasmClient);
renderDebugInfo(game.display.ctx, deltaTime, game);
window.requestAnimationFrame(frame);
Expand Down
15 changes: 1 addition & 14 deletions client.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {SERVER_PORT} from './common.mjs';
const SCREEN_FACTOR = 30;
const SCREEN_WIDTH = Math.floor(16*SCREEN_FACTOR);
const SCREEN_HEIGHT = Math.floor(9*SCREEN_FACTOR);
const MINIMAP = false;

let game: Game;

Expand Down Expand Up @@ -51,15 +50,13 @@ function renderDebugInfo(ctx: CanvasRenderingContext2D, deltaTime: number, game:
interface Display {
ctx: CanvasRenderingContext2D;
backCtx: OffscreenCanvasRenderingContext2D;
minimapPtr: number;
backImagePtr: number;
zBufferPtr: number;
}

interface WasmClient extends common.WasmCommon {
allocate_zbuffer: (width: number) => number,
allocate_sprite_pool: () => number,
reset_sprite_pool: (sprite_pool: number) => void,
render_minimap: (display: number, sprite_pool: number) => void;
allocate_particle_pool: () => number,
allocate_image: (width: number, height: number) => number,
Expand All @@ -70,8 +67,8 @@ interface WasmClient extends common.WasmCommon {
unregister_all_other_players: () => void,
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,
ping_server_if_needed: () => void,
ping_msecs: () => number,
process_message: (message: number, particle_pool: number) => boolean,
}
Expand All @@ -86,9 +83,6 @@ function createDisplay(wasmClient: WasmClient, backImageWidth: number, backImage
if (ctx === null) throw new Error("2D context is not supported");
ctx.imageSmoothingEnabled = false;

const minimapWidth = backImageWidth*0.03;
const minimapHeight = backImageHeight*0.03;
const minimapPtr = wasmClient.allocate_image(minimapWidth, minimapHeight);
const backImagePtr: number = wasmClient.allocate_image(backImageWidth, backImageHeight);
const zBufferPtr: number = wasmClient.allocate_zbuffer(backImageWidth);
const backCanvas = new OffscreenCanvas(backImageWidth, backImageHeight);
Expand All @@ -99,7 +93,6 @@ function createDisplay(wasmClient: WasmClient, backImageWidth: number, backImage
ctx,
backCtx,
backImagePtr,
minimapPtr,
zBufferPtr,
};
}
Expand Down Expand Up @@ -224,7 +217,6 @@ async function instantiateWasmClient(url: string): Promise<WasmClient> {
...wasmCommon,
allocate_zbuffer: wasm.instance.exports.allocate_zbuffer as (width: number) => number,
allocate_sprite_pool: wasm.instance.exports.allocate_sprite_pool as () => number,
reset_sprite_pool: wasm.instance.exports.reset_sprite_pool as (sprite_pool: number) => void,
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 @@ -236,7 +228,6 @@ async function instantiateWasmClient(url: string): Promise<WasmClient> {
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,
ping_server_if_needed: wasm.instance.exports.ping_server_if_needed as () => 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 @@ -340,10 +331,6 @@ async function createGame(): Promise<Game> {
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.ping_server_if_needed();
game.wasmClient.reset_sprite_pool(game.spritePoolPtr);
game.wasmClient.reset_temp_mark();
if (MINIMAP) game.wasmClient.render_minimap(game.display.minimapPtr, game.spritePoolPtr);

displaySwapBackImageData(game.display, game.wasmClient);
renderDebugInfo(game.display.ctx, deltaTime, game);
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 @@ -421,7 +421,7 @@ fn void update_player(Player *player, Scene *scene, float delta_time) {
/// Temporary Memory //////////////////////////////

usz temp_mark = 0;
fn void reset_temp_mark() @extern("reset_temp_mark") @wasm {
fn void reset_temp_mark() {
allocator::temp().reset(temp_mark);
}

Expand Down
1 change: 0 additions & 1 deletion common.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export function makeWasmCommon(wasm) {
wasm,
memory: wasm.instance.exports.memory,
_initialize: wasm.instance.exports._initialize,
reset_temp_mark: wasm.instance.exports.reset_temp_mark,
allocate_temporary_buffer: wasm.instance.exports.allocate_temporary_buffer,
};
}
Expand Down
2 changes: 0 additions & 2 deletions common.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export interface WasmCommon {
wasm: WebAssembly.WebAssemblyInstantiatedSource,
memory: WebAssembly.Memory,
_initialize: () => void,
reset_temp_mark: () => void,
allocate_temporary_buffer: (size: number) => number,
}

Expand All @@ -14,7 +13,6 @@ export function makeWasmCommon(wasm: WebAssembly.WebAssemblyInstantiatedSource):
wasm,
memory: wasm.instance.exports.memory as WebAssembly.Memory,
_initialize: wasm.instance.exports._initialize as () => void,
reset_temp_mark: wasm.instance.exports.reset_temp_mark as () => void,
allocate_temporary_buffer: wasm.instance.exports.allocate_temporary_buffer as (size: number) => number,
}
}
Expand Down
Binary file modified server.wasm
Binary file not shown.

0 comments on commit d39a32b

Please sign in to comment.