Lifecycle
Initialize ccompose, declare the layout each frame, and shut down when the app exits. The lifecycle calls manage the raylib window, Clay arena, and text measurement callback.
The shape of a ccompose program
static void frame(void) {
CC_Begin();
Column("Root", ...) { /* ... */ }
CC_End();
}
int main(void) {
CC_SetWindow(w, h, title); // optional, before CC_Init
CC_Init(); // once at startup
CC_RUN_LOOP(frame); // portable native + web main loop
CC_Shutdown(); // optional: also runs via atexit()
}
CC_Init() registers CC_Shutdown() with atexit(). Call it explicitly when you need a particular cleanup order, such as before destroying state used by the error handler.
Use CC_RUN_LOOP to run the frame function. It selects a native loop or the browser event loop for Emscripten builds.
Lifecycle functions
void CC_Init(void)
CC_Init is safe to call multiple times. The first call performs these steps:
- Create the raylib window using the size, title, and flags set with
CC_SetWindow/CC_SetWindowFlags. - Load the default raylib font into slot 0.
- Allocate Clay's arena via
Clay_MinMemorySize(). - Call
Clay_Initializewith the stored viewport and error handler. - Install
Raylib_MeasureTextas Clay's text-measure function. - Register
CC_Shutdownwithatexit().
Call CC_SetWindow, CC_SetWindowFlags, and CC_SetErrorHandler before CC_Init(). To change a window afterward, use raylib's SetWindowSize() or SetWindowTitle(). CC_SetBackground applies to the next frame because CC_End() reads it each time.
void CC_Shutdown(void)
Unloads custom fonts, closes the raylib window, and frees the Clay arena. Safe to call multiple times and safe to call after CC_Init() has never been called.
bool CC_Running(void)
Returns !WindowShouldClose() with raylib. Use it as the main-loop condition. In headless mode, it always returns true, so use a bounded loop or stop it yourself.
Quitting
WindowShouldClose() becomes true when the user clicks the window's close button or presses raylib's exit key. CC_Init() sets the exit key to KEY_NULL, so ESC does not quit: it releases a focused text input instead. ccompose has no quit API of its own. Use raylib directly:
SetExitKey(KEY_Q); // after CC_Init(): Q quits (ccompose sets KEY_NULL)
bool quit = false;
while (CC_Running() && !quit) {
CC_Begin();
Column("Root", .layout = { .sizing = { Grow(), Grow() } }) {
if (CC_Clicked("Quit")) quit = true;
Button("Quit", .layout = { .padding = PadAll(12) }) {
Text("Quit", .fontSize = 16);
}
}
CC_End();
}
SetExitKey and KEY_* come from raylib, which ccompose.h includes when the raylib backend is on. Call SetExitKey after CC_Init(), because the window doesn't exist before that. For keyboard shortcuts, call raylib's IsKeyPressed(KEY_...) inside the frame. Under Web (WASM), CC_RUN_LOOP hands the loop to the browser, so a quit flag has no effect: the page keeps running until the tab closes.
void CC_Begin(void)
Starts a new frame. Reads raylib's input state (window size, mouse position, mouse button, scroll wheel, frame time), forwards it into Clay via Clay_SetLayoutDimensions / Clay_SetPointerState / Clay_UpdateScrollContainers, then calls Clay_BeginLayout(). After this, container and text macros record into Clay's layout.
If CC_Init() hasn't been called yet, CC_Begin calls it for you.
CC_RenderCommandArray CC_End(void)
Ends the current frame. Calls Clay_EndLayout(GetFrameTime()), then:
BeginDrawing()ClearBackground(...)with the color set byCC_SetBackgroundClay_Raylib_Render(commands, fonts)EndDrawing()
Returns Clay's render command array for inspection or post-processing. With raylib, the frame has already been drawn when this function returns. Headless builds return the commands without drawing.
CC_RUN_LOOP(frame_fn)
Pass a void(void) frame function that calls CC_Begin(), builds the UI, and calls CC_End(). The macro selects the loop for the target:
- On native targets, it expands to
while (CC_Running()) frame_fn();. The loop exits when raylib'sWindowShouldClose()returnstrue. - On Emscripten targets (
__EMSCRIPTEN__defined), it expands toemscripten_set_main_loop(frame_fn, 0, 1)and gives control to the browser's event loop.
static void frame(void) {
CC_Begin();
Column("Root", .layout = { .sizing = { Grow(), Grow() } }) {
Text("hello", .fontSize = 24);
}
CC_End();
}
int main(void) {
CC_SetWindow(800, 600, "demo");
CC_Init();
CC_RUN_LOOP(frame);
CC_Shutdown();
}
The frame function takes no arguments. Pass shared state through a file-scope pointer or static. See examples/demo.c for the pattern.
Window configuration
Set the window size and flags before CC_Init(). To change an initialized window, call raylib's SetWindowSize() or SetWindowTitle().
void CC_SetWindow(int width, int height, const char *title)
Set the window size and title. Default: 800 × 600, title "ccompose". title pointer must outlive the call. Passing NULL leaves the previous title unchanged.
void CC_SetWindowFlags(unsigned int raylib_flags)
Set raylib's window flags. Default: FLAG_WINDOW_RESIZABLE | FLAG_MSAA_4X_HINT | FLAG_VSYNC_HINT. Useful additions: FLAG_WINDOW_HIGHDPI, FLAG_WINDOW_UNDECORATED, FLAG_FULLSCREEN_MODE.
CC_SetWindowFlags(FLAG_WINDOW_RESIZABLE |
FLAG_WINDOW_HIGHDPI |
FLAG_MSAA_4X_HINT |
FLAG_VSYNC_HINT);
void CC_SetBackground(CC_Color color)
Set the clear color used by CC_End(). You can call this at any time. It applies to the next frame.
Error handling
void CC_SetErrorHandler(void (*handler)(CC_ErrorData))
Set a Clay error handler before CC_Init(). Without one, ccompose prints error text to stderr.
static void my_error_handler(CC_ErrorData err) {
fprintf(stderr, "clay error: %.*s\n",
(int)err.errorText.length, err.errorText.chars);
}
int main(void) {
CC_SetErrorHandler(my_error_handler);
CC_Init();
// ...
}
Common errors you'll see in development:
CLAY_ERROR_TYPE_UNBALANCED_OPEN_CLOSE: a container block exited without closing. Most often caused bybreak/returninside aColumn/Row/Box/Buttonblock. See Troubleshooting.CLAY_ERROR_TYPE_ELEMENTS_CAPACITY_EXCEEDED: you have more elements than Clay's arena can hold. Increase viaClay_SetMaxElementCount()afterCC_Init().CLAY_ERROR_TYPE_TEXT_MEASUREMENT_CAPACITY_EXCEEDED: similar, for text.Clay_SetMaxMeasureTextCacheWordCount().
Fonts
Slot 0 is always the raylib default font. Custom fonts go into slots 1 to 15, so you can load up to 15 of them. CC_LoadFont returns -1 when the table is full, when the file fails to load, or when it is called before CC_Init().
int CC_LoadFont(const char *path, int base_size)
Load a font file and return its slot id, or -1 on failure. Must be called after CC_Init() so raylib has an active GL context.
int FONT_UI = CC_LoadFont("resources/Inter-Regular.ttf", 16);
if (FONT_UI < 0) FONT_UI = 0; // fall back to default font
Text("Status",
.fontId = FONT_UI,
.fontSize = 16,
.textColor = Color(220, 220, 220, 255));
Load each font at the largest size you draw it. The glyph atlas is mipmapped and sampled trilinearly, so smaller sizes stay sharp. Drawing larger than base_size blurs.
CC_LoadFont loads 400 glyphs, codepoints U+0020 to U+01AF (ASCII, Latin-1, Latin Extended-A and part of Extended-B). Text is measured and drawn by UTF-8 code point, so ×, ° and accented letters lay out correctly. A character outside the loaded range draws as raylib's fallback glyph.
Loaded fonts are unloaded automatically by CC_Shutdown(). In headless mode this function always returns -1.
int CC_InitFont(int fontId, int base_size)
Select an already-loaded font and the default text size. Text() uses this id when .fontId is omitted or zero, and this size when .fontSize is omitted.
Pass an id from CC_LoadFont, or 0 for raylib's default font. Returns fontId on success. An invalid id returns -1 and leaves both defaults unchanged. A base_size <= 0 leaves the default size unchanged. The initial size is CC_DEFAULT_FONT_SIZE (16).
int FONT_BODY = CC_LoadFont("resources/Inter-Regular.ttf", 28);
CC_InitFont(FONT_BODY, 16);
int FONT_MONO = CC_LoadFont("resources/RobotoMono-Medium.ttf", 14);
Text("Uses global font and size", .textColor = Color(220,220,220,255));
Text("Manual override", .fontId = FONT_MONO, .fontSize = 14,
.textColor = Color(220,220,220,255));
CC_LoadFont uses base_size to rasterize the glyph atlas. CC_InitFont uses it as the default rendered size. For example, you can rasterize at 96 for large headings while keeping body text at 16.
In headless mode, the id and size still flow into text render commands. Since no fonts are loaded, the bounds check accepts any id that fits Clay's uint16_t fontId field.
Replaced
CC_LoadGlobalFont(path, base_size), which loaded a file and set the global default, so a font already loaded byCC_LoadFontcould not become the global one without burning a second slot. Migration:- CC_LoadGlobalFont("resources/Inter-Regular.ttf", 28);+ CC_InitFont(CC_LoadFont("resources/Inter-Regular.ttf", 28), 16);Pass the size you want text to render at as the second argument, not the rasterization size you passed to
CC_LoadFont.
int CC_GetGlobalFontId(void)
Returns the currently configured global font id (default: 0).
int CC_GetGlobalFontSize(void)
Returns the currently configured global text size (default: CC_DEFAULT_FONT_SIZE, 16).
int CC_SetGlobalFontColor(CC_Color color)
Sets the global text color used by every Text() element. Defaults to ColorHex(0xFFFFFF) (opaque white).
CC_SetGlobalFontColor(ColorHex(0xDCDCDC));
// or:
CC_SetGlobalFontColor((CC_Color){220, 220, 220, 255});
Text("Status", .fontSize = 16); // renders with the global color
Per-call .textColor on Text() overrides this global. Use CC_SetGlobalFontColor() to set the project-wide default and skip passing .textColor on every call.
Returns 0. You can call it before or after CC_Init(). Both backends store the color and include it in text render commands.
CC_Color CC_GetGlobalFontColor(void)
Returns the currently configured global text color (default: ColorHex(0xFFFFFF)). Same behavior in both backends.
Headless helpers
void CC_SetViewport(float width, float height)
Set the layout dimensions for headless runs. With raylib, CC_Begin() replaces them each frame with GetScreenWidth() and GetScreenHeight().
CC_SetViewport(400.0f, 300.0f);
CC_Init();
See Headless mode for full CI/test patterns.
Bypassing the lifecycle entirely
For a custom arena, multiple Clay contexts, or another renderer, call Clay_Initialize, Clay_BeginLayout, and Clay_EndLayout directly. The element macros work with a Clay context initialized by your code.