Repository navigation
Web
Build the same C source for the browser with Emscripten. raylib renders to a WebGL canvas while ccompose handles layout.
Install the Emscripten SDK, activate it in your shell, and configure through emcmake:
source /path/to/emsdk/emsdk_env.sh
emcmake cmake -S . -B build-web -DCCOMPOSE_BUILD_TESTS=OFF
cmake --build build-webCCOMPOSE_TARGET_WEB turns on by itself under the Emscripten toolchain. It also selects raylib's Web platform. A desktop raylib install is not usable here, so raylib is normally fetched and built from source.
Each example produces an .html, .js, .wasm and .data file under build-web/examples/.
Browsers refuse to load WASM from file://, so serve the folder over HTTP:
python3 -m http.server -d build-web/examples 8080
# open http://localhost:8080/ccompose_demo.htmlThe browser owns the event loop. A native while (CC_Running()) { ... } loop never returns control to it, so the tab hangs. Move the body of the loop into a function and hand it to CC_RUN_LOOP:
#include "ccompose.h"
static int clicks; // state the frame reads lives at file scope
static void frame(void) {
CC_Begin();
Column("Root", .layout = { .sizing = { Grow(), Grow() },
.padding = PadAll(24) }) {
if (CC_Clicked("Inc")) clicks++;
Button("Inc", .layout = { .padding = PadAll(12) }) {
Text("Click me", .fontSize = 16);
}
}
CC_End();
}
int main(void) {
CC_SetWindow(800, 600, "ccompose");
CC_Init();
CC_RUN_LOOP(frame); // native and web
CC_Shutdown();
return 0;
}| Target |
CC_RUN_LOOP(frame) expands to |
|---|---|
| Native | while (CC_Running()) frame(); |
Web (__EMSCRIPTEN__ defined) |
emscripten_set_main_loop(frame, 0, 1); |
The frame function takes no arguments. Keep shared state at file scope or access it through a file-scope pointer. See examples/landing.c for a complete program.
On the web, emscripten_set_main_loop with simulate_infinite_loop = 1 never returns, so code after CC_RUN_LOOP doesn't run. Don't rely on it for cleanup.
ccompose's own examples get their web link flags from the ccompose_apply_web_flags function in the top-level CMakeLists.txt:
| Flag | Why |
|---|---|
-sUSE_GLFW=3 |
raylib's web platform is built on Emscripten's GLFW port. |
-sASYNCIFY |
Lets raylib's blocking calls yield to the browser. |
-sALLOW_MEMORY_GROWTH=1, -sTOTAL_MEMORY=67108864
|
64 MB starting heap that can grow. |
-sFORCE_FILESYSTEM=1 |
Keeps the virtual filesystem that preloaded assets live in. |
--shell-file=examples/shell.html |
The HTML page that hosts the canvas. |
--preload-file=examples/resources@examples/resources |
Packs fonts and images into the .data file at the same path the native build uses. |
The function resolves paths from CMAKE_SOURCE_DIR, so it points at the wrong folder when ccompose is a subdirectory of your project. For your own app, copy the flags onto your target and point --shell-file and --preload-file at your own files:
if(EMSCRIPTEN)
target_link_options(my_app PRIVATE
-sUSE_GLFW=3 -sASYNCIFY -sALLOW_MEMORY_GROWTH=1 -sFORCE_FILESYSTEM=1
--preload-file=${CMAKE_CURRENT_SOURCE_DIR}/assets@assets)
set_target_properties(my_app PROPERTIES SUFFIX ".html")
endif()Only files packed with --preload-file exist at runtime. Load them with the path on the right of the @, for example CC_LoadFont("assets/Inter.ttf", 16). Loading a path that wasn't packed fails the same way a missing file does natively: CC_LoadFont returns -1.
-
CC_Running()and quit keys mean nothing in the browser. The app runs until the tab closes. See Lifecycle. - Custom drawing callbacks run on WebGL (OpenGL ES 2 level), so raylib calls that need desktop GL features may not work.