Web Porting from C++
Games developed in C++, including those using SDL—whether SDL 1.2 or 2.0—can be compiled to run in the browser using Emscripten. This allows you to bring older or existing projects to the web without changing your engine or rewriting core systems.
spawnd supports this setup and makes it easy to publish and share your game as a playable web demo. This includes games that use software rendering or OpenGL through SDL.
If your game compiles cleanly with Emscripten, it can run on spawnd.
To port a C++ game using SDL to the browser, the standard path is using Emscripten. Here’s the process:
⸻
✅ Requirements
• Emscripten SDK: Compiles C++ to WebAssembly
• Your game must be portable (no OS-specific system calls, etc.)
• SDL version: SDL2 is supported by Emscripten
⸻
⚙️ Porting Steps
- Install Emscripten
git clone [https://github.com/emscripten-core/emsdk.git](https://github.com/emscripten-core/emsdk.git)\
cd emsdk
./emsdk install latest
./emsdk activate latest
source ./emsdk\_env.sh
- Prepare Your Code
• Use SDL2 (or adapt your code if on SDL1.2) • Avoid platform-specific code • No dynamic file system access unless pre-bundled - Build with Emscripten
Example command:
emcc main.cpp -o index.html -s USE\_SDL=2 -s WASM=1
Optional:
• --preload-file assets/ to include game assets
• -O2 or -O3 for optimization
- Test Locally
emrun --no\_browser --port 8080 .
Open http://localhost:8080/index.html
- Deploy
• Upload index.html, .js, and .wasm to a static server (e.g. S3, Netlify, Cloudflare Pages)
⸻
🔁 Common Adjustments
• Replace file I/O with preloaded assets or Emscripten’s virtual FS
• Input handling (keyboard, mouse) should map cleanly with SDL2
• Audio: SDL_mixer works with some limitations
⸻
If your game uses OpenGL, Emscripten maps it to WebGL, but you may need to modify shaders or context creation code.
Updated about 1 month ago
