WebAssembly applications can use high-level C/C++ APIs such as the C and C++ standard libraries, OpenGL, SDL, pthreads, and others, just as in normal C/C++ development. Under the covers, these libraries implement their functionality by using low-level facilities provided by WebAssembly implementations. On the Web, they utilize Web APIs (for example, OpenGL is executed on WebGL, libc date and time methods use the browser’s Date functionality, etc.). In other contexts, other low-level mechanisms may be used.
This is interesting, but I haven’t been able to find any documentation on the subject. How do I get at the SDL bindings in WebAssembly?
I’m gonna write all this out in case it’s helpful for everyone, but disregard whatever you already know here.
One could probably generate wasm without Emscripten, but it’s more or less an assumption at this moment that you’ll be using Emscripten.
Since that’s from-scratch code and some things aren’t doable in a web browser, it’s a sort of Good Enough implementation of the API. If you have an older game, it might Just Work though, which is cool.
For SDL2, the actual SDL2 codebase is ported to Emscripten, so you can literally just compile it and link it to your project. In that scenario, it works when generating either asm.js or WebAssembly.
For video, you can use OpenGL (which Emscripten will convert to WebGL calls). It’s…touchy though. Compatiblity with legacy OpenGL is really spotty, but if you’re looking more like GL3 Core Profile, it’s pretty sweet. That UT99 thing used libRegal with UT99’s GL1.2 code, which sort of stumbled its way to working in a pretty inefficient manner. We did better with Unrealty later, but you need a browser with WebGL2:
No one has tried to use SDL2 with node.js, and there’s a lot of code in our implementation that assumes you are running in a web browser, so it can have access to facilities for video/audio/input/etc. If something similar is available for node, I’d love to hear about it.
I’ve ported several things to Emscripten using SDL 1.2 and SDL 2.0 for Jump: