On this page
  1. Building and running it
  2. The browser owns the loop
  3. The backend
  4. Graphics
  5. Memory
  6. Assets
  7. Saves
  8. Sound
  9. Serving a build
  10. What CI checks

Web

The web target is the same game compiled to WebAssembly with Emscripten, and it plays in a browser tab. Almost everything about it is the same as anywhere else. The one thing that is not shapes the rest: in a browser, the browser owns the frame loop.

Building and running it#

rmp web
python3 -m http.server 8000 --directory build/web

and open http://localhost:8000/my_game.html. rmp web needs the Emscripten SDK activated in the shell first — emsdk_env.sh, or emsdk_env.ps1 on Windows — and says so when it is not: the web build needs the emsdk: activate it first (source emsdk_env.sh, or emsdk_env.ps1 on Windows), so that EMSDK is set. Underneath it is cmake --preset web, a release build optimised for size; CI builds with the Emscripten pinned in its build image.

The build is four files that belong together: my_game.html, the page; my_game.js, the loader; my_game.wasm, the game; and my_game.data, the contents of resources/. The page fetches the other three, so it has to be served — rmp web ends by saying serve it, do not open the file directly — and any static web server will do.

The browser owns the loop#

A desktop game is a while loop: draw a frame, ask whether the window was closed, go round again. A browser tab cannot run that. JavaScript is single-threaded and cooperative, and a function that never returns never gives the page back — nothing is drawn, nothing is heard, and the tab freezes. So on the web the framework's entry point hands the loop to the browser with emscripten_set_main_loop(), and the browser calls the game once per frame, in step with the display. main() never returns, which is the same contract the desktop loop has.

The only way to keep a while loop in a browser is to build with -s ASYNCIFY, which rewrites the whole program so its stack can be put aside and picked up again. Its cost is not charged to the loop: it is charged to every function that could be on the stack, in size and in speed, whether or not anything ever waits. The framework measured it at 26 % more .wasm and 18 % more download, and builds without it.

Four rules come out of that, and the framework already follows each of them for you:

  • No while loop that draws. A loading screen that loops until the work is done freezes the tab. Do the work a slice per frame in _update().
  • No WindowShouldClose(). On the web raylib's version waits with emscripten_sleep(), which needs ASYNCIFY; the framework never calls it there.
  • No SetTargetFPS(). The browser paces the frame, and a cap would be a wait inside its callback. [window] fps and [window] vsync change nothing on the web.
  • No backend that needs ASYNCIFY. See below.

rmp::app::quit() works: the frame that asked finishes, the browser stops calling the game, and the same shutdown as on a desktop runs. The page itself stays open: a browser tab is the player's to close.

The backend#

Emscripten compiles the game whatever [web] backend says; the choice is what raylib talks to in the browser.

[web] backendraylib talks to
"glfw"GLFW, which Emscripten emulates in JavaScript. The default, and the path raylib has shipped on the web for years. Defines PLATFORM_WEB.
"emscripten"The browser, through Emscripten's own API, with no GLFW layer to download. Defines PLATFORM_WEB_EMSCRIPTEN. raylib's code for it still marks its key-code mapping and file dropping as unfinished, which is why it is not the default.

A third, RGFW, is refused, because its web backend waits with emscripten_sleep(): it would build, and then die in the tab.

[web]
backend = "rgfw"
  raylib_multiplatform.toml:2: backend = "rgfw"
                               ^^^^^^^^^^^^^^^^

  configure: error: [web] backend = 'rgfw' cannot work here.
                    RGFW's web backend calls emscripten_sleep, which needs -sASYNCIFY. This framework deliberately has no ASYNCIFY: it hands the frame loop to emscripten_set_main_loop instead, and the measurement behind that decision is +26 % .wasm and +18 % download. Turning it on for one backend would put that cost on the build.
                    It BUILDS without it and then dies in the browser with "Please compile your program with async support" — which is how this was found: the boot test in .github/workflows/web-backends.yml opened it in Chromium.
                    Use one of emscripten, glfw.

Since the two backends define different PLATFORM_* macros, code that has to know it is on the web tests __EMSCRIPTEN__, which both define.

Graphics#

raylib draws with OpenGL ES 2.0 on the web, which the browser provides as WebGL, with either backend. That is a smaller OpenGL than a desktop's 3.3: shaders are written in GLSL 100, and rlgl and OpenGL has what else differs.

Memory#

A WebAssembly module gets its memory up front. [web] memory is how much, in megabytes, between 16 and 4096; the default, 64, is enough for menus and a handful of textures. A game that loads a tile atlas and a few tracks wants more, and when it runs out the tab dies with Emscripten's "Cannot enlarge memory arrays" rather than an error of raylib's.

[web]
memory = 256

[web] grow = true lets the memory grow past that instead of stopping the game, and costs a little speed on every access to it, since each one goes through a bounds check that cannot be lifted out of a loop. A game that knows its ceiling is better served by raising memory.

Assets#

The web build CI makes has no asset pack. The whole of resources/ is preloaded into Emscripten's in-memory file system — that is the .data file — and the game reads it there with the same paths and the same rmp::assets calls as anywhere else. It also means the assets of a web build are ordinary files to anyone who opens the .data: the pack's encryption applies only to the targets that ship the pack.

Saves#

Emscripten's files live in memory, so a save written there would be gone at the next reload. The framework mounts a folder backed by the browser's IndexedDB, /rmp_save/<name>/, and rmp::save uses it: every write is pushed to IndexedDB, and before the game starts the folder is filled from it — the game's main() is held back until that has finished, so the first scene's _ready() finds the save the player left. The folder is named after [project] name, so games served from the same site do not share their saves.

A browser that refuses storage — a private window, site data switched off — does not stop the game. The failure is logged on the browser's console and the saves last as long as the page does.

Sound#

Browsers keep a page's sound suspended until the player does something. raylib's audio backend resumes it on a click or a touch, and the framework adds a key press to those, so a game played from the keyboard is heard from its first key. A gamepad cannot do it: browsers do not count its buttons as the kind of gesture that may start sound, and a game that only ever sees a gamepad stays silent until the first click, touch or key.

Serving a build#

Any static web server that serves the four files from one folder will run the game; the page is <name>.html. CI's web archive is those four files and a LICENSES.txt of the web's own, which credits what a web build contains and none of the desktop windowing libraries a .wasm does not carry. itch.io wants the page called index.html, and the framework's itch.io job uploads the build that way, so it plays in the page rather than as a download. More on hosting, size and the archive in The web build.

What CI checks#

The web target is in the fast lane, on every push. CI builds it, checks the four files are there and not empty, and then plays it: the page is served from a local web server and opened in headless Chromium; the game must report that it booted with every asset loaded (assets_failed=0), after writing a save to IndexedDB, reading it back and removing it; and after the game has had time to draw, the canvas is photographed and measured for drawn pixels. When the render check fails, the picture of the canvas is kept with the run.