Skip to content

Commit

Permalink
Turn Scene into an opaque pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Sep 27, 2024
1 parent d6508b6 commit 162a0df
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 127 deletions.
20 changes: 10 additions & 10 deletions client.c3
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,11 @@ fn Vector2 ray_step(Vector2 p1, Vector2 p2) {
return p3;
}

fn Vector2 cast_ray(bool *scene, int scene_width, int scene_height, Vector2 p1, Vector2 p2) {
fn Vector2 cast_ray(Scene *scene, Vector2 p1, Vector2 p2) {
Vector2 start = p1;
while (start.distance(p1) < FAR_CLIPPING_PLANE) {
Vector2 c = hitting_cell(p1, p2);
if (common::scene_get_tile(scene, scene_width, scene_height, c)) break;
if (common::scene_get_tile(scene, c)) break;
Vector2 p3 = ray_step(p1, p2);
p1 = p2;
p2 = p3;
Expand All @@ -287,17 +287,17 @@ fn Vector2 cast_ray(bool *scene, int scene_width, int scene_height, Vector2 p1,
fn void render_walls(Color *display, int display_width, int display_height, float *zbuffer,
Color *wall, int wall_width, int wall_height,
float position_x, float position_y, float direction,
bool *scene, int scene_width, int scene_height) @extern("render_walls") @wasm {
Scene *scene) @extern("render_walls") @wasm {
Camera camera = { .position = {position_x, position_y}, .direction = direction };
camera.update();

Vector2 d = from_polar(direction, 1.0f);
for (int x = 0; x < display_width; ++x) {
Vector2 p = cast_ray(scene, scene_width, scene_height, camera.position, camera.fovLeft.lerp(camera.fovRight, (float)x/display_width));
Vector2 p = cast_ray(scene, camera.position, camera.fovLeft.lerp(camera.fovRight, (float)x/display_width));
Vector2 c = hitting_cell(camera.position, p);
Vector2 v = p - camera.position;
zbuffer[x] = v.dot(d);
if (common::scene_get_tile(scene, scene_width, scene_height, c)) {
if (common::scene_get_tile(scene, c)) {
render_column_of_wall(display, display_width, display_height, zbuffer, wall, wall_width, wall_height, x, p.x, p.y, c.x, c.y);
}
}
Expand All @@ -308,7 +308,7 @@ fn void render_walls(Color *display, int display_width, int display_height, floa
fn void render_minimap(Color *display, int display_width, int display_height,
float camera_position_x, float camera_position_y, float camera_direction,
float player_position_x, float player_position_y,
bool *scene, int scene_width, int scene_height,
Scene *scene,
SpritePool *sprite_pool) @extern("render_minimap") @wasm {
// ctx.save();

Expand Down Expand Up @@ -499,15 +499,15 @@ fn void emit_particle(float source_x, float source_y, float source_z, ParticlePo
fn void update_particles(Color *image_pixels, int image_width, int image_height,
SpritePool *sprite_pool,
float deltaTime,
bool *scene, int scene_width, int scene_height,
Scene *scene,
ParticlePool *particle_pool) @extern("update_particles") @wasm {
foreach (&particle: particle_pool.items) {
if (particle.lifetime > 0) {
particle.lifetime -= deltaTime;
particle.velocity_z -= common::BOMB_GRAVITY*deltaTime;

Vector2 new_position = particle.position + particle.velocity*deltaTime;
if (common::scene_get_tile(scene, scene_width, scene_height, new_position)) {
if (common::scene_get_tile(scene, new_position)) {
float dx = math::abs(math::floor(particle.position.x) - math::floor(new_position.x));
float dy = math::abs(math::floor(particle.position.y) - math::floor(new_position.y));

Expand Down Expand Up @@ -640,12 +640,12 @@ fn void explode_bomb(float bomb_position_x, float bomb_position_y, float bomb_po
}
}

fn void update_bombs_on_client_side(SpritePool *sprite_pool, ParticlePool *particle_pool, Color *bomb_image_pixels, int bomb_image_width, int bomb_image_height, bool *scene, int scene_width, int scene_height, float player_position_x, float player_position_y, float delta_time, Bombs *bombs) @extern("update_bombs_on_client_side") @wasm {
fn void update_bombs_on_client_side(SpritePool *sprite_pool, ParticlePool *particle_pool, Color *bomb_image_pixels, int bomb_image_width, int bomb_image_height, Scene *scene, float player_position_x, float player_position_y, float delta_time, Bombs *bombs) @extern("update_bombs_on_client_side") @wasm {
foreach (&bomb: *bombs) {
if (bomb.lifetime > 0) {
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, scene_width, scene_height, delta_time)) {
if (common::update_bomb(bomb, scene, delta_time)) {
play_sound(BOMB_RICOCHET, player_position_x, player_position_y, bomb.position.x, bomb.position.y);
}

Expand Down
12 changes: 6 additions & 6 deletions client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -393,26 +393,26 @@ function renderGame(display, deltaTime, time, game) {
game.wasmClient.reset_sprite_pool(game.spritePoolPtr);
game.players.forEach((player) => {
if (player !== game.me)
updatePlayer(game.wasmClient, player, game.level.scene, deltaTime);
updatePlayer(game.wasmClient, player, game.level.scenePtr, deltaTime);
});
updatePlayer(game.wasmClient, game.me, game.level.scene, deltaTime);
updatePlayer(game.wasmClient, game.me, game.level.scenePtr, deltaTime);
updateCamera(game.me, game.camera);
updateItems(game.wasmClient, game.ws, game.spritePoolPtr, time, game.me, game.level.itemsPtr, game.assets);
game.wasmClient.update_bombs_on_client_side(game.spritePoolPtr, game.particlesPtr, game.assets.bombImage.ptr, game.assets.bombImage.width, game.assets.bombImage.height, game.level.scene.wallsPtr, game.level.scene.width, game.level.scene.height, game.me.position.x, game.me.position.y, deltaTime, game.level.bombsPtr);
game.wasmClient.update_particles(game.assets.particleImage.ptr, game.assets.particleImage.width, game.assets.particleImage.height, game.spritePoolPtr, deltaTime, game.level.scene.wallsPtr, game.level.scene.width, game.level.scene.height, game.particlesPtr);
game.wasmClient.update_bombs_on_client_side(game.spritePoolPtr, game.particlesPtr, game.assets.bombImage.ptr, game.assets.bombImage.width, game.assets.bombImage.height, game.level.scenePtr, game.me.position.x, game.me.position.y, deltaTime, game.level.bombsPtr);
game.wasmClient.update_particles(game.assets.particleImage.ptr, game.assets.particleImage.width, game.assets.particleImage.height, game.spritePoolPtr, deltaTime, game.level.scenePtr, game.particlesPtr);
game.players.forEach((player) => {
if (player !== game.me) {
const index = spriteAngleIndex(game.camera.position, player);
game.wasmClient.push_sprite(game.spritePoolPtr, game.assets.playerImage.ptr, game.assets.playerImage.width, game.assets.playerImage.height, player.position.x, player.position.y, 1, 1, 55 * index, 0, 55, 55);
}
});
game.wasmClient.render_floor_and_ceiling(display.backImage.ptr, display.backImage.width, display.backImage.height, game.camera.position.x, game.camera.position.y, game.camera.direction);
game.wasmClient.render_walls(display.backImage.ptr, display.backImage.width, display.backImage.height, display.zBufferPtr, game.assets.wallImage.ptr, game.assets.wallImage.width, game.assets.wallImage.height, game.camera.position.x, game.camera.position.y, game.camera.direction, game.level.scene.wallsPtr, game.level.scene.width, game.level.scene.height);
game.wasmClient.render_walls(display.backImage.ptr, display.backImage.width, display.backImage.height, display.zBufferPtr, game.assets.wallImage.ptr, game.assets.wallImage.width, game.assets.wallImage.height, game.camera.position.x, game.camera.position.y, game.camera.direction, game.level.scenePtr);
game.wasmClient.cull_and_sort_sprites(game.camera.position.x, game.camera.position.y, game.camera.direction, game.spritePoolPtr);
game.wasmClient.render_sprites(display.backImage.ptr, display.backImage.width, display.backImage.height, display.zBufferPtr, game.spritePoolPtr);
displaySwapBackImageData(display, game.wasmClient);
if (MINIMAP)
game.wasmClient.render_minimap(display.minimap.ptr, display.minimap.width, display.minimap.height, game.camera.position.x, game.camera.position.y, game.camera.direction, game.me.position.x, game.me.position.y, game.level.scene.wallsPtr, game.level.scene.width, game.level.scene.height, game.spritePoolPtr);
game.wasmClient.render_minimap(display.minimap.ptr, display.minimap.width, display.minimap.height, game.camera.position.x, game.camera.position.y, game.camera.direction, game.me.position.x, game.me.position.y, game.level.scenePtr, game.spritePoolPtr);
renderDebugInfo(display.ctx, deltaTime, game);
}
(async () => {
Expand Down
Loading

0 comments on commit 162a0df

Please sign in to comment.