A CMake, SDL2, and Emscripten Sample Project
Welcome!
You can take a look over at my cmake-testing-grounds repository for the sample project, in the sdl2-emscripten folder.
The project illustrates a modern (2025) approach that an indie developer might use to build a cross-platform game using CMake, SDL2, and Emscripten:
- A CMake build script with a single executable target, using separate compiler and linker options for the desktop vs. web-based path.
- Only the desktop build path needs to build SDL2, as Emscripten provides an SDL2 port.
- SDL_image, SDL_ttf, and SDL_mixer are all included. As of mid-December 2025, SDL_mixer does not yet have an SDL3-compatible release tag, which is why this sample project has fallen back on SDL2.
- Copies a set of resource files into the build folder. For Emscripten, the files are copied into the virtual file system using the
--preload-filelinker option. - Illustrates setup and teardown of the SDL systems.
- Illustrates appropriate scoping of the game data, to ensure its teardown happens before the SDL teardown.
- Illustrates the use of a timer to cleanly update game state.
- Illustrates how to run the game loop for the desktop vs. web-based build (which uses
emscripten_set_main_loop_arg).
I'll be going over the sample project in a Youtube video, and in a more detailed article here on the website, within the next few weeks. Until then, hopefully this sample project alone might help someone out there get through whatever configuration issues they might have been struggling with to get Emscripten and SDL2 working togther!
The game itself is a variation on Whack-a-Mole. Here's a screenshot with the default HTML wrapper that Emscripten produces:

It's not much of a game, but it illustrates the basic concepts an indie game developer needs who's looking to put together their first desktop + web-based game with SDL2.
According to what I'm seeing on the SDL_mixer issue tracker, the SDL team is extremely close to getting an SDL_mixer release tag that targets SDL3. I can't imagine there will be much of a difference in how this little project gets wired together. But when they do release, I plan to revisit this project to create an sdl3_emscripten version of it. 😊