cub3D
A raycasting engine built in C — ported to the browser via Emscripten.
desktop only
the game runs on WebAssembly and needs a keyboard + mouse.
open this on a desktop to play.
dev notes
Ray Casting Pipeline (C)
- For each screen column, build a camera ray from player direction + camera plane.
- Run DDA grid stepping until the ray hits a wall tile.
- Compute perpendicular wall distance to avoid fish-eye distortion.
- Project wall stripe height on screen and sample texture coordinates.
- Draw floor/ceiling and apply distance-based shading.
for (int x = 0; x < render_w; ++x) {
ray_setup(&ray, player, camera, x);
dda_cast(&ray, map);
wall_project(&ray, render_h);
wall_draw_stripe(&ray, x);
floor_draw_column(&ray, x);
}
Core files: fn_game_ray.c, fn_game_wall_loop.c, fn_game_wall_draw.c, fn_game_floor_draw.c.
WebAssembly Build Strategy
- Built with
emccand MLX42 via GLFW-on-web path. - Graphics flags:
USE_GLFW=3,USE_WEBGL2=1,FULL_ES3=1. - Runtime flags:
NO_EXIT_RUNTIME=1,ALLOW_MEMORY_GROWTH. - Stability: raised stack to
STACK_SIZE=8388608. - Assets/maps bundled with
--preload-file.
Runtime Bridge: Native vs Browser
- Desktop uses blocking
mlx_loop. - Browser uses
emscripten_set_main_loop_argto hook into browser frame scheduling. - The same C game loop logic is reused through MLX loop hooks.
emscripten_set_main_loop_arg(
(em_arg_callback_func)mlx_loop,
g->mlx, 0, 1
);
Input Adaptation For Web
- Native path recenters cursor each frame and reads center delta.
- Web path uses relative-like delta from previous mouse sample.
- Spike filtering drops abnormal jumps; tiny deltas are ignored.
- Sensitivity is slightly scaled for browser control stability.
Bonus Systems Active In This Build
- Bonus mode always enabled for web build.
- Map fixed to
/maps/default.cub. - Keys, doors, minimap, sprites, lava, and checkpoints active.
- On key pickup, nearest checkpoint selected for lava respawn.
- Respawn keeps last look direction and pitch for continuity.
Local Build Quick Notes
source ~/emsdk/emsdk_env.sh
make -f Makefile.web
make -f Makefile.web serve
If emcmake or emcc is missing, emsdk environment is not loaded in the current shell.