emcc or em++.
Build flags
Build with the flags your engine already needs, plus these:-o dist/index.htmlputsindex.htmlat the top of the folder, which the upload needs. Use--shell-fileto supply your own page.-sALLOW_MEMORY_GROWTH=1lets the heap grow, so a big level does not stop the game.-sENVIRONMENT=webdrops the Node and shell code a browser never runs.-lidbfs.jsis needed for saves.--js-library opengame.jsis the JavaScript half of the OpenGame C package, for platform services.-sEXPORTED_FUNCTIONSexports the functions the package calls. Add yours to the list.
-sEXPORTED_RUNTIME_METHODS lists only what your JavaScript calls, such as ccall or FS. The package needs none. With -sMODULARIZE=1, your page starts the game by calling the factory function Emscripten exports.
Upload the folder as Emscripten wrote it, with index.js, index.wasm and the .data file from --preload-file next to index.html. Emscripten loads them by paths relative to the page.
A .br or .gz copy is served with the matching Content-Encoding. You need none. If you ship one, make it from the file next to it, or the check reports compressed-asset-mismatch. Minify and strip debug output in your build, because OpenGame does not rewrite your files.
Size
An upload can be up to 4 GiB, with any one file up to 1 GiB.opengame check reports size-warn above 50 MiB unpacked, because a player waits for the download before the first frame. A decompilation that needs the original game’s data can send that data separately, and upload a large game explains how. Code and .wasm go in the archive.
Load fast
The module and every file packed with--preload-file load before main() runs. opengame check reports boot-download when they take more than 3 s on a 50 Mbps connection.
- Build release with
-O3or-Oz. Emscripten runswasm-optat those levels. - Keep the default loader. OpenGame sends a
.wasmwith the typeapplication/wasm, so it compiles while it downloads; a loader that fetches the module into anArrayBufferfirst waits for the whole file. - Preload only what the first screen needs, and fetch the rest after the game starts.
Threads
Threaded builds (-pthread) work. When the check notes engine-threads, OpenGame serves the release cross-origin isolated, so workers get SharedArrayBuffer, and you change nothing. A private preview of a threaded build plays only in Chrome or Edge 137 or later, which isolate the game’s frame by themselves. Firefox and Safari need the page around the frame isolated too, which OpenGame does only on the published game page. Set -sPTHREAD_POOL_SIZE to the number of threads the game starts at once, because Emscripten starts a thread outside the pool only after the main thread returns to the browser. Call the OpenGame C package, saves included, from the main thread. Threads and WebGPU shows how to check the game frame.
The C API
The OpenGame C package isopengame.h, opengame.c and opengame.js, from integrations/c in the OpenGame repository. Compile opengame.c with your game, link with --js-library opengame.js, and include the header. The package calls window.OpenGame through Emscripten’s JavaScript library mechanism, and adds no protocol or network code.
Ship the SDK with the game, as Load the SDK shows: copy opengame-global.js next to index.html and load it before the engine. OpenGame adds the SDK only to a build that uses threads.
Without the SDK,
og_connect returns 0 and every other call does nothing. With the SDK and away from OpenGame, the calls still run. Under opengame dev the SDK runs services as a local mock: a local player and scores kept in this browser. On any other server, init returns ok with no services, and leaderboard and achievement requests fail with unavailable.
A request returns an id, or 0 when it did not start, and then no callback runs. Results come later as JSON with ok and value, or ok false and error. The calls above escape the strings they put into JSON. For any other operation, call og_request with JSON you write. Services from any engine lists the operations.
og_ready() once the game has drawn the first playable frame and init has returned ok.
Saves
Emscripten holds files in memory, so a file the game writes is lost when the tab closes. The C package stores one folder in IndexedDB instead, through Emscripten’s IDBFS:- Read saves only after
doneruns. Mounting loads what was stored, and replaces any files the folder had. - Call
og_saves_flush()after closing each file you want to keep. Calls close together become one write, and a pending write runs at once when the player switches tabs or closes the page. A file still open at that moment may be lost. - When loading fails,
donegets 0 and the package does not write, so the player’s old saves are not touched. - Only that folder is stored. Files in other folders are held in memory.
- Don’t call
FS.syncfsyourself while the folder is mounted. It would load the stored copy over a save that has not been written yet.
In the player
The player loads yourindex.html in a frame on the game’s address, inside the game page. Size your canvas from the window, not a fixed number of pixels, because the frame changes size, for example in fullscreen or when a phone turns.
We checked fullscreen and sound below, and saves above, in Chromium, the engine of Chrome and Edge. Play your game in Firefox and Safari too: the private preview, or the published game for a threaded build.
- Fullscreen. Call your engine’s fullscreen, such as
SDL_SetWindowFullscreen,emscripten_request_fullscreenorModule.requestFullscreen, while handling a press or a key. The player takes the game page fullscreen with the game in it, and your canvas resizes to fill it. The player’s fullscreen button exits fullscreen. On a computer, Chrome and Edge send a tap of Esc to the game, and holding Esc exits. In Firefox and Safari a tap of Esc exits. An iPhone has no fullscreen for a page, so the game stays in the page. A request without a press fails, as it does in any browser. - Sound. A browser pauses audio that a game opens before the first press, and SDL, OpenAL and most engines open audio while loading. The player starts that audio when the person presses Play, or presses a key or the screen in the game. While the page has the game muted, the audio waits.
- Pointer lock.
requestPointerLockworks, called from a click, as in any page. - Gamepads.
navigator.getGamepadsworks, and SDL’s game controller support uses it. - Keyboard. The game gets keys once it has focus. The player focuses it when it opens, and a click in the game focuses it again.
Decompilations and ports
A decompilation or a recompilation of someone else’s game is adecomp in opengame.json, with basedOn naming the original, as packaging describes. A decomp cannot enter the jam. It runs the original game’s data, so upload only what you have the right to publish. The first time you submit, you certify that you have the rights to what you publish, that data included.
Test it locally
opengame dev . --build serves the build with the game page’s headers. To check the player behaviour on this page, deploy a private preview and play it. Press Play and listen, go fullscreen, save, then reload and check that the save loads. Threads and WebGPU has the frame check for a threaded build.