Skip to content

Conversation

@jserv
Copy link
Contributor

@jserv jserv commented Oct 19, 2025

This introduces a lightweight WASM backend without SDL dependency, achieving 60% binary size reduction (891K→357K) compared to SDL-based approach.

  • Add backend/wasm.c: Native WASM backend using Canvas 2D API directly
    • Direct Canvas rendering via EM_ASM (no SDL overhead)
    • Browser-native image decoding (JPEG/PNG)
    • Optimized for minimal runtime with MINIMAL_RUNTIME=1
    • Framebuffer accessor functions for zero-copy updates
  • Remove twin_dispatch() from API and implementation
    • Eliminated unused function across all backends
    • All backends now use twin_dispatch_once() consistently
  • Make SDL backend native-only
    • Removed Emscripten-specific code paths from backend/sdl.c
    • SDL now exclusively for native development/testing

Summary by cubic

Added a native WebAssembly backend that renders directly to Canvas 2D, removing SDL from web builds and cutting binary size by 60% (891K→357K). Unified the event loop to twin_dispatch_once and made the SDL backend native-only.

  • New Features

    • Native WASM backend (backend/wasm.c) with direct Canvas 2D rendering and browser JPEG/PNG decoding.
    • Framebuffer accessor functions for zero-copy JS updates (MadoCanvas.updateCanvas).
    • Emscripten minimal runtime flags (MINIMAL_RUNTIME=1, emmalloc, no FS) for smaller builds.
  • Migration

    • WebAssembly builds must use CONFIG_BACKEND_WASM; SDL is no longer supported with emcc.
    • Remove twin_dispatch; use twin_dispatch_once (or twin_run).
    • Assets switch to demo-wasm(.js/.wasm) with mado-wasm.js; update references and scripts (serve-wasm.py).

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 11 files

Prompt for AI agents (all 2 issues)

Understand the root cause of the following 2 issues and fix them.


<file name="assets/web/mado-wasm.js">

<violation number="1" location="assets/web/mado-wasm.js:131">
The bitmask mapping for right and middle mouse buttons in `getButtonState` is incorrect. Browser `e.buttons` uses `2` for right and `4` for middle, but the code maps `2` to Mado&#39;s middle (`4`) and `4` to Mado&#39;s right (`2`). This causes incorrect button state information to be sent to `backend/wasm.c` during `mousemove` events, leading to buggy drag behavior with right and middle clicks.</violation>
</file>

<file name="scripts/serve-wasm.py">

<violation number="1" location="scripts/serve-wasm.py:67">
This file check for `demo-wasm` is incorrect and will fail. The `Makefile` configures Emscripten to generate `demo-wasm.js`, and `assets/web/index.html` correctly includes `&lt;script src=&quot;demo-wasm.js&quot;&gt;`. The development server script is inconsistent with the build output and web page, breaking the local testing workflow.</violation>
</file>

React with 👍 or 👎 to teach cubic. Mention @cubic-dev-ai to give feedback, ask questions, or re-run the review.

This introduces a lightweight WASM backend without SDL dependency,
achieving 60% binary size reduction (891K→357K) compared to SDL-based
approach.
- Add backend/wasm.c: Native WASM backend using Canvas 2D API directly
  * Direct Canvas rendering via EM_ASM (no SDL overhead)
  * Browser-native image decoding (JPEG/PNG)
  * Optimized for minimal runtime with MINIMAL_RUNTIME=1
  * Framebuffer accessor functions for zero-copy updates
- Remove twin_dispatch() from API and implementation
  * Eliminated unused function across all backends
  * All backends now use twin_dispatch_once() consistently
- Make SDL backend native-only
  * Removed Emscripten-specific code paths from backend/sdl.c
  * SDL now exclusively for native development/testing

This tweaks the WASM backend to support file-based asset loading (TVG,
PNG, JPEG, GIF) through Emscripten's virtual filesystem, addressing
multiple runtime initialization and resource management issues.
- Replace --preload-file with --embed-file for simplified deployment
- Enable FILESYSTEM=1 to support fopen() in image loaders
- Remove MINIMAL_RUNTIME=1 (incompatible with filesystem)
- Implement lazy framebuffer caching in JavaScript
@jserv jserv merged commit 5bb1c96 into main Oct 19, 2025
8 checks passed
@jserv jserv deleted the wasm branch October 19, 2025 20:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants