|
| 1 | +# Web/WASM Target |
| 2 | + |
| 3 | +Bloom games can run in the browser via WebAssembly. The web target uses WebGPU (with WebGL fallback) for rendering and Web Audio API for sound. |
| 4 | + |
| 5 | +## Architecture |
| 6 | + |
| 7 | +``` |
| 8 | +Game.ts ─(perry --target wasm)──> game.wasm (game logic in WASM) |
| 9 | + │ |
| 10 | + │ FFI imports ("ffi" namespace) |
| 11 | + ▼ |
| 12 | + index.html / JS glue (bridges both WASM modules) |
| 13 | + │ |
| 14 | + │ wasm-bindgen calls |
| 15 | + ▼ |
| 16 | + bloom_web.wasm (Bloom rendering in WASM) |
| 17 | + │ |
| 18 | + ▼ |
| 19 | + Browser: <canvas> + WebGPU + Web Audio + DOM Events |
| 20 | +``` |
| 21 | + |
| 22 | +Both game logic and rendering run in WebAssembly. A thin JS glue layer bridges the two modules, handles DOM events, string conversion, asset fetching, and audio output. |
| 23 | + |
| 24 | +## Building |
| 25 | + |
| 26 | +### Prerequisites |
| 27 | + |
| 28 | +- [wasm-pack](https://rustwasm.github.io/wasm-pack/installer/): `cargo install wasm-pack` |
| 29 | +- [Perry compiler](../../perry/perry): built from source |
| 30 | +- wasm-opt (optional): `cargo install wasm-opt` |
| 31 | + |
| 32 | +### Quick Build |
| 33 | + |
| 34 | +```bash |
| 35 | +./native/web/build.sh path/to/game/main.ts |
| 36 | +``` |
| 37 | + |
| 38 | +This runs: |
| 39 | +1. `wasm-pack build` to compile `native/web/` → `bloom_web.wasm` + JS bindings |
| 40 | +2. `wasm-opt -Oz` for binary size optimization (if installed) |
| 41 | +3. `perry main.ts --target wasm` to compile game TypeScript → WASM |
| 42 | +4. Assembles output directory at `dist/web/` |
| 43 | + |
| 44 | +### Serve Locally |
| 45 | + |
| 46 | +```bash |
| 47 | +cd dist/web |
| 48 | +python3 -m http.server 8080 |
| 49 | +# Open http://localhost:8080 |
| 50 | +``` |
| 51 | + |
| 52 | +## Game Loop |
| 53 | + |
| 54 | +Browsers cannot run blocking `while` loops. Use `runGame()` instead: |
| 55 | + |
| 56 | +```typescript |
| 57 | +import { initWindow, runGame, clearBackground, drawRect, Colors } from "bloom"; |
| 58 | + |
| 59 | +initWindow(800, 600, "My Game"); |
| 60 | + |
| 61 | +runGame((dt) => { |
| 62 | + clearBackground(Colors.BLACK); |
| 63 | + drawRect(100, 100, 50, 50, Colors.RED); |
| 64 | +}); |
| 65 | +``` |
| 66 | + |
| 67 | +On native, `runGame()` enters a blocking loop. On web, it passes the callback to the JS runtime which drives it via `requestAnimationFrame`. |
| 68 | + |
| 69 | +The traditional `while (!windowShouldClose())` pattern still works on native but is not supported on web. |
| 70 | + |
| 71 | +## Asset Loading |
| 72 | + |
| 73 | +Assets are loaded via synchronous HTTP requests from the game's served directory: |
| 74 | + |
| 75 | +```typescript |
| 76 | +const tex = loadTexture("assets/player.png"); // sync fetch from server |
| 77 | +const snd = loadSound("assets/jump.wav"); // WAV or OGG |
| 78 | +const model = loadModel("assets/scene.glb"); // glTF/GLB |
| 79 | +const font = loadFont("assets/font.ttf", 20); // TTF/OTF |
| 80 | +``` |
| 81 | + |
| 82 | +Place asset files in your game's `assets/` directory. The build script copies them to the output. |
| 83 | + |
| 84 | +Supported formats: |
| 85 | +- **Images**: PNG, JPEG, BMP, TGA |
| 86 | +- **Audio**: WAV, OGG (MP3 not supported on web) |
| 87 | +- **Models**: glTF, GLB |
| 88 | +- **Fonts**: TTF, OTF |
| 89 | + |
| 90 | +## Audio |
| 91 | + |
| 92 | +Audio uses the Web Audio API with the shared Rust AudioMixer: |
| 93 | + |
| 94 | +```typescript |
| 95 | +initAudio(); |
| 96 | +const sound = loadSound("assets/click.wav"); |
| 97 | +playSound(sound); |
| 98 | +``` |
| 99 | + |
| 100 | +The JS glue creates an `AudioContext` with a `ScriptProcessorNode` that calls `bloom_audio_mix()` each audio frame. The Rust AudioMixer handles mixing, volume, and spatial audio identically to native. |
| 101 | + |
| 102 | +## File I/O |
| 103 | + |
| 104 | +`writeFile` / `readFile` / `fileExists` use `localStorage` on web (prefixed with `bloom_fs:`): |
| 105 | + |
| 106 | +```typescript |
| 107 | +writeFile("save.json", JSON.stringify(gameState)); |
| 108 | +if (fileExists("save.json")) { |
| 109 | + const data = readFile("save.json"); |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +## Platform Detection |
| 114 | + |
| 115 | +```typescript |
| 116 | +import { getPlatform, Platform } from "bloom"; |
| 117 | + |
| 118 | +if (getPlatform() === Platform.WEB) { |
| 119 | + // web-specific code |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +## Browser Support |
| 124 | + |
| 125 | +- **Chrome 113+**: WebGPU (best performance) |
| 126 | +- **Firefox 141+**: WebGPU |
| 127 | +- **Safari**: WebGPU in Technology Preview; WebGL fallback available |
| 128 | +- **Edge 113+**: WebGPU |
| 129 | + |
| 130 | +The wgpu backend supports both WebGPU and WebGL. WebGL is used automatically as a fallback on browsers without WebGPU support. |
| 131 | + |
| 132 | +## How It Works |
| 133 | + |
| 134 | +### String Handling |
| 135 | + |
| 136 | +Perry WASM uses NaN-boxed string IDs. The JS glue converts these to actual JS strings via `__perryToJsValue` (exposed by Perry's runtime), then passes them to Bloom's `_str` variants via wasm-bindgen. |
| 137 | + |
| 138 | +### Two-Module WASM |
| 139 | + |
| 140 | +Perry compiles game TypeScript to one WASM module. Bloom's Rust backend compiles to a second WASM module via wasm-pack. The JS glue: |
| 141 | +1. Loads bloom_web.wasm and extracts all `bloom_*` exports |
| 142 | +2. Wraps them as FFI imports (converting i64 BigInt args to f64) |
| 143 | +3. Provides string-param functions with NaN-box → string conversion |
| 144 | +4. Boots Perry WASM with these imports under the `"ffi"` namespace |
| 145 | + |
| 146 | +### Shared Code |
| 147 | + |
| 148 | +67% of Bloom's Rust code is in `native/shared/` — the renderer, audio mixer, text renderer, model loader, scene graph. This code compiles identically for native and WASM. Only the platform layer (~1300 lines in `native/web/src/lib.rs`) is web-specific. |
0 commit comments