CMake + SDL2 + Emscripten Sample
C/C++ Icon
SDL Icon
Emscripten Icon

CMake + SDL2 + Emscripten Sample

A sample skeleton game project made for indie game developers to use as a reference for integrating CMake, SDL2, and Emscripten together.

View the full source code (MIT license) over on the cmake-testing-grounds repository.

If you're looking for guidance on how to build a cross-platform C++ game for both the web and desktop, this project is for you! It illustrates a modern (2025 - 2026) CMake build script, and C++ bootstrapping using Emscripten. It includes SDL_image for loading textures, SDL_ttf for loading fonts, and SDL_mixer for loading and playing music and sound effects.

SDL version 2 was chosen because SDL_mixer did not yet have a version 3 release tag at the time this project was created. A separate project targeting SDL3 will be available once the full SDL stack has an official version 3 release!

Version 1.1 Update

The Version 1.1 update illustrates the following additional functionality:

  • Introduces a CMakePresets.json file for desktop and Emscripten builds, which plays nicely with VS Code.
  • Updates the canvas dimensions to fill the remaining space on the web page.
  • Adds `touch-action: none;` CSS to the canvas element, to ensure the web build plays nicely on mobile devices.
  • Adds event handling for window resize events.
  • Adds a fullscreen toggle icon in the top-right corner.
  • Adds support for small and standard size for text rendering, based on window size.